-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.py
More file actions
620 lines (540 loc) · 19.3 KB
/
operators.py
File metadata and controls
620 lines (540 loc) · 19.3 KB
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import builtins
import time
from utils import out_error, operator, Token, expect
from executor import *
import executor
@operator
def declare(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
if expect(args[0], "Identifier", index):
if not executor.GlMemory.mem.__contains__(args[0].content):
executor.GlMemory.mem[args[0].content] = []
else:
out_error(f"Memory stack with name '{args[0].content}' was already declared", index)
quit(1)
return index, 0
@operator
def sleep(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
if expect(args[0], "Number", index):
time.sleep(int(args[0].content))
return index, 0
@operator
def tpop(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
if args[0].content == "ALL":
for i in executor.GlMemory.mem[executor.CurrentMemory].__len__():
executor.GlMemory.temp.append(executor.GlMemory.mem[executor.CurrentMemory].pop(i))
else:
for i in args:
expect(i, "Number", index)
executor.GlMemory.temp.append(executor.GlMemory.mem[executor.CurrentMemory].pop(int(i.content)))
return index, 0
@operator
def pull(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
if args[0].content == "ALL":
for i in executor.GlMemory.mem[executor.CurrentMemory]:
try:
executor.GlMemory.temp.append(i)
except:
out_error("Out of range", index)
quit(1)
else:
for i in args:
expect(i, "Number", index)
try:
executor.GlMemory.temp.append(executor.GlMemory.mem[executor.CurrentMemory][int(i.content)])
except:
out_error("Out of range", index)
quit(1)
return index, 0
@operator
def add(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "Number", index)
val1 = executor.GlMemory.temp.pop(int(args[0].content))
val2 = executor.GlMemory.temp.pop(int(args[1].content) - 1)
res = val1 + val2
executor.GlMemory.temp.append(res)
return index, 0
@operator
def sub(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "Number", index)
val1 = executor.GlMemory.temp.pop(int(args[0].content))
val2 = executor.GlMemory.temp.pop(int(args[1].content) - 1)
res = val1 - val2
executor.GlMemory.temp.append(res)
return index, 0
@operator
def mul(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "Number", index)
val1 = executor.GlMemory.temp.pop(int(args[0].content))
val2 = executor.GlMemory.temp.pop(int(args[1].content) - 1)
res = val1 * val2
executor.GlMemory.temp.append(res)
return index, 0
@operator
def div(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "Number", index)
val1 = executor.GlMemory.temp.pop(int(args[0].content))
val2 = executor.GlMemory.temp.pop(int(args[1].content) - 1)
if val2 == 0:
out_error("Can't divide by zero", index)
quit(0)
res = val1 / val2
executor.GlMemory.temp.append(res)
return index, 0
@operator
def mod(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "Number", index)
val1 = executor.GlMemory.temp.pop(int(args[0].content))
val2 = executor.GlMemory.temp.pop(int(args[1].content) - 1)
if val2 == 0:
out_error("Can't divide by zero", index)
quit(0)
res = val1 % val2
executor.GlMemory.temp.append(res)
return index, 0
@operator
def inp(index: int, args: list[Token]):
if args.__len__() != 0:
expect(args[0], "String", index)
val = input(args[0].content)
else:
val = input()
try:
executor.GlMemory.temp.append(int(val))
except:
for c in val:
executor.GlMemory.temp.append(int(c.encode()[0]))
return index, 0
@operator
def mergewith(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Identifier", index)
expect(args[1], "Identifier", index)
mem1 = args[0].content.replace("THIS", executor.CurrentMemory)
mem2 = args[1].content.replace("THIS", executor.CurrentMemory)
if mem_exist(mem1, index) and mem_exist(mem2, index):
for i in range(0, executor.GlMemory.mem[mem1].__len__()):
try:
executor.GlMemory.mem[mem2][i] = executor.GlMemory.mem[mem1][i]
except:
executor.GlMemory.mem[mem2].append(executor.GlMemory.mem[mem1][i])
return index, 0
@operator
def rawmergewith(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Identifier", index)
expect(args[1], "Identifier", index)
mem1 = args[0].content.replace("THIS", executor.CurrentMemory)
mem2 = args[1].content.replace("THIS", executor.CurrentMemory)
if mem_exist(mem1, index) and mem_exist(mem2, index):
for i in range(0, executor.GlMemory.mem[mem1].__len__()):
try:
executor.GlMemory.mem[mem2][i] = executor.GlMemory.mem[mem1][i]
except:
executor.GlMemory.mem[mem2].append(executor.GlMemory.mem[mem1][i])
executor.GlMemory.mem[mem1] = []
return index, 0
@operator
def mforward(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
if args[1].content != "ALL":
expect(args[0], "Identifier", index)
if mem_exist(args[0].content, index):
for b in range(1, args.__len__()):
i = args[b]
expect(i, "Number", index)
executor.GlMemory.mem[args[0].content].append(
executor.GlMemory.mem[executor.CurrentMemory][int(i.content)])
else:
expect(args[0], "Identifier", index)
if mem_exist(args[0].content, index):
for i in executor.GlMemory.mem[executor.CurrentMemory]:
executor.GlMemory.mem[args[0].content].append(i)
return index, 0
@operator
def forward(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
if args[1].content != "ALL":
expect(args[0], "Identifier", index)
if mem_exist(args[0].content, index):
for b in range(1, args.__len__()):
i = args[b]
expect(i, "Number", index)
executor.GlMemory.mem[args[0].content].append(executor.GlMemory.temp[(int(i.content))])
else:
expect(args[0], "Identifier", index)
if mem_exist(args[0].content, index):
for i in executor.GlMemory.temp:
executor.GlMemory.mem[args[0].content].append(i)
return index, 0
@operator
def push(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
if args[0].content == "SOFT":
for b in range(1, args.__len__()):
i = args[b]
expect(i, "Number", index)
try:
executor.GlMemory.mem[executor.CurrentMemory].append(executor.GlMemory.temp[int(i.content)])
except:
out_error("Out of range", index)
quit(1)
pass
elif args[0].content != "ALL":
for i in args:
expect(i, "Number", index)
try:
executor.GlMemory.mem[executor.CurrentMemory].append(executor.GlMemory.temp.pop(int(i.content)))
except:
out_error("Out of range", index)
quit(1)
else:
for i in executor.GlMemory.temp:
try:
executor.GlMemory.mem[executor.CurrentMemory].append(i)
except:
out_error("Out of range", index)
quit(1)
return index, 0
@operator
def cloneto(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Identifier", index)
expect(args[1], "Identifier", index)
mem1 = args[0].content.replace("THIS", executor.CurrentMemory)
mem2 = args[1].content.replace("THIS", executor.CurrentMemory)
if mem_exist(mem1, index) and mem_exist(mem2, index):
executor.GlMemory.mem[mem2] = executor.GlMemory.mem[mem1].copy()
return index, 0
@operator
def tdel(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
if args[0].content != "ALL":
for i in args:
expect(i, "Number", index)
try:
executor.GlMemory.temp[i.content] = 0
except:
out_error("Tried to clear out of memory", index)
quit(1)
else:
executor.GlMemory.temp = []
return index, 0
@operator
def mdel(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
if args[0].content != "ALL":
for i in args:
expect(i, "Number", index)
try:
executor.GlMemory.mem[executor.CurrentMemory][i.content] = 0
except:
out_error("Tried to clear out of memory", index)
quit(1)
else:
executor.GlMemory.mem[executor.CurrentMemory] = []
return index, 0
@operator
def ddel(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Identifier", index)
mem = args[0].content.replace("THIS", executor.CurrentMemory)
if mem_exist(mem, index) and mem != executor.CurrentMemory:
executor.GlMemory.mem.__delitem__(mem)
else:
out_error(f"Tried deleting memory stack '{mem}', which is being executed", index)
quit(1)
return index, 0
@operator
def writestr(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
if expect(args[0], "String", index):
for character in args[0].content:
executor.GlMemory.temp.append(int(character.encode()[0]))
return index, 0
@operator
def mark(index: int, args: list[Token]):
return index, 0
@operator
def pair(index: int, args: list[Token]):
for i in args:
expect(i, "Number", index)
if args.__len__() % 2 != 0:
out_error("Odd number of arguments", index)
quit(1)
else:
for i in range(0, int(args.__len__()/2)):
executor.GlMemory.temp.insert(i, int(args[int(args.__len__()/2)+i].content))
return index, 0
@operator
def writenum(index: int, args: list[Token]):
for i in args:
expect(i, "Number", index)
executor.GlMemory.temp.append(int(i.content))
return index, 0
@operator
def run(index: int, args: list[Token]):
if args.__len__() == 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Identifier", index)
try:
executor.execute(executor.Executing, args[0].content)
except:
out_error("run: something went wrong", index)
quit(1)
return index, 0
@operator
def pycall(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "String", index)
cmd = args[0].content
try:
result = eval(cmd)
if result is not None:
if isinstance(result, list):
for i in result:
try:
executor.GlMemory.temp.append(int(i))
except:
try:
executor.GlMemory.temp.append(int(i.encode()[0]))
except:
try:
if i:
executor.GlMemory.temp.append(1)
else:
executor.GlMemory.temp.append(0)
except:
executor.GlMemory.temp.append(0)
elif isinstance(result, dict):
for i in list(result.values()):
try:
executor.GlMemory.temp.append(int(i))
except:
try:
executor.GlMemory.temp.append(int(i.encode()[0]))
except:
try:
if i:
executor.GlMemory.temp.append(1)
else:
executor.GlMemory.temp.append(0)
except:
executor.GlMemory.temp.append(0)
elif isinstance(result, int):
executor.GlMemory.temp.append(result)
elif isinstance(result, str):
for i in result:
executor.GlMemory.temp.append(int(i.encode()[0]))
elif isinstance(result, range):
for i in result:
executor.GlMemory.temp.append(i)
else:
out_error("pycall: return value is not supported", index)
quit(1)
except:
out_error("pycall: something went wrong", index)
quit(1)
return index, 0
@operator
def equit(index: int, args: list[Token]):
quit(0)
@operator
def lif(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
val = executor.GlMemory.temp.pop(int(args[0].content))
if val == 0:
index += 1
return index, 0
@operator
def l_not(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
a = executor.GlMemory.temp.pop(int(args[0].content))
executor.GlMemory.temp.append(0 if a == 1 else 1)
return index, 0
@operator
def str_eq(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "String", index)
a, b = int(args[0].content), args[1].content
for i in range(0, b.__len__()):
try:
var = executor.GlMemory.mem[executor.CurrentMemory][a]
if var != int(b.encode()[i]):
executor.GlMemory.temp.append(0)
return index, 0
a += 1
except:
executor.GlMemory.temp.append(0)
return index, 0
executor.GlMemory.temp.append(1)
return index, 0
@operator
def str_noteq(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "String", index)
a, b = int(args[0].content), args[1].content
for i in range(0, b.__len__()):
try:
var = executor.GlMemory.mem[executor.CurrentMemory][a]
if var == int(b.encode()[i]):
executor.GlMemory.temp.append(0)
return index, 0
a += 1
except:
executor.GlMemory.temp.append(0)
return index, 0
executor.GlMemory.temp.append(1)
return index, 0
@operator
def l_eq(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "Number", index)
a, b = executor.GlMemory.temp.pop(int(args[0].content)), executor.GlMemory.temp.pop(int(args[1].content) - 1)
executor.GlMemory.temp.append(1 if a == b else 0)
return index, 0
@operator
def l_noteq(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "Number", index)
a, b = executor.GlMemory.temp.pop(int(args[0].content)), executor.GlMemory.temp.pop(int(args[1].content) - 1)
executor.GlMemory.temp.append(1 if a != b else 0)
return index, 0
@operator
def l_greater(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "Number", index)
a, b = executor.GlMemory.temp.pop(int(args[0].content)), executor.GlMemory.temp.pop(int(args[1].content) - 1)
executor.GlMemory.temp.append(1 if a > b else 0)
return index, 0
@operator
def l_lesser(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "Number", index)
a, b = executor.GlMemory.temp.pop(int(args[0].content)), executor.GlMemory.temp.pop(int(args[1].content) - 1)
executor.GlMemory.temp.append(1 if a < b else 0)
return index, 0
@operator
def l_lesseq(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "Number", index)
a, b = executor.GlMemory.temp.pop(int(args[0].content)), executor.GlMemory.temp.pop(int(args[1].content) - 1)
executor.GlMemory.temp.append(1 if a <= b else 0)
return index, 0
@operator
def l_greatereq(index: int, args: list[Token]):
if args.__len__() < 2:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
expect(args[1], "Number", index)
a, b = executor.GlMemory.temp.pop(int(args[0].content)), executor.GlMemory.temp.pop(int(args[1].content) - 1)
executor.GlMemory.temp.append(1 if a >= b else 0)
return index, 0
@operator
def goto(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
return int(args[0].content)-1, 0
@operator
def gotolim(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
expect(args[0], "Number", index)
executor.GotoLim = int(args[0].content)
return index, 0
@operator
def use(index: int, args: list[Token]):
if args.__len__() == 0:
out_error("Not enough arguments", index)
quit(1)
if expect(args[0], "Identifier", index):
if executor.GlMemory.mem.__contains__(args[0].content):
executor.CurrentMemory = args[0].content
else:
out_error(f"Memory stack with name '{args[0].content}' was not declared", index)
return index, 0