-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimplyMain.py
763 lines (640 loc) · 20.7 KB
/
SimplyMain.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
"""
The SimplyNotJavaOrC interpreter. The semantics of language are as follows:
- Math operators: +, -, *, /, **
- Parenthesis ()
- All operations follow the usual order of operations.
- SinplyNotJavaOrC does floating point or int arithmetic based on if assignment or read is used.
- Read converts anything numeric to a floating point.
- Read command can read a variable from the user.
READ x
Prompt: "x := "
- Assignment is performed by ":="
x := 2
- PRINT < arglist > will print the value or values.
PRINT "HELLO", "WORLD"
- REFER TO THE BNF FOR THE REST OF THE SEMANTICS AND SYNTAX OF THE LANGUAGE
Errors
- identify context sensitive errors
- Variables must be declared before the language allows a person to use them.
- Read or assignment allow users to get input from the user or manually assign a value via assignment.
- The langauge will break typing if made to do so. If a int or float is put in a string array it will put in a int or float because math is a lot more important than strings in this language.
"""
from SimplyParser import Parser, ParseType, ParseTree
from SimplyLexer import Token, Lexer
import sys
from enum import Enum, auto
from collections import ChainMap
import pdb
import argparse
class RefType(Enum):
ID = auto()
FUNCTION = auto()
class Ref:
"""
Reference which is bound to a name.
"""
def __init__(self, ref_type, ref_value):
self.ref_type = ref_type
self.ref_value = ref_value # value of any variable
class RefEnv:
# Has 2 fields, ref type and ref value
def __init__(self, parent=None):
self.tab = ChainMap()
if parent:
self.tab = ChainMap(self.tab, parent.tab)
self.return_value = None
self.break_val = False
def lookup(
self,
name): # change ref value after a lookup is how you do an assignment
"""
Search for a symbol in the reference environment.
"""
# try to find the symbol
if name not in self.tab:
return None
return self.tab[name]
def insert(self, name, ref):
"""
Insert a symbol into the inner-most reference environment.
"""
self.tab[name] = ref
def eval_parse_tree(t, env):
"""
Evaluate the parse tree
"""
if t.node_type == ParseType.PROGRAM:
return eval_program(t, env)
elif t.node_type == ParseType.ATOMIC:
return eval_atomic(t, env)
elif t.node_type == ParseType.ASSIGN:
return eval_assign(t, env)
elif t.node_type == ParseType.ADD:
return eval_add(t, env)
elif t.node_type == ParseType.SUB:
return eval_sub(t, env)
elif t.node_type == ParseType.MUL:
return eval_mul(t, env)
elif t.node_type == ParseType.DIV:
return eval_div(t, env)
elif t.node_type == ParseType.POW:
return eval_pow(t, env)
elif t.node_type == ParseType.NEG:
return eval_neg(t, env)
elif t.node_type in (ParseType.IF, ParseType.IFELSE):
return eval_branch(t, env)
elif t.node_type == ParseType.LT:
return eval_lt(t, env)
elif t.node_type == ParseType.ET:
return eval_et(t, env)
elif t.node_type == ParseType.PRINT:
return eval_print(t, env)
elif t.node_type == ParseType.CALL:
return eval_call(t, env)
elif t.node_type == ParseType.RETURN:
return eval_return(t, env)
elif t.node_type == ParseType.NE:
return eval_ne(t, env)
elif t.node_type == ParseType.LTE:
return eval_lte(t, env)
elif t.node_type == ParseType.GT:
return eval_gt(t, env)
elif t.node_type == ParseType.GTE:
return eval_gte(t, env)
elif t.node_type == ParseType.ARRAY:
return eval_array(t, env)
elif t.node_type == ParseType.BLOCK:
return eval_block(t, env)
elif t.node_type == ParseType.STATEMENT:
return eval_statement(t, env)
elif t.node_type == ParseType.WHILE:
return eval_while(t, env)
elif t.node_type == ParseType.IMPORT:
return eval_import(t, env)
elif t.node_type == ParseType.SPLIT:
return eval_split(t, env)
elif t.node_type == ParseType.STATEMENT_LIST:
return eval_statement_list(t, env)
elif t.node_type == ParseType.PARAMLIST:
return eval_paramlist(t, env)
elif t.node_type == ParseType.BREAK:
return eval_break(t, env)
elif t.node_type == ParseType.READ:
return eval_read(t, env)
elif t.node_type == ParseType.ARGLIST:
return eval_arglist(t, env)
elif t.node_type == ParseType.REFLIST:
return eval_reflist(t, env)
elif t.node_type == ParseType.REF:
return eval_ref(t, env)
elif t.node_type == ParseType.DEF:
return eval_def(t, env)
elif t.node_type == ParseType.BOUNDS:
return eval_bounds(t, env)
elif t.node_type == ParseType.SWAP:
return eval_swap(t, env)
def eval_program(t, env):
"""
Evaluate the program
"""
fun_result = None
for c in t.children:
result = eval_parse_tree(c, env)
# remember any non-none result
if result is not None:
fun_result = result
# check to see if we have returned
if env.return_value is not None:
return env.return_value
# no break here
return fun_result
def eval_atomic(t, env):
"""
Evaluate the atomic value.
"""
# get literals
if t.token.token in (Token.INTLIT, Token.FLOATLIT, Token.STRINGLIT, Token.CHARLIT):
return t.token.value
elif (t.token.token == Token.CHARACTER) or (t.token.token == Token.STRING) or (t.token.token == Token.BOOL) or (t.token.token == Token.NUMBER):
bind(env, t.children[0].token.lexeme, Ref(RefType.ID, None))
else:
# get the variable
v = env.lookup(t.token.lexeme)
if not v:
print(f"Undefined variable {t.token.lexeme} on line {t.token.line}")
sys.exit(-1)
elif v.ref_type != RefType.ID:
print(f"{t.token.lexeme} on line {t.token.line} is not a variable.")
sys.exit(-1)
return v.ref_value
def bind(env, name, ref):
"""
Bind the name in env to ref according to the correct scope
resolution rules. DOES IT WORK FOR ARRAYS?
"""
v = env.lookup(env)
if v:
# rebind to an existing name
v.ref_value = ref.ref_value
v.ref_type = ref.ref_type
else:
env.insert(name, ref)
def eval_read(t, env):
"""
Evaluate an input statement
"""
for c in t.children[0].children:
if len(c.children) == 1:
x = c.children[0].token.lexeme
value = env.lookup(x)
if (type(value.ref_value) == list) and (type(value.ref_value[0]) == list):
for i in range(0, len(value.ref_value)):
for y in range(0, len(value.ref_value[i])):
value2 = input(x + "[" + str(i) + "," + str(y) + "] := ")
if value2.isdigit():
value.ref_value[i][y] = int(value2)
else:
try:
float(value2)
value.ref_value[i][y] = float(value2)
except:
value.ref_value[i][y] = value2
elif (type(value.ref_value) == list) and (type(value.ref_value[0]) != list):
for i in range(0, len(value.ref_value)):
value2 = input(x + "[" + str(i) + "] := ")
if value2.isdigit():
value.ref_value[i] = int(value2)
else:
try:
float(value2)
value.ref_value[i] = float(value2)
except:
value.ref_value[i] = value2
else:
value3 = input(x + " := ")
if value3.isdigit():
value.ref_value = int(value3)
else:
try:
float(value3)
value.ref_value = float(value3)
except:
value.ref_value = value3
else:
x = c.children[0].token.lexeme
ind = eval_parse_tree(c.children[1], env)
if len(ind) == 1:
value = env.lookup(x)
value5 = input(x + "[" + str(ind[0]) + "] := ")
if value5.isdigit():
value.ref_value[ind[0]] = int(value5)
else:
try:
float(value5)
value.ref_value[ind[0]] = float(value5)
except:
value.ref_value[ind[0]] = value5
else:
value = env.lookup(x)
value5 = input(x + "[" + str(ind[0]) + "," + str(ind[1]) + "] := ")
if value5.isdigit():
value.ref_value[ind[0]][ind[1]] = int(value5)
else:
try:
float(value5)
value.ref_value[ind[0]][ind[1]] = float(value5)
except:
value.ref_value[ind[0]][ind[1]] = value5
def eval_print(t, env):
"""
Evaluate a print statement
"""
val = eval_parse_tree(t.children[0], env)
for i in range(0, len(val)):
print(str(val[i]))
def eval_assign(t, env):
if t.children[0].node_type == ParseType.ARRAY:
v2 = t.children[0].children[0].token.lexeme
ind = eval_parse_tree(t.children[0].children[1], env)
if len(ind) == 1:
value = env.lookup(v2)
value.ref_value[ind[0]] = eval_parse_tree(t.children[1], env)
else:
value = env.lookup(v2)
value.ref_value[ind[0]][ind[1]] = eval_parse_tree(t.children[1], env)
else:
value = env.lookup(t.children[0].token.lexeme)
value.ref_value = eval_parse_tree(t.children[1], env)
def eval_add(t, env):
"""
Evaluate an addition operation.
"""
execute = True
# Type checking if left side is ref
if t.children[0].node_type == ParseType.REF:
value = eval_parse_tree(t.children[0], env)
if type(value) not in (int, float):
execute = False
else: # Type checking if left side is not ref
if type(t.children[0].token.value) not in (int, float):
execute = False
# Type checking if right side is ref
if t.children[1].node_type == ParseType.REF:
value2 = eval_parse_tree(t.children[1], env)
if type(value2) not in (int, float):
execute = False
else: # Type checking if right side is not ref
if type(t.children[1].token.value) not in (int, float):
execute = False
if execute:
return eval_parse_tree(t.children[0], env) + eval_parse_tree(t.children[1], env)
elif not execute:
print("Cannot add these types.")
sys.exit(-1)
def eval_sub(t, env):
"""
Evaluate an subtraction operation.
"""
execute = True
# Type checking if left side is ref
if t.children[0].node_type == ParseType.REF:
value = eval_parse_tree(t.children[0], env)
if type(value) not in (int, float):
execute = False
else: # Type checking if left side is not ref
if type(t.children[0].token.value) not in (int, float):
execute = False
# Type checking if right side is ref
if t.children[1].node_type == ParseType.REF:
value2 = eval_parse_tree(t.children[1], env)
if type(value2) not in (int, float):
execute = False
else: # Type checking if right side is not ref
if type(t.children[1].token.value) not in (int, float):
execute = False
if execute:
return eval_parse_tree(t.children[0], env) - eval_parse_tree(t.children[1], env)
elif not execute:
print("Cannot subtract these types.")
sys.exit(-1)
def eval_mul(t, env):
"""
Evaluate an multiplication operation.
"""
execute = True
# Type checking if left side is ref
if t.children[0].node_type == ParseType.REF:
value = eval_parse_tree(t.children[0], env)
if type(value) not in (int, float):
execute = False
else: # Type checking if left side is not ref
if type(t.children[0].token.value) not in (int, float):
execute = False
# Type checking if right side is ref
if t.children[1].node_type == ParseType.REF:
value2 = eval_parse_tree(t.children[1], env)
if type(value2) not in (int, float):
execute = False
else: # Type checking if right side is not ref
if type(t.children[1].token.value) not in (int, float):
execute = False
if execute:
return eval_parse_tree(t.children[0], env) * eval_parse_tree(t.children[1], env)
elif not execute:
print("Cannot multiply these types.")
sys.exit(-1)
def eval_div(t, env):
"""
Evaluate an multiplication operation.
"""
execute = True
# Type checking if left side is ref
if t.children[0].node_type == ParseType.REF:
value = eval_parse_tree(t.children[0], env)
if type(value) not in (int, float):
execute = False
else: # Type checking if left side is not ref
if type(t.children[0].token.value) not in (int, float):
execute = False
# Type checking if right side is ref
if t.children[1].node_type == ParseType.REF:
value2 = eval_parse_tree(t.children[1], env)
if type(value2) not in (int, float):
execute = False
if value2 == 0:
print(f"Division by 0 on line {t.token.line}.")
sys.exit(-1)
else: # Type checking if right side is not ref
if type(t.children[1].token.value) not in (int, float):
execute = False
elif eval_parse_tree(t.children[1], env) == 0:
print(f"Division by 0 on line {t.token.line}.")
sys.exit(-1)
if execute:
return eval_parse_tree(t.children[0], env) / eval_parse_tree(t.children[1], env)
elif not execute:
print("Cannot divide these types.")
sys.exit(-1)
def eval_pow(t, env):
"""
Evaluate an exponent operation.
"""
execute = True
# Type checking if left side is ref
if t.children[0].node_type == ParseType.REF:
value = eval_parse_tree(t.children[0], env)
if type(value) not in (int, float):
execute = False
else: # Type checking if left side is not ref
if type(t.children[0].token.value) not in (int, float):
execute = False
# Type checking if right side is ref
if t.children[1].node_type == ParseType.REF:
value2 = eval_parse_tree(t.children[1], env)
if type(value2) not in (int, float):
execute = False
else: # Type checking if right side is not ref
if type(t.children[1].token.value) not in (int, float):
execute = False
if execute:
return eval_parse_tree(t.children[0], env)**eval_parse_tree(t.children[1], env)
elif not execute:
print("Cannot perform power on these types.")
sys.exit(-1)
def eval_neg(t, env):
"""
Evaluate a negation
"""
execute = True
# Type checking if left side is ref
if t.children[0].node_type == ParseType.REF:
value = eval_parse_tree(t.children[0], env)
if type(value) not in (int, float):
execute = False
else: # Type checking if left side is not ref
if type(t.children[0].token.value) not in (int, float):
execute = False
if execute:
return -eval_parse_tree(t.children[0], env)
elif not execute:
print("Cannot negate this type.")
sys.exit(-1)
def eval_branch(t, env):
"""
Evaluate a branch
"""
if eval_parse_tree(t.children[0], env):
eval_parse_tree(t.children[1], env)
elif t.node_type == ParseType.IFELSE:
eval_parse_tree(t.children[2], env)
def eval_gte(t, env):
"""
Evaluate >= operation
"""
return eval_parse_tree(t.children[0], env) >= eval_parse_tree(
t.children[1], env)
def eval_lte(t, env):
"""
Evaluate <= operation
"""
return eval_parse_tree(t.children[0], env) <= eval_parse_tree(
t.children[1], env)
def eval_gt(t, env):
"""
Evaluate > operation
"""
return eval_parse_tree(t.children[0], env) > eval_parse_tree(
t.children[1], env)
def eval_lt(t, env):
"""
Evaluate < operation
"""
return eval_parse_tree(t.children[0], env) < eval_parse_tree(
t.children[1], env)
def eval_et(t, env):
"""
Evaluate < operation
"""
return eval_parse_tree(t.children[0],
env) == eval_parse_tree(t.children[1], env)
def eval_ne(t, env):
"""
Evaluate ~= operation
"""
return eval_parse_tree(t.children[0], env) != eval_parse_tree(
t.children[1], env)
def eval_def(t, env):
"""
Define a function
"""
name = t.children[0].children[0].token.lexeme
env.insert(name, Ref(RefType.FUNCTION, t))
def eval_call(t, env):
"""
Call a function
"""
name = t.children[0].token.lexeme
arglist = eval_parse_tree(t.children[1], env)
# retrieve the function
fun = env.lookup(name)
if not fun:
print(f"Call to undefined function {name} on line {t.token.line}")
sys.exit(-1)
elif fun.ref_type != RefType.FUNCTION:
print(f"Call to non-function {name} on line {t.token.line}")
sys.exit(-1)
fun = fun.ref_value
# create the local environment
local = RefEnv(env)
if fun.children[2].node_type == ParseType.PARAMLIST: # If there are parameters
# get the parameter list
paramlist = eval_parse_tree(fun.children[2], env)
# verify the parameter list
if len(arglist) != len(paramlist):
print(f"Wrong number of parameters to function {name} on line {t.token.line}")
sys.exit(-1)
# all parameters are local (by design)
for i in range(len(paramlist)):
local.insert(paramlist[i], Ref(RefType.ID, arglist[i]))
# call the function
return eval_parse_tree(fun.children[1], local)
else: # If there are no parameters
return eval_parse_tree(fun.children[1], local)
def eval_return(t, env):
"""
Evaluate Return
"""
env.return_value = eval_parse_tree(t.children[0], env)
return env.return_value
def eval_break(t, env):
"""
Evaluate break.
"""
env.break_val = True
def eval_array(t, env):
"""
Evaluate ARRAY What about the unbounded ones
"""
name = t.children[0].children[0].token.lexeme
bounds = eval_parse_tree(t.children[1], env)
if len(bounds) == 1:
value2 = [None] * bounds[0]
bind(env, name, Ref(RefType.ID, value2))
else:
value3 = [[None] * bounds[1] for i in range(bounds[0])]
bind(env, name, Ref(RefType.ID, value3))
def eval_while(t, env):
"""
Evaluate WHILE
"""
while eval_parse_tree(t.children[0], env) and not env.break_val:
eval_parse_tree(t.children[1], env)
env.break_val = False
def eval_import(t, env):
"""
Evaluate IMPORT
"""
f = open(eval_parse_tree(t.children[0], env), 'r')
contents = f.read()
f.close()
return contents
def eval_split(t, env):
"""
Evaluate SPLIT
"""
return eval_parse_tree(t.children[0],
env).split(eval_parse_tree(t.children[1], env))
def eval_block(t, env):
#leads to statement list
fun_result = None
for c in t.children:
if env.break_val:
return fun_result
result = eval_parse_tree(c, env)
if result is not None:
fun_result = result
return fun_result
def eval_statement(t, env):
"""
Evaluate Statement
"""
if not env.break_val:
return eval_parse_tree(t.children[0], env)
return
def eval_statement_list(t, env):
"""
Evaluate Statement_list
"""
fun_result = None
for c in t.children:
if env.break_val:
return
result = eval_parse_tree(c, env)
# remember any non-none result
if result is not None:
fun_result = result
# check to see if we have returned
if env.return_value is not None:
return env.return_value
return fun_result
def eval_paramlist(t, env):
fun_result = []
for c in t.children:
fun_result.append(c.children[0].token.lexeme)
return fun_result
def eval_arglist(t, env):
fun_result = []
for c in t.children:
result = eval_parse_tree(c, env)
if result is not None:
fun_result.append(result)
return fun_result
def eval_reflist(t, env):
fun_result = []
for c in t.children:
result = eval_parse_tree(c, env)
if result is not None:
fun_result.append(result)
return fun_result
def eval_ref(t, env):
if len(t.children) == 2:
v = t.children[0].token.lexeme
arglist = eval_parse_tree(t.children[1], env)
if len(arglist) == 2:
bigval = env.lookup(v)
value2 = bigval.ref_value[arglist[0]][arglist[1]]
return value2
else:
bigval2 = env.lookup(v)
value22 = bigval2.ref_value[arglist[0]]
return value22
else:
return eval_parse_tree(t.children[0], env)
def eval_bounds(t, env):
fun_result = []
for c in t.children:
result = eval_parse_tree(c, env)
if result is not None:
fun_result.append(result)
return fun_result
def eval_swap(t, env):
# lookup to ref objects and swap the ref values
value = t.children[0]
value2 = t.children[1]
if ((len(value.children) == 1) and (len(value2.children) == 1)):
name1 = value.children[0].token.lexeme
name1_val = env.lookup(name1)
name2 = value.children[1].token.lexeme
name2_val = env.lookup(name2)
temp = name2_val.ref_value
name2_val.ref_value = name1_val.ref_value
name1_val.ref_value = temp
if __name__ == "__main__":
if len(sys.argv) == 2:
f = open(sys.argv[1])
l = Lexer(f)
else:
l = Lexer()
parser = Parser(l)
pt = parser.parse()
eval_parse_tree(pt, RefEnv())