Пример #1
0
def main():
    """ Print test expression """
    expression = loxExprAST.Binary(
        loxExprAST.Unary(
            loxtoken.Token(loxtoken.TokenType.MINUS, "-", None, 1),
            loxExprAST.Literal(123)),
        loxtoken.Token(loxtoken.TokenType.STAR, "*", None, 1),
        loxExprAST.Grouping(loxExprAST.Literal(45.67)))

    print(ASTPrinter().print(expression))
Пример #2
0
    def multiplication(self) -> loxExprAST.Expr:
        """ multiplication → unary ( ( "/" | "*" ) unary )* """

        expr: loxExprAST.Expr = self.unary()

        while self.match(Parser.tokentypes.SLASH, Parser.tokentypes.STAR):
            operator: loxtoken.Token = self.previous()
            right_expr: loxExprAST.Expr = self.unary()
            expr = loxExprAST.Binary(expr, operator, right_expr)

        return expr
Пример #3
0
    def addition(self) -> loxExprAST.Expr:
        """ addition → multiplication ( ( "-" | "+" ) multiplication )* """

        expr: loxExprAST.Expr = self.multiplication()

        while self.match(Parser.tokentypes.MINUS, Parser.tokentypes.PLUS):
            operator: loxtoken.Token = self.previous()
            right_expr: loxExprAST.Expr = self.multiplication()
            expr = loxExprAST.Binary(expr, operator, right_expr)

        return expr
Пример #4
0
    def equality(self) -> loxExprAST.Expr:
        """ equality → comparison ( ( "!=" | "==" ) comparison )* """

        expr: loxExprAST.Expr = self.comparison()

        while self.match(Parser.tokentypes.BANG_EQUAL,
                         Parser.tokentypes.EQUAL_EQUAL):
            operator: loxtoken.Token = self.previous()
            right_expr: loxExprAST.Expr = self.comparison()
            expr = loxExprAST.Binary(expr, operator, right_expr)

        return expr
Пример #5
0
    def comparison(self) -> loxExprAST.Expr:
        """ comparison → addition ( ( ">" | ">=" | "<" | "<=" ) addition )* """

        expr: loxExprAST.Expr = self.addition()

        while self.match(Parser.tokentypes.GREATER,
                         Parser.tokentypes.GREATER_EQUAL,
                         Parser.tokentypes.LESS, Parser.tokentypes.LESS_EQUAL):
            operator: loxtoken.Token = self.previous()
            right_expr: loxExprAST.Expr = self.addition()
            expr = loxExprAST.Binary(expr, operator, right_expr)

        return expr