Beispiel #1
0
 def parse_prefix_expression(self) -> ast.Node:
     expression = ast.PrefixExpression(token=self.cur_token,
                                       operator=self.cur_token.literal,
                                       right=None)
     self.next_token()
     expression.right = self.parse_expression(OperatorPrecedence.PREFIX)
     return expression
Beispiel #2
0
    def parsePrefixExpression(self) -> Optional[ast.Expression]:
        curToken = self.curToken
        self.nextToken()
        expression = ast.PrefixExpression(Token=curToken,
                                          Operator=curToken.Literal,
                                          Right=self.parseExpression(PREFIX))

        return expression
Beispiel #3
0
 def parse_prefix_expression(self):
     # begin = self.tracer.trace('parse_prefix_expression')
     expression = ast.PrefixExpression(self.cur_token,
                                       self.cur_token.Literal)
     self.next_token()
     expression.right = self.parse_expression(Precedence.PREFIX.value)
     # self.tracer.untrace(begin)
     return expression
Beispiel #4
0
    def parse_prefix_expression(self) -> ast.Expression:
        expression = ast.PrefixExpression(self.cur_token,
                                          self.cur_token.literal)

        self.next_token()

        expression.right = self.parse_expression(Precedence.PREFIX)

        return expression
Beispiel #5
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))
Beispiel #6
0
    def test_modify(self):
        one = lambda: ast.ExpressionStatement(expression=ast.IntegerLiteral(
            value=1))
        two = lambda: ast.ExpressionStatement(expression=ast.IntegerLiteral(
            value=2))

        def turn_one_into_two(node):
            """ Modifier function to convert IntegerLiteral of value 1 to 2 """
            integer = node
            if not isinstance(integer, ast.IntegerLiteral):
                return node
            elif integer.value != 1:
                return node
            integer.value = 2
            return integer

        tests = [
            (one(),
             ast.ExpressionStatement(expression=ast.IntegerLiteral(value=2))),
            (two(),
             ast.ExpressionStatement(expression=ast.IntegerLiteral(value=2))),
            (ast.InfixExpression(operator="+", left=one(), right=two()),
             ast.InfixExpression(operator="+", left=two(), right=two())),
            (ast.InfixExpression(operator="+", left=two(), right=one()),
             ast.InfixExpression(operator="+", left=two(), right=two())),
            (ast.PrefixExpression(operator="-", right=one()),
             ast.PrefixExpression(operator="-", right=two())),
            (ast.IndexExpression(left=one(), index=one()),
             ast.IndexExpression(left=two(), index=two())),
            (ast.IfExpression(
                condition=one(),
                consequence=ast.BlockStatement(
                    statements=[ast.ExpressionStatement(expression=one())]),
                alternative=ast.BlockStatement(
                    statements=[ast.ExpressionStatement(expression=one())])),
             ast.IfExpression(
                 condition=two(),
                 consequence=ast.BlockStatement(
                     statements=[ast.ExpressionStatement(expression=two())]),
                 alternative=ast.BlockStatement(
                     statements=[ast.ExpressionStatement(expression=two())]))),
            (ast.ReturnStatement(return_value=one()),
             ast.ReturnStatement(return_value=two())),
            (ast.LetStatement(value=one()), ast.LetStatement(value=two())),
            (ast.FunctionLiteral(
                parameters=[],
                body=ast.BlockStatement(
                    statements=[ast.ExpressionStatement(expression=one())])),
             ast.FunctionLiteral(
                 parameters=[],
                 body=ast.BlockStatement(
                     statements=[ast.ExpressionStatement(expression=two())]))),
            (ast.ArrayLiteral(elements=[one(), one()]),
             ast.ArrayLiteral(elements=[two(), two()]))
        ]
        for t in tests:
            modified = modify.Modify(t[0], turn_one_into_two)
            deep_equals = (modified == t[1])
            self.assertTrue(deep_equals,
                            'not equal. got={} want={}'.format(modified, t[1]))
        # HashLiteral needs to be tested slightly different
        hash_literal = ast.HashLiteral(pairs={one(): one(), two(): two()})
        hash_literal = modify.Modify(hash_literal, turn_one_into_two)
        for key, value in hash_literal.pairs.items():
            self.assertTrue(key == two(),
                            'value is not {}. got={}'.format(2, key))
            self.assertTrue(value == two(),
                            'value is not {}. got={}'.format(2, value))