Пример #1
0
 def parse_program(self):
     program = ast.Program()
     while self.cur_token.Type != token.EOF:
         stmt = self.parse_statement()
         if stmt != None:
             program.statements.append(stmt)
         self.next_token()
     return program
Пример #2
0
 def test_string(self):
     let = ast.LetStatement(
         token.Token(token.LET, "let"),
         ast.Identifier(token.Token(token.IDENT, "myVar"), "myVar"),
         ast.Identifier(token.Token(token.IDENT, "anotherVar"),
                        "anotherVar"))
     program = ast.Program([let])
     self.assertEqual(str(program), "let myVar = anotherVar;")
Пример #3
0
    def ParseProgram(self) -> ast.Program:
        program = ast.Program([])

        while self.curToken.Type != token.EOF:
            stmt = self.parseStatement()
            if stmt is not None:
                program.Statements.append(stmt)
            self.nextToken()

        return program
Пример #4
0
    def test_string(self):
        program = ast.Program(Statements=[
            ast.LetStatement(Token=token.Token(Type=token.LET, Literal='let'),
                             Name=ast.Identifier(Token=token.Token(
                                 Type=token.IDENT, Literal='myVar'),
                                                 Value='myVar'),
                             Value=ast.Identifier(Token=token.Token(
                                 Type=token.IDENT, Literal='anotherVar'),
                                                  Value='anotherVar'))
        ])

        if program.String() != 'let myVar = anotherVar;':
            self.fail('program.String() wrong. got=\'%s\'' % program.String())
Пример #5
0
def test_string():

    program = ast.Program()
    statements = [
        ast.LetStatement(
            token=token.Token(token.LET, "let"),
            name=ast.Identifier(token=token.Token(token.IDENT, "myVar"),
                                value="myVar"),
            value=ast.Identifier(token=token.Token(token.IDENT, "anotherVar"),
                                 value="anotherVar"),
        )
    ]
    program.statements = statements
    assert str(program) == "let myVar = anotherVar"
Пример #6
0
    def parse_program(self) -> ast.Node:
        program = ast.Program()

        # Continue until EOF
        while not self.cur_token_is(token.EOF):

            # Try and parse a statement
            stmt = self.parse_statement()
            if stmt is not None:
                program.statements.append(stmt)

            # Advance the parser
            self.next_token()

        # Return the 'parsed' program
        return program
Пример #7
0
def test_string():
    program = ast.Program([
        ast.LetStatement(
            token.Token(token.LET, 'let'),
            ast.Identifier(
                token.Token(token.IDENT, 'myVar'),
                'myVar'
            ),
            ast.Identifier(
                token.Token(token.IDENT, 'anotherVar'),
                'anotherVar'
            )
        )
    ])

    assert program.string() == 'let myVar = anotherVar;', "program.string() wrong. got='{}'".format(program.string())
Пример #8
0
    def test_modify(self):
        one: Callable[[], ast.Expression] = lambda: ast.IntegerLiteral(
            Token=token.Token(token.INT, 'Unknown'), Value=1)
        two: Callable[[], ast.Expression] = lambda: ast.IntegerLiteral(
            Token=token.Token(token.INT, 'Unknown'), Value=2)

        def turnOneIntoTwo(node: ast.Node) -> ast.Node:
            integer = node
            if type(node) != ast.IntegerLiteral:
                return node

            if integer.Value != 1:
                return node

            integer.Value = 2
            return integer

        @dataclass
        class Test:
            input: ast.Node
            expected: ast.Node

        tests: List[Test] = [
            Test(one(), two()),
            Test(
                ast.Program(Statements=[
                    ast.ExpressionStatement(Token=token.Token(
                        token.INT, 'Unknown'),
                                            ExpressionValue=one()),
                ]),
                ast.Program(Statements=[
                    ast.ExpressionStatement(Token=token.Token(
                        token.INT, 'Unknown'),
                                            ExpressionValue=two()),
                ])),
            Test(
                ast.InfixExpression(Token=token.Token(token.ILLEGAL,
                                                      'ILLEGAL'),
                                    Left=one(),
                                    Operator='+',
                                    Right=two()),
                ast.InfixExpression(Token=token.Token(token.ILLEGAL,
                                                      'ILLEGAL'),
                                    Left=two(),
                                    Operator='+',
                                    Right=two())),
            Test(
                ast.InfixExpression(Token=token.Token(token.ILLEGAL,
                                                      'ILLEGAL'),
                                    Left=two(),
                                    Operator='+',
                                    Right=one()),
                ast.InfixExpression(Token=token.Token(token.ILLEGAL,
                                                      'ILLEGAL'),
                                    Left=two(),
                                    Operator='+',
                                    Right=two())),
            Test(
                ast.IndexExpression(Token=token.Token(token.ILLEGAL,
                                                      'ILLEGAL'),
                                    Left=one(),
                                    Index=one()),
                ast.IndexExpression(Token=token.Token(token.ILLEGAL,
                                                      'ILLEGAL'),
                                    Left=two(),
                                    Index=two())),
            Test(
                ast.PrefixExpression(Token=token.Token(token.ILLEGAL,
                                                       'ILLEGAL'),
                                     Operator='-',
                                     Right=one()),
                ast.PrefixExpression(Token=token.Token(token.ILLEGAL,
                                                       'ILLEGAL'),
                                     Operator='-',
                                     Right=two())),
            Test(
                ast.IfExpression(
                    Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                    Condition=one(),
                    Consequence=ast.BlockStatement(
                        Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                        Statements=[
                            ast.ExpressionStatement(Token=token.Token(
                                token.ILLEGAL, 'ILLEGAL'),
                                                    ExpressionValue=one())
                        ]),
                    Alternative=ast.BlockStatement(
                        Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                        Statements=[
                            ast.ExpressionStatement(Token=token.Token(
                                token.ILLEGAL, 'ILLEGAL'),
                                                    ExpressionValue=one())
                        ]),
                ),
                ast.IfExpression(
                    Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                    Condition=two(),
                    Consequence=ast.BlockStatement(
                        Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                        Statements=[
                            ast.ExpressionStatement(Token=token.Token(
                                token.ILLEGAL, 'ILLEGAL'),
                                                    ExpressionValue=two())
                        ]),
                    Alternative=ast.BlockStatement(
                        Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                        Statements=[
                            ast.ExpressionStatement(Token=token.Token(
                                token.ILLEGAL, 'ILLEGAL'),
                                                    ExpressionValue=two())
                        ]))),
            Test(
                ast.IndexExpression(Token=token.Token(token.ILLEGAL,
                                                      'ILLEGAL'),
                                    Left=one(),
                                    Index=one()),
                ast.IndexExpression(Token=token.Token(token.ILLEGAL,
                                                      'ILLEGAL'),
                                    Left=two(),
                                    Index=two())),
            Test(
                ast.ReturnStatement(Token=token.Token(token.ILLEGAL,
                                                      'ILLEGAL'),
                                    ReturnValue=one()),
                ast.ReturnStatement(Token=token.Token(token.ILLEGAL,
                                                      'ILLEGAL'),
                                    ReturnValue=two())),
            Test(
                ast.LetStatement(Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                                 Value=one(),
                                 Name=''),
                ast.LetStatement(Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                                 Value=two(),
                                 Name=''),
            ),
            Test(
                ast.FunctionLiteral(
                    Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                    Parameters=[],
                    Body=ast.BlockStatement(
                        Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                        Statements=[
                            ast.ExpressionStatement(Token=token.Token(
                                token.ILLEGAL, 'ILLEGAL'),
                                                    ExpressionValue=one()),
                        ])),
                ast.FunctionLiteral(
                    Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                    Parameters=[],
                    Body=ast.BlockStatement(
                        Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                        Statements=[
                            ast.ExpressionStatement(Token=token.Token(
                                token.ILLEGAL, 'ILLEGAL'),
                                                    ExpressionValue=two()),
                        ])),
            ),
            Test(
                ast.ArrayLiteral(Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                                 Elements=[one(), one()]),
                ast.ArrayLiteral(Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
                                 Elements=[two(), two()]),
            ),
        ]

        for tt in tests:
            modified = ast.Modify(tt.input, turnOneIntoTwo)

            if modified != tt.expected:
                self.fail('not equal. got=%s, want=%s' %
                          (modified, tt.expected))

        hashLiteral = ast.HashLiteral(
            Token=token.Token(token.ILLEGAL, 'ILLEGAL'),
            Pairs=[
                (one(), one()),
                (one(), one()),
            ],
        )

        ast.Modify(hashLiteral, turnOneIntoTwo)

        for key, val in hashLiteral.Pairs:
            if key.Value != 2:
                self.fail('value is not %s, got=%s' % (2, key.Value))

            if val.Value != 2:
                self.fail('value is not %s, got=%s' % (2, val.Value))