コード例 #1
0
 def sequence(self) -> Expr.Expr:
     result = self.expression()
     while self.match(COMMA):
         operator = self.previous()  # save the COMMA token
         rhs = self.sequence()  # and around we go!
         result = Expr.Binary(result, operator, rhs)
     return result
コード例 #2
0
 def addition(self) -> Expr.Expr:
     result = self.multiplication()
     while self.match(MINUS, PLUS):
         # capture the -/+ that a successful match stepped over
         operator = self.previous()
         rhs = self.multiplication()
         result = Expr.Binary(result, operator, rhs)
     return result
コード例 #3
0
 def comparison(self) -> Expr.Expr:
     result = self.addition()
     while self.match(GREATER, GREATER_EQUAL, LESS, LESS_EQUAL):
         # capture the one of those, that match stepped over
         operator = self.previous()
         rhs = self.addition()
         result = Expr.Binary(result, operator, rhs)
     return result
コード例 #4
0
 def multiplication(self) -> Expr.Expr:
     result = self.unary()
     while self.match(SLASH, STAR):
         # capture the / or * that a successful match stepped over
         operator = self.previous()
         rhs = self.unary()
         result = Expr.Binary(result, operator, rhs)
     return result
コード例 #5
0
ファイル: Parser.py プロジェクト: connorjan/lox
 def multiplication(self) -> Expr.Expr:
     """ Parses a binary expression of multiplication precedence or higher"""
     expr = self.unary()
     while self.match(TokenType.SLASH, TokenType.STAR):
         operator = self.previous()
         right = self.unary()
         expr = Expr.Binary(expr, operator, right)
     return expr
コード例 #6
0
 def equality(self) -> Expr.Expr:
     result = self.comparison()
     while self.match(BANG_EQUAL, EQUAL_EQUAL):
         # capture the != or == that successful match stepped over
         operator = self.previous()
         rhs = self.comparison()
         result = Expr.Binary(result, operator, rhs)
     return result
コード例 #7
0
def main():
    astPrinter = AstPrinter()
    expression = Expr.Binary(
        Expr.Unary(Token(TokenType.MINUS, '-', None, 1), Expr.Literal(123)),
        Token(TokenType.STAR, '*', None, 1),
        Expr.Grouping(Expr.Literal(45.67)))

    print(astPrinter.print(expression))
コード例 #8
0
ファイル: Parser.py プロジェクト: connorjan/lox
 def bitor(self) -> Expr.Expr:
     """ Parses a binary expression of bitor precedence or higher """
     expr = self.bitxor()
     while self.match(TokenType.BAR):
         operator = self.previous()
         right = self.bitxor()
         expr = Expr.Binary(expr, operator, right)
     return expr
コード例 #9
0
ファイル: Parser.py プロジェクト: connorjan/lox
 def addition(self) -> Expr.Expr:
     """ Parses a binary expression of additive precedence or higher"""
     expr = self.multiplication()
     while self.match(TokenType.MINUS, TokenType.PLUS):
         operator = self.previous()
         right = self.multiplication()
         expr = Expr.Binary(expr, operator, right)
     return expr
コード例 #10
0
ファイル: Parser.py プロジェクト: connorjan/lox
 def equality(self) -> Expr.Expr:
     """ Parses a binary expression of equality precedence or higher"""
     expr = self.comparison()
     while self.match(TokenType.BANG_EQUAL, TokenType.EQUAL_EQUAL):
         operator = self.previous()
         right = self.comparison()
         expr = Expr.Binary(expr, operator, right)
     return expr
コード例 #11
0
    def addition(self) -> Expr.Expr:
        expression = self.multiplication()
        while self.match(Token.TokenType.MINUS, Token.TokenType.PLUS):
            operator = self.previous()
            right = self.multiplication()
            expression = Expr.Binary(expression, operator, right)

        return expression
コード例 #12
0
    def multiplication(self) -> Expr.Expr:
        expression = self.unary()
        while self.match(Token.TokenType.SLASH, Token.TokenType.STAR):
            operator = self.previous()
            right = self.unary()
            expression = Expr.Binary(expression, operator, right)

        return expression
コード例 #13
0
ファイル: Parser.py プロジェクト: connorjan/lox
 def exp(self) -> Expr.Expr:
     """ Parses an exponent expression """
     expr = self.call()
     while self.match(TokenType.STAR_STAR):
         operator = self.previous()
         right = self.call()
         expr = Expr.Binary(expr, operator, right)
     return expr
コード例 #14
0
ファイル: Parser.py プロジェクト: connorjan/lox
 def bitshift(self) -> Expr.Expr:
     """ Parses a binary expression of bitshift precedence or higher """
     expr = self.addition()
     while self.match(TokenType.GREATER_GREATER, TokenType.LESS_LESS):
         operator = self.previous()
         right = self.addition()
         expr = Expr.Binary(expr, operator, right)
     return expr
コード例 #15
0
    def factor(self):
        expr = self.unary()

        while self.match(TokenType.SLASH, TokenType.STAR):
            operator = self.previous()
            right = self.unary()
            expr = Expr.Binary(expr, operator, right)

        return expr
コード例 #16
0
    def term(self):
        expr = self.factor()

        while self.match(TokenType.PLUS, TokenType.MINUS):
            operator = self.previous()
            right = self.factor()
            expr = Expr.Binary(expr, operator, right)

        return expr
コード例 #17
0
    def equality(self):
        expr = self.comparison()

        while self.match(TokenType.BANG_EQUAL, TokenType.EQUAL_EQUAL):
            operator = self.previous()
            right = self.comparison()
            expr = Expr.Binary(expr, operator, right)

        return expr
コード例 #18
0
ファイル: Parser.py プロジェクト: connorjan/lox
 def comparison(self) -> Expr.Expr:
     """ Parses a binary expression of comparitive precedence or higher"""
     expr = self.bitor()
     while self.match(TokenType.GREATER, TokenType.GREATER_EQUAL,
                      TokenType.LESS, TokenType.LESS_EQUAL):
         operator = self.previous()
         right = self.bitor()
         expr = Expr.Binary(expr, operator, right)
     return expr
コード例 #19
0
    def comparison(self) -> Expr.Expr:
        expression = self.addition()
        while self.match(Token.TokenType.GREATER,
                         Token.TokenType.GREATER_EQUAL, Token.TokenType.LESS,
                         Token.TokenType.LESS_EQUAL):
            operator = self.previous()
            right = self.addition()
            expression = Expr.Binary(expression, operator, right)

        return expression
コード例 #20
0
ファイル: AstPrinter.py プロジェクト: Holland-k/plox
def run_main():
    exp = Expr.Binary(
        Expr.Unary(
            Token(TokenType.MINUS, "-", null, 1),
            Expr.Litaral(123)),
        Toke(TokenType.Star, "*", null, 1),
        Expr.Grouping(
            Expr.Literal(45.67)))

    print(AstPrinter.print(exp))
コード例 #21
0
 def multiplication(self):
     '''
     Similar process to "equality" with the left and right operands recursively descending
     from "unary".
     We bundle again into an Expression syntax class and return   
     '''
     expr = self.unary()
     while self.match(TokenType.SLASH, TokenType.STAR):
         operator = self.previous()
         right = self.unary()
         expr = Expr.Binary(expr, operator, right)
     return expr
コード例 #22
0
 def comparison(self):
     '''
     Similar process to "equality" with the left and right operands recursively descending 
     from "addition". 
     We bundle again into an Expression syntax class and return 
     '''
     expr = self.addition()
     while self.match(TokenType.GREATER, TokenType.GREATER_EQUAL,
                      TokenType.LESS, TokenType.LESS_EQUAL):
         operator = self.previous()
         right = self.addition()
         expr = Expr.Binary(expr, operator, right)
     return expr
コード例 #23
0
    def comparison(self):
        expr = self.term()

        while self.match(
                TokenType.GREATER,
                TokenType.GREATER_EQUAL,
                TokenType.LESS,
                TokenType.LESS_EQUAL,
        ):
            operator = self.previous()
            right = self.term()
            expr = Expr.Binary(expr, operator, right)

        return expr
コード例 #24
0
 def equality(self):
     '''
     We call the comparison() rule first to define our left operand. After that's done, 
     which means it ended up in some type of "primitive" terminal, we can then check 
     if our tokens are "!=" or "==". We store that in our "operator" and, like the left
     operand, do the same recursion for the right operand. 
     We bundle these operands and the operator into an Expression syntax class and return
     '''
     expr = self.comparison()
     while self.match(TokenType.BANG_EQUAL, TokenType.EQUAL_EQUAL):
         operator = self.previous()
         right = self.comparison()
         expr = Expr.Binary(expr, operator, right)
     return expr
コード例 #25
0
ファイル: Parser.py プロジェクト: Jin-W-FS/pyLox
 def comparison(self):
     ast = self.addition()
     while self.match(TokenType.GREATER, TokenType.GREATER_EQUAL,
                      TokenType.LESS, TokenType.LESS_EQUAL):
         ast = Expr.Binary(ast, self.nextToken(), self.addition())
     return ast
コード例 #26
0
ファイル: ASTPrinter.py プロジェクト: varghesetom/PyLox
        return self.parenthesize(expr.operator.lexeme, expr.left, expr.right) 

    def visit_Grouping(self, expr):
        assert isinstance(expr, Expr.Grouping), "Must be Grouping Expression otherwise cannot print its AST" 
        return self.parenthesize("group", expr.expression) 

    def visit_Literal(self, expr):
        assert isinstance(expr, Expr.Literal), "Must be Literal Expression otherwise cannot print its AST" 
        return str(expr.value)

    def visit_Unary(self, expr):
        assert isinstance(expr, Expr.Unary), "Must be Unary Expression otherwise cannot print its AST"
        return self.parenthesize(expr.operator.lexeme, expr.right) 

    def parenthesize(self, name, *exprs):
        builder = ["(", name] 
        for expr in exprs:
            builder.append(expr.accept(self)) 
        builder.append(")") 
        return " ".join(builder) 

if __name__ == "__main__":
    ## Test Expression below 
    left_expression = Expr.Unary(Token(TokenType.MINUS, "-", 1, None), Expr.Literal(123)) 
    operator = Token(TokenType.STAR, "*", 1, None) 
    right_expression = Expr.Grouping(Expr.Literal(45.67)) 
    main_expression = Expr.Binary(left_expression, operator, right_expression) 
    print(ASTPrinter().print_ast(main_expression))


コード例 #27
0
ファイル: Parser.py プロジェクト: Jin-W-FS/pyLox
 def multiplication(self):
     ast = self.unary()
     while self.match(TokenType.SLASH, TokenType.STAR):
         ast = Expr.Binary(ast, self.nextToken(), self.unary())
     return ast
コード例 #28
0
def _test():
    ast = Expr.Binary(
        Expr.Unary(Token(TokenType.MINUS, "-", None, 1), Expr.Literal(123)),
        Token(TokenType.STAR, "*", None, 1),
        Expr.Grouping(Expr.Literal(45.67)))
    print(LispPrinter().visit(ast))
コード例 #29
0
    def visit_binary_expr(self, expr: Expr.Binary):
        return self.parenthesize(expr.operator.lexeme, expr.left, expr.right)

    def visit_grouping_expr(self, expr: Expr.Grouping):
        return self.parenthesize("group", expr.expression)

    def visit_literal_expr(self, expr: Expr.Literal):
        return str(expr.value) if expr.value != None else "nil"

    def visit_unary_expr(self, expr: Expr.Unary):
        return self.parenthesize(expr.operator.lexeme, expr.right)

    def parenthesize(self, name: str, *expressions):
        res = "(" + name

        for expression in expressions:
            res += " " + expression.accept(self)

        res += ")"

        return res


if __name__ == "__main__":
    expression = Expr.Binary(
        Expr.Unary(Token.Token(Token.TokenType.MINUS, "-", None, 1),
                   Expr.Literal(123)),
        Token.Token(Token.TokenType.STAR, "*", None, 1),
        Expr.Grouping(Expr.Literal(45.67)))

    print(AstPrinter().print_ast(expression))
コード例 #30
0
ファイル: Parser.py プロジェクト: Jin-W-FS/pyLox
 def addition(self):
     ast = self.multiplication()
     while self.match(TokenType.MINUS, TokenType.PLUS):
         ast = Expr.Binary(ast, self.nextToken(), self.multiplication())
     return ast