-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpretty_printer.py
More file actions
37 lines (29 loc) · 797 Bytes
/
pretty_printer.py
File metadata and controls
37 lines (29 loc) · 797 Bytes
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
from __future__ import print_function
from tokens import *
def pretty_print(goal):
indent=0
print_goal(goal)
def print_goal(goal):
print_main(goal.main)
def print_main(main):
print('Main: [')
for s in main.statements:
print_statement(s, 4)
print(']')
def print_statement(stmt, indent):
if isinstance(stmt, PrintStatement):
print_print_stmt(stmt, indent)
def print_print_stmt(stmt, indent):
print('%sPrint: ' % (' '*indent), end='')
print_expr(stmt.expr, 0)
def print_expr(expr, indent):
if isinstance(expr, AddExpression):
print_add_expr(expr)
else:
print_int_expr(expr)
def print_add_expr(expr):
print(expr.lhs, end='')
print('+',end='')
print(expr.rhs)
def print_int_expr(expr):
print(expr.num)