-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKegLib.py
716 lines (565 loc) · 16 KB
/
KegLib.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
'''KegLib - Powering the transpilation of Keg since 2019'''
import Coherse
from Coherse import char
from Stackd import Stack
_register = None
variables = {}
code_page = ""
function_list = []
seperator = ""
inputs = Stack()
# BASIC STACK PUSHING
def character(stack, letter):
stack.push(char(letter))
def integer(stack, number):
stack.push(number)
def iterable(stack, item):
stack.push(item)
# Coherse.py handles mathematics, so no need to define here
# But still:
def maths(stack, operator, debug=False):
rhs, lhs = stack.pop(), stack.pop() #Sorry about the switched sides, But
#that's the way it has to be in order
#for maths to work as expected.
if debug:
print("DEBUG: in maths:", lhs, rhs, operator)
print("DEBUG:", Coherse.operate(lhs, rhs, operator))
stack.push(Coherse.operate(lhs, rhs, operator))
# LOGIC
def comparative(stack, operator):
rhs, lhs = stack.pop(), stack.pop()
result = Coherse.do_compare(lhs, rhs, operator)
stack.push(result)
# BUILT-IN FUNCTIONS
def length(stack, pop_if_empty=False):
if pop_if_empty:
if len(stack) == 0:
stack.push(stack.pop())
stack.push(len(stack))
def reverse(stack):
length(stack)
stack.pop()
stack._Stack__stack.reverse() #Lazy moment, using Python built-ins
def swap(stack):
stack[-1], stack[-2] = stack[-2], stack[-1] #Welcome to python, how may I
#help you?
def duplicate(stack):
temp = stack.pop() #Can't use stack.push(stack[-1]) because then implicit
#input doesn't get a chance to work
stack.push(temp); stack.push(temp)
def shift(stack, direction):
if direction == "left":
stack.push(stack[0])
del stack[0]
else:
stack._Stack__stack.insert(0, stack.pop()) #Wow, reeeal cryptic
#Legit just put the last item at position 0
def nice(stack, keep=False):
#This takes the top of the stack and prints it "nicely"
#i.e. str() but even nicer. Also, implements some other rules a standard
#call to a fn like str() might not handle
item = stack.pop()
if type(item) is int:
print(_chr(item),
end=seperator) #Preserve Keg's ability to print integers as chars
elif type(item) is float:
print(item,
end=seperator) #I mean, floats can't really be conv'd to chars, can they?
elif type(item) is char:
print(item.v, end=seperator) #Because python doesn't have a char type.
elif type(item) is Stack:
print(*[x for x in item], end=seperator)
else:
print(custom_format(item), end=seperator)
if keep:
stack.push(item)
def raw(stack, keep=False):
#Like nice(), but Keg's version of repr()
item = stack.pop()
if type(item) is int:
print(item,
end=seperator) #Integers are printed as integers
elif type(item) is float:
print(item,
end=seperator) #Floats -> Floats
elif type(item) is char:
print(_ord(item.v), end=seperator) #Char -> Integer
elif type(item) is Stack:
print(repr(stack), end=seperator) #I actually made a repr() fn for Stacks
else:
print("`" + custom_format(item) + "`", end=seperator) #Makes quines possible
if keep:
stack.push(item)
def Input(stack):
#This is the first of many input functions.
#"Why are there more than one input function though?"
#Well, because ?¿᠀ and implict input. That's why.
#This one is "take input and push as ord"
item = input()
if item:
for Char in reversed(item):
stack.push(char(Char))
inputs.push(item)
else:
item = inputs.pop()
inputs.push(item)
if type(item) is str:
for Char in reversed(item):
stack.push(char(Char))
else:
stack.push(item)
shift(inputs, "R")
#See you soon with another input fn!
def random(stack):
#Welcome to big_boy and small_boy
import Keg_Nums, random
stack.push(random.randint(Keg_Nums.small_boy, Keg_Nums.big_boy))
def pop_top(stack):
stack.pop()
def register(stack):
global _register
if _register is not None:
stack.push(_register)
_register = None
else:
_register = stack.pop()
#print("In KegLib, _register equals", _register)
# FOR-LOOP HELPER
def loop_eval(expr):
if type(expr) in [float, int]:
return range(int(expr))
if type(expr) is char:
return range(ord(expr.v))
return expr
def condition_eval(expr_list, stack):
for expr in expr_list:
eval(expr)
return stack.pop()
# REG EXTENSION
def iota(stack):
try_cast(stack, 'ℤ')
k = stack.pop()
for i in range(k, -1, -1):
stack.push(i)
def sine(stack):
import math
x = stack.pop()
stack.push(math.sin(x)) #I FOUND x EVERYONE!!!!!
def decrement(stack):
item = stack.pop()
stack.push(Coherse.operate(item, 1, "-"))
def increment(stack):
item = stack.pop()
stack.push(Coherse.operate(item, 1, "+"))
def nice_input(stack):
#As aforementioned, round 2 of input has arrived.
#This one is "take input and best guess what it is the user wants"
temp = input()
try:
if type(eval(temp)) is float:
temp = float(temp)
elif type(eval(temp)) is int:
temp = int(temp)
elif type(eval(temp)) is list:
temp = Stack(eval(temp))
elif type(eval(temp)) is str:
temp = temp
else:
for char in reversed(temp):
stack.push(char)
return
except:
temp = temp
if temp:
stack.push(temp)
inputs.push(temp)
else:
item = inputs.pop()
inputs.push(item)
stack.push(item)
shift(inputs, "R")
#Float > Integer > List > String
def excl_range(stack):
query = stack.pop()
x, y = stack.pop(), stack.pop()
values = [to_integer(x), to_integer(y)]
start, stop = sorted(values)
range_object = range(start, stop)
if to_integer(query) in range_object:
stack.push(1)
else:
stack.push(0)
def incl_range(stack):
query = stack.pop()
x, y = stack.pop(), stack.pop()
values = [to_integer(x), to_integer(y)]
start, stop = sorted(values)
range_object = range(start, stop + 1)
if to_integer(query) in range_object:
stack.push(1)
else:
stack.push(0)
def smart_range(stack):
x, y = stack.pop(), stack.pop()
values = [to_integer(x), to_integer(y)]
start, stop = sorted(values)
range_object = range(start, stop + 1)
for item in range_object:
stack.push(item)
def to_integer(item):
return _ord(item.v) if type(item) is char else int(item)
def item_split(stack):
item = stack.pop()
_type = type(item)
if _type is int:
for number in str(item):
stack.push(int(number) if number in "0123456789" else number)
elif _type is float:
for number in str(item):
stack.push(int(number) if number in "0123456789" else number)
elif _type is char:
for number in str(_ord(item.v)):
stack.push(int(number))
else:
for value in item:
stack.push(value)
def factorial(stack):
number = stack.pop()
import math
try:
result = math.factorial(number)
except Exception as e:
result = "" #A whole lot of who knows what?
stack.push(result)
def empty(stack):
stack.clear()
#Yes, it really is that simple.
def print_all(stack):
for item in stack:
nice(stack)
def not_top(stack):
item = stack.pop()
stack.push(0 if item else 1)
def pi(stack):
import math
stack.push(math.pi)
def halve_top(stack):
item = stack.pop()
stack.push(Coherse.operate(item, 2, "/"))
def double(stack):
item = stack.pop()
stack.push(Coherse.operate(item, 2, "*"))
def negate(stack):
item = stack.pop()
stack.push(Coherse.operate(item, -1, "*"))
#Keg+ Functions
def convert(stack, _type):
item = stack.pop()
try:
item = _type(item)
except:
pass
stack.push(item)
def case_switch(stack, how):
string = stack.pop()
if type(string) is not str:
stack.push(string)
return
if how == "upper": stack.push(string.upper())
elif how == "lower": stack.push(string.lower())
else: stack.push(string.swapcase())
def square(stack):
item = stack.pop()
stack.push(Coherse.operate(item, item, "*"))
def string_input(stack):
#The third and final method of input: string input
item = input()
if item:
stack.push(item)
inputs.push(item)
else:
item = inputs.pop()
inputs.push(item)
stack.push(item)
shift(inputs, "R")
def all_true(stack):
if all(stack):
stack.push(1)
else:
stack.push(0)
def all_equal(stack):
equal = 1
last = None
for item in stack:
if last is None:
last = item
continue
else:
if item != last:
equal = 0
break
stack.push(equal)
def summate(stack):
x = len(stack) - 1
for n in range(x):
maths(stack, "+")
def var_set(stack, name):
variables[name] = stack.pop()
def var_get(stack, name):
stack.push(variables[name])
def custom_format(source):
#Using the © format
#first space after a © doesn't count and is removed.
result = ""
temp = ""
escaped = False
var_mode = False
import string
for char in source:
if escaped:
escaped = False
result += char
continue
elif char == "\\":
escaped = True
result += char
continue
if var_mode:
if char in string.ascii_letters:
temp += char
continue
else:
result += to_string(variables.get(temp, '©' + temp))
temp = ""
var_mode = False
if char == " ":
continue
if char == "©":
var_mode = True
continue
result += char
if var_mode:
result += to_string(variables.get(temp, '©' + temp))
return result
def to_string(item):
if type(item) in [float, int]:
return str(item)
if type(item) == char:
return str(_ord(item.v))
if type(item) == str:
return item
return str(item)
def _ord(character):
if str(character) in code_page:
return code_page.index(str(character))
return ord(character)
def _chr(i):
if code_page:
if i == 10:
return "\n"
if 0 < i < 256:
return code_page[i]
return chr(i)
else:
return chr(i)
def try_cast(stack, what_type):
INTEGER, FLOAT, STRING, STACK, CHARACTER = "ℤℝ⅍℠ⁿ"
if what_type == INTEGER:
item = stack.pop()
try:
item = int(item)
except:
pass
stack.push(item)
elif what_type == FLOAT:
item = stack.pop()
try:
item = float(item)
except:
pass
stack.push(item)
elif what_type == STRING:
stack.push(str(stack.pop()))
elif what_type == CHARACTER:
item = stack.pop()
if Coherse._type(item) == "Number":
stack.push(_chr(int(item)))
elif Coherse._type(item) == "Character":
stack.push(item)
elif Coherse._type(item) == "String":
stack.push(char(item[0]))
elif Coherse._type(item) == "Stack":
stack.push(try_cast(item, CHARACTER))
def keg_exec(big_stack):
import Keg
code = big_stack.pop()
code = Keg.balance(code)
code = Keg.transpile(code)
header = f"""
from KegLib import *
from Stackd import Stack
stack = Stack()
"""
footer = """
for item in stack:
big_stack.push(item)
"""
exec(header + code + footer)
def item_in(stack):
query = stack.pop()
if query in stack:
stack.push(1)
else:
stack.push(0)
def perform_index(stack):
position = stack.pop()
stack.push(stack.index([position]))
def multiline(stack):
temp = 1
while temp:
temp = input()
for Char in temp:
stack.push(char(Char))
stack.push("\n")
stack.pop()
def exponate(stack):
power = stack.pop()
base = stack.pop()
result = base
if type(power) in [float, int] and type(base) in [float, int]:
stack.push(pow(base, power))
else:
for n in loop_eval(Coherse.operate(power, 1, "-")):
base = Coherse.operate(base, result, "*")
stack.push(base)
def to_percentage(stack):
item = stack.pop()
stack.push(Coherse.operate(item, 100, "/"))
def reciprocal(stack):
item = stack.pop()
stack.push(Coherse.operate(1, item, "/"))
def keg_round(stack):
item = stack.pop()
if type(item) in [int, float]:
stack.push(round(item))
else:
stack.push(item)
def length_top(stack):
item = stack.pop()
if type(item) in [int, float]:
stack.push(len(str(item)))
else:
stack.push(len(item))
def reverse_top(stack):
item = stack.pop()
if type(item) is int:
if item < 0:
item = int(str(item)[::-1][:-1]) * -1
else:
item = int(str(item)[::-1])
elif type(item) is float:
if item < 0:
item = float(str(item)[::-1][:-1]) * -1
else:
item = float(str(item)[::-1])
else:
item = item[::-1]
stack.push(item)
def pop_item(stack):
item = stack.pop()
for i in range(len(stack)):
temp = stack.pop()
if not Coherse.do_compare(item, temp, '='):
stack.push(temp)
def sort_stack(stack):
stack._Stack__stack.sort()
def increment_register(stack):
register(stack)
integer(stack,1)
maths(stack, "+")
register(stack)
def decrement_register(stack):
register(stack)
integer(stack,1)
maths(stack, "-")
register(stack)
def register_dont_empty(stack):
register(stack)
duplicate(stack)
register(stack)
def register_aug_assign(stack, operator):
register(stack)
maths(stack, operator)
register(stack)
def set_register_dont_empty(stack):
_register = stack.pop()
def register_length(stack):
register_dont_empty(stack)
length_top(stack)
def reverse_register(stack):
register_dont_empty(stack)
reverse_top(stack)
def keg_map(stack, functions):
import Keg
for i in range(len(stack)):
small = Stack([stack[i]])
code = Keg.balance(functions)
code = Keg.transpile(code)
header = f"""
from KegLib import *
from Stackd import Stack
stack = small
"""
exec(header + code)
stack[i] = smart_summate(small)
def smart_summate(stack):
if all(map(lambda x : type(x) in [float, int], stack)):
return stack.pop()
if all(map(lambda x : type(x) == char, stack)):
return "".join([c.v for c in stack])
return str(stack)
def truthify(stack):
temp = stack.pop()
if bool(temp):
stack.push(1)
else:
stack.push(0)
def keg_filter(stack):
import Keg
code = stack.pop()
code = Keg.balance(code)
code = Keg.transpile(code)
final = Stack()
header = f"""
from KegLib import *
from Stackd import Stack
stack = item
"""
for i in range(len(stack)):
item = Stack([stack[i]])
temp = stack[i]
exec(header + code)
if bool(item.pop()):
final.push(temp)
stack._Stack__stack = final._Stack__stack
# https://stackoverflow.com/a/2267446/9363594
def int2base(stack):
import string
digs = string.digits + string.ascii_letters
base = stack.pop()
x = stack.pop()
if x < 0:
sign = -1
elif x == 0:
stack.push(digs[0])
else:
sign = 1
x *= sign
digits = []
while x:
digits.append(digs[int(x % base)])
x = int(x // base)
if sign < 0:
digits.append('-')
digits.reverse()
stack.push(''.join(digits))