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")
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 operate(lhs, rhs, op): from Stackd import Stack tLhs, tRhs = _type(lhs), _type(rhs) choices = relations[tLhs][tRhs] expr = choices["+-*/%".index(op)] x, y = lhs, rhs if type(lhs) is Stack: x = eval(str(lhs)) if type(rhs) is Stack: y = eval(str(rhs)) z = eval(expr) if type(z) is list: z = Stack(z) return z
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
'''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:
from KegLib import * from Stackd import Stack stack = Stack() printed = False Input(stack) condition = condition_eval( ["length(stack)", "integer(stack, 1)", "maths(stack, '-')"], stack) print(condition) while condition: nice(stack) printed = True condition = 1 integer(stack, 10) if not printed: printing = "" for item in stack: if type(item) in [str, Stack]: printing += item elif type(item) == Coherse.char: printing += item.v elif item < 10 or item > 256: printing += str(item) else: printing += chr(item) print(printing)
from KegLib import * from Stackd import Stack stack = Stack() printed = False _register = None character(stack, 'c') register(stack) character(stack, 'd') for _ in loop_eval(stack.pop()): register(stack) duplicate(stack) raw(stack) register(stack) character(stack, ' ') character(stack, 'b') character(stack, 'o') character(stack, 't') character(stack, 't') character(stack, 'l') character(stack, 'e') character(stack, 's') character(stack, ' ') character(stack, 'o') character(stack, 'f') character(stack, ' ') character(stack, 'b') character(stack, 'e') character(stack, 'e') character(stack, 'r') character(stack, ' ')
def run(source, master_stack, sub_stack=None): global register, comment, escape, printed, pushed code = source stack = Stack() do_repush = False #Indicate whether or not sub needs to push its contents #back onto master_stack if sub_stack is None: stack = master_stack else: stack = sub_stack do_repush = True for Tkn in code: cmd = Tkn.data print(Tkn, stack, register) #Handle effect of comments and escape chars first if comment: if cmd == NEWLINE: comment = False continue if Tkn.name == Parse.CMDS.ESC: #print(cmd, _ord(cmd)) escape = False stack.push(_ord(cmd)) continue #Now, do all the functions if cmd == LENGTH: stack.push(len(stack)) elif cmd == DUPLICATE: temp = stack.pop() stack.push(temp) stack.push(temp) elif cmd == POP: stack.pop() elif cmd == PRINT_CHR: #print(stack, stack.pop(), stack) print(_chr(stack.pop()), end="") printed = True elif cmd == PRINT_INT: print(stack.pop(), end="") printed = True elif cmd == L_SHIFT: stack.push(stack[0]) del stack[0] elif cmd == R_SHIFT: stack._Stack__stack.insert(0, stack.pop()) elif cmd == REVERSE: if not len(stack): keg_input(stack) stack._Stack__stack.reverse() elif cmd == SWAP: stack[-1], stack[-2] = stack[-2], stack[-1] elif cmd == INPUT: keg_input(stack) pushed = True #Reg starts now elif cmd == IOTA: k = stack[-1] stack.pop() for i in range(k, -1, -1): stack.push(i) elif cmd == DECREMENT: stack.push(stack.pop() - 1) elif cmd == SINE: stack.push(math.sin(stack.pop())) continue elif cmd == NICE_INPUT: temp = input() if "." in temp: stack.push(float(temp)) elif temp.isnumeric() or (temp[0] == "-" and temp[1:].isnumeric()): stack.push(int(temp)) else: for char in reversed(temp): stack.push(_ord(char)) pushed = True elif cmd == EXCLUSIVE_RANGE: _range = generate_range(stack.pop(), stack.pop(), "e") query = stack.pop() if query in _range: stack.push(1) else: stack.push(0) elif cmd == INCLUSIVE_RANGE: _range = generate_range(stack.pop(), stack.pop()) query = stack.pop() if query in _range: stack.push(1) else: stack.push(0) elif cmd == GENERATE_RANGE: _range = generate_range(stack.pop(), stack.pop()) for item in _range: stack.push(item) elif cmd == GENERATE_RANGE_0: _range = generate_range(0, stack.pop()) for item in _range: stack.push(item) elif cmd == NUMBER_SPLIT: temp = str(stack.pop()) for thing in temp: stack.push(int(thing)) elif cmd == FACTORIAL: stack.push(math.factorial(stack.pop())) #Next step, keywords elif cmd == COMMENT: comment = True elif cmd == BRANCH: continue elif cmd == ESCAPE: escape = True #print("ESCCAPE") elif cmd == REGISTER: if register is None: register = stack.pop() else: stack.push(register) register = None #Now, structures elif Tkn.name == Parse.CMDS.IF: condition = stack.pop() if condition: run(Parse.parse(Tkn.data[0]), stack) else: run(Parse.parse(Tkn.data[1]), stack) elif Tkn.name == Parse.CMDS.FOR: n = _eval(Tkn.data[0], stack) for q in range(int(n)): run(Parse.parse(Tkn.data[1]), stack) elif Tkn.name == Parse.CMDS.WHILE: condition = Tkn.data[0] while _eval(condition): run(Parse.parse(Tkn.data[1]), stack) elif Tkn.name == Parse.CMDS.FUNCTION: if Tkn.data[0] == 1: function_name, number_params = Tkn.data[1],\ int(functions[function_name]["number"]) function_stack = Stack() for _ in range(number_params): function_stack.push(master_stack.pop()) run(Parse.parse(functions[function_name]["body"]), stack, function_stack) else: function_name, number_params = Tkn.data[0]["name"],\ int(Tkn.data[0]["number"]) functions[function_name] = { "number": number_params, "body": Tkn.data[1] } #Now, operators elif cmd in MATHS: x, y = stack.pop(), stack.pop() temp = cmd if cmd == "Ë": temp = "**" stack.push(eval(f"y{temp}x")) elif cmd in CONDITIONAL: lhs, rhs = stack.pop(), stack.pop() temp = cmd if cmd == "=": temp = "==" elif cmd == "≬": temp = "> 0 and lhs <" result = eval(f"rhs{temp}lhs") if result: stack.push(1) else: stack.push(0) elif cmd in NUMBERS: stack.push(int(cmd)) elif Tkn.name == Parse.CMDS.STRING: stack.push(Tkn.data) #Keg+ elif Tkn.data == DIV_MOD: quot, remainder = divmod(stack.pop(), stack.pop()) stack.push(quot) stack.push(remainder) #Whitespace elif cmd == TAB: continue elif cmd == NEWLINE: stack.push(10) else: stack.push(_ord(cmd)) if do_repush: for item in stack: master_stack.push(item)
def _eval(expr, stack=main_stack): #Evaluate the given expression as Keg code temp = Stack() for Token in Parse.parse(expr): #print(Token.name, Token.data, temp) if Token.name not in [Parse.CMDS.CMD, Parse.CMDS.NOP, Parse.CMDS.ESC]: raise Bruh("""You can't just go placing forbidden characters in expressions and expect to get away with it.""") else: if Token.name == Parse.CMDS.ESC: temp.push(_ord(Token.data)) elif Token.data in NUMBERS: temp.push(int(Token.data)) elif Token.data in MATHS: x, y = temp.pop(), temp.pop() op = Token.data if op == MATHS[-1]: op = "**" temp.push(eval(f"y{op}x")) elif Token.data in CONDITIONAL: lhs, rhs = temp.pop(), temp.pop() op = Token.data if op == "=": op = "==" elif op == "≬": op = "> 0 and lhs <" result = eval(f"lhs{op}rhs") if result: temp.push(1) else: temp.push(0) elif Token.data == LENGTH: temp.push(len(stack)) elif Token.data == DUPLICATE: item = temp.pop() temp.push(item) temp.push(item) elif Token.data == RANDOM: temp.push(random.randint(Keg_Nums.small_boy, Keg_Nums.big_boy)) elif Token.data == POP: temp.push(stack.pop()) elif Token.data == NEWLINE: temp.push(10) elif Token.data == TAB: continue elif Token.data in "#|@": raise Bruh("You can't just do that in the expression " + expr) #Start of Reg's extra commands elif Token.data == IOTA: k = temp[-1] temp.pop() for i in range(k, -1, -1): temp.push(i) elif Token.data == EXCLUSIVE_RANGE: _range = generate_range(temp.pop(), temp.pop(), "e") query = temp.pop() if query in _range: temp.push(1) else: temp.push(0) elif Token.data == INCLUSIVE_RANGE: _range = generate_range(temp.pop(), temp.pop()) query = temp.pop() if query in _range: temp.push(1) else: temp.push(0) elif Token.data == GENERATE_RANGE: _range = generate_range(temp.pop(), temp.pop()) for item in _range: temp.push(item) elif Token.data == GENERATE_RANGE_0: _range = generate_range(0, temp.pop()) for item in _range: temp.push(item) elif Token.data == DECREMENT: temp[-1] -= 1 elif Token.data == SINE: temp.push(math.sin(temp.pop())) elif Token.data == NUMBER_SPLIT: item = str(temp.pop()) for thing in item: temp.push(int(thing)) elif Token.data == FACTORIAL: temp.push(math.factorial(temp.pop())) elif Token.data == DIV_MOD: quot, remainder = divmod(temp.pop(), temp.pop()) temp.push(quot) temp.push(remainder) else: temp.push(_ord(Token.data)) return temp[0]
unicode += "`abcdefghijklmno" unicode += "pqrstuvwxyz{|}~ø" unicode += "¶\n\t⊂½‡™±¦→←↶↷" unicode += "✏█↗↘□²ⁿ║ṡ⟰⟱⟷" unicode += "ℤℝ⅍℠א∀≌᠀⊙᠈⅀" unicode += "ȦƁƇƉƐƑƓǶȊȷǨȽƜƝǪǷɊƦȘȚȔƲɅƛƳƵ" #push'n'print unicode += "☭" #I don't know what this'll do. But it looks cool unicode += "⬠⬡⬢⬣⬤⬥⬦⬧⬨⬩⬪⬫⬬⬭⬮⬯" #drawing unicode += "⯑" #Do something random unicode += "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂⒃⒄⒅⒆⒇" for n in range(127234, 127243): unicode += chr(n) #Variables main_stack = Stack() functions = {} register = None #The register used comment = False #Whether or not in a comment escape = False #Escape next character? printed = False #Used to determine whether or not to do implicit printing pushed = False #Used to determine whether or not to implicit cat def keg_input(stack): global pushed pushed = True temp = input() for char in reversed(temp): stack.push(_ord(char))