-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmach.py
209 lines (191 loc) · 8.62 KB
/
vmach.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
class VM:
def Order66(self, mem_dict, operator_dict, QUADS):
variable_types = {
1000: int, # vgi - int
2000: float, # vgf - float
3000: int, # vli - int
4000: float, # vlf - float
5000: int, # ti - int
6000: float, # tf - float
7000: bool, # tb - bool
8000: int, # ci - int
9000: float, # cf - float
10000: str # cs - string
}
print("hi!")
print(mem_dict)
print(operator_dict)
cont = 0
while cont < len(QUADS):
quad = QUADS[cont]
action = operator_dict[quad[0]]
"""
'+': 1,
'-': 2,
'*': 3,
'/': 4,
'<': 5,
'>': 6,
'==': 7,
'!=': 8,
'<=': 9,
'>=': 10,
'(': 11,
')': 12,
'if': 13,
'else': 14,
'=': 15,
'print': 16,
'GoToF': 17,
'GoTo': 18,
'GoToV': 19
"""
if action == "+":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
type_2 = variable_types[abs(quad[2]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])]) * (-1 if quad[1] < 0 else 1)
var2 = type_2(mem_dict[abs(quad[2])]) * (-1 if quad[2] < 0 else 1)
mem_dict[quad[3]] = var1 + var2
print("LALALA",var1, " ", action, " ", var2, " = ", mem_dict[abs(quad[3])])
cont+=1
pass
elif action == "-":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
type_2 = variable_types[abs(quad[2]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])]) * (-1 if quad[1] < 0 else 1)
var2 = type_2(mem_dict[abs(quad[2])]) * (-1 if quad[2] < 0 else 1)
mem_dict[quad[3]] = var1 - var2
print("LALALA",var1, " ", action, " ", var2, " = ", mem_dict[abs(quad[3])])
cont+=1
pass
elif action == "*":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
type_2 = variable_types[abs(quad[2]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])]) * (-1 if quad[1] < 0 else 1)
var2 = type_2(mem_dict[abs(quad[2])]) * (-1 if quad[2] < 0 else 1)
mem_dict[quad[3]] = var1 * var2
print("LALALA",var1, " ", action, " ", var2, " = ", mem_dict[abs(quad[3])])
cont+=1
pass
elif action == "/":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
type_2 = variable_types[abs(quad[2]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])]) * (-1 if quad[1] < 0 else 1)
var2 = type_2(mem_dict[abs(quad[2])]) * (-1 if quad[2] < 0 else 1)
if var2 == 0:
raise ZeroDivisionError("Division by zero is not allowed.")
mem_dict[quad[3]] = var1 / var2
print("LALALA",var1, " ", action, " ", var2, " = ", mem_dict[abs(quad[3])])
cont+=1
pass
elif action == "<":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
type_2 = variable_types[abs(quad[2]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])]) * (-1 if quad[1] < 0 else 1)
var2 = type_2(mem_dict[abs(quad[2])]) * (-1 if quad[2] < 0 else 1)
mem_dict[quad[3]] = var1 < var2
print("LALALA",var1, " ", action, " ", var2, " = ", mem_dict[abs(quad[3])])
cont+=1
pass
elif action == ">":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
type_2 = variable_types[abs(quad[2]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])]) * (-1 if quad[1] < 0 else 1)
var2 = type_2(mem_dict[abs(quad[2])]) * (-1 if quad[2] < 0 else 1)
mem_dict[quad[3]] = var1 > var2
print("LALALA",var1, " ", action, " ", var2, " = ", mem_dict[abs(quad[3])])
cont+=1
pass
elif action == "==":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
type_2 = variable_types[abs(quad[2]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])]) * (-1 if quad[1] < 0 else 1)
var2 = type_2(mem_dict[abs(quad[2])]) * (-1 if quad[2] < 0 else 1)
mem_dict[quad[3]] = var1 == var2
print("LALALA",var1, " ", action, " ", var2, " = ", mem_dict[abs(quad[3])])
cont+=1
pass
elif action == "!=":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
type_2 = variable_types[abs(quad[2]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])]) * (-1 if quad[1] < 0 else 1)
var2 = type_2(mem_dict[abs(quad[2])]) * (-1 if quad[2] < 0 else 1)
mem_dict[quad[3]] = var1 != var2
print("LALALA",var1, " ", action, " ", var2, " = ", mem_dict[abs(quad[3])])
cont+=1
pass
elif action == "<=":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
type_2 = variable_types[abs(quad[2]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])]) * (-1 if quad[1] < 0 else 1)
var2 = type_2(mem_dict[abs(quad[2])]) * (-1 if quad[2] < 0 else 1)
mem_dict[quad[3]] = var1 <= var2
print("LALALA",var1, " ", action, " ", var2, " = ", mem_dict[abs(quad[3])])
cont+=1
pass
elif action == ">=":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
type_2 = variable_types[abs(quad[2]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])]) * (-1 if quad[1] < 0 else 1)
var2 = type_2(mem_dict[abs(quad[2])]) * (-1 if quad[2] < 0 else 1)
mem_dict[quad[3]] = var1 >= var2
print("LALALA",var1, " ", action, " ", var2, " = ", mem_dict[abs(quad[3])])
cont+=1
pass
elif action == "(":
cont+=1
pass
elif action == ")":
cont+=1
pass
elif action == "if":
cont+=1
pass
elif action == "else":
cont+=1
pass
elif action == "=":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])]) * (-1 if quad[1] < 0 else 1)
mem_dict[abs(quad[3])] = var1
print("LALALA",var1, " ", action, " ", " = ", mem_dict[abs(quad[3])])
cont+=1
pass
elif action == "print":
type_1 = variable_types[abs(quad[3]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[3])])
print("LALALA",var1, " ", action, " ", " = ", mem_dict[abs(quad[3])])
print(var1)
cont+=1
pass
elif action == "GoToF":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])])
#print("LALALA",var1, " ", action, " ", " = ", mem_dict[abs(quad[3])])
print('BBBB', var1)
jump = int(abs(quad[3]))
if var1:
cont+=1
else:
cont = jump
pass
elif action == "GoTo":
print(quad)
jump = int(abs(quad[3]))
cont = jump
pass
elif action == "GoToV":
type_1 = variable_types[abs(quad[1]) // 1000 * 1000]
var1 = type_1(mem_dict[abs(quad[1])])
#print("LALALA",var1, " ", action, " ", " = ", mem_dict[abs(quad[3])])
jump = int(abs(quad[3]))
if var1:
cont = jump
else:
cont +=1
print("gotov", var1)
pass
# Add more elif statements for additional operators if needed
else:
# Handle the case where the operator is not recognized
print(f"Operator '{action}' not recognized.")