-
Notifications
You must be signed in to change notification settings - Fork 0
/
expressioner.py
246 lines (208 loc) · 8.68 KB
/
expressioner.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
from helping_functions import *
class Expression:
def __init__(self, variable1, variable2=None):
self.variable1 = variable1
self.variable2 = variable2
def generate_code(self, register):
pass
class ValueExpression(Expression):
def generate_code(self, register):
return load_value_to_register(self.variable1, register)
class AddExpression(Expression):
def generate_code(self, register):
register = "b"
second_register = "b" if register != "b" else "c"
if type(self.variable1) == int and type(self.variable2) == int:
return load_value_to_register(self.variable1 + self.variable2, register)
elif isinstance(self.variable1, Variable) and type(self.variable2) == int:
if self.variable2 == 0:
return ValueExpression(self.variable1).generate_code(register)
elif type(self.variable1) == int and isinstance(self.variable2, Variable):
if self.variable1 == 0:
return ValueExpression(self.variable2).generate_code(register)
code = load_two_values_to_registers(
self.variable1, self.variable2, register, second_register)
code.append("ADD " + register + " " + second_register)
return code
class SubExpression(Expression):
def generate_code(self, register):
register = "b"
second_register = "b" if register != "b" else "c"
if type(self.variable1) == int and type(self.variable2) == int:
return load_value_to_register(self.variable1 - self.variable2, register)
elif isinstance(self.variable1, Variable) and type(self.variable2) == int:
if self.variable2 == 0:
return ValueExpression(self.variable1).generate_code(register)
elif type(self.variable1) == int and isinstance(self.variable2, Variable):
if self.variable1 == 0:
return ValueExpression(self.variable2).generate_code(register)
code = load_two_values_to_registers(
self.variable1, self.variable2, register, second_register)
code.append("SUB " + register + " " + second_register)
return code
class MulExpression(Expression):
def generate_code(self, register):
register = "b"
second_register = "b" if register != "b" else "c"
if type(self.variable1) == int and type(self.variable2) == int:
return load_value_to_register(self.variable1 * self.variable2, register)
elif isinstance(self.variable1, Variable) and type(self.variable2) == int:
if self.variable2 == 1:
return ValueExpression(self.variable1).generate_code(register)
elif self.variable2 == 0:
return ["RESET " + register]
elif type(self.variable1) == int and isinstance(self.variable2, Variable):
if self.variable1 == 1:
return ValueExpression(self.variable2).generate_code(register)
elif self.variable1 == 0:
return ["RESET " + register]
code = load_two_values_to_registers(
self.variable1, self.variable2, register, second_register)
code.append("JZERO b 31") # czy first = 0
code.append("DEC b")
code.append("JZERO b 2") # czy first = 1?
code.append("JUMP 3") # gdy first > 1
code.append("ADD b c")
code.append("JUMP 26")
code.append("INC b") # restore first
code.append("JZERO c 2") # czy second = 0
code.append("JUMP 3")
code.append("RESET b")
code.append("JUMP 21")
code.append("DEC c")
code.append("JZERO c 19") # czy second = 1?
code.append("INC c")
code.append("RESET d") # Sprawdzenie któa większa
code.append("ADD d c")
code.append("SUB d b")
# jump jeśli B-first, C-second, D-second<first
code.append("JZERO d 5")
code.append("RESET c") # Przed tym: B-first, C-second, D-second-first
code.append("ADD c b")
code.append("ADD b d")
code.append("RESET d")
# mnożenie
code.append("JODD c 5")
code.append("ADD " + register + " " + register)
code.append("SHR " + second_register)
code.append("JZERO " + second_register + " 4")
code.append("JUMP -4")
code.append("ADD d " + register)
code.append("JUMP -5")
code.extend(["RESET " + register, "ADD " + register + " d"])
return code
class DivExpression(Expression):
def generate_code(self, register):
register = "b"
second_register = "b" if register != "b" else "c"
if type(self.variable1) == int and type(self.variable2) == int:
return load_value_to_register(self.variable1 // self.variable2 if self.variable2 != 0 else 0, register)
elif isinstance(self.variable1, Variable) and type(self.variable2) == int:
if self.variable2 == 0:
return ["RESET a"]
if self.variable2 == 1:
return ValueExpression(self.variable1).generate_code(register)
elif type(self.variable1) == int and isinstance(self.variable2, Variable):
if self.variable1 == 0:
return ["RESET a"]
code = load_two_values_to_registers(
self.variable1, self.variable2, register, second_register)
code.extend(["RESET d", "RESET e", "RESET f"])
code.append("JZERO c 2") # gdy drugi jest 0
code.append("JUMP 3")
code.append("RESET b")
code.append("JUMP 31") # skok ponad wszystko
# sprawdzenie czy 1
code.append("DEC c")
code.append("JZERO c 29")
# sprawdzeie czy 2
code.append("DEC c")
code.append("JZERO c 4")
code.append("INC c")
code.append("INC c")
code.append("JUMP 3")
code.append("SHR b")
code.append("JUMP 22")
#
##
code.append("INC e") # e=1
code.append("RESET f")
code.append("ADD f b")
code.append("SUB f c")
code.append("JZERO f 4")
code.append("SHL c")
code.append("SHL e")
code.append("JUMP -6")
code.append("RESET f")
code.append("ADD f c")
code.append("SUB f b")
code.append("JZERO f 2")
code.append("JUMP 3")
code.append("SUB b c")
code.append("ADD d e")
code.append("SHR c")
code.append("SHR e")
code.append("JZERO e 2") # exit
code.append("JUMP -10")
code.append("RESET b")
code.append("ADD b d")
return code
class ModExpression(Expression):
def generate_code(self, register):
register = "b"
second_register = "b" if register != "b" else "c"
if type(self.variable1) == int and type(self.variable2) == int:
return load_value_to_register(self.variable1 % self.variable2 if self.variable2 != 0 else 0, register)
elif isinstance(self.variable1, Variable) and type(self.variable2) == int:
if self.variable2 == 0 or self.variable2 == 1:
return ["RESET b"]
elif type(self.variable1) == int and isinstance(self.variable2, Variable):
if self.variable1 == 0:
return ["RESET b"]
code = load_two_values_to_registers(
self.variable1, self.variable2, register, second_register)
code.extend(["RESET d", "RESET e", "RESET f"])
code.append("JZERO c 2") # gdy drugi jest 0
code.append("JUMP 3")
code.append("RESET b")
code.append("JUMP 38") # skok ponad wszystko
# sprawdzenie czy 1
code.append("DEC c")
code.append("JZERO c 2")
code.append("JUMP 3")
code.append("RESET b")
code.append("JUMP 33")
# sprawdzeie czy 2
code.append("DEC c")
code.append("JZERO c 4")
code.append("INC c")
code.append("INC c")
code.append("JUMP 9")
code.append("JODD b 3")
code.append("RESET b")
code.append("JUMP 25")
code.append("RESET b")
code.append("INC b")
code.append("JUMP 22")
code.append("INC b")
code.append("JUMP 20")
code.append("INC e") # e=1
code.append("RESET f")
code.append("ADD f b")
code.append("SUB f c")
code.append("JZERO f 4")
code.append("SHL c")
code.append("SHL e")
code.append("JUMP -6")
code.append("RESET f")
code.append("ADD f c")
code.append("SUB f b")
code.append("JZERO f 2")
code.append("JUMP 3")
code.append("SUB b c")
code.append("ADD d e")
code.append("SHR c")
code.append("SHR e")
code.append("JZERO e 2") # exit
code.append("JUMP -10")
return code