-
Notifications
You must be signed in to change notification settings - Fork 1
/
minkolang_0.15.py
1688 lines (1418 loc) · 79 KB
/
minkolang_0.15.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
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import json
import math
import cmath
import random
import itertools
from copy import deepcopy
debug = 0
if "idlelib" in sys.modules:
sys.argv = ["minkolang_0.14.py", ".", "c2"]
debug = 1
numSteps = 100
if len(sys.argv) > 1 and sys.argv[1][-4:] == ".mkl":
file = open(sys.argv[1], encoding="utf-8").read()
if '\ufeff' in file: file = file[1:]
elif len(sys.argv) > 1 and "idlelib" in sys.modules:
file = sys.argv[1]
else:
file = None
class Program:
global debug
def __init__(self, code, inputStr="", debugFlag=0, outfile=sys.stdout):
global debug
debug = debugFlag
self.code = []
for layer in filter(bool, code.split("$$$\n")):
self.code.append([list(s) for s in layer.rstrip("\n").split("\n")])
if debug: print("Code:",self.code)
self.codeput = {}
self.codeChanged = 0
self.inputStr = inputStr
if debug: print("Input:",self.inputStr)
self.position = [0,0,0] #[x,y,z]
self.velocity = [1,0,0] #[dx,dy,dz]
self.oldposition = self.position[:]
self.stack = []
self.array = [[]]
self.loops = []
self.gosub = []
self.strLiteral = ""
self.strMode = 0
self.numLiteral = ""
self.numMode = 0
self.register = 0
self.fallable = 1
self.toggleFlag = 0
self.oldToggle = 0
self.fallFlag = 0
self.stuckFlag = 0
self.ignoreFlag = ""
self.ternaryFlag = ""
self.escapeFlag = 0
self.bounds = [[0,max([ max(map(len,layer)) for layer in self.code])],
[0,max(map(len,self.code))],
[0,len(self.code)]]
if debug: print(self.bounds)
for layer in self.code:
for row in layer:
row.extend([" "]*(self.bounds[0][1]-len(row)))
if debug: print(row)
while len(layer) < self.bounds[1][1]:
layer.append([" "]*self.bounds[0][1])
self.currChar = ""
self.output = ""
if outfile == None:
self.outfile = None #open(os.devnull, 'w')
else:
self.outfile = outfile
self.stopNow = False
self.isDone = False
self.errorType = ""
self.caught = 0
def runCatch(self, steps=-1):
self.errorType = ""
try:
self.run(steps)
except Exception as e:
self.errorType = e.args[0]
tempPosition = self.position
tempVelocity = self.velocity
tempCurrChar = self.currChar
self.move()
self.getCurrent()
if self.currChar == "E":
self.caught = 1
self.run(self.steps)
else:
self.position = tempPosition
self.velocity = tempVelocity
self.currChar = tempCurrChar
def run(self, steps=-1): #steps = -1 for run-until-halt
self.stopNow = False
self.codeChanged = 0
self.arrayChanged = 0
while steps != 0 and self.stopNow == False and not self.isDone:
steps -= 1
self.steps = steps
self.getCurrent()
movedir = ""
arg2 = None
stack = self.loops[-1][3] if self.loops else self.stack
self.oldToggle = self.toggleFlag
if self.currChar == '"' and not self.numMode:
self.fallable = not self.fallable
self.strMode = not self.strMode
if not self.escapeFlag: self.escapeFlag = self.toggleFlag
if not self.strMode:
if self.escapeFlag: self.strLiteral = bytes(self.strLiteral, "utf-8").decode("unicode_escape")
if not self.ignoreFlag: stack.extend(list(map(ord,self.strLiteral[::-1])))
self.strLiteral = ""
self.escapeFlag = 0
if self.currChar == "'" and not self.strMode:
self.fallable = not self.fallable
if not self.numMode:
self.numMode = stack.pop() if self.toggleFlag and stack else 10
if self.numMode == 0: self.numMode = 16
else:
self.numMode = 0
if not self.numMode:
result = 0
if debug: print(self.numLiteral)
try:
result = int(self.numLiteral)
except ValueError:
try:
result = float(self.numLiteral)
except ValueError:
try:
result = complex(self.numLiteral)
except ValueError:
pass
if not self.ignoreFlag: stack.append(result)
self.numLiteral = ""
if self.currChar not in "'\"":
if not self.strMode and not self.numMode and not self.ignoreFlag:
if self.currChar != " " and not self.fallable and not self.fallFlag:
self.fallable = 1
if self.currChar == ".": #stop execution
if not self.toggleFlag: #'$.' is a soft halt (breakpoint)
self.oldposition = self.position
self.isDone = True
return
else:
self.stopNow = True
elif self.currChar == "$": #toggle functionality
if self.stuckFlag:
self.toggleFlag = 0
self.stuckFlag = 0
elif self.toggleFlag:
self.stuckFlag = 1
else:
self.toggleFlag = 1
elif self.currChar == "e": #throw exception
raise Exception("'e'")
elif self.currChar == "E":
stack.append(self.caught)
self.caught = 0
elif self.currChar == "C": #comments
self.ignoreFlag = " C"
elif self.currChar == "V":
if self.fallFlag:
self.fallable = 1
self.fallFlag = 0
else:
self.fallable = 0
self.fallFlag = self.toggleFlag
elif self.currChar == "#": #net
pass
elif self.fallable and self.currChar == " ":
movedir = "fall"
elif self.currChar in "v<>^":
movedir = {"v":"down","<":"left",">":"right","^":"up"}[self.currChar]
elif self.currChar in "/\\_|":
if self.currChar == "/":
self.velocity = [-self.velocity[1],
-self.velocity[0],
self.velocity[2]]
elif self.currChar == "\\":
self.velocity = [self.velocity[1],
self.velocity[0],
self.velocity[2]]
elif self.currChar == "_":
self.velocity = [self.velocity[0],
-self.velocity[1],
self.velocity[2]]
elif self.currChar == "|":
self.velocity = [-self.velocity[0],
self.velocity[1],
self.velocity[2]]
elif self.currChar in "!?@&":
movedir = "jump"
if self.currChar == "!":
arg2 = 1
else:
tos = stack.pop() if stack else 0
if self.currChar == "?" and tos:
arg2 = 1
elif self.currChar == "@":
arg2 = tos
elif self.currChar == "&" and (stack.pop() if stack else 0):
arg2 = tos
else:
movedir = ""
elif self.currChar == "K":
n = 3 + self.toggleFlag
direc = random.randint(0,n)
if direc == 0: #down
self.velocity = [0,1,0]
elif direc == 1: #left
self.velocity = [-1,0,0]
elif direc == 2: #right
self.velocity = [1,0,0]
elif direc == 3: #up
self.velocity = [0,-1,0]
elif direc == 4: #forward
self.velocity = [0,0,1]
else:
pass
elif self.currChar in "0123456789" and not self.toggleFlag:
stack.append(int(self.currChar))
elif self.currChar == "l" and not self.toggleFlag:
stack.append(10)
elif self.currChar == "j" and not self.toggleFlag:
stack.append(1j)
elif self.currChar in "0123456789lj" and self.toggleFlag:
if self.currChar == "0":
stack.append(0.1)
elif self.currChar in "123456":
stack.append(int(self.currChar)+10)
elif self.currChar == "7":
stack.append(1/2)
elif self.currChar == "8":
stack.append((1+5**.5)/2) #phi
elif self.currChar == "9":
stack.append(2**.5)
elif self.currChar == "l":
stack.append(100)
elif self.currChar == "j":
stack.append(2**.5/2+2**.5*1j/2)
elif self.currChar == "L":
s = stack.pop() if stack and self.toggleFlag else 1
b = stack.pop() if stack else 0
a = stack.pop() if stack else 0
while a <= b:
stack.append(a)
a += s
elif self.currChar in "hH": #random number
if self.currChar == "h":
n = stack.pop() if stack else 0
if not self.toggleFlag:
stack.append(random.randint(0,n))
else:
stack.append(random.random()*n)
elif self.currChar == "H":
b = stack.pop() if stack else 0
a = stack.pop() if stack else 0
if not self.toggleFlag:
stack.append(random.randint(a,b))
else:
stack.append(random.random()*(b-a)+a)
elif self.currChar in "+-*:;%=`": #operators and comparators
if self.toggleFlag and self.currChar in "+*":
if self.currChar == "+":
result = sum(stack)
elif self.currChar == "*":
result = 1
for s in stack: result *= s
stack.clear()
else:
b = stack.pop() if stack else 0
a = stack.pop() if stack else 0
if self.currChar == "+":
result = a+b
elif self.currChar == "-":
if not self.toggleFlag:
result = a-b
else:
stack.append(a)
result = math.copysign(1,b) if b else 0
if result.is_integer(): result=int(result)
elif self.currChar == "*":
result = a*b
elif self.currChar == ":":
result = a//b if not self.toggleFlag else a/b
elif self.currChar == ";":
result = a**b if not self.toggleFlag else math.log(b,a)
elif self.currChar == "%":
result = a%b if not self.toggleFlag else [a//b,a%b]
elif self.currChar == "=":
if not self.toggleFlag:
result = int(a==b)
else:
n,b = b,a
a = stack.pop() if stack else 0
result = int(a%n == b)
elif self.currChar == "`":
result = int(a>b if not self.toggleFlag else b>a)
if type(result) != list:
stack.append(result)
else:
stack.extend(result)
elif self.currChar in "~,": #negation and not
b = stack.pop() if stack else 0
if self.currChar == "~":
stack.append(-b if not self.toggleFlag else abs(b))
elif self.currChar == ",":
stack.append(int(not b if not self.toggleFlag else bool(b)))
elif self.currChar == "y":
a = stack.pop() if stack else 0
if type(a) != complex:
stack.append(int(a) if not self.toggleFlag else (a-int(a)))
else:
ar = a.real
ai = a.imag
ar2 = int(ar) if not self.toggleFlag else (ar-int(ar))
ai2 = int(ai) if not self.toggleFlag else (ai-int(ai))
stack.append((ar2+ai2*1j))
elif self.currChar == "Y":
a = stack.pop() if stack else 0
if type(a) != complex:
stack.append(math.floor(a) if not self.toggleFlag else math.ceil(a))
else:
ar = a.real
ai = a.imag
ar2 = math.floor(ar) if not self.toggleFlag else math.ceil(ar)
ai2 = math.floor(ai) if not self.toggleFlag else math.ceil(ai)
stack.append((ar2+ai2*1j))
elif self.currChar in "no": #input
if self.currChar == "n":
times = 1 if not self.toggleFlag else -1
found = 0
while times and self.inputStr:
found = 0
num = 0
for beg in range(0,len(self.inputStr)):
for end in range(len(self.inputStr)+1,beg,-1):
part = self.inputStr[beg:end]
try:
num = int(part)
except ValueError:
try:
num = float(part)
except ValueError:
try:
num = complex(part)
except ValueError:
continue
else:
found = 1
else:
found = 1
else:
found = 1
if found: break
if found: break
if found:
stack.append(num)
self.inputStr = self.inputStr[end:]
else:
self.inputStr = ""
break
times -= 1
if not found:
stack.append(-1)
elif self.currChar == "o":
if not len(self.inputStr):
stack.append(0)
else:
if not self.toggleFlag:
stack.append(ord(self.inputStr[0]))
self.inputStr = self.inputStr[1:]
else:
stack.extend(map(ord,self.inputStr))
self.inputStr = ""
elif self.currChar in "NO": #output
if self.currChar == "N":
if not self.toggleFlag:
out = [stack.pop() if stack else 0]
else:
out = stack[::-1]; stack.clear()
for elem in out:
if self.outfile: print(elem, end=' ', flush=True, file=self.outfile)
self.output += str(elem) + ' '
elif self.currChar == "O":
if not self.toggleFlag:
out = [stack.pop() if stack else 0]
else:
out = stack[::-1]; stack.clear()
for elem in out:
try:
c = chr(int(elem))
except ValueError:
c = ""
if self.outfile: print(c, end='', flush=True, file=self.outfile)
self.output += c
elif self.currChar in "dD": #duplication
if not self.toggleFlag:
tos = stack.pop() if stack else 0
if self.currChar == "d":
stack.extend([tos]*2)
elif self.currChar == "D":
n = stack.pop() if stack else 0
stack.extend([n]*tos)
else:
if self.currChar == "d":
stack.extend(stack)
elif self.currChar == "D":
n = stack.pop() if stack else 0
stack.extend(stack*(n-1))
elif self.currChar == "z": #register
if self.toggleFlag:
self.register = stack.pop() if stack else 0
else:
stack.append(self.register)
elif self.currChar in "bB": #branches
tos = stack.pop() if stack else 0
if self.toggleFlag: tos = not tos
if self.currChar == "b":
if not tos: self.velocity = [-v for v in self.velocity]
if self.currChar == "B":
self.velocity = [self.velocity[1],self.velocity[0],self.velocity[2]]
if not tos: self.velocity = [-v for v in self.velocity]
elif self.currChar == "w": #wormhole
nz = stack.pop() if stack and self.toggleFlag else self.position[2]
ny = stack.pop() if stack else 0
nx = stack.pop() if stack else 0
movedir = "wormhole"
arg2 = [[nx,ny,nz],self.velocity]
elif self.currChar in "gG": #stack index/insert
if not self.toggleFlag:
n = stack.pop() if stack else 0
if self.currChar == "g" and stack:
stack.append(stack.pop(n))
elif self.currChar == "G" and stack:
toput = stack.pop() if stack else 0
stack.insert(n, toput)
else:
b = stack.pop() if stack else 0
a = stack.pop() if stack else 0
if self.currChar == "g" and stack:
newstack = stack[a:b]
for k in newstack: stack.pop(a)
stack.extend(newstack)
elif self.currChar == "G" and stack:
newstack = stack[b:-a]
for k in newstack: stack.pop(b)
stack.extend(newstack)
elif self.currChar == "c": #stack copy/slice
tos = stack.pop() if stack else 0
if not self.toggleFlag:
try:
stack.append(stack[tos])
except IndexError:
stack.append(0)
else:
tos2 = stack.pop() if stack else 0
stack.extend(stack[tos2:tos])
elif self.currChar in "xX": #dump
if stack:
if self.currChar == "x":
stack.pop() if not self.toggleFlag else stack.pop(0)
if self.currChar == "X":
tos = stack.pop() if stack else 0
for i in range(min([tos,len(stack)])): stack.pop() if not self.toggleFlag else stack.pop(0)
elif self.currChar in "i": #loop counter
if self.toggleFlag: #for loop iters
stack.append(self.loops[-1][5] if self.loops and self.loops[-1][0] == "for" else -1)
else:
stack.append(self.loops[-1][4] if self.loops else -1)
elif self.currChar == "I":
if self.toggleFlag: #input length
stack.append(len(self.inputStr))
else: #stack length
stack.append(len(stack))
elif self.currChar == "r": #reverse stack
if self.toggleFlag: #swap top two
if len(stack) == 1:
stack.append(0)
else:
y = stack.pop()
x = stack.pop()
stack.append(y)
stack.append(x)
else:
stack.reverse()
elif self.currChar == "R": #rotates stack
tos = stack.pop() if stack else 0
mod = tos % len(stack)
newstack = stack[-mod:] + stack[:-mod]
stack.clear()
stack.extend(newstack)
elif self.currChar == "s": #sort
if self.toggleFlag:
tos = stack.pop() if stack else 0
newstack = stack[-tos:]
for n in newstack: stack.pop()
newstack.sort()
stack.extend(newstack)
else:
stack.sort()
elif self.currChar == "S": #set (removes duplicates)
tos = stack.pop() if stack and self.toggleFlag else 0
newstack = stack[-tos:]
for n in newstack: stack.pop()
j = len(newstack)-1
seen = []
while j >= 0:
if newstack[j] not in seen: seen.append(newstack[j])
j -= 1
stack.extend(seen[::-1])
elif self.currChar == "m": #merge/interleave
k = stack.pop() if stack and self.toggleFlag else 2
newstack = []
m = len(stack)//abs(k)
if k < 0: k,m = m,-k-1
for i in range(m+1): newstack.extend(stack[i::m+1])
stack.clear()
stack.extend(newstack)
elif self.currChar == "p": #code put
if self.toggleFlag:
z = stack.pop() if stack else 0
else:
z = self.position[2]
y = stack.pop() if stack else 0
x = stack.pop() if stack else 0
n = stack.pop() if stack else 0
b = self.bounds
if b[0][0] <= x < b[0][1] and b[1][0] <= y < b[1][1] and b[2][0] <= z < b[2][1]:
self.code[z][y][x] = n
else:
self.codeput[str((x,y,z))] = n
self.codeChanged = 1
elif self.currChar == "q": #code get
if self.toggleFlag:
z = stack.pop() if stack else 0
else:
z = self.position[2]
y = stack.pop() if stack else 0
x = stack.pop() if stack else 0
b = self.bounds
if b[0][0] <= x < b[0][1] and b[1][0] <= y < b[1][1] and b[2][0] <= z < b[2][1]:
q = self.code[z][y][x]
stack.append(ord(q) if type(q) == str else q)
else:
if str((x,y,z)) in self.codeput:
q = self.codeput[str((x,y,z))]
stack.append(ord(q) if type(q) == str else q)
else:
stack.append(0)
elif self.currChar in "aA": #array get/put
y = stack.pop() if stack else 0
x = stack.pop() if stack else 0
k = stack.pop() if stack and self.currChar == "A" else 0
if x>=0 and y>=0:
if self.currChar == "a":
if 0 <= y < len(self.array) and 0 <= x < len(self.array[0]):
stack.append(self.array[y][x])
else:
stack.append(0)
elif self.currChar == "A":
if debug: print(*self.array)
if 0 <= y < len(self.array) and 0 <= x < len(self.array[0]):
pass
else:
for line in self.array:
line.extend([0]*(x-len(line)+1))
while len(self.array) <= y:
self.array.append([0]*(max([x+1,len(self.array[0])])))
if debug: print(*self.array)
self.array[y][x] = k
self.arrayChanged = 1
elif self.currChar == "u":
if not self.toggleFlag:
print(stack, file=self.outfile)
else:
print(*self.code, file=self.outfile)
print(*self.loops, file=self.outfile)
elif self.currChar == "M": #MATH
tos = stack.pop() if stack else 0
if tos == 0:
n = stack.pop() if stack else 0
if not self.toggleFlag: #factorial
stack.append(math.factorial(n))
else: #gamma
stack.append(math.gamma(n))
elif tos == 1:
n = stack.pop() if stack else 0
if not self.toggleFlag: #sqrt
stack.append(math.sqrt(n))
else: #nth root
r = (stack.pop() if stack else 0)**(1/n)
if r.is_integer(): r = int(r)
stack.append(r)
elif tos == 2:
n = stack.pop() if stack else 0
P = getPrimes_parallelized()
if n <= 1:
stack.append(0)
else:
for p in P:
if p**2 > n: #prime
stack.append(not self.toggleFlag)
break
if n%p == 0: #composite
stack.append(self.toggleFlag)
break
elif tos == 3:
n = stack.pop() if stack else 0
P = getPrimes_parallelized()
if not self.toggleFlag: #nth prime
for i,p in enumerate(P):
if i == n:
stack.append(p)
break
else: #nth composite
c = -1
prevPrime = 0
for p in P:
c += (p-prevPrime-1)
prevPrime = p
if c >= n+1:
stack.append(p-(c-n))
break
elif tos == 4:
b = stack.pop() if stack else 0
a = stack.pop() if stack else 0
g = gcd(a,b)
if not self.toggleFlag: #gcd
stack.append(g)
else: #lcm
L = a*b/g
if L.is_integer(): L = int(L)
stack.append(L)
elif tos == 5:
mean = sum(stack)/len(stack) if stack else 0
if not self.toggleFlag: #mean
stack.clear()
stack.append(mean)
else: #standard deviation
total = 0
for s in stack:
total += (s-mean)**2
stack.append(math.sqrt(total)/len(stack) if stack else 0)
elif tos == 6:
r = stack.pop() if stack else 0
n = stack.pop() if stack else 0
num = 1
for i in range(n,r,-1):
num *= i
if not self.toggleFlag: #binomial (nCr)
for j in range(1,(n-r)+1):
num //= j
else: #nPr
pass
stack.append(num)
elif tos == 7:
n = complex(stack.pop() if stack else 0)
if not self.toggleFlag: #pushes real, imag
stack.append(n.real)
stack.append(n.imag)
else: #complex conjugate
stack.append(n.conjugate())
elif tos == 8:
if not self.toggleFlag: #2D distance
n = 2
else: #N-d distance
n = stack.pop() if stack else 0
if not n:
stack.append(0)
else:
x = [stack.pop() if stack else 0 for i in range(2*n)]
stack.append(math.sqrt(sum([(x[i]-x[i+n])**2 for i in range(n)])))
elif tos == 9:
n = stack.pop() if stack else 0
P = getPrimes_parallelized()
total = 0
if not self.toggleFlag: #pi(n)
for p in P:
if p <= n: total += 1
else: break
else: #phi(n)
for i in range(1,n+1):
if gcd(i,n) == 1: total += 1
stack.append(total)
elif tos == 10:
n = stack.pop() if stack else 0
if not self.toggleFlag: #log
try:
stack.append(math.log(n))
except ValueError:
stack.append(cmath.log(n))
else: #log_n
b = stack.pop() if stack else 0
try:
stack.append(math.log(b,n))
except ValueError:
stack.append(cmath.log(b,n))
elif self.currChar == "T": #TRIG
tos = stack.pop() if stack else 0
if tos == 0:
stack.append(math.pi if not self.toggleFlag else math.e)
elif tos == 1:
n = stack.pop() if stack else 0
if not self.toggleFlag: #convert to radians
stack.append(n*math.pi/180)
else: #convert to degrees
stack.append(n*180/math.pi)
elif tos == 2:
n = stack.pop() if stack else 0
if not self.toggleFlag: #sine
stack.append(math.sin(n))
else: #arcsine
stack.append(math.asin(n))
elif tos == 3:
n = stack.pop() if stack else 0
if not self.toggleFlag: #cosine
stack.append(math.cos(n))
else: #arccosine
stack.append(math.acos(n))
elif tos == 4:
n = stack.pop() if stack else 0
if not self.toggleFlag: #tangent
stack.append(math.tan(n))
else: #arctangent
stack.append(math.atan(n))
elif tos == 5:
y = stack.pop() if stack else 0
x = stack.pop() if stack else 0
if not self.toggleFlag: #atan2
stack.append(math.atan2(y,x))
else: #hypotenuse
stack.append(math.hypot(x,y))
elif tos == 6:
##using http://stackoverflow.com/a/7869457/1473772
if not self.toggleFlag: #angle diff (angles)
ang2 = stack.pop() if stack else 0
ang1 = stack.pop() if stack else 0
stack.append((ang2 - ang1 + 180) % 360 - 180)
else: #angle diff (coords)
y2 = stack.pop() if stack else 0
x2 = stack.pop() if stack else 0
y1 = stack.pop() if stack else 0
x1 = stack.pop() if stack else 0
ang1 = math.atan2(y1,x1)
ang2 = math.atan2(y2,x2)
stack.append((ang2 - ang1 + math.pi) % (2*math.pi) - math.pi)
elif tos == 7:
n = stack.pop() if stack else 0
if not self.toggleFlag: #hyperbolic sine
stack.append(math.sinh(n))
else: #hyperbolic arcsine
stack.append(math.asinh(n))
elif tos == 8:
n = stack.pop() if stack else 0
if not self.toggleFlag: #hyperbolic cosine
stack.append(math.cosh(n))
else: #hyperbolic arccosine
stack.append(math.acosh(n))
elif tos == 9:
n = stack.pop() if stack else 0
if not self.toggleFlag: #hyperbolic tangent
stack.append(math.tanh(n))
else: #hyperbolic arctangent
stack.append(math.atanh(n))
elif tos == 10:
if not self.toggleFlag: #?
pass
else: #?
pass
elif self.currChar == "Z": #LISTS/STRINGS
tos = stack.pop() if stack else 0
if tos == 0:
if not self.toggleFlag: #count single item
a = stack.pop() if stack else 0
stack.append(stack.count(a))
else: #count multi item
a = stack.pop() if stack else 0
needle = stack[-a:]
for k in needle: stack.pop()
count = 0
for i in range(len(stack)-len(needle)):
if stack[i:i+len(needle)] == needle:
count += 1
stack.append(count)
elif tos == 1 or tos == 2:
if not self.toggleFlag: #find [first] single item
needle = [stack[-1] if stack else 0]
else: #find [first] multi item
needle = stack[-(stack.pop() if stack else 0):]
for k in needle: stack.pop()
count = 0
for i in range(len(stack)-len(needle)+1):
if stack[i:i+len(needle)] == needle:
stack.append(i)
count += 1
if tos == 1: break
if not self.toggleFlag and count == 0: stack.append(-1)
if tos == 2: stack.append(count)
elif tos == 3 or tos == 4:
if not self.toggleFlag: #remove [first] single item
needle = [stack[-1] if stack else 0]
else: #remove [first] multi item
needle = stack[-(stack.pop() if stack else 0):]
for k in needle: stack.pop()
i = 0
while i < len(stack)-len(needle):
if stack[i:i+len(needle)] == needle:
for k in needle: stack.pop(i)
if tos == 3: break
i += 1
elif tos == 5: #replace
b = stack.pop() if stack else 0
a = stack.pop() if stack else 0
B = stack[-b:]
A = stack[-b-a:-b]
for k in A+B: stack.pop()
newstack = []
i = 0
while i < len(stack):
if stack[i:i+len(A)] == A:
newstack.extend(B)
i += len(A)-1
if self.toggleFlag: #replace only once
newstack.extend(stack[i+1:])
break
else:
newstack.append(stack[i])
i += 1
stack.clear()
stack.extend(newstack)
elif tos == 6:
if not self.toggleFlag: #convert number to string
strnum = list(map(ord,str(stack.pop() if stack else 0)))
stack.extend(strnum[::-1])
else: #convert string to number
numstr = ''.join(map(chr,stack))[::-1]
try:
num = int(numstr)
except ValueError:
try:
num = float(numstr)
except ValueError:
try:
num = complex(numstr)
except ValueError:
num = 0
stack.clear()
stack.append(num)
elif tos == 7:
if not self.toggleFlag: #lowercase
for i in range(len(stack)):
if 65 <= stack[i] <= 90: stack[i] += 32
else: #uppercase
for i in range(len(stack)):
if 97 <= stack[i] <= 122: stack[i] -= 32
elif tos == 8:
if not self.toggleFlag: #is alphanumeric?
try: