示例#1
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)
示例#2
0
 def test___repr___method(self):
     tokens = lex("1 + 1;")
     ast = AST()
     parser = Parser(tokens)
     parser_repr = repr(parser)
     expected_repr = "Parser({}, 0, {})".format(tokens, ast)
     self.assertEqual(parser_repr, expected_repr)
示例#3
0
 def test___repr___method(self):
     tokens = lex("1 + 1;")
     ast = AST()
     parser = Parser(tokens)
     parser_repr = repr(parser)
     expected_repr = "Parser({}, 0, {})".format(tokens, ast)
     self.assertEqual(parser_repr, expected_repr)
示例#4
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)
示例#5
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)
示例#6
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)
示例#7
0
 def test_parse_if_statement_method(self):
     tokens = lex("if x > 22 then\nprint x;\ndone\n")
     parser = Parser(tokens)
     parser.advance()
     ast = parser.parse_if_statement()
     expected_ast = IfStatement(
         RelOpexpr(">", Varexpr("x"), Integer(22)),
         AST([PrintStatement(Varexpr("x"))]),
         false_stmt=None
     )
     self.assertEqual(ast, expected_ast)
示例#8
0
文件: test_api.py 项目: afaki077/vsi
    def test_lex_function(self):
        string = "x := 10;"
        tokens = lex(string)
        expected_tokens = [Token("x", "ID", None, 0),
                           Token(":=", "RESERVED", "OPERATOR", 0),
                           Token(10, "NUMBER", "INT", 0),
                           Token(";", "RESERVED", "SYMBOL", 0),
                           Token("EOF", "RESERVED", "SYMBOL", 0)
                           ]

        self.assertEqual(tokens, expected_tokens)
示例#9
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)
示例#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)
示例#11
0
    def test_lex_function(self):
        string = "x := 10;"
        tokens = lex(string)
        expected_tokens = [
            Token("x", "ID", None, 0),
            Token(":=", "RESERVED", "OPERATOR", 0),
            Token(10, "NUMBER", "INT", 0),
            Token(";", "RESERVED", "SYMBOL", 0),
            Token("EOF", "RESERVED", "SYMBOL", 0)
        ]

        self.assertEqual(tokens, expected_tokens)
示例#12
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)
示例#13
0
 def test_token_ahead_method(self):
     tokens = lex("x := 1")
     parser = Parser(tokens)
     parser.advance()
     self.assertEqual(parser.token_ahead().value, 1)
示例#14
0
 def test_advance_method(self):
     tokens = lex("x := 1")
     parser = Parser(tokens)
     parser.advance()
     self.assertEqual(parser.pos, 1)
示例#15
0
 def test_optional_method(self):
     tokens = lex("if x > 10 then\nx:=1\nelse x:=10")
     parser = Parser(tokens)
     self.assertIsNone(parser.optional("else"))
     parser.advance(by=8)
     self.assertTrue(parser.optional("else"))
示例#16
0
 def test_expect_one_of_method(self):
     tokens = lex("print 1;")
     parser = Parser(tokens)
     self.assertTrue(parser.expect_one_of(["RESERVED", "ID"], by="type"))
示例#17
0
 def test_found_one_of_method(self):
     tokens = lex("x := 1 + 1;")
     parser = Parser(tokens)
     self.assertTrue(parser.found_one_of(["ID", "RESERVED"], by="type"))
     parser.advance(by=2)
     self.assertTrue(parser.found_one_of(["NUMBER", "OPERATOR"], by="type"))
示例#18
0
 def test_advance_method(self):
     tokens = lex("x := 1")
     parser = Parser(tokens)
     parser.advance()
     self.assertEqual(parser.pos, 1)
示例#19
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)
示例#20
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)
示例#21
0
 def test_found_method(self):
     tokens = lex("x := 1 + 1;")
     parser = Parser(tokens)
     self.assertTrue(parser.found("ID", by="type"))
     parser.advance(by=2)
     self.assertTrue(parser.found(1, by="value"))
示例#22
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)
示例#23
0
 def test_found_one_of_method(self):
     tokens = lex("x := 1 + 1;")
     parser = Parser(tokens)
     self.assertTrue(parser.found_one_of(["ID", "RESERVED"], by="type"))
     parser.advance(by=2)
     self.assertTrue(parser.found_one_of(["NUMBER", "OPERATOR"], by="type"))
示例#24
0
 def test_expect_one_of_method(self):
     tokens = lex("print 1;")
     parser = Parser(tokens)
     self.assertTrue(parser.expect_one_of(["RESERVED", "ID"], by="type"))
示例#25
0
 def test_token_ahead_method(self):
     tokens = lex("x := 1")
     parser = Parser(tokens)
     parser.advance()
     self.assertEqual(parser.token_ahead().value, 1)
示例#26
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)
示例#27
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)
示例#28
0
 def test_found_method(self):
     tokens = lex("x := 1 + 1;")
     parser = Parser(tokens)
     self.assertTrue(parser.found("ID", by="type"))
     parser.advance(by=2)
     self.assertTrue(parser.found(1, by="value"))
示例#29
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)
示例#30
0
 def test_optional_method(self):
     tokens = lex("if x > 10 then\nx:=1\nelse x:=10")
     parser = Parser(tokens)
     self.assertIsNone(parser.optional("else"))
     parser.advance(by=8)
     self.assertTrue(parser.optional("else"))