-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction.py
347 lines (289 loc) · 13.8 KB
/
instruction.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
import GBM_generator
import random
from typing import *
# this file contains the class definition for the instruction class
# this class is used to hold the information about the price of the instruction
class Instruction:
def __init__(self, name: str, runtime: object) -> None:
self.name = name
self.runtime = runtime
def get_price(self) -> float:
return 0
def buy(self, count: int):
price = self.get_price()*count
return price
def sell(self, count: int):
price = self.get_price()*count
return price
class PayedInstruction(Instruction):
def __init__(self, name: str, runtime: object, start_price: int=100) -> None:
self.generator = GBM_generator.GBM_generator()
self.generator.simulate_gbm(x0=start_price)
self.mu = 0
self.last_price = start_price
super().__init__(name, runtime)
def get_price(self) -> float:
self.last_price = self.generator.generator.send(self.mu)
return self.last_price
def wait(self):
if self.mu > 0:
self.mu -= random.uniform(0, 5)
elif self.mu < 0:
self.mu += random.uniform(0, 5)
self.get_price()
return self.last_price
class UnpayedInstruction(Instruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def get_price(self) -> float:
return 0
class PriceInstruction(UnpayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, inst: str) -> Union[Exception, bool]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log(f"Executing price instruction for instruction: {inst} on line {self.runtime.current_line}")
try:
if inst not in self.runtime.instructions:
self.runtime.logger.log(Exception("Instruction not found"))
else:
return self.runtime.instructions[inst].get_price()
except Exception as e:
self.runtime.logger.log( e)
class BuyInstruction(UnpayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, inst: str, count: str) -> Union[Exception, bool]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log(f"Executing buy instruction for instruction: {inst} on line {self.runtime.current_line}")
try:
count = int(count)
if inst not in self.runtime.instructions:
self.runtime.logger.log(Exception("Instruction not found"))
else:
if count < 0:
self.runtime.logger.log( Exception("Invalid count"))
if self.runtime.user_instructions[inst].__class__.__name__ == "UnpayedInstruction":
self.runtime.logger.log( Exception("Instruction is not buyable"))
price = self.runtime.instructions[inst].buy(count)
self.runtime.wallet -= price
self.runtime.user_instructions[inst] += count
return True
except Exception as e:
self.runtime.logger.log(e)
class SellInstruction(UnpayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, inst: str, count: int) -> Union[Exception, bool]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log(f"Executing sell instruction for instruction: {inst} on line {self.runtime.current_line}")
try:
if inst not in self.runtime.instructions:
self.runtime.logger.log(Exception("Instruction not found"))
else:
if count < 0:
self.runtime.logger.log(Exception("Invalid count"))
if self.runtime.user_instructions[inst].__class__.__name__ == "UnpayedInstruction":
self.runtime.logger.log(Exception("Instruction is not sellable"))
price = self.runtime.instructions[inst].sell(count)
if self.runtime.user_instructions[inst] < count:
self.runtime.logger.log(Exception("Not enough instructions"))
else:
self.runtime.wallet += price
self.runtime.user_instructions[inst] -= count
return True
except Exception as e:
self.runtime.logger.log(e)
class CountInstruction(UnpayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, inst: str) -> Union[Exception, int]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log(f"Executing Count instruction for instruction: {inst}")
try:
if inst not in self.runtime.instructions:
self.runtime.logger.log(Exception("Instruction not found"))
else:
if self.runtime.user_instructions[inst].__class__.__name__ == "UnpayedInstruction":
self.runtime.logger.log(Exception("You have infinite number of these instructions"))
return self.runtime.user_instructions[inst]
except Exception as e:
self.runtime.logger.log(e)
class WalletInstruction(UnpayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self) -> float:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log("Executing wallet instruction")
return self.runtime.wallet
class WaitInstruction(UnpayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, time: str) -> Union[Exception, bool]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log("Executing wait instruction for time: "+time+ " ticks")
try:
for i in range(int(time)):
for inst in self.runtime.instructions.keys():
if self.runtime.instructions[inst].__class__.__name__ == "PayedInstruction":
self.runtime.instructions[inst].wait()
rentcost = self.runtime.rented_instructions*10*time
self.runtime.wallet -= rentcost
return True
except Exception as e:
self.runtime.logger.log(e)
class PrintInstruction(UnpayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, *variable_name: str) -> Union[Exception, bool]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log("Executing print instruction for variable: "+variable_name[0])
try:
if variable_name[0] not in self.runtime.variables:
variable_name = " ".join(variable_name)
self.runtime.logger.log(variable_name)
else:
self.runtime.logger.log(self.runtime.variables[variable_name[0]])
return True
except Exception as e:
self.runtime.logger.log(e)
class EndInstruction(UnpayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self) -> bool:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log("Executing end instruction")
self.runtime.stop()
return True
class RentInstruction(UnpayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, variable_name: str) -> Union[Exception, bool]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log("Executing rent instruction for variable: "+variable_name)
try:
if variable_name in self.runtime.variables:
self.runtime.logger.log(Exception("Variable already exists"))
else:
self.runtime.variables[variable_name] = 0
self.runtime.rented_instructions += 1
return True
except Exception as e:
self.runtime.logger.log(e)
class ReleaseInstruction(UnpayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, variable_name: str) -> Union[Exception, bool]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log("Executing release instruction for variable: "+variable_name)
try:
if variable_name not in self.runtime.variables:
if self.runtime.rented_instructions == 0:
self.runtime.logger.log(Exception("No rented instructions"))
self.runtime.logger.log(Exception("Variable not found"))
else:
del self.runtime.variables[variable_name]
self.runtime.rented_instructions -= 1
return True
except Exception as e:
self.runtime.logger.log(e)
# Payed instructions
class GetCharInstruction(PayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, variable, pos) -> Union[Exception, str]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log("Executing getchar instruction")
try:
if variable not in self.runtime.variables:
self.runtime.logger.log(Exception("Variable not found"))
else:
return self.runtime.variables[variable][int(pos)]
except Exception as e:
self.runtime.logger.log(e)
class IfInstruction(PayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, var1: str, condition: str, var2: str) -> Union[Exception, bool]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log(f"Executing if instruction for condition: {var1} {condition} {var2}")
try:
if var1 not in self.runtime.variables:
var1 = self.compute_type(var1)
else:
var1 = self.runtime.variables[var1]
if var2 not in self.runtime.variables:
var2 = self.compute_type(var2)
else:
var2 = self.runtime.variables[var2]
if condition == "==":
return var1 == var2
elif condition == "!=":
return var1 != var2
elif condition == ">":
return var1 > var2
elif condition == "<":
return var1 < var2
elif condition == ">=":
return var1 >= var2
elif condition == "<=":
return var1 <= var2
except Exception as e:
self.runtime.logger.log(e)
def compute_type(self,value):
if value[0] == "\"" and value[-1] == "\"":
return str(value)
else:
try:
return float(value)
except:
self.runtime.logger.log(Exception(f"Invalid value: {value}"))
class GotoInstruction(PayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, block_name: str) -> Union[Exception, bool]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log("Executing goto instruction for block: "+block_name)
try:
if block_name not in self.runtime.codeblocks:
self.runtime.logger.log(Exception("Codeblock not found"))
else:
self.runtime.current_line = self.runtime.codeblocks[block_name]
return True
except Exception as e:
self.runtime.logger.log( e)
class ReadMemInstruction(PayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, pos: str) -> Union[Exception, bool]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log("Executing readmem instruction for memory position "+pos+" on line "+str(self.runtime.current_line)+" of code")
try:
if pos in self.runtime.variables:
pos = self.runtime.variables[pos]
else:
pos = int(pos)
if pos not in self.runtime.memory:
return 0
return self.runtime.memory[pos]
except Exception as e:
self.runtime.logger.log(e)
class WriteMemInstruction(PayedInstruction):
def __init__(self, name: str, runtime: object) -> None:
super().__init__(name, runtime)
def execute(self, pos: str, value: str) -> Union[Exception, bool]:
if self.runtime.logger.loglevel >= 2:
self.runtime.logger.log("Executing writemem instruction" + " for memory position "+pos+" with value "+value +" on line "+str(self.runtime.current_line)+" of code")
try:
if pos in self.runtime.variables:
pos = self.runtime.variables[pos]
else:
pos = int(pos)
if value in self.runtime.variables:
value = self.runtime.variables[value]
else:
value = int(value)
self.runtime.memory[pos] = value
return True
except Exception as e:
self.runtime.logger.log(e)