import sys, os, time from textbuffer import Grid from copy import copy grid = Grid(16,16) def print_divider(): print("---------------------------------------------------------------") def print_header(): print(" /$$ /$$ /$$$$$$$ /$$ \n| $$$ | $$ | $$__ $$ |__/ \n| $$$$| $$ /$$ /$$| $$ \\ $$ /$$$$$$ /$$$$$$ /$$ /$$$$$$$ \n| $$ $$ $$| $$ | $$| $$$$$$$ /$$__ $$|____ $$| $$| $$__ $$\n| $$ $$$$| $$ | $$| $$__ $$| $$ \\__/ /$$$$$$$| $$| $$ \ $$\n| $$\ $$$| $$ | $$| $$ \\ $$| $$ /$$__ $$| $$| $$ | $$\n| $$ \ $$| $$$$$$/| $$$$$$$/| $$ | $$$$$$$| $$| $$ | $$\n|__/ \\__/ \\______/ |_______/ |__/ \\_______/|__/|__/ |__/\n") def setGlobals(): global code,default_args,show_debug,show_char_codes,slow_range,slow_speed,high_speed,max_stack_size,numbers,allowed_symbols,alphabet,inp,outp,running,memory,cursors,error default_args = {"show_debug":False,"show_char_codes":False,"slow_range":[0,0],"slow_speed":0.2,"high_speed":0,"max_stack_size":0} show_debug = False show_char_codes = False slow_range = [0,0]#[13,59]#[17,31] slow_speed = 0.2 high_speed = 0 max_stack_size = 0 numbers = "0123456789" allowed_symbols = "_" alphabet = " abcdefghijklmnopqrstuvwxyz"+numbers+allowed_symbols inp = "" outp = "" running = True memory = [0 for x in range(256)] cursors = [ [0,0,0,0,1,0,0,[],1] #[cursor_pos,pointer_pos,int_math,comment_flag,condition_flag,escapeflag,scope_depth,stack,running] ] error = False code = "" setGlobals() def update_cursor(c): global code,memory,cursors,running,inp,outp,show_debug,show_char_codes,numbers,allowed_symbols,alphabet,max_stack_size if show_debug: print code[c[0]] if c[5] > 0: #deincrement escape flag c[5] -= 1 if code[c[0]] == "\\":# toggle escape flag if c[5] == 0: c[5] = 2 if code[c[0]] == "\"" and c[5] == 0:# start end comment if c[3] == 1: c[3] = 0 else: c[3] = 1 if (c[8] and c[5] == 0 and c[3] == 0): if code[c[0]] == "+":# set int-math to add c[2] = 0 if code[c[0]] == "-":# set int-math to subract c[2] = 1 if code[c[0]] == "*":# set int_math to multiply c[2] = 2 if code[c[0]] == "/":# set int-math to divide c[2] = 3 if code[c[0]] == "#":# set int-math to RAW INT c[2] = 4 if code[c[0]] == "$":# set int-math to STR CHR c[2] = 5 if code[c[0]] == "%":# set int-math to modulo c[2] = 6 if code[c[0]] == "!":# toggle conditon flag if c[4] == 1: c[4] = 0 else: c[4] = 1 if code[c[0]] == "@":# goto pointer location c[1] = memory[c[1]] if code[c[0]] == "&":# drop pointer location memory[c[1]] = c[1] if code[c[0]] == ":":# pickup into pointer stack c[7].append(memory[c[1]]) if len(c[7]) > max_stack_size and max_stack_size > 0: del c[7][0] if code[c[0]] == ";":# drop from pointer stack if len(c[7]) > 0: if c[2] == 0: memory[c[1]] += int(c[7][-1]) if c[2] == 1: memory[c[1]] -= int(c[7][-1]) if c[2] == 2: memory[c[1]] = memory[c[1]] * int(c[7][-1]) if c[2] == 3: memory[c[1]] = memory[c[1]] / int(c[7][-1]) if c[2] == 4: memory[c[1]] = int(c[7][-1]) if c[2] == 6: memory[c[1]] = memory[c[1]] % int(c[7][-1]) del c[7][-1] else: memory[c[1]] = 0 if code[c[0]] == ".":# output if not show_debug: sys.stdout.write(chr(memory[c[1]])) ##print(chr(memory[c[1]])) else: outp += chr(memory[c[1]]) if code[c[0]] == ",":# input memory[c[1]] = inp[-1] inp = inp[:-1]#sys.stdout.read(1) if code[c[0]] == ">":# move pointer right c[1] += 1 if code[c[0]] == "<":# mover pointer left c[1] -= 1 if code[c[0]] == "[":# open loop if c[4]: n = not memory[c[1]] else: n = memory[c[1]] h = copy(c[6]) if not n: c[6] += 1 else: c[6] += 1 while c[6] != h: c[0] += 1 if code[c[0]] == "[" or code[c[0]] == "{": c[6] += 1 if code[c[0]] == "]"or code[c[0]] == "}": c[6] -= 1 if code[c[0]] == "]":# close loop if c[4]: n = memory[c[1]] else: n = not memory[c[1]] h = copy(c[6]) if not n: c[6] -= 1 else: c[6] -= 1 while c[6] != h: c[0] -= 1 if code[c[0]] == "[" or code[c[0]] == "{": c[6] += 1 if code[c[0]] == "]" or code[c[0]] == "}": c[6] -= 1 if code[c[0]] == "{":# open process if c[4]: n = memory[c[1]] else: n = not memory[c[1]] h = copy(c[6]) if n: cursors.append([c[0]+1,c[1],0,0,1,0,0,[],1]) c[6] += 1 while c[6] != h: c[0] += 1 #print c[0] if code[c[0]] == "{" or code[c[0]] == "[": c[6] += 1 if code[c[0]] == "}" or code[c[0]] == "]": c[6] -= 1 elif code[c[0]] == "}":# close process if c[4]: #check condition flag n = memory[c[1]] else: n = not memory[c[1]] h = copy(c[6])#copy "loop depth" if not n: #if memory pointer reference is true c[6] += 1 #increment "loop depth" c[8] = 0 #terminate process else: c[6] += 1 #increment "loop depth" while c[6] != h: #repeat until current loop depth = old loop depth c[0] -= 1 #move code cursor backward #print(c[0]) if code[c[0]] == "{" or code[c[0]] == "[": #check for opening brackets c[6] += 1 #increment "loop depth" if code[c[0]] == "}" or code[c[0]] == "]":#check for closing brackets c[6] -= 1 #deincrement "loop depth" if code[c[0]] in alphabet and c[2] == 5 and c[3] == 0 and c[5] == 0: memory[c[1]] = ord(code[c[0]]) if code[c[0]] in numbers and c[2] != 5 and c[3] == 0 and c[5] == 0: if c[2] == 0: memory[c[1]] += int(code[c[0]]) if c[2] == 1: memory[c[1]] -= int(code[c[0]]) if c[2] == 2: memory[c[1]] = memory[c[1]] * int(code[c[0]]) if c[2] == 3: memory[c[1]] = memory[c[1]] / int(code[c[0]]) if c[2] == 4: memory[c[1]] = int(code[c[0]]) if c[2] == 6: memory[c[1]] = memory[c[1]] % int(code[c[0]]) c[0] += 1 if c[0] >= len(code): c[8] = 0 def run_code(c="",input_args=default_args): global code,default_args,show_debug,show_char_codes,slow_range,slow_speed,high_speed,max_stack_size,numbers,allowed_symbols,alphabet,inp,outp,running,memory,cursors,error code = c default_args = {"show_debug":False, "show_char_codes":False, "slow_range":[0,0], "slow_speed":0.2, "high_speed":0, "max_stack_size":0} ## show_debug = False ## show_char_codes = False ## slow_range = [0,0]#[13,59]#[17,31] ## slow_speed = 0.2 ## high_speed = 0 ## max_stack_size = 0 args = copy(default_args) for k in input_args: if input_args[k] != default_args[k]: args[k] = input_args[k] show_debug = args['show_debug'] show_char_codes = args['show_char_codes'] slow_range = args['slow_range'] slow_speed = args['slow_speed'] high_speed = args['high_speed'] max_stack_size = args['max_stack_size'] numbers = "0123456789" allowed_symbols = "_" alphabet = " abcdefghijklmnopqrstuvwxyz"+numbers+allowed_symbols inp = "" outp = "" running = True memory = [0 for x in range(256)] cursors = [ [0,0,0,0,1,0,0,[],1] #[cursor_pos,pointer_pos,int_math,comment_flag,condition_flag,escapeflag,scope_depth,stack,running] ] error = False while running: if show_debug: os.system("cls") for x in range(len(memory)): if not show_char_codes: gch = str(memory[x]) else: if memory[x] == 0: gch = " " else: gch = chr(memory[x]) grid.put(x/16,x%16,gch) if show_debug: print_header() print_divider() grid.update() print_divider() for i in cursors: print i for i in range(len(cursors)): if cursors[i][8]: try: update_cursor(cursors[i]) except Exception() as e: print e running = False error = True if show_debug: print(outp) if cursors[i][0] >= slow_range[0] and cursors[i][0] <= slow_range[1]: time.sleep(slow_speed) else: if high_speed != 0 and high_speed != 0.0: time.sleep(high_speed) i = 0 while i < len(cursors): if not cursors[i][8]: cursors.pop(i) else: i += 1 if len(cursors) == 0: running = False if show_debug and not error: os.system("cls") print_header() grid.update() print_divider() print outp else: print("") raw_input("\"Press [Enter] to exit\"") #print cursors if __name__ == "__main__": setGlobals() try: iargs = sys.argv #print args if len(iargs) == 1: iargs = [iargs[0],None,None] elif len(iargs) == 2: iargs = [iargs[0],iargs[1],None] except Exception as e: iargs = [None,None,None] print e if (iargs[2] == "-p"): show_debug = bool(int(iargs[3])) show_char_codes = bool(int(iargs[4])) slow_range = [int(x) for x in iargs[5][1:-1].split(",")]#[13,59]#[17,31] slow_speed = float(iargs[6]) high_speed = float(iargs[7]) max_stack_size = int(iargs[8]) elif (iargs[2] == "-d"): show_debug = True if iargs[1] != None : code = open(iargs[1]).read() else: code = '!${$h.e.l.l.o.>#1<}>[]_.w.o.r.l.d.' #run_code(code,{'show_debug':1,'show_char_codes':0,'slow_range':[0,0],'slow_speed':0,'high_speed':0.3}) run_code(code,{'show_debug':show_debug,'show_char_codes':show_char_codes,'slow_range':slow_range,'slow_speed':slow_speed,'high_speed':high_speed,'max_stack_size':max_stack_size})