-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.py
598 lines (517 loc) · 20.8 KB
/
variables.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
import sys
TYPE_NONE = 0
TYPE_INT = 1
TYPE_BOOL = 2
TYPE_STRING = 3
TYPE_FLOAT = 4
class VariablesFactory:
def __init__(self, frames):
"""
Sets default values
"""
self.frames = frames
self.data_stack = []
def def_var(self, var):
"""
Defines variable
:param var: variable
"""
frame, name = var[1].split("@")
new_variable = Variable(name)
if frame == "GF":
self.frames.add_to_global_frame(new_variable)
elif frame == "TF":
self.frames.add_to_temporary_frame(new_variable)
elif frame == "LF":
self.frames.add_to_local_frame(new_variable)
def move_to_var(self, var, symb):
"""
Moves symbol to variable
:param var: variable
:param symb: symbol
"""
variable = self.get_var(var)
if symb[0] == "string":
variable.variable_type = TYPE_STRING
variable.value = symb[1]
elif symb[0] == "int":
variable.variable_type = TYPE_INT
variable.value = symb[1]
elif symb[0] == "float":
variable.variable_type = TYPE_FLOAT
variable.value = symb[1]
elif symb[0] == "bool":
variable.variable_type = TYPE_BOOL
variable.value = symb[1]
elif symb[0] == "var":
symb_var = self.get_var(symb, True)
variable.value = symb_var.value
variable.variable_type = symb_var.variable_type
def get_var(self, var, check_if_initialized=False):
"""
Gets variable by name
:param var: Variable
:param check_if_initialized: If true, raises error when variable is not inizialized
:return: Variable
"""
if var == "stack":
return Variable("stack")
frame, name = var[1].split("@")
variable = None
if frame == "GF":
variable = self.frames.get_from_global_frame(name)
elif frame == "TF":
variable = self.frames.get_from_temporary_frame(name)
elif frame == "LF":
variable = self.frames.get_from_local_frame(name)
if variable.variable_type == TYPE_NONE and check_if_initialized:
sys.stderr.write("ERROR: %s is not initialized!\n" % var[1])
exit(56)
return variable
def print_var(self, symb, debug=False):
"""
Prints symbol value
:param symb: Symbol
:param debug: True if DPRINT is used
"""
if symb[0] == "var":
variable = self.get_var(symb, True)
data_type = variable.variable_type
value = variable.value
else:
data_type, value = self.get_symbol_type_and_value(symb)
if data_type == TYPE_FLOAT:
value = float.hex(value)
if value is None:
sys.stderr.write("ERROR: %s does not have value!\n" % symb[1])
exit(56)
if debug:
sys.stderr.write(value)
else:
print(value)
def aritmetic_operation(self, var, symb1, symb2, operation):
"""
Interprets all aritmetic operations
:param var: Variable, where result is saved
:param symb1: First opearnd
:param symb2: Second operand
:param operation: Operation
"""
if var is None and symb1 is None and symb2 is None:
variable = self.get_var("stack")
symb2_type, symb2_value = self.pop_data_stack()
symb1_type, symb1_value = self.pop_data_stack()
else:
variable = self.get_var(var)
symb1_type, symb1_value = self.get_symbol_type_and_value(symb1)
symb2_type, symb2_value = self.get_symbol_type_and_value(symb2)
if symb1_type != TYPE_FLOAT and symb1_type != TYPE_INT:
sys.stderr.write("ERROR: Aritmetic instructions requires two float or int types!\n")
self.wrong_operands_exit(symb1, symb2, types=[TYPE_FLOAT, TYPE_INT])
elif operation == "idiv" and symb1_type != TYPE_INT:
sys.stderr.write("ERROR: IDIV instruction requires two int types!\n")
self.wrong_operands_exit(symb1, symb2, types=[TYPE_INT])
elif operation == "div" and symb1_type != TYPE_FLOAT:
sys.stderr.write("ERROR: DIV instruction requires two float types!\n")
self.wrong_operands_exit(symb1, symb2, types=[TYPE_FLOAT])
elif symb1_type != symb2_type:
sys.stderr.write("ERROR: Aritmetic instruction requires same types!\n")
exit(53)
variable.variable_type = symb1_type
if operation == "add":
variable.value = symb1_value + symb2_value
elif operation == "sub":
variable.value = symb1_value - symb2_value
elif operation == "mul":
variable.value = symb1_value * symb2_value
elif operation == "idiv":
try:
variable.value = symb1_value // symb2_value
except ZeroDivisionError:
sys.stderr.write("ERROR: IDIV division by zero!\n")
exit(57)
elif operation == "div":
try:
variable.value = symb1_value / symb2_value
except ZeroDivisionError:
sys.stderr.write("ERROR: IDIV division by zero!\n")
exit(57)
if var is None and symb1 is None and symb2 is None:
self.data_stack.append([variable.variable_type, variable.value])
def relation_operator(self, var, symb1, symb2, operator):
"""
Interprets all relation operations
:param var: Variable, where result is saved
:param symb1: First opearnd
:param symb2: Second operand
:param operator: Operator
"""
if var is None and symb1 is None and symb2 is None:
variable = self.get_var("stack")
symb2_type, symb2_value = self.pop_data_stack()
symb1_type, symb1_value = self.pop_data_stack()
else:
variable = self.get_var(var)
symb1_type, symb1_value = self.get_symbol_type_and_value(symb1)
symb2_type, symb2_value = self.get_symbol_type_and_value(symb2)
if symb1_type != symb2_type:
sys.stderr.write("ERROR: Both symbols must have same type for relation operators use!\n")
exit(53)
variable.variable_type = TYPE_BOOL
if operator == "lt":
variable.value = str(symb1_value < symb2_value).lower()
elif operator == "gt":
variable.value = str(symb1_value > symb2_value).lower()
elif operator == "eq":
variable.value = str(symb1_value == symb2_value).lower()
if var is None and symb1 is None and symb2 is None:
self.data_stack.append([variable.variable_type, variable.value])
def bool_operator(self, var, symb1, symb2, operator):
"""
Interprets all boolean operations
:param var: Variable, where result is saved
:param symb1: First opearnd
:param symb2: Second operand
:param operator: Operator
"""
if var is None and symb1 is None and symb2 is None:
variable = self.get_var("stack")
symb2_type, symb2_value = self.pop_data_stack()
if operator == "not":
symb1_type, symb1_value = symb2_type, symb2_value
else:
symb1_type, symb1_value = self.pop_data_stack()
else:
variable = self.get_var(var)
symb1_type, symb1_value = self.get_symbol_type_and_value(symb1)
symb2_type, symb2_value = self.get_symbol_type_and_value(symb2)
if symb1_type != symb2_type or symb1_type != TYPE_BOOL:
sys.stderr.write("ERROR: Bool instructions can only have two symbols with bool types!\n")
self.wrong_operands_exit(symb1, symb2, types=[TYPE_BOOL])
if symb1_value == "true":
symb1_actual = True
else:
symb1_actual = False
if symb2_value == "true":
symb2_actual = True
else:
symb2_actual = False
variable.variable_type = TYPE_BOOL
if operator == "and":
variable.value = str(symb1_actual and symb2_actual).lower()
elif operator == "or":
variable.value = str(symb1_actual or symb2_actual).lower()
elif operator == "not":
variable.value = str(not symb1_actual).lower()
if var is None and symb1 is None and symb2 is None:
self.data_stack.append([variable.variable_type, variable.value])
def get_symbol_type_and_value(self, symb, check_if_initialized=True):
"""
Gets value and type of symbol
:param check_if_initialized: If true, then raises error when variable is not initialized
:param symb: Symbol
"""
if symb[0] == "var":
var = self.get_var(symb, check_if_initialized)
return var.variable_type, var.value
elif symb[0] == "float":
return TYPE_FLOAT, symb[1]
elif symb[0] == "int":
return TYPE_INT, symb[1]
elif symb[0] == "bool":
return TYPE_BOOL, symb[1]
elif symb[0] == "string":
return TYPE_STRING, symb[1]
def get_type(self, var, symb):
"""
Gets symbol type
:param var: Variable, where result is saved
:param symb: Symbol
"""
variable = self.get_var(var)
symb_type, symb_value = self.get_symbol_type_and_value(symb, False)
variable.variable_type = TYPE_STRING
if symb_type == TYPE_STRING:
variable.value = "string"
elif symb_type == TYPE_INT:
variable.value = "int"
elif symb_type == TYPE_FLOAT:
variable.value = "float"
elif symb_type == TYPE_BOOL:
variable.value = "bool"
else:
variable.value = ""
def int_to_char(self, var, symb):
"""
Interprets INT2CHAR instruction
:param var: Variable, where result is saved
:param symb: Operand
"""
if var is None and symb is None:
variable = self.get_var("stack")
symb_type, symb_value = self.pop_data_stack()
else:
variable = self.get_var(var)
symb_type, symb_value = self.get_symbol_type_and_value(symb)
if symb_type != TYPE_INT:
sys.stderr.write("ERROR: INT2CHAR requires symbol with int type!\n")
self.wrong_operands_exit(symb, types=[TYPE_INT])
variable.variable_type = TYPE_STRING
try:
variable.value = chr(symb_value)
except ValueError:
sys.stderr.write("ERROR: INT2CHAR requires symbol with valid ordinary char value in UNICODE!\n")
exit(58)
if var is None and symb is None:
self.data_stack.append([variable.variable_type, variable.value])
def stri_to_int(self, var, symb1, symb2):
"""
Interprets STRI2INT instruction
:param var: Variable, where result is saved
:param symb1: First operand
:param symb2: Second operand
"""
if var is None and symb1 is None and symb2 is None:
variable = self.get_var("stack")
symb2_type, symb2_value = self.pop_data_stack()
symb1_type, symb1_value = self.pop_data_stack()
else:
variable = self.get_var(var)
symb1_type, symb1_value = self.get_symbol_type_and_value(symb1)
symb2_type, symb2_value = self.get_symbol_type_and_value(symb2)
if symb1_type != TYPE_STRING:
sys.stderr.write("ERROR: STRI2INT must have first symbol with string type!\n")
self.wrong_operands_exit(symb1, types=[TYPE_STRING])
elif symb2_type != TYPE_INT:
sys.stderr.write("ERROR: STRI2INT must have second symbol with int type!\n")
self.wrong_operands_exit(symb2, types=[TYPE_INT])
variable.variable_type = TYPE_INT
try:
variable.value = ord(symb1_value[symb2_value])
except IndexError:
sys.stderr.write("ERROR: STRI2INT string out of range!\n")
exit(58)
if var is None and symb1 is None and symb2 is None:
self.data_stack.append([variable.variable_type, variable.value])
def is_equal(self, symb1, symb2):
"""
Checks if symbols are equal
:param symb1: First operand
:param symb2: Second operand
"""
if symb1 is None and symb2 is None:
symb2_type, symb2_value = self.pop_data_stack()
symb1_type, symb1_value = self.pop_data_stack()
else:
symb2_type, symb2_value = self.get_symbol_type_and_value(symb2)
symb1_type, symb1_value = self.get_symbol_type_and_value(symb1)
if symb1_type != symb2_type:
sys.stderr.write("ERROR: Both symbols in JUMPIFEQ must be same type!\n")
exit(53)
return symb1_value == symb2_value
def is_not_equal(self, symb1, symb2):
"""
Checks if symbols are not equal
:param symb1: First operand
:param symb2: Second operand
"""
if symb1 is None and symb2 is None:
symb2_type, symb2_value = self.pop_data_stack()
symb1_type, symb1_value = self.pop_data_stack()
else:
symb2_type, symb2_value = self.get_symbol_type_and_value(symb2)
symb1_type, symb1_value = self.get_symbol_type_and_value(symb1)
if symb1_type != symb2_type:
sys.stderr.write("ERROR: Both symbols in JUMPIFNEQ must be same type!\n")
exit(53)
return symb1_value != symb2_value
def clear_stack(self):
"""
Clears data stack
"""
self.data_stack = []
def push_stack(self, symb):
"""
Pushes symbol to data stack
:param symb: symbol
"""
symb_type, symb_value = self.get_symbol_type_and_value(symb)
self.data_stack.append([symb_type, symb_value])
def pop_stack(self, var):
"""
Pops symbol from data stack to variable
:param var: variable
"""
variable = self.get_var(var)
variable.variable_type, variable.value = self.pop_data_stack()
def pop_data_stack(self):
"""
Checks if data stack can be popped and pops it
:return: Returns popped symbol
"""
if len(self.data_stack) == 0:
sys.stderr.write("ERROR: Data stack is empty!\n")
exit(56)
return self.data_stack.pop()
def concat_strings(self, var, symb1, symb2):
"""
Interprets CONCAT instruction
:param var: Variable, where result is saved
:param symb1: First operand
:param symb2: Second operand
"""
variable = self.get_var(var)
symb1_type, symb1_value = self.get_symbol_type_and_value(symb1)
symb2_type, symb2_value = self.get_symbol_type_and_value(symb2)
if symb1_type != TYPE_STRING or symb2_type != TYPE_STRING:
sys.stderr.write("ERROR: CONCAT needs two string symbols!\n")
self.wrong_operands_exit(symb1, symb2, types=[TYPE_STRING])
variable.variable_type = TYPE_STRING
variable.value = symb1_value + symb2_value
def len_string(self, var, symb):
"""
Interprets STRLEN instruction
:param var: Variable, where result is saved
:param symb: Operand
"""
variable = self.get_var(var)
symb_type, symb_value = self.get_symbol_type_and_value(symb)
if symb_type != TYPE_STRING:
sys.stderr.write("ERROR: STRLEN needs string symbol!\n")
self.wrong_operands_exit(symb, types=[TYPE_STRING])
variable.variable_type = TYPE_INT
variable.value = len(symb_value)
def get_char(self, var, symb1, symb2):
"""
Interprets GETCHAR instruction
:param var: Variable, where result is saved
:param symb1: First operand
:param symb2: Second operand
"""
variable = self.get_var(var)
symb1_type, symb1_value = self.get_symbol_type_and_value(symb1)
symb2_type, symb2_value = self.get_symbol_type_and_value(symb2)
if symb1_type != TYPE_STRING:
sys.stderr.write("ERROR: GETCHAR's first symbol must be string!\n")
self.wrong_operands_exit(symb1, types=[TYPE_STRING])
elif symb2_type != TYPE_INT:
sys.stderr.write("ERROR: GETCHAR's second symbol must be int!\n")
self.wrong_operands_exit(symb2, types=[TYPE_INT])
variable.variable_type = TYPE_STRING
try:
variable.value = symb1_value[symb2_value]
except IndexError:
sys.stderr.write("ERROR: GETCHAR string out of range!\n")
exit(58)
def set_char(self, var, symb1, symb2):
"""
Interprets SETCHAR instruction
:param var: Variable to modify
:param symb1: First operand
:param symb2: Second operand
"""
variable = self.get_var(var)
symb1_type, symb1_value = self.get_symbol_type_and_value(symb1)
symb2_type, symb2_value = self.get_symbol_type_and_value(symb2)
if variable.variable_type != TYPE_STRING:
sys.stderr.write("ERROR: SETCHAR needs string var!\n")
exit(53)
elif symb1_type != TYPE_INT:
sys.stderr.write("ERROR: SETCHAR's first symbol must be int!\n")
self.wrong_operands_exit(symb1, types=[TYPE_INT])
elif symb2_type != TYPE_STRING:
sys.stderr.write("ERROR: SETCHAR's second symbol must be string!\n")
self.wrong_operands_exit(symb2, types=[TYPE_STRING])
try:
if symb1_value > (len(variable.value) - 1) or symb1_value < 0:
raise IndexError
variable.value = variable.value[:symb1_value] + symb2_value[0] + variable.value[symb1_value + 1:]
except IndexError:
sys.stderr.write("ERROR: SETCHAR string out of range!\n")
exit(58)
def read_var(self, var, var_type):
"""
Reads variable value from stdin
:param var: Variable
:param var_type: Variable type
"""
var_type = var_type[1]
try:
value = input()
except (EOFError, KeyboardInterrupt):
value = None
if var_type == "float":
var_type = TYPE_FLOAT
try:
value = float.fromhex(value)
except (ValueError, TypeError):
value = float(0)
elif var_type == "int":
var_type = TYPE_INT
try:
value = int(value)
except (ValueError, TypeError):
value = 0
elif var_type == "bool":
var_type = TYPE_BOOL
if value is not None:
value = value.lower()
if value != "true" and value != "false":
value = "false"
elif var_type == "string":
var_type = TYPE_STRING
if value is None:
value = ""
variable = self.get_var(var)
variable.variable_type = var_type
variable.value = value
def int_to_float(self, var, symb):
"""
Converts int to float
:param var: Variable, where float value is saved
:param symb: Int symbol
"""
variable = self.get_var(var)
symb_type, symb_value = self.get_symbol_type_and_value(symb)
if symb_type != TYPE_INT:
sys.stderr.write("ERROR: INT2FLOAT requires symbol with int type!\n")
self.wrong_operands_exit(symb, types=[TYPE_INT])
variable.variable_type = TYPE_FLOAT
variable.value = float(symb_value)
def float_to_int(self, var, symb):
"""
Converts float to int
:param var: Variable, where int value is saved
:param symb: Float symbol
"""
variable = self.get_var(var)
symb_type, symb_value = self.get_symbol_type_and_value(symb)
if symb_type != TYPE_FLOAT:
sys.stderr.write("ERROR: FLOAT2INT requires symbol with float type!\n")
self.wrong_operands_exit(symb, types=[TYPE_FLOAT])
variable.variable_type = TYPE_INT
variable.value = int(symb_value)
def wrong_operands_exit(self, *symbols, types=None):
"""
Checks operands and exits on wrong operand types
:param symbols: Operands
:param types: Allowd types
"""
if types is None:
types = []
for symb in symbols:
data_type, value = self.get_symbol_type_and_value(symb)
if data_type not in types:
if symb[0] == "var":
exit(53)
else:
exit(52)
class Variable:
def __init__(self, name):
"""
Defines variable default values
:param name: Variable name
"""
self.name = name
self.variable_type = TYPE_NONE
self.value = None