def _multiplication(self): expr = self._unary() while self._match('SLASH', 'STAR'): operator = self._previous() right = self._unary() expr = Binary(expr, operator, right) return expr
def _comparison(self) -> Expr: expr = self._addition() while self._match('LESS', 'LESS_EQUAL', 'GREATER', 'GREATER_EQUAL'): operator = self._previous() right = self._addition() expr = Binary(expr, operator, right) return expr
def _addition(self): expr = self._multiplication() while self._match('MINUS', 'PLUS'): operator = self._previous() right = self._multiplication() expr = Binary(expr, operator, right) return expr
def comparison(self): expr = self.addition() while self.match(TT.GREATER, TT.GREATER_EQUAL, TT.LESS, TT.LESS_EQUAL): operator = self.previous() right = self.addition() expr = Binary(expr, operator, right) return expr
def _equality(self) -> Expr: expr = self._comparison() while self._match('BANG_EQUAL', 'EQUAL_EQUAL'): operator: Token = self._previous( ) #Since match() advances the parser right: Expr = self._comparison() expr = Binary(expr, operator, right) return expr
def exponentiation(self): expr = self.unary() while self.match(TT.HAT): operator = self.previous() right = self.unary() expr = Binary(expr, operator, right) return expr
def multiplication(self): expr = self.exponentiation() while self.match(TT.SLASH, TT.STAR): operator = self.previous() right = self.exponentiation() expr = Binary(expr, operator, right) return expr
def addition(self): expr = self.list_construction() while self.match(TT.MINUS, TT.PLUS): operator = self.previous() right = self.list_construction() expr = Binary(expr, operator, right) return expr
def equality(self): expr = self.comparison() while self.match(TT.BANG_EQUAL, TT.EQUAL): operator = self.previous() right = self.comparison() expr = Binary(expr, operator, right) return expr
def factor(self): expr = self.unary() while self.match(TokenType.SLASH, TokenType.STAR): operator = self.previous() right = self.unary() expr = Binary(expr, operator, right) return expr
def term(self): expr = self.factor() while self.match(TokenType.MINUS, TokenType.PLUS): operator = self.previous() right = self.factor() expr = Binary(expr, operator, right) return expr
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 = Binary(expr, operator, right) return expr
class ASTPrinter(Expr.Visitor): def print(self, expr: Expr) -> str: return expr.accept(self) def visitBinaryExpr(self, expr: 'Binary'): return self.parenthesize(expr.operator.lexeme, expr.left, expr.right) def visitGroupingExpr(self, expr: 'Grouping'): return self.parenthesize("group", expr.expression) def visitLiteralExpr(self, expr: 'Literal'): if expr.value is None: return 'nil' return str(expr.value) def visitUnaryExpr(self, expr: 'Unary'): return self.parenthesize(expr.operator.lexeme, expr.right) def parenthesize(self, name: str, *exprs): ret = '(' + name for expr in exprs: ret += " " ret += expr.accept(self) ret += ")" return ret expression = Binary(Unary(Token(TokenType.MINUS, "-", None, 1), Literal(123)), Token(TokenType.STAR, "*", None, 1), Grouping(Literal(45.67))) print(ASTPrinter().print(expression))