def interpret_token(s): from system.bool import w_true, w_false from system.core import symbol if s == "nil": return None if s == "true": return w_true if s == "false": return w_false if s == "/": return symbol("/") if s == "system//": return symbol("system", "/") ret = None ret = match_symbol(s) if ret is not None: return ret raise Exception("Unknown Symbol " + s)
def compile_impl(name, argsv, body, env): methname = "invoke" + str(count(argsv).int()) self = tr.Argument("self") args = {symbol(methname): self} trarg = [self] for x in range(count(argsv).int()): s = nth(argsv, integer(x)) args[s] = tr.Argument(s.repr()) trarg.append(args[s]) with merge_locals(env, args): expr = tr.Func(trarg, compile_do(body, env)) return [tr.Const(methname), expr]
def init(): from system.core import symbol from system.evaluation import ResolveFrame import sys from system.reader import read_from_string from system.app_compiler import compile_in_module form = read_from_string("((fn baz [x y] (add x y)))") comped = compile_in_module(form, sys.modules["system.rt"]) globals()["baz"] = comped() names = [] values = [] for k in globals(): val = globals()[k] if isinstance(val, Object) and hasattr(val, "_symbol_"): names.append(symbol(None, val._symbol_)) values.append(val) globals()["builtins"] = ResolveFrame(names, values)
import core from system.bool import w_true, w_false import util from system.core import symbol import system.rt as rt from system.rt import extend from system.helpers import equals from system.jit import * _tp_integer = symbol("system", "Integer") class W_Integer(core.Object): def __init__(self, v): self.int_value = v def int(self): return self.int_value def type(self): return _tp_integer def repr(self): return str(self.int_value) def int_repr(self): return str(self.int()) # core.repr.install(core.symbol("system", "Integer"), util.interp2app(int_repr, "repr"))
import system.rt as rt from system.core import Object, symbol, integer from system.bool import w_true, w_false from system.rt import extend from helpers import * def _get_idx(data_w, w_item): for x in range(0, len(data_w), 2): if equals(data_w[x], w_item) is w_true: return x return -1 tp_sym = symbol("system", "PersistentArrayMap") class PersistentArrayMap(Object): def __init__(self, data_w, w_meta): self._data_w = data_w self._w_meta = w_meta def type(self): return tp_sym class Get(rt.Func): def __init__(self): pass def invoke2(self, a, b): return a.invoke2(b, None) def invoke3(self, a, b, c): idx = _get_idx(a._data_w, b) if idx == -1:
from system.core import Object, symbol, integer import system.rt as rt from system.rt import extend _tp = symbol("system", "PersistentArrayVector") class PersistentArrayVector(Object): def __init__(self, lst_w, meta): self.lst_w = lst_w self._meta = meta def type(self): return _tp def repr(self): a = [] for x in range(len(self.lst_w)): if self.lst_w[x] is None: a.append("nil") else: a.append(self.lst_w[x].repr()) return "[" + " ".join(a) + "]" @extend(rt._count, _tp) def persistent_array_vector_count(self): return integer(len(self.lst_w)) @extend(rt._nth, _tp) def persistent_array_vector_nth(self, w_nth):
from system.core import Object, symbol from system.rt import extend import system.rt as rt from system.bool import w_true, w_false from system.util import intern _tp = symbol("system", "Keyword") class Keyword(Object): def __init__(self, ns, name): self._ns = ns self._name = name def type(self): return _tp @extend(rt.equals, _tp) def keword_equals(self, other): if self is other: return w_true return w_false class KeywordCache(object): def __init__(self): self._cache = {} def intern(self, ns, name):
from system.core import symbol, Object, integer import system.rt as rt from system.rt import extend from system.jit import elidable _tp = symbol("system", "PersistentList") class PersistentList(Object): def __init__(self, w_head, w_tail, count, w_meta): self._w_head = w_head self._w_tail = w_tail self._count = count self._w_meta = w_meta def repr(self): from system.helpers import first, next s = self a = [] while s is not None: if first(s) is None: a.append("nil") else: a.append(first(s).repr()) s = next(s) return "(" + " ".join(a) + ")" def type(self): return _tp EMPTY = PersistentList(None, None, 0, None)
def run_benchmark(times): c = list.invoke_args([symbol(None, "+"), integer(2), integer(3)]) return eval(c).int()
def match_symbol(s): if "/" in s: sp = s.split("/") assert len(sp) == 2 return symbol(sp[0], sp[1]) return symbol(s)
a = [] s = form while s is not None: a.append(compile(first(s), Ctx())) s = next(s) return tr.Do(*a).toFunc(module.__dict__) class merge_locals(object): def __init__(self, ctx, locals = {}): self._ctx = ctx self._locals = locals def __enter__(self): import copy self._old = copy.copy(self._ctx._locals) for x in self._locals: self._ctx._locals[x] = self._locals[x] def __exit__(self, type, value, tb): self._ctx._locals = self._old def merge(d1, d2): d1 = copy.copy(d1) for x in d2: d1[x] = d2[x] return d1 dispatchers = {symbol("fn") : compile_fn}
def test_add(self): lst = rt.list.invoke_args([symbol(None, "+"), integer(1), integer(2)]) result = eval(lst) assertEqual(self, result, integer(3))