Beispiel #1
0
 def test_parse_method(self):
     tokens = lex("print 1 + 1;")
     parser = Parser(tokens)
     ast = parser.parse()
     expected_ast = AST(
         [PrintStatement(BinOpexpr("+", Integer(1), Integer(1)))])
     self.assertEqual(ast, expected_ast)
Beispiel #2
0
 def test_eval_method_with_false_expression(self):
     env = {"x": 0}
     while_stmt = WhileStatement(
         RelOpexpr(">", Varexpr("x"), Integer(10)),
         AssignStatement("x", BinOpexpr("+", Varexpr("x"), Integer(1))))
     while_stmt.eval(env)
     self.assertEqual(env["x"], 0)
Beispiel #3
0
 def test_eval_method_with_false_condition_and_no_else_stmt(self):
     if_stmt = IfStatement(
         Integer(0),
         AssignStatement("x", Integer(10)),
         # eno lse stmt
         None)
     env = {}
     if_stmt.eval(env)
     self.assertNotIn("x", env)
Beispiel #4
0
 def parse_if_statement_method_with_else_statement(self):
     tokens = lex("if x > 22 then\nprint x;\nelse\nprint 0;\ndone\n")
     parser = Parser(tokens)
     parser.advance()
     ast = parser.parse_if_statement()
     expected_ast = IfStatement(RelOpexpr(">", Varexpr("x"), Integer(22)),
                                AST([PrintStatement(Varexpr("x"))]),
                                AST([PrintStatement(Integer(0))]))
     self.assertEqual(ast, expected_ast)
Beispiel #5
0
 def test___repr___method(self):
     while_stmt = WhileStatement(
         Integer(1),
         BinOpexpr("+", Integer(1), Integer(2)),
     )
     while_stmt_repr = repr(while_stmt)
     expected_repr = "WhileStatement(Integer(1), " \
                     "BinOpexpr(+, Integer(1), Integer(2)))"
     self.assertEqual(while_stmt_repr, expected_repr)
Beispiel #6
0
 def test_eval_method_with_true_condition_and_no_else_stmt(self):
     if_stmt = IfStatement(
         Integer(1),
         AssignStatement("x", Integer(10)),
         # no else stmt
         None)
     env = {}
     if_stmt.eval(env)
     self.assertIn("x", env)
     self.assertEqual(env["x"], 10)
Beispiel #7
0
 def test___repr___method(self):
     if_stmt = IfStatement(
         Integer(1),
         BinOpexpr("+", Integer(1), Integer(2)),
         # no else stmt
         None)
     if_stmt_repr = repr(if_stmt)
     expected_repr = "IfStatement(Integer(1), " \
                     "BinOpexpr(+, Integer(1), Integer(2)), None)"
     self.assertEqual(if_stmt_repr, expected_repr)
Beispiel #8
0
 def test_create_ast_from_postfix_method(self):
     postfix = [
         Token(1, "NUMBER", "INT"),
         Token(1, "NUMBER", "INT"),
         Token("+", "RESERVED", "OPERATOR")
     ]
     parser = Parser([])
     ast = parser.create_ast_from_postfix(postfix)
     expected_ast = BinOpexpr("+", Integer(1), Integer(1))
     self.assertEqual(ast, expected_ast)
Beispiel #9
0
 def test_eval_method_with_false_condition_and_else_stmt(self):
     if_stmt = IfStatement(
         Integer(0),
         AssignStatement("x", Integer(10)),
         # else stmt
         AssignStatement("y", Integer(12)),
     )
     env = {}
     if_stmt.eval(env)
     self.assertNotIn("x", env)
     self.assertIn("y", env)
     self.assertEqual(env["y"], 12)
Beispiel #10
0
 def test_parse_while_statement_method(self):
     tokens = lex("while x > 22 do\nprint x;\nx := x - 1;\ndone\n")
     parser = Parser(tokens)
     parser.advance()
     ast = parser.parse_while_statement()
     expected_ast = WhileStatement(
         RelOpexpr(">", Varexpr("x"), Integer(22)),
         AST([
             PrintStatement(Varexpr("x")),
             AssignStatement("x", BinOpexpr("-", Varexpr("x"), Integer(1)))
         ]),
     )
     self.assertEqual(ast, expected_ast)
Beispiel #11
0
 def test_interpret_method(self):
     env = {}
     ast = AST([AssignStatement("x", Integer(10))])
     interpreter = Interpreter(ast, env)
     interpreter.interpret()
     self.assertIn("x", env)
     self.assertEqual(env["x"], 10)
Beispiel #12
0
 def test_eval_method(self):
     ast = AST()
     ast.insert_node(AssignStatement("x", Integer(10)))
     env = {}
     ast.eval(env)
     self.assertIn("x", env)
     self.assertEqual(env["x"], 10)
Beispiel #13
0
 def test_parse_print_statement_method(self):
     tokens = lex("print 10;")
     parser = Parser(tokens)
     parser.advance()
     ast = parser.parse_print_statement()
     expected_ast = PrintStatement(Integer(10))
     self.assertEqual(ast, expected_ast)
Beispiel #14
0
 def test_eval_method_with_true_expression(self):
     notexpr = Notexpr(Integer(0))
     env = {}
     result = notexpr.eval(env)
     self.assertTrue(result)
Beispiel #15
0
 def test_eval_method_with_false_expression(self):
     notexpr = Notexpr(Integer(1))
     env = {}
     result = notexpr.eval(env)
     self.assertFalse(result)
Beispiel #16
0
 def test_parse_statements_method(self):
     tokens = lex("print 1;")
     parser = Parser(tokens)
     parser.parse()
     expected_ast = AST([PrintStatement(Integer(1))])
     self.assertEqual(parser.ast, expected_ast)
Beispiel #17
0
 def test_insert_node_method(self):
     ast = AST()
     ast.insert_node(Integer(10))
     ast.insert_node(Varexpr("x"))
     expected_nodes = [Integer(10), Varexpr("x")]
     self.assertEqual(ast.nodes, expected_nodes)
Beispiel #18
0
 def test_create_ast_from_expr_method(self):
     parser = Parser([])
     ast = parser.create_ast_from_expr("+", Integer(1), Integer(1))
     expected_ast = BinOpexpr("+", Integer(1), Integer(1))
     self.assertEqual(ast, expected_ast)
Beispiel #19
0
 def test___repr___method(self):
     assign_stmt = AssignStatement("x", Integer(10))
     assign_stmt_repr = repr(assign_stmt)
     expected_repr = "AssignStatement(x, Integer(10))"
     self.assertEqual(assign_stmt_repr, expected_repr)
Beispiel #20
0
 def test_eval_method(self):
     assign_stmt = AssignStatement("x", Integer(10))
     env = {}
     assign_stmt.eval(env)
     self.assertIn("x", env)
     self.assertEqual(env["x"], 10)
Beispiel #21
0
 def test_parse_var_assignment_method(self):
     tokens = lex("x := 10;")
     parser = Parser(tokens)
     ast = parser.parse_var_assignment()
     expected_ast = AssignStatement("x", Integer(10))
     self.assertEqual(ast, expected_ast)
Beispiel #22
0
 def test___repr___method(self):
     binopexpr = BinOpexpr("+", Integer(1), Integer(1))
     binopexpr_repr = repr(binopexpr)
     expected_repr = "BinOpexpr(+, Integer(1), Integer(1))"
     self.assertEqual(binopexpr_repr, expected_repr)
Beispiel #23
0
 def test_eval_method_with_times_operator(self):
     binopexpr = BinOpexpr("*", Integer(12), Integer(5))
     env = {}
     result = binopexpr.eval(env)
     self.assertEqual(result, 60)
Beispiel #24
0
 def test___repr___method(self):
     ast = AST()
     ast.insert_node(Integer(10))
     ast_repr = repr(ast)
     expected_repr = "Statements([Integer(10)])"
     self.assertEqual(ast_repr, expected_repr)
Beispiel #25
0
 def test___repr___method(self):
     notexpr = Notexpr(Integer(1))
     notexpr_repr = repr(notexpr)
     expected_repr = "Notexpr(Integer(1))"
     self.assertEqual(notexpr_repr, expected_repr)
Beispiel #26
0
 def test___repr___method(self):
     integer = Integer(10)
     integer_repr = repr(integer)
     expected_repr = "Integer(10)"
     self.assertEqual(integer_repr, expected_repr)
Beispiel #27
0
 def test_parse_function(self):
     string = "x := 10;"
     ast = parse(string)
     expected_ast = AST([AssignStatement("x", Integer(10))])
     self.assertEqual(ast, expected_ast)
Beispiel #28
0
 def test_parse_expr_method(self):
     tokens = lex("1+1;")
     parser = Parser(tokens)
     ast = parser.parse_expr()
     expected_ast = BinOpexpr("+", Integer(1), Integer(1))
     self.assertEqual(ast, expected_ast)
Beispiel #29
0
 def test_eval_method(self):
     integer = Integer(10)
     env = {}
     result = integer.eval(env)
     self.assertEqual(result, 10)
Beispiel #30
0
 def test___repr___method(self):
     print_statment = PrintStatement(Integer(10))
     print_statment_repr = repr(print_statment)
     expected_repr = "PrintStatement(Integer(10))"
     self.assertEqual(print_statment_repr, expected_repr)