Example #1
0
def test_parser():
    parse = lispparser()
    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()
Example #2
0
def test_to_string():
    parse = lispparser()
    assert tostring(parse("nil")) =="nil"
    assert tostring(parse("666")) =="666"
    assert tostring(parse("()")) == "()"
    assert tostring(parse("(a)")) == "(a)"
    assert tostring(parse("(a b)")) == "(a b)"
    assert tostring(parse("(a (b c))")) == "(a (b c))"
    assert tostring(parse("[]")) == "[]"
    assert tostring(parse(":a")) == ":a"
    assert tostring(parse("{}")) == "{}"
    assert tostring(parse("{1 2}")) == "{1 2}"
    assert tostring(parse("{1 2 3 4}")) == "{1 2, 3 4}"
Example #3
0
def test_eval():
    parse = lispparser()
    scopechain = [Scope()]
    def evalparse(x):
        return evaluate(parse(x), scopechain)
    assert evalparse("666") == 666
    assert evalparse("666") == 666
    assert evalparse("nil") == None
    assert evalparse("()") == List()
    assert evalparse("[]") == Vector()
    assert evalparse("[1 2 3]") == Vector(1, 2, 3)
    assert evalparse("{}") == Map()
    m = Map()
    m[1] = 2
    assert evalparse("{1 2}") == m
    m[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
    scopechain[-1]["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
Example #4
0
    import readline
except ImportError:
    pass
else:
    import os
    histfile = os.path.join(os.path.expanduser("~"), ".pyclojurehist")
    try:
        readline.read_history_file(histfile)
    except IOError:
        # Pass here as there isn't any history file, so one will be
        # written by atexit
        pass
    import atexit
    atexit.register(readline.write_history_file, histfile)

parse = lispparser()
lexer = lisplexer()

def main():
    global_scope = Scope()
    scopechain = [global_scope]
    while True:
        try:
            txt = raw_input("pylisp> ")
            if re.search('^\s*$', txt):
                continue
            else:
                print(tostring(evaluate(parse(txt), scopechain)))
        except EOFError:
            break
        except KeyboardInterrupt: