-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyParser.py
331 lines (240 loc) · 9.09 KB
/
MyParser.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
INTEGER, PLUS, EOF, FUNC, CHAR, VAR, ASSIGN, WORD, ERROR = (
"INTEGER",
"PLUS",
"EOF",
"FUNC",
"CHAR",
"VAR",
"=",
"WORD",
"ERROR",
)
decl_variable_statement = {}
CURRENT_SCOPE = {}
GLOBAL_SCOPE = {}
LOCAL_LOG = []
def switchScope(scope):
"""Switches scopes used by statements during execution"""
global CURRENT_SCOPE
CURRENT_SCOPE = scope
def my_print(*txt):
"""Utility printing function. Appends text to LOCAL_LOG"""
for n in txt:
LOCAL_LOG.append(n)
class Token(object):
"""Represents token identifier and its declared value"""
def __init__(self, type, value):
self.type = type
self.value = value
def __str__(self):
return "Token({type}, {value})".format(type=self.type, value=repr(self.value))
def __repr__(self):
return self.__str__()
class StatementDeclareVariable(object):
"""Declares a variable in current scope with given name and value"""
def __init__(self, name, value):
self.name = name
self.value = value
def __str__(self):
return "StatementDeclareVariable({name}, {value})".format(
name=self.name, value=repr(self.value)
)
def __repr__(self):
return self.__str__()
def execute(self):
my_print("declaring variable:", self.name, "=", self.value)
CURRENT_SCOPE[self.name] = self.value
class StatementDeclareFunction(object):
"""Declares a function in global scope with given name and statements(function body)"""
def __init__(self, name, statements):
self.name = name
self.statements = statements
def __str__(self):
return "StatementDeclareFunction({name}, {statements})".format(
name=self.name, statements=repr(self.statements)
)
def __repr__(self):
return self.__str__()
def execute(self):
my_print("declaring function named:", self.name)
GLOBAL_SCOPE[self.name] = self
def INVOKE(self):
LOCAL_SCOPE = {}
switchScope(LOCAL_SCOPE)
for statement in self.statements:
statement.execute()
switchScope(GLOBAL_SCOPE)
class StatementAdd(object):
"""Adds value of var2 to var1 and saves result in var1."""
def __init__(self, var1, var2):
self.var1 = var1
self.var2 = var2
def __str__(self):
return "StatementAdd({var1}, {var2})".format(var1=self.var1, var2=self.var2)
def __repr__(self):
return self.__str__()
def execute(self):
var1 = CURRENT_SCOPE[self.var1]
var2 = CURRENT_SCOPE[self.var2]
var3 = var1 + var2
CURRENT_SCOPE[self.var1] = var3
my_print(
"adding:", var1, "+", var2, "result =", var3, "saved under:" + self.var1
)
class StatementPrint(object):
"""Prints variable name or text(if variable under given name was not found)"""
def __init__(self, var1):
self.var1 = var1
def __str__(self):
return "StatementPrint({var1})".format(var1=self.var1,)
def __repr__(self):
return self.__str__()
def execute(self):
if self.var1 in CURRENT_SCOPE:
var1 = CURRENT_SCOPE[self.var1]
my_print(var1)
else:
my_print(self.var1)
class StatementInvokeFunction(object):
"""Invokes function with given name by executing all its statement in order."""
def __init__(self, var1):
self.var1 = var1
def __str__(self):
return "StatementInvokeFunction({var1})".format(var1=self.var1,)
def __repr__(self):
return self.__str__()
def execute(self):
var1 = GLOBAL_SCOPE[self.var1]
return var1.INVOKE()
class Interpreter(object):
"""Interprets passed program text string. Can be executed to run each interpreted statement."""
def __init__(self, split_words):
self.current_word = 0
self.split_words = split_words
self.final_program = []
self.lookup={
"function":self.function_statement,
"variable":self.variable_statement,
"add":self.add_statement,
"print":self.print_statement,
"invoke":self.invoke_statement,
}
def execute_program(self):
"""Executes each interpreted statement."""
for statement in self.final_program:
statement.execute()
def step(self):
"""Does one step of interpretation."""
next_word = self.split_words[self.current_word]
statement = self.interpret_token(next_word)
self.final_program.append(statement)
return self.current_word >= len(self.split_words)
def add_statement(self):
"""Appends statement to program list."""
self.current_word = self.current_word + 1
var1 = self.split_words[self.current_word]
self.current_word = self.current_word + 1
var2 = self.split_words[self.current_word]
self.current_word = self.current_word + 1
final_add_statement = StatementAdd(str(var1), str(var2))
my_print(final_add_statement)
return final_add_statement
def function_statement(self):
'''Interprets next words as function statement.
Returns:
function statement
'''
self.current_word = self.current_word + 1
function_name = self.split_words[self.current_word]
self.current_word = self.current_word + 1
self.current_word = self.current_word + 1 # {
statements = []
while True:
next_token = self.split_words[self.current_word]
if next_token == "}":
break
statement = self.interpret_token(next_token)
statements.append(statement)
self.current_word = self.current_word + 1 # }
final_function_statement = StatementDeclareFunction(function_name, statements)
my_print(final_function_statement)
return final_function_statement
def variable_statement(self):
'''Interprets next words as variable statement.
Returns:
variable statement
'''
ignore = self.split_words[self.current_word]
self.current_word = self.current_word + 1
variable_name = self.split_words[self.current_word]
self.current_word = self.current_word + 1
equation_sign = self.split_words[self.current_word]
self.current_word = self.current_word + 1
if equation_sign != "=":
raise Exception("Expected =. Actual:" + equation_sign)
variable_value = self.split_words[self.current_word]
self.current_word = self.current_word + 1
statement = StatementDeclareVariable(variable_name, int(variable_value))
return statement
def print_statement(self):
'''Interprets next words as print statement.
Returns:
print statement
'''
ignore = self.split_words[self.current_word]
self.current_word = self.current_word + 1
variable_name = self.split_words[self.current_word]
self.current_word = self.current_word + 1
statement = StatementPrint(variable_name)
return statement
def invoke_statement(self):
'''Interprets next words as function invocation statement.
Returns:
function invoke statement
'''
ignore = self.split_words[self.current_word]
self.current_word = self.current_word + 1
variable_name = self.split_words[self.current_word]
self.current_word = self.current_word + 1
statement = StatementInvokeFunction(variable_name)
return statement
def interpret_token(self, token):
'''Interprets next words one of possible statements
Raises
------
Exception
Invalid token
'''
return self.lookup[token]()
if True:
raise Exception("Error invalid token:" + token)
def error(self):
raise Exception("Error parsing input")
def INTERPRET(text):
'''Interprets text program
Returns:
Interpreter with interpreted program
'''
splitted = text.split(" ")
interpreter = Interpreter(splitted)
while True:
flag = interpreter.step()
if flag == True:
break
return interpreter
def RUN_PROGRAM(interpreter):
'''Runs program interpreted by given interpreter
Returns:
log of program execution
'''
LOCAL_LOG.clear()
interpreter.execute_program()
return LOCAL_LOG
def main():
text = "function hi { print hello } function something { variable z = 5 variable x = 150 add x z invoke hi } invoke something print x print x"
interpreter = INTERPRET(text)
ret = RUN_PROGRAM(interpreter)
print(ret)
return
if __name__ == "__main__":
main()