-
Notifications
You must be signed in to change notification settings - Fork 0
/
pprint_ast.py
53 lines (40 loc) · 1.69 KB
/
pprint_ast.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from typing import List
from expr import Binary, Expr, Grouping, IVisitor, Literal, Unary
from collections import deque
from token_ import Token
from token_type import TokenType
class AstPrinter(IVisitor):
'''Given a Abstract Syntax Tree, AstPrinter.print() produces a string of text that is
valid syntax in source language (used for post-order traversal eval on AST as done by the interpreter)
Adds functionality to the Expr subclasses
'''
def print(self, expr: Expr):
return expr.accept(self)
def visit_binary_expr(self, expr: Binary):
return self._parenthesize(expr.operator.lexeme, expr.left, expr.right)
def visit_grouping_expr(self, expr: Grouping):
return self._parenthesize("group", expr.expression)
def visit_literal_expr(self, expr: Literal):
if expr.value is None:
return "nil"
return str(expr.value)
def visit_unary_expr(self, expr: Unary):
return self._parenthesize(expr.operator.lexeme, expr.right)
def _parenthesize(self, name: str, *expr_attrs: List[str]):
''' Builds the expression as a string in prefix/polish notation (similar to lisp)'''
out = deque()
out.append("(")
out.append(name)
for expr in expr_attrs:
out.append(" ")
out.append(expr.accept(self))
out.append(")")
return ''.join(out)
def test():
expression = Binary(Unary(Token('MINUS', "-", None, 1), Literal(123)),
Token('STAR', "*", None, 1),
Grouping(Literal(45.67))) # Visitor
print(
AstPrinter().print(expression)) # Expected: (* (- 123) (group 45.67))
if __name__ == "__main__":
test()