def test_no_tokens_should_return_empty_root_node(self):
     node = build_tree([])
     assert isinstance(node, RootNode)
     assert node.eval() is None
 def test_nested_expression(self):
     lexer = Lexer()
     tokens = lexer.analyze('(+ 1 (+ 1 (+ 1 1)))')
     node = build_tree(tokens)
     assert 4 == node.eval()
 def test_empty_expression(self):
     lexer = Lexer()
     tokens = lexer.analyze('()')
     node = build_tree(tokens)
     assert node.eval() is None
 def test_simple_expression(self):
     lexer = Lexer()
     tokens = lexer.analyze('(+ 1 1)')
     node = build_tree(tokens)
     assert 2 == node.eval()
Beispiel #5
0
def eval(expr):
    lexer = Lexer()
    node = build_tree(lexer.analyze(expr))
    return node.eval()