Пример #1
0
def populate_globals(env):
    # Builtin *functions* take an environment, rather than individual
    # args.

    ##
    # Add:
    #
    def builtin_add(env):
        x = env.lookup("x").number
        y = env.lookup("y").number
        return Number(x + y)

    # Builtin *objects* have arglists, though.
    iplus = Builtin(builtin_add, [Symbol("x"), Symbol("y")])

    env.set("add", iplus)

    ##
    # Lambda:
    #
    def builtin_lambda(env):
        args = env.lookup("args")
        body = env.lookup("body")
        return Lambda(args.items, body, env.calling_environment)

    fn = Builtin(builtin_lambda, [
        List([Symbol("quote"), Symbol("args")]),
        List([Symbol("quote"), Symbol("body")])
    ])
    env.set("fn", fn)
    env.set("λ", fn)

    ##
    # Set:
    #
    def builtin_set(env):
        sym = env.lookup("symbol")
        val = env.lookup("value")
        env.calling_environment.set(sym.symbol, val)
        return val

    env.set(
        "set",
        Builtin(builtin_set,
                [List([Symbol("quote"), Symbol("symbol")]),
                 Symbol("value")]))

    return env
Пример #2
0
def coerce_by_default(method):
    def default(argv):
        args = coerce.call(argv)
        assert isinstance(args, List)
        return method.call_suppressed(args.contents)

    method.default = Builtin(default)
Пример #3
0
def coerce_by_default(method):
    def default(argv):
        args = coerce.call(argv)
        if not isinstance(args, List):
            raise space.unwind(space.LError(u"coerce should return list"))
        return method.call_suppressed(args.contents)

    method.default = Builtin(default)
Пример #4
0
 def _decarotar_(fn):
     from builtin import Builtin
     builtin = Builtin(decorator(fn), name)
     cls.interface.methods[builtin.name] = builtin
     return fn
Пример #5
0
 def builtin_method(cls, fn):
     from builtin import Builtin
     builtin = Builtin(fn)
     cls.interface.methods[builtin.name] = builtin
Пример #6
0
@le.multimethod_s(String, String)
def _(a, b):
    return boolean(a.string <= b.string)


@ge.multimethod_s(String, String)
def _(a, b):
    return boolean(a.string >= b.string)


@signature(Object, Object)
def ne_default(a, b):
    return boolean(is_false(eq.call([a, b])))


ne.default = Builtin(ne_default)


@ne.multimethod_s(Integer, Integer)
def _(a, b):
    return boolean(a.value != b.value)


@ne.multimethod_s(Float, Float)
def _(a, b):
    return boolean(a.number != b.number)


@ne.multimethod_s(String, String)
def _(a, b):
    return boolean(a.string != b.string)
Пример #7
0
 def _impl_(fn):
     self.methods[vec] = Builtin(fn)
     return fn
Пример #8
0
 def _impl_(fn):
     self.multimethod_table[vec] = Builtin(fn)
     return fn
Пример #9
0
@le.multimethod_s(Float, Float)
def cmp_le(a, b):
    return boolean(a.number <= b.number)


@ge.multimethod_s(Float, Float)
def cmp_ge(a, b):
    return boolean(a.number >= b.number)


@signature(Object, Object)
def ne_default(a, b):
    return boolean(a != b)


ne.default = Builtin(ne_default)


@ne.multimethod_s(Integer, Integer)
def _(a, b):
    return boolean(a.value != b.value)


@ne.multimethod_s(Float, Float)
def _(a, b):
    return boolean(a.number != b.number)


@ne.multimethod_s(String, String)
def _(a, b):
    return boolean(not a.eq(b))
Пример #10
0
@le.multimethod_s(String, String)
def _(a, b):
    return boolean(a.string <= b.string)


@ge.multimethod_s(String, String)
def _(a, b):
    return boolean(a.string >= b.string)


@signature(Object, Object)
def ne_default(a, b):
    return boolean(is_false(eq.call([a, b])))


ne.default = Builtin(ne_default)


@ne.multimethod_s(Integer, Integer)
def _(a, b):
    return boolean(a.value != b.value)


@ne.multimethod_s(Float, Float)
def _(a, b):
    return boolean(a.number != b.number)


@ne.multimethod_s(String, String)
def _(a, b):
    return boolean(a.string != b.string)
Пример #11
0
 def _impl_(fn):
     bfn = Builtin(fn)
     record = MultimethodRecord(self, vec, bfn)
     self.register_record(record)
     return fn