-
Notifications
You must be signed in to change notification settings - Fork 0
/
AstPrinter.py
39 lines (31 loc) · 1.19 KB
/
AstPrinter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import Expr
import Token
class AstPrinter(Expr.ExprVisitor):
def print_ast(self, expression : Expr.Expr):
return expression.accept(self)
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))