-
Notifications
You must be signed in to change notification settings - Fork 2
/
interpret.py
1080 lines (736 loc) · 24.8 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
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
#!/usr/bin/env python3
"""IPPcode18 language interpret
@author Jiri Furda (xfurda00)
"""
# Libraries
import sys
import xml.etree.ElementTree as ET
import re
import logging
# === Main function ===
def main():
"""Main body of the interpret"""
filePath = processProgramArguments()
# --- Open input file ---
try:
tree = ET.ElementTree(file=filePath)
except IOError:
Error.exit(Error.file, "Opening input file error")
except ET.ParseError:
Error.exit(Error.structure, "No element found in the file")
# --- Check root node ---
root = tree.getroot()
# --- Process instructions ---
Interpret.loadInstructions(root)
# --- Successful end ---
sys.exit(0)
# === Other functions ===
def processProgramArguments():
"""Checks and process interpret's start parameters
Returns path to source file to be interpreted
"""
# --- Check argument count ---
if len(sys.argv) != 2:
Error.exit(Error.argument, "Invalid argument count")
# --- Print argument "--help" ---
if sys.argv[1] == "--help":
print("This program interprets code in language IPPcode18 parsed to XML")
print("Author: Jiri Furda (xfurda00)")
print("Usage:")
print("python3.6 interpret.py --source=<path to .src>")
sys.exit(0)
# --- Load arguemnt "--source" ---
elif sys.argv[1][:9] == "--source=":
return sys.argv[1][9:]
# --- Check illegal argument ---
else:
Error.exit(Error.argument, "Invalid argument")
# === Classes ===
class Error:
"""Class used to store error codes and to print them"""
# Input errors
argument = 10
file = 11
# Pre-run errors
structure = 31
syntax = 32
# Running errors
semantic = 52
operands = 53
varExistence = 54
scopeExistence = 55
missingValue = 56
zeroDivide = 57
string = 58
# Other errors
custom = 59
internal = 99
@staticmethod
def exit(code, msg):
"""Prints error message to STDERR and ends with defined return code"""
print("ERROR: {0}".format(msg), file=sys.stderr)
sys.exit(code)
class Frames:
"""Class working with IPPcode18 frames to store values (Global Frame, Local Frame and Temporary Frame)"""
globalFrame = {}
localFrame = None
temporaryFrame = None
stack = [] # Stack used to store temporary frames when PUSHFRAME and POPFRAME is called
@classmethod
def add(cls, name):
"""Creates new variable in the frame defined in its name"""
# --- Identify frame ---
frame = cls.__identifyFrame(name)
# --- Remove frame prefix ---
name = name[3:]
# --- Check for duplicity ---
if name in frame:
Error.exit(Error.custom, "Variable '{0}' already exist in global frame".format(name))
# --- Create var in frame ---
frame[name] = None;
@classmethod
def set(cls, name, value):
"""Sets value to variable stored in certain frame"""
# --- Identify frame ---
frame = cls.__identifyFrame(name)
# --- Remove frame prefix ---
name = name[3:]
# --- Check if exists ---
if name not in frame:
Error.exit(Error.varExistence, "Couldn't set value to non-existing variable '{0}'".format(name))
# --- Get actual value ---
if type(value) == var: # If trying to add var (e.g. MOVE GF@aaa GF@bbb)
value = value.getValue() # Save its value not whole object
# --- Save value to frame ---
frame[name] = value;
@classmethod
def get(cls, name):
"""Returns value of variable stored in certain frame"""
# --- Identify frame ---
frame = cls.__identifyFrame(name)
# --- Remove frame prefix ---
name = name[3:]
# --- Check if exists ---
if name not in frame:
Error.exit(Error.varExistence, "Variable '{0}' does not exist".format(name))
# --- Get value from frame ---
result = frame[name]
# --- Check if initialized ---
if type(result) == type(None):
Error.exit(Error.missingValue, "Tried to get non-initilaized value")
# --- Result ---
return result;
@classmethod
def __identifyFrame(cls, name):
"""Returns specific frame depending on preffix (e.g. GF@) in variable name"""
# --- Find certain frame ---
if name[:3] == "GF@":
frame = cls.globalFrame
elif name[:3] == "LF@":
frame = cls.localFrame
elif name[:3] == "TF@":
frame = cls.temporaryFrame
# --- Check for invalid frame ---
else:
Error.exit(Error.syntax, "Invalid frame prefix") # Maybe should be Error.internal because it should be already chceked in Instruction.__loadArguments()
# --- Check for not initialized frame ---
if frame == None:
Error.exit(Error.scopeExistence, "Cannot access not initialized frame")
# --- Result frame ---
return frame
class Stack:
"""Class used for stack (values and calls)"""
def __init__(self):
"""Creates empty list for stack"""
self.content = []
def pop(self):
"""Pops value on top of the stack"""
# --- Check for empty stack ---
if len(self.content) == 0:
Error.exit(Error.missingValue, "Cannot pop empty stack")
# --- Pop value ---
return self.content.pop()
def push(self, value):
"""Pushes value to the stack"""
self.content.append(value)
class Labels:
"""Class used to store IPPcode18 labels and to jump to them"""
labels = {}
@classmethod
def add(cls, name):
"""Saves new label and its value"""
# --- Convert type label to str ---
name = str(name)
# --- Check for duplicity ---
if name in cls.labels:
Error.exit(Error.semantic, "Label '{0}' already exists".format(name))
# --- Save label ---
cls.labels[name] = Interpret.instrOrder
@classmethod
def jump(cls, name):
"""Jump interpret reading to certain label"""
# --- Convert type label to str ---
name = str(name)
# --- Check for existence ---
if name not in cls.labels:
Error.exit(Error.semantic, "Label '{0}' does not exist".format(name))
# --- Jump interpret reading to label ---
Interpret.instrOrder = cls.labels[name]
class var:
"""Class representing IPPcode18 type var"""
def __init__(self, name):
"""Sets name of var"""
self.name = name
def getValue(self):
"""Returns value stored inside var"""
return Frames.get(self.getName())
def getName(self):
"""Returns name of var including frame prefix"""
return self.name
def setValue(self, value):
"""Changes value stored inside var"""
Frames.set(self.getName(), value)
# == Actual value convert method ==
def __getValueWithType(self, expectedType):
"""Get value stored inside var and check its type"""
# --- Get value stored in var ---
value = self.getValue()
# --- Check if value is really str ---
if type(value) != expectedType:
Error.exit(Error.operands, "Unexpected type stored inside variable")
# --- Return result ---
return value
def __str__(self):
"""Get str value stored inside var"""
return self.__getValueWithType(str)
def __int__(self):
"""Get int value stored inside var"""
return self.__getValueWithType(int)
def __bool__(self):
"""Get bool value stored inside var"""
return self.__getValueWithType(bool)
class symb:
"""Dummy class representing str, int, bool or var in instruction.checkArguments()"""
pass
class label:
"""Class representing IPPcode18 type label"""
def __init__(self, name):
"""Sets name of the label"""
self.name = name
def __str__(self):
"""Gets name of the label"""
return self.name
class Interpret():
"""Main class of this program. It represents the interpret itself"""
instrOrder = 1 # Defines order number of instruction which is currently loaded
valStack = Stack() # Used by POPS and PUSHS
callStack = Stack() # Used by CALL and RETURN
@staticmethod
def checkRoot(root):
"""Checks if root node is valid"""
if root.tag != "program":
Error.exit(Error.structure, "Root node <program> not found")
if "language" in root.attrib:
if root.attrib["language"].lower() != "ippcode18":
Error.exit(Error.syntax, "Invalid language attribute")
del root.attrib["language"]
else:
Error.exit(Error.structure, "Language attribute missing")
if "name" in root.attrib:
del root.attrib["name"]
if "description" in root.attrib:
del root.attrib["description"]
if len(root.attrib) != 0:
Error.exit(Error.structure, "Invalid <program> attributes")
@classmethod
def loadInstructions(cls, root):
"""Loads all instruction nodes in source file and executes them"""
# --- Search all nodes ---
instrNodes = root.findall("./")
instrNodesCount = len(instrNodes)
# --- Search for LABEL nodes ---
cls.__findLabels(instrNodes)
cls.instrOrder = 1 # Reset instruction counter
# --- Cycle throught every node ---
while cls.instrOrder <= instrNodesCount: # Watchout! instrOrder starts at 1
# -- Get current node --
node = instrNodes[cls.instrOrder-1]
# -- Skip LABEL nodes --
if node.attrib["opcode"].upper() == "LABEL":
cls.instrOrder = cls.instrOrder+1
continue # They are already loaded by __findLabels()
# -- Processing instruction --
instruction = Instruction(node)
instruction.execute()
# -- Add counter --
cls.instrOrder = cls.instrOrder+1
@classmethod
def __findLabels(cls, instrNodes):
"""Search every LABEL instruction used and saves it"""
for node in instrNodes:
if node.attrib["opcode"].upper() == "LABEL":
cls.instrOrder = int(node.attrib["order"]) # This is read from Labels.add
instruction = Instruction(node)
instruction.execute()
@staticmethod
def convertValue(xmlType, xmlValue, die):
"""Converts XML value (str in python) to actual type (int, str, bool or var)
Parameter die is bool value determining if program ends with error or if it
reuturn default value when invalid input is given
"""
# --- Variable type ---
if xmlType == "var":
if not re.search(r"^(LF|TF|GF)@[\w_\-$&%*][\w\d_\-$&%*]*$", xmlValue):
Error.Exit(Error.syntax, "Invalid var name")
return var(xmlValue)
# --- Integer type ---
elif xmlType == "int":
if not re.search(r"^[-+]?\d+$$", xmlValue):
if die == True:
Error.Exit(Error.syntax, "Invalid int value")
else:
return 0
return int(xmlValue) # Convert str to int
# --- String type ---
elif xmlType == "string":
# -- Check empty string --
if xmlValue == None:
xmlValue = ""
if re.search(r"(?!\\[0-9]{3})[\s\\#]", xmlValue): # @see parse.php for regex legend
if die == True:
Error.Exit(Error.syntax, "Illegal characters in string")
else:
return ""
# -- Search escape sequences --
groups = re.findall(r"\\([0-9]{3})", xmlValue) # Find escape sequences
groups = list(set(groups)) # Remove duplicates
# -- Decode escape sqeuences --
for group in groups:
if group == "092": # Special case for \ (I don't even know why)
xmlValue = re.sub("\\\\092", "\\\\", xmlValue)
continue
xmlValue = re.sub("\\\\{0}".format(group), chr(int(group)), xmlValue)
# -- Return decoded string --
return xmlValue
# --- Boolean type ---
elif xmlType == "bool":
if xmlValue == "true":
boolean = True
elif xmlValue == "false":
boolean = False
else:
if die == True:
Error.Exit(Error.syntax, "Invalid bool value (given {0})".format(xmlValue))
else:
return False
return boolean
# --- Type type ---
if xmlType == "type":
if not re.search(r"^(int|string|bool)$", xmlValue):
Error.Exit(Error.syntax, "Invalid type value")
return xmlValue
# --- Type label ---
if xmlType == "label":
if not re.search(r"^[\w_\-$&%*][\w\d_\-$&%*]*$", xmlValue):
Error.Exit(Error.syntax, "Invalid label name")
return label(xmlValue)
# --- Invalid type ---
else:
Error.Exit(Error.syntax, "Unknown argument type (given {0})".format(xmlType))
class Instruction():
"""Class representing one IPPcode18 instruction"""
def __init__(self, node):
"""Initialization of internal strcture of XML <instruction> node"""
# --- Check node ---
if node.tag != "instruction":
Error.exit(Error.structure, "Wrong node loaded (Expected instruction)")
# --- Order check ---
if int(node.attrib["order"]) != Interpret.instrOrder:
Error.exit(Error.structure, "Wrong instruction order")
# --- Process node ---
self.opCode = node.attrib["opcode"].upper()
self.args = self.__loadArguments(node)
self.argCount = len(self.args)
def __loadArguments(self, instrNode):
"""Loads child nodes (<argX>) of <instruction> node"""
# --- Create list for arguments ---
args = [None] * len(instrNode)
# --- Load child nodes ---
for argNode in instrNode:
if argNode.tag[:3] != "arg":
Error.exit(Error.structure, "Wrong node loaded (expected arg node given)")
# -- Get arg index --
argIndex = int(argNode.tag[3:])-1
if argIndex > len(args):
Error.exit(Error.structure, "Argument node out of range")
if args[argIndex] != None:
Error.exit(Error.structure, "Duplicated argument node")
# --- Save arg value ---
args[argIndex] = Interpret.convertValue(argNode.attrib["type"], argNode.text, True)
# --- Check if loaded all expected arguments ---
for arg in args:
if arg == None:
Error.exit(Error.structure, "Argument node missing")
# --- Return loaded arguments ---
return(args)
def __checkArguments(self, *expectedArgs):
"""Checks if arguments have expected type"""
# --- Checking arguments count ---
if self.argCount != len(expectedArgs):
Error.exit(Error.semantic, "Invalid argument count")
# --- Converting tuple to list ---
expectedArgs = list(expectedArgs)
# --- Checking arguments type ---
i = 0;
for arg in self.args: # Check every argument
# -- Replacing <symb> --
if expectedArgs[i] == symb:
expectedArgs[i] = [int, bool, str, var]
argType = type(arg) # Saved argument's type
# -- Only one allowed type --
if type(expectedArgs[i]) == type:
if argType != expectedArgs[i]:
Error.exit(Error.operands, "Invalid argument type (expected {0} given {1})".format(expectedArgs[i],argType))
# -- More allowed types --
elif type(expectedArgs[i]) == list:
if argType not in expectedArgs[i]: # Check if used argument has one of expected types
Error.exit(Error.operands, "Invalid argument type (expected {0} given {1})".format(expectedArgs[i],argType))
# -- Wrong method parameters --
else:
Error.exit(Error.internal, "Illegal usage of Instruction.checkArguments()")
i = i+1
def execute(self):
"""Executes instruction depending on opCode"""
if self.opCode == "DEFVAR":
self.__DEFVAR()
elif self.opCode == "ADD":
self.__ADD()
elif self.opCode == "SUB":
self.__SUB()
elif self.opCode == "MUL":
self.__MUL()
elif self.opCode == "IDIV":
self.__IDIV()
elif self.opCode == "WRITE":
self.__WRITE()
elif self.opCode == "MOVE":
self.__MOVE()
elif self.opCode == "PUSHS":
self.__PUSHS()
elif self.opCode == "POPS":
self.__POPS()
elif self.opCode == "STRLEN":
self.__STRLEN()
elif self.opCode == "CONCAT":
self.__CONCAT()
elif self.opCode == "GETCHAR":
self.__GETCHAR()
elif self.opCode == "SETCHAR":
self.__SETCHAR()
elif self.opCode == "TYPE":
self.__TYPE()
elif self.opCode == "AND":
self.__AND()
elif self.opCode == "OR":
self.__OR()
elif self.opCode == "NOT":
self.__NOT()
elif self.opCode == "LT" or self.opCode == "EQ" or self.opCode == "GT":
self.__LT_EQ_GT(self.opCode)
elif self.opCode == "INT2CHAR":
self.__INT2CHAR()
elif self.opCode == "STRI2INT":
self.__STRI2INT()
elif self.opCode == "READ":
self.__READ()
elif self.opCode == "LABEL": # Called from Interpret.__findLabels()
self.__LABEL()
elif self.opCode == "JUMP":
self.__JUMP()
elif self.opCode == "JUMPIFEQ":
self.__JUMPIFEQ_JUMPIFNEQ(True)
elif self.opCode == "JUMPIFNEQ":
self.__JUMPIFEQ_JUMPIFNEQ(False)
elif self.opCode == "DPRINT" or self.opCode == "BREAK":
pass
elif self.opCode == "CREATEFRAME":
self.__CREATEFRAME()
elif self.opCode == "PUSHFRAME":
self.__PUSHFRAME()
elif self.opCode == "POPFRAME":
self.__POPFRAME()
elif self.opCode == "CALL":
self.__CALL()
elif self.opCode == "RETURN":
self.__RETURN()
else:
Error.exit(Error.syntax, "Unknown instruction code")
# === IPPcode18 methods ===
# --- Instrcution DEFVAR ---
def __DEFVAR(self):
"""@see zadani.pdf"""
self.__checkArguments(var)
Frames.add(self.args[0].getName())
# --- Instrcution ADD ---
def __ADD(self):
"""@see zadani.pdf"""
self.__checkArguments(var, [int, var], [int, var])
# -- Count and save result --
result = int(self.args[1]) + int(self.args[2])
self.args[0].setValue(result)
# --- Instrcution SUB ---
def __SUB(self):
"""@see zadani.pdf"""
self.__checkArguments(var, [int, var], [int, var])
# -- Count and save result --
result = int(self.args[1]) - int(self.args[2])
self.args[0].setValue(result)
# --- Instrcution ADD ---
def __MUL(self):
"""@see zadani.pdf"""
self.__checkArguments(var, [int, var], [int, var])
# -- Count and save result --
result = int(self.args[1]) * int(self.args[2])
self.args[0].setValue(result)
# --- Instrcution ADD ---
def __IDIV(self):
"""@see zadani.pdf"""
self.__checkArguments(var, [int, var], [int, var])
# -- Check for zero divide --
if int(self.args[2]) == 0:
Error.exit(Error.zeroDivide, "Tried to divide by zero")
# -- Count and save result --
result = int(self.args[1]) // int(self.args[2])
self.args[0].setValue(result)
# --- Instrcution WRITE ---
def __WRITE(self):
"""@see zadani.pdf"""
self.__checkArguments(symb)
# --- Get value stored in var ---
if type(self.args[0]) == var:
value = self.args[0].getValue()
else:
value = self.args[0]
# --- Prepare print for bool ---
if type(value) == bool:
if value == True:
value = "true"
else:
value = "false"
# --- Print result ---
result = str(value)
print(result)
# --- Instrcution MOVE ---
def __MOVE(self):
"""@see zadani.pdf"""
self.__checkArguments(var, symb)
self.args[0].setValue(self.args[1])
# --- Instrcution PUSHS ---
def __PUSHS(self):
"""@see zadani.pdf"""
self.__checkArguments(symb)
Interpret.valStack.push(self.args[0])
# --- Instrcution POPS ---
def __POPS(self):
"""@see zadani.pdf"""
self.__checkArguments(var)
value = Interpret.valStack.pop()
self.args[0].setValue(value)
# --- Instrcution STRLEN ---
def __STRLEN(self):
"""@see zadani.pdf"""
self.__checkArguments(var, [str, var])
result = len(str(self.args[1]))
self.args[0].setValue(result)
# --- Instrcution CONCAT ---
def __CONCAT(self):
"""@see zadani.pdf"""
self.__checkArguments(var, [str, var], [str, var])
result = str(self.args[1]) + str(self.args[2])
self.args[0].setValue(result)
# --- Instrcution GETCHAR ---
def __GETCHAR(self):
"""@see zadani.pdf"""
self.__checkArguments(var, [str, var], [int, var])
string = str(self.args[1])
position = int(self.args[2])
if position >= len(string):
Error.exit(Error.string, "GETCHAR/STRI2INT position out of range")
result = string[position]
self.args[0].setValue(result)
# --- Instrcution GETCHAR ---
def __SETCHAR(self):
"""@see zadani.pdf"""
self.__checkArguments(var, [int, var], [str, var])
string = str(self.args[0])
position = int(self.args[1])
character = str(self.args[2])
if position >= len(string):
Error.exit(Error.string, "SETCHAR position out of range")
if len(character) == 0:
Error.exit(Error.string, "SETCHAR replacement character not given")
result = string[:position] + character[0] + string[position+1:]
self.args[0].setValue(result)
# --- Instrcution TYPE ---
def __TYPE(self):
"""@see zadani.pdf"""
self.__checkArguments(var, symb)
# -- Get value inside var --
if type(self.args[1]) == var:
value = self.args[1].getValue()
else:
value = self.args[1]
# -- Convert value type name to str --
valueType = re.search(r"<class '(str|bool|int)'>", str(type(value))).group(1)
# -- Rename str to string --
if valueType == "str":
result = "string"
else:
result = valueType
# -- Save value --
self.args[0].setValue(result)
# --- Instrcution AND ---
def __AND(self):
"""@see zadani.pdf"""
self.__checkArguments(var, [bool, var], [bool, var])
result = bool(self.args[1]) and bool(self.args[2])
self.args[0].setValue(result)
# --- Instrcution OR ---
def __OR(self):
"""@see zadani.pdf"""
self.__checkArguments(var, [bool, var], [bool, var])
result = bool(self.args[1]) or bool(self.args[2])
self.args[0].setValue(result)
# --- Instrcution NOT ---
def __NOT(self):
"""@see zadani.pdf"""
self.__checkArguments(var, [bool, var])
result = not bool(self.args[1])
self.args[0].setValue(result)
# --- Instrcution LT/EQ/GT ---
def __LT_EQ_GT(self, operation):
"""@see zadani.pdf"""
self.__checkArguments(var, symb, symb)
# -- Get values inside var --
if type(self.args[1]) == var:
valueA = self.args[1].getValue()
else:
valueA = self.args[1]
if type(self.args[2]) == var:
valueB = self.args[2].getValue()
else:
valueB = self.args[2]
# -- Check for same type --
if type(valueA) != type(valueB):
Error.exit(Error.operands, "Can't compare different types")
# -- Compare values --
if operation == "LT":
result = valueA < valueB
elif operation == "EQ":
result = valueA == valueB
elif operation == "GT":
result = valueA > valueB
else:
Error.exit(Error.internal, "Invalid operation in Instruction.LT_EQ_GT")
# -- Save result --
self.args[0].setValue(result)
# --- Instrcution INT2CHAR ---
def __INT2CHAR(self):
"""@see zadani.pdf"""
self.__checkArguments(var, [int, var])
value = int(self.args[1])
try:
result = chr(value)
except ValueError:
Error.exit(Error.string, "INT2CHAR invalid character code")
# -- Save result --
self.args[0].setValue(result)
# --- Instrcution STRI2INT ---
def __STRI2INT(self):
"""@see zadani.pdf"""
self.__GETCHAR()
result = ord(self.args[0].getValue()) # Get char's ASCII code
# -- Save result --
self.args[0].setValue(result)
# --- Instrcution READ ---
def __READ(self):
"""@see zadani.pdf"""
self.__checkArguments(var, str) # Should be <var> <type> but there is no class Type
inputStr = input()
# -- Bool input special rules --
inputStr = inputStr.lower()
# -- Convert input type --
result = Interpret.convertValue(self.args[1], inputStr, False)
# -- Save result --
self.args[0].setValue(result)
# --- Instrcution LABEL ---
def __LABEL(self): # Called from Interpret.__findLabels()
"""@see zadani.pdf"""
self.__checkArguments(label)
Labels.add(self.args[0])
# --- Instrcution JUMP ---
def __JUMP(self):
"""@see zadani.pdf"""
self.__checkArguments(label)
Labels.jump(self.args[0])
# --- Instrcutions JUMPIFEQ & JUMPIFNEQ ---
def __JUMPIFEQ_JUMPIFNEQ(self, expectedResult):
"""@see zadani.pdf"""
self.__checkArguments(label, symb, symb)
# -- Get values inside var --
if type(self.args[1]) == var:
valueA = self.args[1].getValue()
else:
valueA = self.args[1]
if type(self.args[2]) == var:
valueB = self.args[2].getValue()
else: