import re from time import clock from operator import isNumberType import random oblist = {} iline = None cfun = None arglist = None istack = [] iresult = None step = None procstop = False boxes = {'box1': 0, 'box2': 0} stacks = {} utime_start = 0 class symbol: def __init__(self, name): self.name = name self.nargs = None self.fcn = None def __str__(self): return self.name def __repr__(self): return '#'+self.name class logoerror(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) def run_blocks(spr, blocks): stacks['stack1'] = None stacks['stack2'] = None for i in blocks: if i.proto.name=='hat1': stacks['stack1']= readline(blocks_to_code(i)) if i.proto.name=='hat2': stacks['stack2']= readline(blocks_to_code(i)) code = blocks_to_code(spr) print code setup_cmd(code) def blocks_to_code(spr): if spr==None: return ['%nothing%'] code = [] dock = spr.proto.docks[0] if len(dock)>4: code.append(dock[4]) if spr.proto.primname != '': code.append(spr.proto.primname) else: code.append(float(spr.label)) for i in range(1,len(spr.connections)): b = spr.connections[i] dock = spr.proto.docks[i] if len(dock)>4: for c in dock[4]: code.append(c) if b!=None: code.extend(blocks_to_code(b)) elif spr.proto.docks[i][0] not in ['flow','numend','unavailable','logi-']: code.append('%nothing%') return code def intern(str): if str in oblist: return oblist[str] sym = symbol(str) oblist[str] = sym return sym def parseline(str): split = re.split(r"\s|([\[\]()])", str) return [x for x in split if x and x != ""] def readline(line): res = [] while line: token = line.pop(0) if isNumberType(token): res.append(token) elif token.isdigit(): res.append(float(token)) elif token[0]=='-' and token[1:].isdigit(): res.append(-float(token[1:])) elif token[0] == '"': res.append(token[1:]) elif token == '[': res.append(readline(line)) elif token == ']': return res else: res.append(intern(token)) return res def setup_cmd(str): global iline, step, procstop stopsignon(); procstop=False list = readline(str) step = start_eval(list) def start_eval(list): icall(evline, list); yield True yield False def evline(list): global cfun, arglist, iline oldiline = iline iline = list[:] arglist = None while iline: token = iline[0] if token==symopar: token=iline[1] icall(eval); yield True if procstop: break if iresult==None: continue raise logoerror("You don't say what to do with %s" % token) iline = oldiline ireturn(); yield True def eval(infixarg=False): token = iline.pop(0) if type(token) == symtype: icall(evalsym, token); yield True res = iresult else: res = token if not infixarg: while infixnext(): icall(evalinfix, res); yield True res = iresult ireturn(res); yield True def evalsym(token): global cfun, arglist undefined_check(token) oldcfun, oldarglist = cfun, arglist cfun, arglist = token, [] for i in range(token.nargs): no_args_check() icall(eval); yield True arglist.append(iresult) if cfun.rprim: if type(cfun.fcn)==listtype: icall(ufuncall, cfun.fcn); yield True else: icall(cfun.fcn, *arglist); yield True result = None else: result = cfun.fcn(*arglist) cfun, arglist = oldcfun, oldarglist if arglist!=None and result==None: raise logoerror("%s didn't output to %s" % (oldcfun.name, cfun.name)) ireturn(result); yield True def evalinfix(firstarg): global cfun, arglist token = iline.pop(0) oldcfun, oldarglist = cfun, arglist cfun, arglist = token, [firstarg] no_args_check() icall(eval,True); yield True arglist.append(iresult) result = cfun.fcn(*arglist) cfun, arglist = oldcfun, oldarglist ireturn (result); yield True def infixnext(): if len(iline)==0: return False if type(iline[0])!=symtype: return False return iline[0].name in ['+', '-', '*', '/','%','and','or'] def undefined_check(token): if token.fcn != None: return False raise logoerror("I don't know how to %s" % token.name) def no_args_check(): if iline and iline[0]!=symnothing : return raise logoerror("Not enough inputs to %s" % cfun.name) def prim_wait(time): turtle_spr.setlayer(630) endtime = millis()+an_int(time)*100 while millis()float(y)) defprim('less?', 2, lambda x,y: float(x)