-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators.py
391 lines (321 loc) · 9.03 KB
/
operators.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
import decimal
import rpn_types
import errors
import copy
def substack_distribute(func):
def wrapped(interp, a):
if type(a) is rpn_types.Function:
newfunc = rpn_types.Function()
for item in a.stack:
newfunc.stack.append(func(interp, item)[0])
return [newfunc]
else:
return func(interp, a)
return wrapped
def substack_distribute_2(func):
def wrapped(interp, b, a):
if type(a) is rpn_types.Function:
newfunc = rpn_types.Function()
for item in a.stack:
newfunc.stack.append(func(interp, b, item)[0])
return [newfunc]
else:
return func(interp, b, a)
return wrapped
@substack_distribute_2
def add(interp, b, a):
comment = ''
if a.comment and b.comment:
comment = a.comment + '+' + b.comment
return [rpn_types.Value(a.val + b.val, comment)]
@substack_distribute_2
def sub(interp, b, a):
comment = ''
if a.comment and b.comment:
comment = a.comment + '-' + b.comment
return [rpn_types.Value(a.val - b.val, comment)]
@substack_distribute_2
def mult(interp, b, a):
comment = ''
result = 0
if a.comment and b.comment:
comment = a.comment + '*' + b.comment
result = a.val * b.val
return [rpn_types.Value(result)]
@substack_distribute_2
def div(interp, b, a):
comment = ''
if a.comment and b.comment:
comment = a.comment + '/' + b.comment
return [rpn_types.Value(a.val / b.val, comment)]
def convert_int(interp, a):
return [rpn_types.Value(int(a.val))]
def convert_dec(interp, a):
return [rpn_types.Value(decimal.Decimal(a.val))]
def convert_float(interp, a):
return [rpn_types.Value(decimal.Decimal(a.val))]
@substack_distribute_2
def modulus(interp, b,a):
comment = ''
if a.comment and b.comment:
comment = a.comment + '%' + b.comment
return [rpn_types.Value(a.val % b.val, comment)]
def swap(interp, a,b):
return (a, b)
def assign(interp, var, val):
result = var.reassign(interp,val)
if result is not None:
return [result]
else:
raise errors.CantAssign('Cannot Do Assignment')
def remove(interp, a):
return None
def removes(interp, count):
interp.message('Dropping ' + str(count.val) )
interp.pop(count.val)
def group(interp, count):
newstack = []
for item in interp.pop(count.val):
newstack.append(str(item))
return [rpn_types.Function(stack=newstack[::-1])]
def append(interp, func, item):
if not type(func) is rpn_types.Function:
raise errors.FunctionRequired()
func.stack.append(str(item))
return [func]
def pop(interp, func):
if not type(func) is rpn_types.Function:
raise errors.FunctionRequired()
item = func.pop()
return [func, item]
def prepend(interp, func, item):
if not type(func) is rpn_types.Function:
raise errors.FunctionRequired()
func.stack.insert(0, str(item))
return [func]
def show_vars(interp):
for value in interp.variables.itervalues():
interp.message(str(value))
def comment(interp, a, b):
if hasattr(a, 'name'):
b.comment = a.name
interp.variables.pop(a.name)
return [b]
else:
raise errors.CantAssign('Cannot create comment')
@substack_distribute_2
def equal(interp, a, b):
if type(b) is rpn_types.Function:
if type(a) is rpn_types.Function:
return [rpn_types.Value(int(b.stack == a.stack))]
else:
return False
if type(b.val) is rpn_types.NULL:
if type(a.val) is rpn_types.NULL:
return [rpn_types.Value(1)]
else:
return [rpn_types.Value(0)]
if a.val == b.val:
return [rpn_types.Value(1)]
else:
return [rpn_types.Value(0)]
@substack_distribute_2
def lequal(interp, a, b):
if type(b.val) is rpn_types.NULL:
return [rpn_types.Value(1)]
if type(a.val) is rpn_types.NULL:
return [rpn_types.Value(1)]
if a.val <= b.val:
return [rpn_types.Value(1)]
else:
return [rpn_types.Value(0)]
@substack_distribute_2
def gequal(interp, a, b):
if type(b.val) is rpn_types.NULL:
return [rpn_types.Value(1)]
if type(a.val) is rpn_types.NULL:
return [rpn_types.Value(1)]
if a.val >= b.val:
return [rpn_types.Value(1)]
else:
return [rpn_types.Value(0)]
@substack_distribute_2
def less(interp, a, b):
if type(b.val) is rpn_types.NULL:
return [rpn_types.Value(1)]
if type(a.val) is rpn_types.NULL:
return [rpn_types.Value(1)]
if a.val < b.val:
return [rpn_types.Value(1)]
else:
return [rpn_types.Value(0)]
@substack_distribute_2
def greater(interp, a, b):
if type(b.val) is rpn_types.NULL:
return [rpn_types.Value(1)]
if type(a.val) is rpn_types.NULL:
return [rpn_types.Value(1)]
if a.val > b.val:
return [rpn_types.Value(1)]
else:
return [rpn_types.Value(0)]
def call(interp, a):
if type(a) is rpn_types.Function:
interp.call(a)
else:
raise errors.CantExecute('Cannot Execute a Non-Function')
def call_as_list(interp, a):
if type(a) is rpn_types.Function:
interp.call_as_list(a)
else:
raise errors.CantExecute('Cannot Execute a Non-Function')
def condition_if(interp, func, condition):
if condition.val == 1:
if type(func) is rpn_types.Function:
interp.call(func)
else:
return [ func ]
def condition_ifelse(interp, func_false, func_true, condition):
if condition.val == 1:
if type(func_true) is rpn_types.Function:
interp.call(func_true)
else:
return [ func_true ]
else:
if type(func_false) is rpn_types.Function:
interp.call(func_false)
else:
return [ func_false ]
def condition_while(interp, func):
#TODO: need to make the while loop work with code execution break / pause
interp.loop_count += 1
if type(func) is not rpn_types.Function:
raise errors.CantExecute('While loop needs a function')
result = 1
try:
while result == 1:
interp.call(func)
condition_result = interp.pop()[0]
result = condition_result.val
except errors.WhileBreak:
pass
interp.loop_count -= 1
return []
def condition_while_break(interp):
if interp.loop_count > 0:
raise errors.WhileBreak()
else:
interp.message("Not in a loop, cannot break")
return []
def duplicate(interp, a):
new_a = copy.deepcopy(a)
return [a, new_a]
def rotate(interp, a, b, c):
return [ b, a, c ]
def over (interp, a, b):
return [b, a, b]
def tuck (interp, a, b):
return [a, b, a]
def pick(interp, number):
items = interp.pop(number.val + 1)
items.reverse()
return items + [items[0]]
def roll(interp, number):
items = interp.pop(number.val + 1)
items.reverse()
return items[1:] + [items[0]]
@substack_distribute_2
def exponent(interp, b, a):
comment = ''
if a.comment and b.comment:
comment = a.comment + '^' + b.comment
return [rpn_types.Value(a.val ** b.val, comment)]
def size(interp):
return [rpn_types.Value(len(interp))]
#return [rpn_types.Value(interp.stacksize())]
def subsize(interp, a):
return [a, rpn_types.Value(len(a))]
@substack_distribute
def negate(interp, a):
if a.val == 1:
return [ rpn_types.Value(0) ]
else:
return [ rpn_types.Value(1) ]
def binary(interp):
original = interp.stack[-1].mode
try:
interp.stack[-1].mode = rpn_types.DISPLAY_BIN
str(interp.stack[-1])
except:
#interp.stack[-1].mode = original
interp.message("Could not change display mode to binary for " + str(interp.stack[-1]))
def comma(interp):
original = interp.stack[-1].mode
try:
interp.stack[-1].mode = rpn_types.DISPLAY_COMMA
str(interp.stack[-1])
except:
#interp.stack[-1].mode = original
interp.message("Could not change display mode to comma for " + str(interp.stack[-1]))
def ascii_mode(interp):
original = interp.stack[-1].mode
try:
interp.stack[-1].mode = rpn_types.DISPLAY_ASCII
str(interp.stack[-1])
except Exception as e:
raise e
interp.stack[-1].mode = original
interp.message("Could not change display mode to ascii for " + str(interp.stack[-1]))
def hexadecimal(interp):
original = interp.stack[-1].mode
try:
interp.stack[-1].mode = rpn_types.DISPLAY_HEX
str(interp.stack[-1])
except Exception as e:
interp.stack[-1].mode = original
interp.message("Could not change display mode to hex for " + str(interp.stack[-1]))
def octal(interp):
original = interp.stack[-1].mode
try:
interp.stack[-1].mode = rpn_types.DISPLAY_OCT
str(interp.stack[-1])
except:
interp.stack[-1].mode = original
interp.message("Could not change display mode to octal for " + str(interp.stack[-1]))
def set_decimal(interp):
interp.stack[-1].mode = rpn_types.DISPLAY_DEC
def add_null(interp):
interp.push(rpn_types.NULL())
def is_null(interp, a):
if type(a) is rpn_types.NULL:
return [rpn_types.Value(1)]
else:
return [rpn_types.Value(0)]
@substack_distribute_2
def concat(interp, item0, item1):
if type(item0) is not rpn_types.Function:
func = rpn_types.Function()
#func.stack.insert(0, str(item0))
func.stack.insert(0, item0)
else:
func = item0
if type(item1) is not rpn_types.Function:
func.stack.insert(0, item1)
#func.stack.insert(0, str(item1))
else:
func.stack = item1.stack + func.stack
return [func]
def halt_catch_fire(interp):
interp.debug = True
if interp.last_fault is not None:
interp.message("Catching Fire on " + str(interp.last_fault))
raise interp.last_fault
def reference(interp, index, substack):
val = int(index.val)
#if not type(substack) is rpn_types.Function:
#raise errors.FunctionRequired()
try:
v = substack.get_index(val)
interp.parse(str(v))
except IndexError:
raise errors.OutOfBounds("Cannot access out of array bounds")