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
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)
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)
def _decarotar_(fn): from builtin import Builtin builtin = Builtin(decorator(fn), name) cls.interface.methods[builtin.name] = builtin return fn
def builtin_method(cls, fn): from builtin import Builtin builtin = Builtin(fn) cls.interface.methods[builtin.name] = builtin
@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)
def _impl_(fn): self.methods[vec] = Builtin(fn) return fn
def _impl_(fn): self.multimethod_table[vec] = Builtin(fn) return fn
@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))
def _impl_(fn): bfn = Builtin(fn) record = MultimethodRecord(self, vec, bfn) self.register_record(record) return fn