def test_parser(): parse = PyClojureParse().build().parse assert parse("an_atom") == Atom('an_atom') assert parse("(simple_list)") == List(Atom('simple_list')) assert parse('(two elements)') == List(Atom('two'), Atom('elements')) assert (parse("(three element list)") == List(Atom('three'), Atom('element'), Atom('list'))) assert parse('666') == 666 assert (parse('(a (nested (list)))') == List( Atom('a'), List(Atom('nested'), List(Atom('list'))))) assert parse('()') == List()
def test_eval(): evalparse = evalparser() assert evalparse("666") == 666 assert evalparse("6.66") == 6.66 assert evalparse("nil") == None assert evalparse("()") == List() assert evalparse("[]") == Vector() assert evalparse("[1 2 3]") == Vector(1, 2, 3) assert evalparse("{}") == Map() m = Map({1: 2}) assert evalparse("{1 2}") == m m = Map({1: 2, 3: 4}) assert evalparse("{1 2, 3 4}") == m try: evalparse("a") assert False, "UnknownVariable exception not raised!" except UnknownVariable: pass try: evalparse("(x)") assert False, "UnknownVariable exception not raised!" except UnknownVariable: pass evalparse("(def a 777)") assert evalparse("a") == 777 assert evalparse("a") == 777 evalparse("(def a 666)") assert evalparse("a") == 666 assert evalparse("[1 a]") == Vector(1, 666) assert evalparse(":a") == Keyword("a") assert evalparse("(+ 2 2)") == 4 assert evalparse("(+)") == 0 assert evalparse("(+ 1 2 3 4)") == 10 assert evalparse("(*)") == 1 assert evalparse("(* 1 2 3 4 5)") == 120 assert evalparse("(+ 2 (+ 2 3))") == 7 assert evalparse("{}") == Map() assert evalparse("{1 2}") == Map({1: 2}) assert evalparse("({1 2} 1)") == 2 assert evalparse("({a 1} 666)") == 1 assert evalparse("({666 1} a)") == 1 assert evalparse("({a 2 3 a} a)") == 2 assert evalparse("({a 2 3 a} 3)") == 666
def test_python_compat(): assert List(1, 2, 3) == [1, 2, 3] assert Map() == {} assert Map(a=3) == {'a': 3} assert Map(a=3) != ['a', 3] assert Vector(*range(10)) == range(10) assert map(abs, List(-1, -2, -3)) == List(1, 2, 3) def infinite_gen(): x = 1 while 1: x += 1 yield x assert List(1, 2, 3) != infinite_gen() assert List(1, 2) != List(1, 2, 3)
def eval_list(contents, scopes): if contents.empty(): return List() # () first = contents.first() rest = contents.rest() if type(first) is Map: if not rest.rest().empty(): raise TypeError("Map lookup takes one argument") return evaluate(first, scopes)[evaluate(rest.first(), scopes)] elif type(first) is Atom: name = first.name() if name in BUILTIN_FUNCTIONS: func = BUILTIN_FUNCTIONS[name] return func(rest, scopes) else: val = find_in_scopechain(scopes, name) if not val: raise UnknownVariable("Function %s is unknown" % name) if callable(val): args = map((lambda obj: evaluate(obj, scopes)), rest) return val(*args) else: raise TypeError("%s is not callable" % name)
def test_core(): Atom() Atom('a') Atom(name='a', value=6) List() List(Atom('car')) List(Atom('car'), Atom('cadr'), 666) List(List()) List(List('car')) Vector() Vector(1, 2, 3) Keyword("a") assert Atom() == Atom() assert List() == List() assert List(1) == List(1) assert List(2) != List(1) assert List(1, 2) != List(2, 1) assert List(1, 2) == List(1, 2) assert List(Atom()) == List(Atom()) assert List(Atom('a')) == List(Atom('a')) assert List(Atom('b')) != List(Atom('a')) assert Vector(1, 2) != Vector(2, 1) assert Vector(1, 2) == Vector(1, 2) assert Vector(1, 2) == List(1, 2) assert Keyword("a") == Keyword("a") assert Keyword("a") != Keyword("b") Map() Map(x=1) assert Map(x=1).keys() == ['x'] assert Map(x=1) == Map(x=1) assert Map(x=1) != Map(x=2) assert Map(x=1) != Map(x=1, a=3) assert Map(x=1)["x"] == 1
def deref_expr(raw): return List(Atom('deref'), raw)
def quote_expr(raw): return List(Atom('quote'), raw)
def p_empty_list(self, p): 'list : LPAREN RPAREN' p[0] = List()
def p_list(self, p): 'list : LPAREN sexprs RPAREN' try: p[0] = apply(List, p[2]) except TypeError: p[0] = List(p[2])