Exemplo n.º 1
0
def main():
    "Starts the main REPL loop"
    print "Hello!"
    while True:
        # read
        try:
            i = raw_input(">>> ")
            if i == "":
                continue
            if i == "DEBUG":
                # activate debug mode
                lisp_interpreter.debug = True
                continue
            while i.count("(") > i.count(")"):
                i += raw_input("... ")
        except (EOFError, KeyboardInterrupt):
            break
        if lisp_interpreter.debug:
            print "<<< ", i
        # eval
        t = tree_parse.createTree(i).toList()
        l = lisp_interpreter.LispStatement(t)
        r = l.evaluate()
        # print
        print r
        # loop ... (while)
    print "\nBye!"
Exemplo n.º 2
0
 def testIntegerStatement(self):
     statement = '2'
     result = lisp_interpreter.LispStatement(
         tree_parse.createTree(statement).toList()).evaluate()
     expected = 2
     self.assertEqual(expected, result)
Exemplo n.º 3
0
 def testIntLiteral(self):
     t = tree_parse.createTree("2").toList()
     self.assertEqual(2, t)
Exemplo n.º 4
0
 def testSimpleExpression(self):
     t = tree_parse.createTree("(- 2 3)").toList()
     self.assertEqual(['-', 2, 3], t)
Exemplo n.º 5
0
 def testComplexExpression(self):
     t = tree_parse.createTree("(+ (- 2 3) (a))").toList()
     self.assertEqual(['+', ['-', 2, 3], ['a']], t)
Exemplo n.º 6
0
 def testArithmeticStatement(self):
     statement = '(+ 2 2)'
     result = lisp_interpreter.LispStatement(
         tree_parse.createTree(statement).toList()).evaluate()
     expected = 4
     self.assertEqual(expected, result)