-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_calc_nodes.py
161 lines (120 loc) · 2.97 KB
/
py_calc_nodes.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class Node:
def __init__(self):
print("init node")
def evaluate(self):
return 0
def execute(self):
return 0
class NumberNode(Node):
def __init__(self, v):
if ('.' in v):
self.value = float(v)
else:
self.value = int(v)
def evaluate(self):
return self.value
class PrintNode(Node):
def __init__(self, v):
self.value = v
def execute(self):
self.value = self.value.evaluate()
print(self.value)
class BopNode(Node):
def __init__(self, op, v1, v2):
self.v1 = v1
self.v2 = v2
self.op = op
def evaluate(self):
if (self.op == '+'):
return self.v1.evaluate() + self.v2.evaluate()
elif (self.op == '-'):
return self.v1.evaluate() - self.v2.evaluate()
elif (self.op == '*'):
return self.v1.evaluate() * self.v2.evaluate()
elif (self.op == '/'):
return self.v1.evaluate() / self.v2.evaluate()
class ModNode(Node):
def __init__(self, v1, v2):
self.v1 = v1
self.v2 = v2
def execute(self):
return self.v1.evaluate() % self.v2.evaluate()
tokens = (
'PRINT', 'LPAREN', 'RPAREN',
'NUMBER',
'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'MOD'
)
# Tokens
t_PRINT = 'print'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_MOD = 'mod'
def t_NUMBER(t):
r'-?\d*(\d\.|\.\d)\d* | \d+'
try:
print(t.value)
t.value = NumberNode(t.value)
except ValueError:
print("Integer value too large %d", t.value)
t.value = 0
return t
# Ignored characters
t_ignore = " \t"
def t_error(t):
print("Syntax error at '%s'" % t.value)
# Build the lexer
import ply.lex as lex
lex.lex()
# Parsing rules
precedence = (
('left', 'PLUS', 'MINUS'),
('left', 'TIMES', 'DIVIDE')
)
def p_print_smt(t):
"""
print_smt : PRINT LPAREN expression RPAREN
"""
t[0] = PrintNode(t[3])
def p_mod_smt(t):
"""
mod_smt : expression MOD factor
"""
t[0] = ModNode(t[1], t[3])
def p_expression_binop(t):
'''expression : expression PLUS factor
| expression MINUS factor
| expression TIMES factor
| expression DIVIDE factor'''
t[0] = BopNode(t[2], t[1], t[3])
def p_expression_factor(t):
'''expression : factor'''
t[0] = t[1]
def p_factor_number(t):
'factor : NUMBER'
t[0] = t[1]
def p_error(t):
print("Syntax error at '%s'" % t.value)
import ply.yacc as yacc
yacc.yacc()
import sys
if (len(sys.argv) != 2):
sys.exit("invalid arguments")
fd = open(sys.argv[1], 'r')
code = ""
for line in fd:
code = line.strip()
try:
lex.input(code)
while True:
token = lex.token()
if not token: break
print(token)
print(code)
ast = yacc.parse(code)
ast.execute()
except Exception:
print("ERROR")