-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
534 lines (451 loc) · 13.1 KB
/
main.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import ply.lex as lex
import ply.yacc as yacc
import sys
import re
class Node:
def __init__(self):
print("init node")
def evaluate(self):
return 0
def execute(self):
return 0
class VariableNode(Node):
def __init__(self, name):
self.name = name
def evaluate(self):
if self.name not in names:
raise Semantic_Error()
else:
return names[self.name]
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 BooleanNode(Node):
def __init__(self, v):
self.value = v
def evaluate(self):
return self.value
class StringNode(Node):
def __init__(self, v):
self.value = v
def evaluate(self):
return self.value
class ListNode(Node):
def __init__(self):
self.value = []
def evaluate(self):
return [x.evaluate() for x in self.value]
class IndexNode(Node):
def __init__(self, l, idx):
self.l = l
self.index = idx
def evaluate(self):
l = self.l.evaluate()
idx = self.index.evaluate()
if type(l) in {str, list} and type(idx) == int and (-len(l) <= idx < len(l)):
return l[idx]
else:
raise Semantic_Error()
class GroupNode(Node):
def __init__(self, v):
self.value = v
def evaluate(self):
return self.value.evaluate()
class BopNode(Node):
def __init__(self, op, v1, v2):
self.v1 = v1
self.v2 = v2
self.op = op
def evaluate(self):
num_types = {int, float}
v1 = self.v1.evaluate()
v2 = self.v2.evaluate()
if self.op == '+':
if (type(v1) in num_types and type(v2) in num_types) or ((type(v1) in {str, list}) and type(v1) == type(v2)):
return v1 + v2
else:
raise Semantic_Error()
elif self.op == '-':
if type(v1) in num_types and type(v2) in num_types:
return v1 - v2
else:
raise Semantic_Error()
elif self.op == '*':
if type(v1) in num_types and type(v2) in num_types:
return v1 * v2
else:
raise Semantic_Error()
elif self.op == '/':
if type(v1) in num_types and type(v2) in num_types and v2 != 0:
return v1 / v2
else:
raise Semantic_Error()
elif self.op == '//':
if type(v1) in num_types and type(v2) in num_types and v2 != 0:
return v1 // v2
else:
raise Semantic_Error()
elif self.op == '%':
if type(v1) in num_types and type(v2) in num_types and v2 != 0:
return v1 % v2
else:
raise Semantic_Error()
elif self.op == '**':
if type(v1) in num_types and type(v2) in num_types:
return v1**v2
else:
raise Semantic_Error()
elif self.op == 'and':
if type(v1) != bool or type(v2) != bool:
raise Semantic_Error()
else:
return v1 and v2
elif self.op == 'or':
if type(v1) != bool or type(v2) != bool:
raise Semantic_Error()
else:
return v1 or v2
elif self.op == '<':
if (type(v1) in num_types and type(v2) in num_types) or (type(v1) == type(v2) == str):
return v1 < v2
else:
raise Semantic_Error()
elif self.op == '<=':
if (type(v1) in num_types and type(v2) in num_types) or (type(v1) == type(v2) == str):
return v1 <= v2
else:
raise Semantic_Error()
elif self.op == '>':
if (type(v1) in num_types and type(v2) in num_types) or (type(v1) == type(v2) == str):
return v1 > v2
else:
raise Semantic_Error()
elif self.op == '>=':
if (type(v1) in num_types and type(v2) in num_types) or (type(v1) == type(v2) == str):
return v1 >= v2
else:
raise Semantic_Error()
elif self.op == '==':
if (type(v1) in num_types and type(v2) in num_types) or (type(v1) == type(v2) == str):
return v1 == v2
else:
raise Semantic_Error()
elif self.op == '<>':
if (type(v1) in num_types and type(v2) in num_types) or (type(v1) == type(v2) == str):
return v1 != v2
else:
raise Semantic_Error()
elif self.op == 'in':
if (type(v1) == type(v2) == str) or type(v2) == list:
return v1 in v2
else:
raise Semantic_Error()
class UopNode(Node):
def __init__(self, op, v):
self.op = op
self.value = v
def evaluate(self):
v = self.value.evaluate()
if self.op == '-':
if type(v) in {int, float}:
return -v
else:
raise Semantic_Error()
elif self.op == 'not':
if type(p[2]) == bool:
return not v
else:
raise Semantic_Error()
class PrintNode(Node):
def __init__(self, v):
self.value = v
def execute(self):
print(self.value.evaluate())
class AssignNode(Node):
def __init__(self, t, val):
self.target = t
self.value = val
def execute(self):
idxs = []
x = self.target
while (type(x)) != VariableNode:
if (type(x)) != IndexNode:
#print('not index node!!!')
raise Semantic_Error()
idx = x.index.evaluate()
x = x.l
if type(idx) == int:
idxs.insert(0, idx)
else:
raise Semantic_Error()
if len(idxs) == 0:
names[x.name] = self.value.evaluate()
else:
try:
l = names[x.name]
for idx in idxs[:-1]:
l = l[idx]
l[idxs[-1]] = self.value.evaluate()
except:
raise Semantic_Error()
class BlockNode(Node):
def __init__(self, s):
self.sl = [s]
def execute(self):
for statement in self.sl:
if statement is not None:
statement.execute()
class IfNode(Node):
def __init__(self, condition, block):
self.condition = condition
self.block = block
def execute(self):
if self.condition.evaluate() == True:
self.block.execute()
class IfElNode(Node):
def __init__(self, condition, if_block, el_block):
self.condition = condition
self.if_block = if_block
self.el_block = el_block
def execute(self):
if self.condition.evaluate() == True:
self.if_block.execute()
else:
self.el_block.execute()
class WhileNode(Node):
def __init__(self, condition, block):
self.condition = condition
self.block = block
def execute(self):
while self.condition.evaluate() == True:
self.block.execute()
class Semantic_Error(Exception):
pass
class Syntax_Error(Exception):
pass
reserved = {
'if': 'IF',
'else': 'ELSE',
'while': 'WHILE',
'print': 'PRINT',
'in': 'IN',
'not': 'NOT',
'and': 'AND',
'or': 'OR',
}
tokens = [
'NAME','NUMBER','STRING','BOOLEAN',
'ASSIGN','PLUS','MINUS','TIMES','DIVIDE','QUOTIENT','MODULUS','POW',
'LT','LEQ','GT','GEQ','EQ','NEQ',
'LPAREN','RPAREN','LBRACK','RBRACK','LBRACE','RBRACE','COMMA','SEMICOLON'
] + list(reserved.values())
# Tokens
t_ASSIGN = r'='
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_QUOTIENT = r'//'
t_MODULUS = r'%'
t_POW = r'\*\*'
t_LT = r'<'
t_LEQ = r'<='
t_GT = r'>'
t_GEQ = r'>='
t_EQ = r'=='
t_NEQ = r'<>'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LBRACK = r'\['
t_RBRACK = r'\]'
t_LBRACE = r'\{'
t_RBRACE = r'\}'
t_COMMA = r','
t_SEMICOLON = r';'
t_ignore = " \t\n"
def t_BOOLEAN(t):
r'True|False'
if t.value == 'True':
t.value = BooleanNode(True)
else:
t.value = BooleanNode(False)
return t
def t_NAME(t):
r'[a-zA-Z][a-zA-Z0-9_]*'
t.type = reserved.get(t.value,'NAME') # Check for reserved words
return t
def t_NUMBER(t):
r'-?\d*(\d\.|\.\d)\d* | \d+'
try:
t.value = NumberNode(t.value)
except ValueError:
print("Value too large %d", t.value)
t.value = 0
return t
def t_STRING(t):
r'\'[^\']*\'|\"[^\"]*\"'
t.value = StringNode(t.value[1:-1])
return t
def t_newline(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
def t_error(t):
print("Illegal character '{}'".format(t.value[0]))
t.lexer.skip(1)
lexer = lex.lex()
####################################################################
# Parsing rules
precedence = (
('nonassoc', 'LBRACE', 'RBRACE'),
('nonassoc', 'ASSIGN'),
('left','OR'),
('left','AND'),
('nonassoc','NOT'),
('nonassoc','LT','LEQ','GT','GEQ','NEQ','EQ','IN'),
('left','PLUS','MINUS'),
('left','TIMES','DIVIDE','QUOTIENT','MODULUS'),
('nonassoc','UMINUS'),
('right','POW'),
('nonassoc','LBRACK','RBRACK')
)
# dictionary of names
names = {}
def p_block(p):
'block : LBRACE inblock RBRACE'
#print('reducing inblock to block')
p[0] = p[2]
def p_inblock(p):
'inblock : inblock statement'
#print('reducing to inblock')
p[0] = p[1]
p[0].sl.append(p[2])
def p_inblock_base(p):
'inblock : statement'
#print('reducing statement to inblock')
p[0] = BlockNode(p[1])
def p_statement_print(p):
'statement : PRINT LPAREN expression RPAREN SEMICOLON'
#print('reducing to print stmt')
p[0] = PrintNode(p[3])
def p_statement_assign(p):
'statement : expression ASSIGN expression SEMICOLON'
#print('reducing to assign stmt')
p[0] = AssignNode(p[1], p[3])
def p_statement_else(p):
'statement : IF LPAREN expression RPAREN block ELSE block'
#print('reducing to ifelse stmt')
p[0] = IfElNode(p[3], p[5], p[7])
def p_statement_if(p):
'statement : IF LPAREN expression RPAREN block'
#print('reducing to if stmt')
p[0] = IfNode(p[3],p[5])
def p_statement_while(p):
'statement : WHILE LPAREN expression RPAREN block'
#print('reducing to while stmt')
p[0] = WhileNode(p[3], p[5])
def p_statement_group(p):
'statement : LBRACE statement RBRACE'
#print('reducing statement to statement')
p[0] = p[2]
def p_statement_empty(p):
'statement : empty'
#print('reducing empty to statement')
pass
def p_expression_binop(p):
'''
expression : expression PLUS expression
| expression MINUS expression
| expression TIMES expression
| expression DIVIDE expression
| expression QUOTIENT expression
| expression MODULUS expression
| expression POW expression
| expression AND expression
| expression OR expression
| expression LT expression
| expression LEQ expression
| expression GT expression
| expression GEQ expression
| expression EQ expression
| expression NEQ expression
| expression IN expression
'''
p[0] = BopNode(p[2], p[1], p[3])
def p_expression_uop(p):
'''
expression : MINUS expression %prec UMINUS
| NOT expression
'''
p[0] = UopNode(p[1], p[2])
def p_expression_group(p):
'expression : LPAREN expression RPAREN'
p[0] = p[2]
def p_list(p):
'list : LBRACK inlist RBRACK'
p[0] = p[2]
def p_inlist(p):
'inlist : inlist COMMA expression'
p[0] = p[1]
p[0].value.append(p[3])
def p_inlist_expression(p):
'inlist : expression'
p[0] = ListNode()
p[0].value.append(p[1])
def p_inlist_empty(p):
"inlist : empty"
p[0] = ListNode()
def p_expression_index(p):
'expression : expression LBRACK expression RBRACK'
p[0] = IndexNode(p[1], p[3])
def p_expression_base(p):
'''
expression : NUMBER
| STRING
| BOOLEAN
'''
p[0] = p[1]
def p_expression_list(p):
"expression : list"
p[0] = p[1]
def p_expression_name(p):
'expression : NAME'
p[0] = VariableNode(p[1])
def p_empty(p):
'empty :'
pass
def p_error(p):
'Syntax error :'
parser.token()
raise Syntax_Error()
lexer = lex.lex()
parser = yacc.yacc()
def main():
try:
f = open(sys.argv[1])
except OSError:
print('Cannot open file "{}"'.format(sys.argv[1]))
return
instream = f.read()
'''
lexer.input(instream)
while True:
tok = lexer.token()
if not tok:
break
print(tok)
'''
try:
ast = parser.parse(instream)
ast.execute()
except Semantic_Error:
print('Semantic error')
except Syntax_Error:
print('Syntax error')
f.close()
if __name__ == '__main__':
main()