-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpret.py
972 lines (757 loc) · 29.5 KB
/
interpret.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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
import sys
import re
import argparse
import xml.etree.ElementTree as ET
import sys
# Base class from which all instructions inherit
# This class is never instanced (pseudo-abstract class)
# Instance contains opcode and a list of arguments
# Each argument is a dictionary: {arg_type = "", arg_value = ""}
class Instruction:
inst_counter = 0
arg_num = 0
order_numbers = []
def __init__(self, opcode, order) -> None:
self.opcode = opcode
self.args = []
Instruction.inst_counter += 1
self.order = order
if order in Instruction.order_numbers:
sys.exit(32)
Instruction.order_numbers.append(order)
if order < 1:
sys.exit(32)
# Creates argument dict and adds to list
def add_argument(self, typ, value, index):
self.args.insert(index, dict(arg_type = typ, arg_value = value))
# Each inherited class overrides this method
def execute():
print("not implemented!")
# Called before execution
# Checks, whether actual argument count corresponds with expected argument count
def check_arg_quantity(self):
if self.__class__.arg_num != len(self.args):
sys.exit(32)
# If argument is variable, returns variable from saved variables
# If argument is constant, returns argument
def retrieve_argument(self, arg: dict):
if (arg["arg_type"] == "var"):
return program.get_variable(arg["arg_value"])
else:
return arg
# Program class
# Singleton
class Program():
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(Program, cls).__new__(cls)
return cls.instance
def __init__(self):
self.instructions = [] # List of instructions
self.frames_stack = [] # List of frames, used as a stack
self.global_frame = dict() # Global frame
self.temporary_frame = None # Temporary frame
self.local_frame = None # Local Frame
self.instruction_index = 0 # Instruction index, used as iterator in instructions list
self.labels = dict() # Labels, {label_name = number}
self.call_stack = [] # Call stack, used with CALL/RETURN instructions
self.data_stack = [] # Data stack, used with PUSHS/POPS
def add_instruction(self, instr):
self.instructions.append(instr)
pass
# Creates new temporary frame
def create_frame(self):
self.temporary_frame = dict()
# Pushes temporary frame onto stack, sets local frame
def push_frame(self):
if self.temporary_frame is not None:
self.frames_stack.append(self.temporary_frame)
self.local_frame = self.frames_stack[-1]
self.temporary_frame = None
else:
sys.exit(55)
# Pops frame from stack, sets local frame
def pop_frame(self):
if self.local_frame is not None:
self.temporary_frame = self.frames_stack.pop()
if self.frames_stack:
self.local_frame = self.frames_stack[-1]
else:
self.local_frame = None
else:
sys.exit(55)
# Returns variable dict from correct frame
def get_variable(self, var: str):
if (re.match(r"^GF@", var)):
if (var[3:] in program.global_frame.keys()):
return program.global_frame[var[3:]]
else:
sys.exit(54)
elif (re.match(r"^LF@", var)):
if (program.local_frame is not None):
if (var[3:] in program.local_frame.keys()):
return program.local_frame[var[3:]]
else:
sys.exit(54)
else:
sys.exit(55)
elif (re.match(r"^TF@", var)):
if (program.temporary_frame is not None):
if (var[3:] in program.temporary_frame.keys()):
return program.temporary_frame[var[3:]]
else:
sys.exit(54)
else:
sys.exit(55)
else:
sys.exit(31)
# Saves variable to correct frame
# var: variable name
# symb: variable type and value pair
def save_to_variable(self, var: str, symb: dict):
if (re.match(r"^GF@", var)):
if (var[3:] in program.global_frame.keys()):
program.global_frame[var[3:]] = symb
else:
sys.exit(54)
elif (re.match(r"^LF@", var)):
if (var[3:] in program.local_frame.keys()):
program.local_frame[var[3:]] = symb
else:
sys.exit(54)
elif (re.match(r"^TF@", var)):
if (var[3:] in program.temporary_frame.keys()):
program.temporary_frame[var[3:]] = symb
else:
sys.exit(54)
else:
print("bad, very bad") #change
sys.exit() #figure this out
# Adds label to labels list
def add_label(self, label_name: str, label_num: int):
if (label_name not in self.labels.keys()):
self.labels[label_name] = label_num
elif (self.labels[label_name] == label_num):
pass
else:
sys.exit(52)
def get_current_line(self):
return self.instruction_index
def jump_to_index(self, index):
self.instruction_index = index
def incr_instr_index(self):
self.instruction_index += 1
def call_stack_push(self, value):
self.call_stack.append(value)
def call_stack_pop(self):
if (self.call_stack):
return self.call_stack.pop()
else:
sys.exit(56)
def data_stack_push(self, symb):
self.data_stack.append(symb)
def data_stack_pop(self):
if (self.data_stack):
return self.data_stack.pop()
else:
sys.exit(56)
def get_label_names(self):
return self.labels.keys()
# Factory class, creates instances of instruction subclasses based on opcode
class InstructionFactory():
def create_instruction(self, opcode: str, order: int):
if opcode == "WRITE":
return WRITE(opcode, order)
elif opcode == "CREATEFRAME":
return CREATEFRAME(opcode, order)
elif opcode == "PUSHFRAME":
return PUSHFRAME(opcode, order)
elif opcode == "JUMPIFNEQ":
return JUMPIFNEQ(opcode, order)
elif opcode == "JUMPIFEQ":
return JUMPIFEQ(opcode, order)
elif opcode == "POPFRAME":
return POPFRAME(opcode, order)
elif opcode == "INT2CHAR":
return INT2CHAR(opcode, order)
elif opcode == "STRI2INT":
return STRI2INT(opcode, order)
elif opcode == "GETCHAR":
return GETCHAR(opcode, order)
elif opcode == "SETCHAR":
return SETCHAR(opcode, order)
elif opcode == "RETURN":
return RETURN(opcode, order)
elif opcode == "DEFVAR":
return DEFVAR(opcode, order)
elif opcode == "CONCAT":
return CONCAT(opcode, order)
elif opcode == "STRLEN":
return STRLEN(opcode, order)
elif opcode == "DPRINT":
return DPRINT(opcode, order)
elif opcode == "LABEL":
return LABEL(opcode, order)
elif opcode == "BREAK":
return BREAK(opcode, order)
elif opcode == "PUSHS":
return PUSHS(opcode, order)
elif opcode == "POPS":
return POPS(opcode, order)
elif opcode == "TYPE":
return TYPE(opcode, order)
elif opcode == "JUMP":
return JUMP(opcode, order)
elif opcode == "MOVE":
return MOVE(opcode, order)
elif opcode == "READ":
return READ(opcode, order)
elif opcode == "IDIV":
return IDIV(opcode, order)
elif opcode == "CALL":
return CALL(opcode, order)
elif opcode == "EXIT":
return EXIT(opcode, order)
elif opcode == "AND":
return AND(opcode, order)
elif opcode == "ADD":
return ADD(opcode, order)
elif opcode == "SUB":
return SUB(opcode, order)
elif opcode == "MUL":
return MUL(opcode, order)
elif opcode == "NOT":
return NOT(opcode, order)
elif opcode == "EQ":
return EQ(opcode, order)
elif opcode == "LT":
return LT(opcode, order)
elif opcode == "GT":
return GT(opcode, order)
elif opcode == "OR":
return OR(opcode, order)
else:
sys.exit(32)
# WRITE <symb>
class WRITE(Instruction):
arg_num = 1
def execute(self):
arg = self.args[0]
if (arg["arg_type"] == "var"):
variable = program.get_variable(arg["arg_value"])
else:
variable = arg
if (variable["arg_type"] == "nil"):
print("", end='')
return
string = variable["arg_value"]
if (variable["arg_type"] == "bool"):
print(str(string).lower(), end='')
return
if (variable["arg_type"] == "string"):
pattern = r'\\([0-9]{3})'
escape_seqs = re.findall(pattern, string)
for escape_seq in escape_seqs:
char_code = int(escape_seq)
ascii_char = chr(char_code)
string = string.replace('\\' + escape_seq, ascii_char)
print(string, end='')
# CREATEFRAME
class CREATEFRAME(Instruction):
arg_num = 0
def execute(self):
program.create_frame()
# PUSHFRAME
class PUSHFRAME(Instruction):
arg_num = 0
def execute(self):
program.push_frame()
# POPFRAME
class POPFRAME(Instruction):
arg_num = 0
def execute(self):
program.pop_frame()
# DEFVAR <var>
class DEFVAR(Instruction):
arg_num = 1
def execute(self):
var_name_frame = self.args[0].get("arg_value")
var_name = var_name_frame[3:]
if (re.match(r"^GF@", var_name_frame)):
if var_name not in program.global_frame.keys():
program.global_frame[var_name] = dict(arg_type = None, arg_value = None)
else:
sys.exit(52)
elif (re.match(r"^LF@", var_name_frame)):
if (program.local_frame is not None):
if var_name not in program.local_frame.keys():
program.local_frame[var_name] = dict(arg_type = "", arg_value = "")
else:
sys.exit(52)
else:
sys.exit(55)
elif (re.match(r"^TF@", var_name_frame)):
if (program.temporary_frame is not None):
if var_name not in program.temporary_frame.keys():
program.temporary_frame[var_name] = dict(arg_type = "", arg_value = "")
else:
sys.exit(52)
else:
sys.exit(55)
else:
sys.exit(57)
# MOVE <var> <symb>
class MOVE(Instruction):
arg_num = 2
def execute(self):
var_name = self.args[0]["arg_value"]
symb = self.retrieve_argument(self.args[1])
program.save_to_variable(var_name, symb)
# ADD <var> <symb> <symb>
class ADD(Instruction):
arg_num = 3
def execute(self):
operand1 = self.retrieve_argument(self.args[1])
operand2 = self.retrieve_argument(self.args[2])
if (operand1["arg_type"] != "int" or operand2["arg_type"] != "int"):
sys.exit(53)
value1 = operand1["arg_value"]
value2 = operand2["arg_value"]
value = value1 + value2
symb = dict(arg_type = "int", arg_value = value)
program.save_to_variable(self.args[0].get("arg_value"), symb)
# SUB <var> <symb> <symb>
class SUB(Instruction):
arg_num = 3
def execute(self):
operand1 = self.retrieve_argument(self.args[1])
operand2 = self.retrieve_argument(self.args[2])
if (operand1["arg_type"] != "int" or operand2["arg_type"] != "int"):
sys.exit(53)
value1 = operand1["arg_value"]
value2 = operand2["arg_value"]
value = value1 - value2
symb = dict(arg_type = "int", arg_value = value)
program.save_to_variable(self.args[0].get("arg_value"), symb)
# MUL <var> <symb> <symb>
class MUL(Instruction):
arg_num = 3
def execute(self):
operand1 = self.retrieve_argument(self.args[1])
operand2 = self.retrieve_argument(self.args[2])
if (operand1["arg_type"] != "int" or operand2["arg_type"] != "int"):
sys.exit(53)
value1 = operand1["arg_value"]
value2 = operand2["arg_value"]
value = value1 * value2
symb = dict(arg_type = "int", arg_value = value)
program.save_to_variable(self.args[0].get("arg_value"), symb)
# IDIV <var> <symb> <symb>
class IDIV(Instruction):
arg_num = 3
def execute(self):
operand1 = self.retrieve_argument(self.args[1])
operand2 = self.retrieve_argument(self.args[2])
if (operand1["arg_type"] != "int" or operand2["arg_type"] != "int"):
sys.exit(53)
value1 = operand1["arg_value"]
value2 = operand2["arg_value"]
if (value2 == 0):
sys.exit(57)
value = value1 // value2
symb = dict(arg_type = "int", arg_value = value)
program.save_to_variable(self.args[0].get("arg_value"), symb)
# READ <var> <type>
class READ(Instruction):
arg_num = 2
def execute(self):
try:
user_input = inputfile.readline().strip()
except:
symb = dict(arg_type = "nil", arg_value = "nil")
program.save_to_variable(self.args[0].get("arg_value"), symb)
return
if not user_input:
symb = dict(arg_type = "nil", arg_value = "nil")
program.save_to_variable(self.args[0].get("arg_value"), symb)
return
if (self.args[1]["arg_type"] != "type"):
sys.exit(32)
if (self.args[1].get("arg_value") == "int"):
try:
user_input = int(user_input)
symb = dict(arg_type = self.args[1].get("arg_value"), arg_value = user_input)
except:
symb = dict(arg_type = "nil", arg_value = "nil")
elif (self.args[1].get("arg_value") == "bool"):
if (user_input == "true"):
symb = dict(arg_type = "bool", arg_value = "true")
else:
symb = dict(arg_type = "bool", arg_value = "false")
elif (self.args[1].get("arg_value") == "string"):
symb = dict(arg_type = "string", arg_value = user_input)
else:
sys.exit(32)
program.save_to_variable(self.args[0].get("arg_value"), symb)
# JUMP <label>
class JUMP(Instruction):
arg_num = 1
def execute(self):
label_name = self.args[0].get("arg_value")
if (label_name in program.get_label_names()):
program.jump_to_index(program.labels[label_name])
else:
sys.exit(52)
# LABEL <label>
class LABEL(Instruction):
arg_num = 1
def execute(self):
pass
# CALL <label>
class CALL(Instruction):
arg_num = 1
def execute(self):
label_name = self.args[0].get("arg_value")
program.call_stack_push(self.order - 1) #i can explain
if (label_name in program.get_label_names()):
program.jump_to_index(program.labels[label_name])
else:
sys.exit(52)
# RETURN
class RETURN(Instruction):
arg_num = 0
def execute(self):
if (program.call_stack):
program.jump_to_index(program.call_stack_pop())
else:
sys.exit(56)
# EQ <var> <symb> <symb>
class EQ(Instruction):
arg_num = 3
def execute(self):
var_name = self.args[0]["arg_value"]
symb1 = self.retrieve_argument(self.args[1])
symb2 = self.retrieve_argument(self.args[2])
if (symb1["arg_type"] != "nil" and symb2["arg_type"] != "nil"):
if (symb1["arg_type"] != symb2["arg_type"]):
sys.exit(53)
if (symb1["arg_value"] == symb2["arg_value"]):
symb = dict(arg_type = "bool", arg_value = "true")
else:
symb = dict(arg_type = "bool", arg_value = "false")
program.save_to_variable(var_name, symb)
# GT <var> <symb> <symb>
class GT(Instruction):
arg_num = 3
def execute(self):
var_name = self.args[0]["arg_value"]
symb1 = self.retrieve_argument(self.args[1])
symb2 = self.retrieve_argument(self.args[2])
if (symb1["arg_type"] != symb2["arg_type"]):
sys.exit(53)
if (symb1["arg_type"] == "nil" or symb2["arg_type"] == "nil"):
sys.exit(53)
if (symb1["arg_value"] > symb2["arg_value"]):
symb = dict(arg_type = "bool", arg_value = "true")
else:
symb = dict(arg_type = "bool", arg_value = "false")
program.save_to_variable(var_name, symb)
# LT <var> <symb> <symb>
class LT(Instruction):
arg_num = 3
def execute(self):
var_name = self.args[0]["arg_value"]
symb1 = self.retrieve_argument(self.args[1])
symb2 = self.retrieve_argument(self.args[2])
if (symb1["arg_type"] != symb2["arg_type"]):
sys.exit(53)
if (symb1["arg_type"] == "nil" or symb2["arg_type"] == "nil"):
sys.exit(53)
if (symb1["arg_value"] < symb2["arg_value"]):
symb = dict(arg_type = "bool", arg_value = "true")
else:
symb = dict(arg_type = "bool", arg_value = "false")
program.save_to_variable(var_name, symb)
# AND <var> <symb> <symb>
class AND(Instruction):
arg_num = 3
def execute(self):
var_name = self.args[0]["arg_value"]
symb1 = self.retrieve_argument(self.args[1])
symb2 = self.retrieve_argument(self.args[2])
if (symb1["arg_type"] != "bool" or symb2["arg_type"] != "bool"):
sys.exit(53)
result = symb1["arg_value"] and symb2["arg_value"]
symb = dict(arg_type = "bool", arg_value = result)
program.save_to_variable(var_name, symb)
# OR <var> <symb> <symb>
class OR(Instruction):
arg_num = 3
def execute(self):
var_name = self.args[0]["arg_value"]
symb1 = self.retrieve_argument(self.args[1])
symb2 = self.retrieve_argument(self.args[2])
if (symb1["arg_type"] != "bool" or symb2["arg_type"] != "bool"):
sys.exit(53)
result = symb1["arg_value"] or symb2["arg_value"]
symb = dict(arg_type = "bool", arg_value = result)
program.save_to_variable(var_name, symb)
# NOT <var> <symb>
class NOT(Instruction):
arg_num = 2
def execute(self):
var_name = self.args[0]["arg_value"]
symb1 = self.retrieve_argument(self.args[1])
if (symb1["arg_type"] != "bool"):
sys.exit(53)
symb = dict(arg_type = "bool", arg_value = not symb1["arg_value"])
program.save_to_variable(var_name, symb)
# INT2CHAR <var> <symb>
class INT2CHAR(Instruction):
arg_num = 2
def execute(self):
var_name = self.args[0]["arg_value"]
symb1 = self.retrieve_argument(self.args[1])
if (symb1["arg_type"] != "int"):
sys.exit(53)
try:
char_value = chr(symb1["arg_value"])
except:
sys.exit(58)
symb = dict(arg_type = "string", arg_value = char_value)
program.save_to_variable(var_name, symb)
# STRI2INT <var> <symb> <symb>
class STRI2INT(Instruction):
arg_num = 3
def execute(self):
var_name = self.args[0]["arg_value"]
symb1 = self.retrieve_argument(self.args[1])
symb2 = self.retrieve_argument(self.args[2])
index = symb2["arg_value"]
if (symb1["arg_type"] != "string"):
sys.exit(53)
if (symb2["arg_type"] != "int"):
sys.exit(53)
if (index >= len(symb1["arg_value"])):
sys.exit(58)
symb = dict(arg_type = "int", arg_value = ord(symb1["arg_value"][index]))
program.save_to_variable(var_name, symb)
# CONCAT <var> <symb> <symb>
class CONCAT(Instruction):
arg_num = 3
def execute(self):
var_name = self.args[0].get("arg_value")
symb1 = self.retrieve_argument(self.args[1])
symb2 = self.retrieve_argument(self.args[2])
if (symb1["arg_type"] != "string" or symb2["arg_type"] != "string"):
sys.exit(53)
result_string = symb1["arg_value"] + symb2["arg_value"]
symb = dict(arg_type = "string", arg_value = result_string)
program.save_to_variable(var_name, symb)
# STRLEN <var> <symb>
class STRLEN(Instruction):
arg_num = 2
def execute(self):
var_name = self.args[0].get("arg_value")
symb1 = self.retrieve_argument(self.args[1])
if (symb1["arg_type"] != "string"):
sys.exit(53)
symb = dict(arg_type = "int", arg_value = len(symb1["arg_value"]))
program.save_to_variable(var_name, symb)
# GETCHAR <var> <symb> <symb>
class GETCHAR(Instruction):
arg_num = 3
def execute(self):
var_name = self.args[0]["arg_value"]
symb1 = self.retrieve_argument(self.args[1])
symb2 = self.retrieve_argument(self.args[2])
if (symb2["arg_type"] != "int" or symb1["arg_type"] != "string"):
sys.exit(53)
if (symb2["arg_value"] >= len(symb1["arg_value"])):
sys.exit(58)
symb = dict(arg_type = "string", arg_value = symb1["arg_value"][symb2["arg_value"]])
program.save_to_variable(var_name, symb)
# SETCHAR <var> <symb> <symb>
class SETCHAR(Instruction):
arg_num = 3
def execute(self):
arg1 = self.args[0]
symb1 = program.get_variable(arg1["arg_value"])
symb2 = self.retrieve_argument(self.args[1])
symb3 = self.retrieve_argument(self.args[2])
if (symb2["arg_type"] != "int" or symb3["arg_type"] != "string"):
sys.exit(53)
if (symb2["arg_value"] >= len(symb1["arg_value"])):
sys.exit(58)
str_list = list(symb1["arg_value"])
str_list[symb2["arg_value"]] = symb3["arg_value"][0]
str_replaced = ''.join(str_list)
symb1 = dict(arg_type = "string", arg_value = str_replaced)
program.save_to_variable(arg1["arg_value"], symb1)
# TYPE <var> <symb>
class TYPE(Instruction):
arg_num = 2
def execute(self):
var_name = self.args[0]["arg_value"]
symb1 = self.retrieve_argument(self.args[1])
if (symb1["arg_value"] == None):
symb = dict(arg_type = "string", arg_value = "")
else:
symb = dict(arg_type = "string", arg_value = symb1["arg_type"])
program.save_to_variable(var_name, symb)
# JUMPIFEQ <label> <symb> <symb>
class JUMPIFEQ(Instruction):
arg_num = 3
def execute(self):
label_name = self.args[0]["arg_value"]
symb1 = self.retrieve_argument(self.args[1])
symb2 = self.retrieve_argument(self.args[2])
if (symb2["arg_type"] != "nil" and symb1["arg_type"] != "nil"):
if (symb1["arg_type"] != symb2["arg_type"]):
sys.exit(53)
if (symb1["arg_value"] == symb2["arg_value"]):
if (label_name in program.get_label_names()):
program.jump_to_index(program.labels[label_name])
else:
sys.exit(52)
# JUMPIFNEQ <label> <symb> <symb>
class JUMPIFNEQ(Instruction):
arg_num = 3
def execute(self):
label_name = self.args[0]["arg_value"]
symb1 = self.retrieve_argument(self.args[1])
symb2 = self.retrieve_argument(self.args[2])
if (symb2["arg_type"] != "nil" and symb1["arg_type"] != "nil"):
if (symb1["arg_type"] != symb2["arg_type"]):
sys.exit(53)
if (symb1["arg_value"] != symb2["arg_value"]):
if (label_name in program.get_label_names()):
program.jump_to_index(program.labels[label_name])
else:
sys.exit(52)
# DPRINT <symb>
class DPRINT(Instruction):
arg_num = 1
def execute(self):
symb = self.retrieve_argument(self.args[0])
print(symb["arg_value"], file=sys.stderr)
# EXIT <symb>
class EXIT(Instruction):
arg_num = 1
def execute(self):
symb = self.retrieve_argument(self.args[0])
if (symb["arg_type"] != "int"):
sys.exit(53)
if (symb["arg_value"] > 49 or symb["arg_value"] < 0):
sys.exit(57)
sys.exit(symb["arg_value"])
class BREAK(Instruction):
arg_num = 0
def execute(self):
print("global frame:", file=sys.stderr)
print(program.global_frame, file=sys.stderr)
print("local frame:", file=sys.stderr)
print(program.local_frame, file=sys.stderr)
print("temporary frame:", file=sys.stderr)
print(program.temporary_frame, file=sys.stderr)
print("instruction index: ", file=sys.stderr)
print(program.instruction_index, file=sys.stderr)
class PUSHS(Instruction):
arg_num = 1
def execute(self):
symb = self.retrieve_argument(self.args[0])
program.data_stack_push(symb)
class POPS(Instruction):
arg_num = 1
def execute(self):
var_name = self.args[0]["arg_value"]
symb = program.data_stack_pop()
program.save_to_variable(var_name, symb)
#****************************************************************************************************************
#set up argument parser
parser = argparse.ArgumentParser(prog='interpret.py', description='Interprets XML representation of IPPcode23')
parser.add_argument('--input', help='Specify the input file')
parser.add_argument('--source', help='Specify the source file')
#process arguments
args = parser.parse_args()
if (args.input):
input_closed = args.input
inputfile = open(input_closed, 'r')
else:
inputfile = sys.stdin
if (args.source):
sourcefile = args.source
else:
sourcefile = sys.stdin
if not(args.input or args.source):
print("no source or input specified")
sys.exit(1)
else:
try:
tree=ET.parse(sourcefile)
except:
sys.exit(31)
root=tree.getroot()
# XML format checks
if root.tag != 'program':
sys.exit(32)
for inst in root:
if inst.tag != 'instruction':
sys.exit(32)
attributes=list(inst.attrib.keys())
if not('order' in attributes and 'opcode' in attributes):
sys.exit(32)
arg_nums = []
for arg in inst:
if not(re.match(r"arg[123]", arg.tag)):
sys.exit(32)
arg_nums.append(int(arg.tag[3:]))
if len(arg_nums) != 0:
arg_nums = sorted(arg_nums)
iter = 1
for i in arg_nums:
if i != iter:
sys.exit(32)
iter += 1
factory = InstructionFactory()
program = Program()
# Create instructions
for inst in root:
try:
order = int(inst.attrib["order"])
except:
sys.exit(32)
current = factory.create_instruction(inst.attrib["opcode"], order)
for i in inst:
number = int(i.tag[3:])
index = number - 1
if (i.attrib["type"] == "int"):
try:
int_text = int(i.text)
except:
sys.exit(32)
current.add_argument("int", int_text, index)
elif (i.attrib["type"] == "bool"):
if (i.text == "true"):
bool_text = True
else:
bool_text = False
current.add_argument("bool", bool_text, index)
else:
current.add_argument(i.attrib["type"], i.text, index)
program.add_instruction(current)
inst_count = len(program.instructions)
# Sort instructions based on order number
program.instructions = sorted(program.instructions, key=lambda instruction: instruction.order)
# Look for labels and add them to the list
iter = 0
for i in program.instructions:
if i.opcode == "LABEL":
program.add_label(i.args[0]["arg_value"], iter)
iter += 1
# Execute each instruction
while (program.instruction_index < inst_count):
program.instructions[program.instruction_index].check_arg_quantity()
program.instructions[program.instruction_index].execute()
program.incr_instr_index()
#print(program.labels)