-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpret.py
More file actions
1212 lines (1008 loc) · 38.3 KB
/
interpret.py
File metadata and controls
1212 lines (1008 loc) · 38.3 KB
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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Název: 2. úloha do IPP 2022/2023
Login: xjetma02
Autor: Adam Jetmar
"""
import argparse
import sys
import xml.etree.ElementTree as ET
#Třída instrukce
class Instruction:
#seznam návěští
_labelFrame = {}
#datový zásobník
_dataFrame = []
#List instrukcí
_instrList = {}
#definice rámců
_framesDefinition = {
"TF": False,
"LF": False,
"GF": True
}
#slovník pro ukládání
_frames = {
"GF": {},
"LF": {},
"TF": {}
}
#konstruktor třídy Instruction
def __init__(self, initOpcode, order: int):
self._opcode = initOpcode
self._order = order
self._instrList.update({order: initOpcode})
#Getters
#vrací název instrukce
def getOpcode(self):
return self._opcode
#vrací hodnotu order pro danou instrukci
def getOrder(self):
return self._order
#vrací seznam instrukcí
def getInstrList(self):
return self._instrList
#vrátí rámec podle hodnoty v parametru index
def getFrame(self, index):
return self._frames[index]
#vrátí hodnotu proměnné v rámci
def getVarFromFrame(self, var):
info = var.split("@")
name = info[1]
frame = info[0]
#pokud je rámec nedefinovaný, vyhodí chybu 55
if self.isFrameDefined(frame) == False or self.isFrameDefined(frame) is None:
exit(55)
result = self._frames[frame][name]
return result
#Setters
#Aktualizuje opcode instrukce na novou hodnotu
def setOpcode(self, opcodeVal):
self._opcode = opcodeVal
#Nastaví pořadí instrukce
def setOrder(self, order):
self._order = order
"""
Funkce addToFrame vloží do rámce název proměnné a její hodnotu
param. frame - název rámce
param. name - název proměnné
param. value - hodnota proměnné
"""
def addToFrame(self, frame: str, name, value):
if self._frames[frame].get(name) is None and self._opcode != "DEFVAR":
exit(54)
if self._frames[frame].get(name) is not None and self._opcode == "DEFVAR":
exit(52)
self._frames[frame].update({name: value})
#přidá hodnou do datového zásobníku
def addToDataFrame(self, value):
self._dataFrame.append(value)
#popne hodnotu z datového zásobníku
def popDataFrame(self):
item = self._dataFrame.pop()
return item
#přidá návěští do rámce návěští
def addToLabelFrame(self, label, order):
self._labelFrame.update({label: order})
#vymaže hodnoty rámce
def cleanFrame(self, index):
self._frames[index].clear()
#definuje rámec a uloží do něj další rámec "anotherFrame"
def updateFrame(self,frame, anotherFrame: dict):
self._frames[frame].update(anotherFrame)
#nastavý rámec na definovaný nebo nedefinovaný podle parametru "is_def"
def defineFrame(self, frame, is_def: bool):
self._framesDefinition.update({frame: is_def})
#vrací boolean hodnotu podle hodnoty ve "_framesDefinition"
def isFrameDefined(self, frame):
return self._framesDefinition[frame]
class Argument:
def __init__(self, num: int, typ, value):
self._num = num
self._typ = typ
self._value = value
#vrácí typ uložený v argumentě "typ"
def getType(self):
return self._typ
#vrací hodnotu uloženou v argumentě "value"
def getVal(self):
return self._value
#vrací pořadí argumentu v instrukci
def getArgOrder(self):
return self._num
#Instrukce pro práci s rámci, volání funkcí
class Move(Instruction):
def __init__(self, arg1: Argument, arg2: Argument, order: int):
super().__init__("MOVE", order)
self._arg1: Argument = arg1
self._arg2: Argument = arg2
def execute(self):
# print('Executing MOVE instruction')
if self._arg2.getType() == "var" and self._arg1.getType() == "var":
varVal = super().getVarFromFrame(self._arg2.getVal())
info = self._arg1.getVal().split("@")
super().addToFrame(info[0], info[1], varVal)
elif self._arg1.getType() != "var" and self._arg2.getType() == "var":
varVal = self._arg1.getVal()
info = self._arg2.getVal().split("@")
super().addToFrame(info[0], info[1], varVal)
else:
info = self._arg1.getVal().split("@")
super().addToFrame(info[0], info[1], self._arg2.getVal())
class Createframe(Instruction):
def __init__(self, order: int):
super().__init__("CREATEFRAME", order)
def execute(self):
super().cleanFrame("TF")
super().defineFrame("TF", True)
super().defineFrame("LF", True)
class Pushframe(Instruction):
def __init__(self, order: int):
super().__init__("PUSHFRAME", order)
def execute(self):
if not super().isFrameDefined("LF") or not super().isFrameDefined("TF"):
exit(55)
frame = super().getFrame("TF")
super().defineFrame("TF", False)
super().updateFrame("LF", frame)
class Popframe(Instruction):
def __init__(self, order: int):
super().__init__("POPFRAME", order)
def execute(self):
if not super().isFrameDefined("LF"):
exit(55)
frame = super().getFrame("LF")
super().defineFrame("TF", True)
super().updateFrame("TF", frame)
super().defineFrame("LF", False)
class Defvar(Instruction):
def __init__(self, argument: Argument, order: int):
super().__init__("DEFVAR", order)
if argument.getType() == "var":
self._arg: Argument = argument
else:
exit(53)
def execute(self):
# print("Executing DEFVAR instruction")
info = self._arg.getVal().split("@")
name: str = info[1]
frame: str = info[0]
value = ""
if not super().isFrameDefined(frame):
exit(55)
super().addToFrame(frame, name, value)
#Instrukce pro práci s datovým zásobníkem
class Pushs(Instruction):
def __init__(self, argument: Argument, initOrder: int):
super().__init__("PUSHS", initOrder)
self._arg: Argument = argument
def execute(self):
super().addToDataFrame(self._arg.getVal())
class Pops(Instruction):
def __init__(self, argument: Argument, order: int):
super().__init__("POPS", order)
if argument.getType() == "var":
self._arg: Argument = argument
else:
exit(53)
def execute(self):
value = super().popDataFrame()
var = self._arg.getVal().split("@")
super().addToFrame(var[0], var[1], value)
#Aritmetické, relační, booleovské a konverzní instrukce
class Operation(Instruction):
def __init__(self, initOpcode, arg1: Argument, arg2: Argument, arg3: Argument, order: int):
super().__init__(initOpcode, order)
if arg1.getType() == "var":
self._arg1: Argument = arg1
else:
exit(53)
self._arg2 = arg2
self._arg3 = arg3
def execute(self):
firstVal = self._arg2.getVal()
secondVal = self._arg3.getVal()
if self._arg2.getType() != "var" and self._arg2.getType() != "int":
exit(53)
if self._arg3.getType() != "var" and self._arg3.getType() != "int":
exit(53)
if self._arg2.getType() == "var":
firstVal = super().getVarFromFrame(firstVal)
if self._arg3.getType() == "var":
secondVal = super().getVarFromFrame(secondVal)
try:
firstVal = int(firstVal)
secondVal = int(secondVal)
except ValueError:
exit(32)
result = 0
if super().getOpcode() == "ADD":
result = firstVal + secondVal
elif super().getOpcode() == "SUB":
result = firstVal - secondVal
elif super().getOpcode() == "MUL":
result = firstVal * secondVal
elif super().getOpcode() == "IDIV":
if secondVal == 0:
exit(57)
else:
result = firstVal / secondVal
result = int(result)
result = str(result)
info = self._arg1.getVal().split("@")
super().addToFrame(info[0], info[1], result)
class BoolOperation(Instruction):
def __init__(self, initOpcode, arg1: Argument, arg2: Argument, arg3: Argument, order: int):
super().__init__(initOpcode, order)
if arg1.getType() == "var":
self._arg1: Argument = arg1
else:
exit(53)
self._arg2 = arg2
self._arg3 = arg3
def execute(self):
firstVal = self._arg2.getVal()
secondVal = self._arg3.getVal()
if self._arg2.getType() == "var":
firstVal = super().getVarFromFrame(firstVal)
if self._arg3.getType() == "var":
secondVal = super().getVarFromFrame(secondVal)
result = "false"
if super().getOpcode() == "AND":
if firstVal.lower() == "true" and secondVal.lower() == "true":
result = "true"
else:
result = "false"
if super().getOpcode() == "OR":
if firstVal.lower() == "true" or secondVal.lower() == "true":
result = "true"
else:
result = "false"
if super().getOpcode() == "NOT":
if firstVal.lower() == "false":
result = "true"
else:
result = "false"
info = self._arg1.getVal().split("@")
super().addToFrame(info[0], info[1], result)
class Int2Char(Instruction):
def __init__(self, arg1: Argument, arg2: Argument, order: int):
super().__init__("INT2CHAR", order)
self._arg1: Argument = arg1
self._arg2: Argument = arg2
def execute(self):
intVal = ""
if self._arg2.getType() == "int":
intVal = self._arg2.getVal()
elif self._arg2.getType() == "var":
var = self._arg2.getVal()
intVal = super().getVarFromFrame(var)
try:
intVal = int(intVal)
except ValueError:
exit(58)
if intVal < 0 or intVal > 127:
exit(58)
info = self._arg1.getVal().split("@")
super().addToFrame(info[0], info[1], chr(intVal))
class Stri2Int(Instruction):
def __init__(self, arg1: Argument, arg2: Argument, arg3: Argument, order: int):
super().__init__("STRI2INT", order)
self._arg1 = arg1
self._arg2 = arg2
self._arg3 = arg3
def execute(self):
firstVal = self._arg2.getVal()
secondVal = self._arg3.getVal()
if self._arg2.getType() == "var":
firstVal = super().getVarFromFrame(firstVal)
if self._arg3.getType() == "var":
secondVal = super().getVarFromFrame(secondVal)
index = 0
try:
index = int(secondVal)
except ValueError:
exit(53)
if index >= len(firstVal):
exit(58)
orderChar = ord(firstVal[index])
info = self._arg1.getVal().split("@")
super().addToFrame(info[0], info[1], orderChar)
#Vstupně-výstupní instrukce
class Read(Instruction):
def __init__(self, arg1: Argument, arg2: Argument, order: int):
super().__init__("READ", order)
self._arg1: Argument = arg1
self._arg2: Argument = arg2
def execute(self):
global inputFile
typ = self._arg2.getVal()
info = self._arg1.getVal().split("@")
inputVal = "nil"
if typ == "string":
inputVal = inputFile.readline()
elif typ == "int":
inputVal = inputFile.readline()
try:
int(inputVal)
except ValueError:
exit(53)
elif typ == "bool":
inputVal = inputFile.readline()
if inputVal.lower() == "true":
inputVal = "true"
else:
inputVal = "false"
inputVal = inputVal.replace("\n", "")
super().addToFrame(info[0], info[1], inputVal)
class Write(Instruction):
def __init__(self, argument: Argument, order: int):
super().__init__("WRITE", order)
self._arg: Argument = argument
def execute(self):
# print('Executing WRITE instruction')
typ = self._arg.getType()
if typ == "string":
string = self._arg.getVal()
output = string.replace("\\032", " ")
output = output.replace("\\010", "\n")
output = output.replace("\\035", "#")
output = output.replace("\\009", "\t")
output = output.replace("\\092", "\\")
print(output, end='')
if typ == "var":
value = super().getVarFromFrame(self._arg.getVal())
output = value.replace("\\032", " ")
output = output.replace("\\035", "#")
output = output.replace("\\092", "\\")
output = output.replace("\\010", "\n")
output = output.replace("\\009", "\t")
print(output, end='')
if typ == "bool":
write = self._arg.getVal()
if write == "false":
print("false", end='')
elif write == "true":
print("true", end='')
if typ == "int":
value = int(self._arg.getVal())
print(value, end='')
if typ == "nil":
print("", end='')
#Instrukce pro práci s řetězci
class Concat(Instruction):
def __init__(self, arg1: Argument, arg2: Argument, arg3: Argument, order: int):
super().__init__("CONCAT", order)
self._arg1 = arg1
self._arg2 = arg2
self._arg3 = arg3
def execute(self):
typ1 = self._arg2.getType()
typ2 = self._arg3.getType()
firstVal = self._arg2.getVal()
secondVal = self._arg3.getVal()
newString = ""
if typ1 == "var" or typ2 == "var":
if typ1 == "var":
firstVal = super().getVarFromFrame(firstVal)
if typ2 == "var":
secondVal = super().getVarFromFrame(secondVal)
elif typ1 == "int" or typ2 == "int":
exit(53)
try:
newString = str(firstVal) + str(secondVal)
except ValueError:
exit(53)
info = self._arg1.getVal().split("@")
super().addToFrame(info[0], info[1], newString)
class Strlen(Instruction):
def __init__(self, arg1: Argument, arg2: Argument, order: int):
super().__init__("STRLEN", order)
self._arg1: Argument = arg1
self._arg2: Argument = arg2
def execute(self):
stringVal = ""
if self._arg2.getType() == "var":
stringVal = super().getVarFromFrame(self._arg2.getVal())
else:
stringVal = self._arg2.getVal()
info = self._arg1.getVal().split("@")
super().addToFrame(info[0], info[1], len(stringVal))
class Getchar(Instruction):
def __init__(self, arg1: Argument, arg2: Argument, arg3: Argument, order: int):
super().__init__("GETCHAR", order)
self._arg1 = arg1
self._arg2 = arg2
self._arg3 = arg3
def execute(self):
firstVal = self._arg2.getVal()
secondVal = self._arg3.getVal()
if self._arg2.getType() == "var":
firstVal = super().getVarFromFrame(firstVal)
if self._arg3.getType() == "var":
secondVal = super().getVarFromFrame(secondVal)
try:
secondVal = int(secondVal)
except ValueError:
exit(53)
if secondVal >= len(firstVal):
exit(58)
char = firstVal[secondVal]
info = self._arg1.getVal().split("@")
super().addToFrame(info[0], info[1], char)
class Setchar(Instruction):
def __init__(self, arg1: Argument, arg2: Argument, arg3: Argument, order: int):
super().__init__("SETCHAR", order)
self._arg1 = arg1
self._arg2 = arg2
self._arg3 = arg3
def execute(self):
string = super().getVarFromFrame(self._arg1.getVal())
firstVal = self._arg2.getVal()
secondVal = self._arg3.getVal()
if self._arg2.getType() == "var":
firstVal = super().getVarFromFrame(firstVal)
if self._arg3.getType() == "var":
secondVal = super().getVarFromFrame(secondVal)
try:
firstVal = int(firstVal)
except ValueError:
exit(53)
if secondVal == "" or firstVal >= len(string):
exit(58)
char = secondVal[0]
stringList = list(string)
stringList[firstVal] = char
newString = "".join(stringList)
info = self._arg1.getVal().split("@")
super().addToFrame(info[0], info[1], newString)
#Práce s typy
class Typ(Instruction):
def __init__(self, arg1: Argument, arg2: Argument, order: int):
super().__init__("TYPE", order)
self._arg1: Argument = arg1
self._arg2: Argument = arg2
def execute(self):
typ = self._arg2.getType()
if typ == "var":
value = super().getVarFromFrame(self._arg2.getVal())
if value.lower() == "true" or value.lower() == "false":
typ = "bool"
elif value == "":
typ = ""
elif value == "nil":
typ = "nil"
else:
typ = "string"
info = self._arg1.getVal().split("@")
super().addToFrame(info[0], info[1], typ)
#Instrukce pro řízení toku programu
class Label(Instruction):
def __init__(self, argument: Argument, order: int):
super().__init__("LABEL", order)
self._arg: Argument = argument
def execute(self):
super().addToLabelFrame(self._arg.getVal(), super().getOpcode())
class Jumpifeq(Instruction):
def __init__(self, arg1: Argument, arg2: Argument, arg3: Argument, order: int):
super().__init__("JUMPIFEQ", order)
self._arg1 = arg1
self._arg2 = arg2
self._arg3 = arg3
def execute(self):
typ1 = self._arg2.getType()
typ2 = self._arg3.getType()
firstVal = self._arg2.getVal()
secondVal = self._arg3.getVal()
if typ1 == "var" or typ2 == "var":
if typ1 == "var":
firstVal = super().getVarFromFrame(firstVal)
if typ2 == "var":
secondVal = super().getVarFromFrame(secondVal)
if firstVal == secondVal:
return True
else:
return False
elif typ1 == "nil" or typ2 == "nil":
if firstVal == secondVal:
return True
else:
return False
elif typ1 == typ2:
if firstVal == secondVal:
return True
else:
return False
else:
exit(53)
class Exit(Instruction):
def __init__(self, arg: Argument, order: int):
super().__init__("EXIT", order)
self._arg: Argument = arg
def execute(self):
exitVal = 0
if self._arg.getType() == "var":
exitVal = super().getVarFromFrame(self._arg.getVal())
else:
exitVal = self._arg.getVal()
try:
exitVal = int(exitVal)
except ValueError:
exit(57)
if exitVal < 0 or exitVal > 49:
exit(57)
exit(exitVal)
#Ladicí instrukce
class Break(Instruction):
def __init__(self, order: int):
super().__init__("BREAK", order)
def execute(self):
print("Instrukce: ", super().getInstrList(), file=sys.stderr, end='')
print("Aktuální pozice: ", super().getOrder(), file=sys.stderr, end='')
print("Rámec GF: ", super().getFrame("GF"), file=sys.stderr, end='')
print("Rámec LF: ", super().getFrame("LF"), file=sys.stderr, end='')
print("Rámec TF: ", super().getFrame("TF"), file=sys.stderr, end='')
class Compare(Instruction):
def __init__(self, initOpcode, arg1: Argument, arg2: Argument, arg3: Argument, order: int):
super().__init__(initOpcode.upper(), order)
self._arg1 = arg1
self._arg2 = arg2
self._arg3 = arg3
def execute(self):
firstVal = self._arg2.getVal()
secondVal = self._arg3.getVal()
typ1 = self._arg2.getType()
typ2 = self._arg3.getType()
result = "false"
if typ1 == "nil" or typ2 == "nil":
if super().getOpcode() == "EQ":
if typ1 == "var":
firstVal = super().getVarFromFrame(firstVal)
if typ2 == "var":
secondVal = super().getVarFromFrame(secondVal)
if firstVal == secondVal:
result = "true"
else:
exit(53)
if (typ1 == "int" and typ2 == "int") or (typ1 == "string" and typ2 == "string"):
if typ1 == "int" and typ2 == "int":
try:
firstVal = int(firstVal)
secondVal = int(secondVal)
except ValueError:
exit(53)
if super().getOpcode() == "LT":
if firstVal < secondVal:
result = "true"
if super().getOpcode() == "GT":
if firstVal > secondVal:
result = "true"
if super().getOpcode() == "EQ":
if firstVal == secondVal:
result = "true"
elif typ1 == "bool" and typ2 == "bool":
if super().getOpcode() == "LT":
if firstVal.lower() == "false" and secondVal.lower() == "true":
result = "true"
if super().getOpcode() == "GT":
if firstVal.lower() == "true" and secondVal.lower() == "false":
result = "true"
if super().getOpcode() == "EQ":
if firstVal.lower() == secondVal.lower():
result = "true"
elif typ1 == "var" or typ2 == "var":
if typ1 == "var":
firstVal = super().getVarFromFrame(firstVal)
if typ2 == "var":
secondVal = super().getVarFromFrame(secondVal)
if typ1 == "var" and typ2 == "var":
if super().getOpcode() == "LT":
if firstVal < secondVal:
result = "true"
if super().getOpcode() == "GT":
if firstVal > secondVal:
result = "true"
if super().getOpcode() == "EQ":
if firstVal == secondVal:
result = "true"
else:
exit(53)
#TODO - ostatne pripady
info = self._arg1.getVal().split("@")
super().addToFrame(info[0], info[1], result)
#Třída Factory vrátí vytvořenou instanci konkrétní instrukce podle zadaného opcode
class Factory:
@classmethod
def resolve(cls, operation: str, elem):
factoryOrder = elem.attrib["order"]
argDef = Argument(1, "int", "-1")
arg = argDef
arg1 = argDef
arg2 = argDef
arg3 = argDef
try:
factoryOrder = int(factoryOrder)
except ValueError:
exit(32)
tempOpcode = operation.upper();
match tempOpcode:
case "BREAK":
if len(elem) > 0:
exit(32)
return Break(factoryOrder)
case "CREATEFRAME":
if len(elem) > 0:
exit(32)
return Createframe(factoryOrder)
case "PUSHFRAME":
if len(elem) > 0:
exit(32)
return Pushframe(factoryOrder)
case "POPFRAME":
if len(elem) > 0:
exit(32)
return Popframe(factoryOrder)
case "DEFVAR":
if len(elem) > 1:
exit(32)
try:
if elem[0].tag != "arg1":
exit(32)
arg = Argument(1, elem[0].attrib["type"], elem[0].text)
except IndexError:
exit(32)
return Defvar(arg, factoryOrder)
case "PUSHS":
if len(elem) > 1:
exit(32)
try:
if elem[0].tag != "arg1":
exit(32)
arg = Argument(1, elem[0].attrib["type"], elem[0].text)
except IndexError:
exit(32)
return Pushs(arg, factoryOrder)
case "POPS":
if len(elem) > 1:
exit(32)
try:
if elem[0].tag != "arg1":
exit(32)
arg = Argument(1, elem[0].attrib["type"], elem[0].text)
except IndexError:
exit(32)
return Pops(arg, factoryOrder)
case "WRITE":
if len(elem) > 1:
exit(32)
try:
if elem[0].tag != "arg1":
exit(32)
arg = Argument(1, elem[0].attrib["type"], elem[0].text)
except IndexError:
exit(32)
return Write(arg, factoryOrder)
case "DPRINT":
if len(elem) > 1:
exit(32)
try:
if elem[0].tag != "arg1":
exit(32)
arg = Argument(1, elem[0].attrib["type"], elem[0].text)
except IndexError:
exit(32)
return Write(arg, factoryOrder, sys.stderr)
case "LABEL":
if len(elem) > 1:
exit(32)
try:
if elem[0].tag != "arg1":
exit(32)
arg = Argument(1, "label", elem[0].text)
except IndexError:
exit(32)
return Label(arg, factoryOrder)
case "EXIT":
if len(elem) > 1:
exit(32)
arg = argDef
try:
if elem[0].tag != "arg1":
exit(32)
arg = Argument(1, elem[0].attrib["type"], elem[0].text)
except IndexError:
exit(32)
return Exit(arg, factoryOrder)
case "INT2CHAR":
if len(elem) > 2:
exit(32)
try:
if elem[0].tag != "arg1":
exit(32)
arg1 = Argument(1, elem[0].attrib["type"], elem[0].text)
arg2 = Argument(2, elem[1].attrib["type"], elem[1].text)
except IndexError:
exit(32)
return Int2Char(arg1, arg2, factoryOrder)
case "MOVE":
if len(elem) > 2:
exit(32)
try:
if elem[0].tag != "arg1":
exit(32)
if elem[1].tag != "arg2":
exit(32)
arg1 = Argument(1, elem[0].attrib["type"], elem[0].text)
arg2 = Argument(2, elem[1].attrib["type"], elem[1].text)
except IndexError:
exit(32)
return Move(arg1, arg2, factoryOrder)
case "READ":
if len(elem) > 2:
exit(32)
try:
arg1 = Argument(1, elem[0].attrib["type"], elem[0].text)
arg2 = Argument(2, elem[1].attrib["type"], elem[1].text)
except IndexError:
exit(32)
return Read(arg1, arg2, factoryOrder)
case "TYPE":
if len(elem) > 2:
exit(32)
try:
arg1 = Argument(1, elem[0].attrib["type"], elem[0].text)
arg2 = Argument(2, elem[1].attrib["type"], elem[1].text)
except IndexError:
exit(32)
return Typ(arg1, arg2, factoryOrder)
case "STRLEN":
if len(elem) > 2:
exit(32)
try:
arg1 = Argument(1, elem[0].attrib["type"], elem[0].text)
arg2 = Argument(2, elem[1].attrib["type"], elem[1].text)
except IndexError:
exit(32)
return Strlen(arg1, arg2, factoryOrder)
case "ADD" | "SUB" | "MUL" | "IDIV":
if len(elem) > 3:
exit(32)
try:
arg1 = Argument(1, elem[0].attrib["type"], elem[0].text)
arg2 = Argument(2, elem[1].attrib["type"], elem[1].text)
arg3 = Argument(3, elem[2].attrib["type"], elem[2].text)
except IndexError:
exit(32)
return Operation(operation.upper(), arg1, arg2, arg3, factoryOrder)
case "AND" | "OR" | "NOT":
try:
arg1 = Argument(1, elem[0].attrib["type"], elem[0].text)
arg2 = Argument(2, elem[1].attrib["type"], elem[1].text)
if "NOT":
arg3 = arg2
else:
arg3 = Argument(3, elem[2].attrib["type"], elem[2].text)
except IndexError:
exit(32)
return BoolOperation(operation.upper(), arg1, arg2, arg3, factoryOrder)
case "GETCHAR":
if len(elem) > 3:
exit(32)
try:
arg1 = Argument(1, elem[0].attrib["type"], elem[0].text)
arg2 = Argument(2, elem[1].attrib["type"], elem[1].text)
arg3 = Argument(3, elem[2].attrib["type"], elem[2].text)
except IndexError:
exit(32)
return Getchar(arg1, arg2, arg3, factoryOrder)
case "JUMPIFEQ":
try:
arg1 = Argument(1, elem[0].attrib["type"], elem[0].text)
arg2 = Argument(2, elem[1].attrib["type"], elem[1].text)
arg3 = Argument(3, elem[2].attrib["type"], elem[2].text)
except IndexError:
exit(32)
return Jumpifeq(arg1, arg2, arg3, factoryOrder)
case "CONCAT":
if len(elem) > 3:
exit(32)
try:
arg1 = Argument(1, elem[0].attrib["type"], elem[0].text)
arg2 = Argument(2, elem[1].attrib["type"], elem[1].text)
arg3 = Argument(3, elem[2].attrib["type"], elem[2].text)
except IndexError:
exit(32)
return Concat(arg1, arg2, arg3, factoryOrder)
case _:
return None
if __name__ == '__main__':
source = "stdin"
inputFile = "stdin"
tree = None
parser = argparse.ArgumentParser()
parser.add_argument("--source", type=str, help="Soubor pro načítání XML instrukcí")
parser.add_argument("--input", type=str, help="Soubor pro vstupní data")