From 9a009852c722f50c33b144b0d3d76730b23aaaf3 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 05 Feb 2010 23:34:35 +0000 Subject: highlighter mostly working --- (limited to 'talogo.py') diff --git a/talogo.py b/talogo.py index 5623a3a..8495aab 100644 --- a/talogo.py +++ b/talogo.py @@ -77,20 +77,6 @@ def careful_divide(x,y): except: return 0 -def taequal(x,y): - try: - return float(x)==float(y) - except: - if type(x) == str or type(x) == unicode: - xx = ord(x[0]) - else: - xx = x - if type(y) == str or type(y) == unicode: - yy = ord(y[0]) - else: - yy = y - return xx==yy - def taless(x, y): try: return float(x)4: # There could be a '(', ')', '[' or ']'. code.append(dock[4]) + self.blk_index.append(dock[4]) if blk.primitive is not None: code.append(blk.primitive) if blk.name not in BOX_STYLE: @@ -404,6 +394,7 @@ class LogoCode: else: print "%s had no primitive." % (blk.name) return ['%nothing%'] + self.blk_index.append(-1) else: print "%s had no value." % (blk.name) return ['%nothing%'] @@ -413,6 +404,7 @@ class LogoCode: if len(dock)>4: # There could be a '(', ')', '[' or ']'. for c in dock[4]: code.append(c) + self.blk_index.append(c) if b is not None: code.extend(self.blocks_to_code(b)) elif blk.docks[i][0] not in ['flow', 'unavailable']: @@ -425,16 +417,20 @@ class LogoCode: def setup_cmd(self, str): self.tw.active_turtle.hide() # Hide the turtle while we are running. self.procstop = False - list = self.readline(str) + list = self.readline(str, self.blk_index) + print list self.step = self.start_eval(list) """ Convert the pseudocode into a list of commands. + The block associated with the command is stored as the second element + in a tuple, e.g., (#forward, 16) """ - def readline(self, line): + def readline(self, line, bline): res = [] while line: token = line.pop(0) + btoken = bline.pop(0) if isNumberType(token): res.append(token) elif token.isdigit(): @@ -446,11 +442,11 @@ class LogoCode: elif token[0:2] == "#s": res.append(token[2:]) elif token == '[': - res.append(self.readline(line)) + res.append(self.readline(line, bline)) elif token == ']': return res else: - res.append(self.intern(token)) + res.append((self.intern(token),btoken)) return res """ @@ -478,22 +474,34 @@ class LogoCode: self.iline = list[:] self.arglist = None while self.iline: + token = self.iline[0] + btoken = None + if type(token) == type((1,2)): + (token, btoken) = self.iline[0] if self.tw.step_time > 0: - b = self.tw.block_list.list[self.blk_index[self.bi]] - b.highlight() + if btoken is not None and type(btoken) is int: + self.tw.block_list.list[btoken].highlight() self.tw.active_turtle.show() endtime = millis()+self.an_int(self.tw.step_time)*100 while millis() 0: + if btoken is not None and type(btoken) is int: + self.tw.block_list.list[btoken].unhighlight() + if self.procstop: break if self.iresult == None: @@ -509,14 +517,21 @@ class LogoCode: """ def eval(self, infixarg=False): token = self.iline.pop(0) + btoken = None + if type(token) == type((1,2)): + (token, btoken) = token if type(token) == self.symtype: + print "eval: token %s (%s) is a symtype" % (token, btoken) self.icall(self.evalsym, token) yield True res = self.iresult else: + if btoken is not None: + print "eval: token %s (%s) is a token" % (token, btoken) res = token if not infixarg: while self.infixnext(): + # print "evalinfix %s" % (res) self.icall(self.evalinfix, res) yield True res = self.iresult @@ -527,31 +542,44 @@ class LogoCode: Processing flow (vertical) """ def evalsym(self, token): + btoken = None + if type(token) == type((1,2)): + (token, btoken) = token self.debug_trace(token) self.undefined_check(token) oldcfun, oldarglist = self.cfun, self.arglist self.cfun, self.arglist = token, [] + if btoken is not None: + print "evalsym: %s (%s)" % (token, btoken) + # self.tw.block_list.list[btoken].highlight() + if token.nargs == None: raise logoerror("#noinput") for i in range(token.nargs): self.no_args_check() + # print "evalsym: calling eval %s" % (str(i)) self.icall(self.eval) yield True self.arglist.append(self.iresult) if self.cfun.rprim: if type(self.cfun.fcn) == self.listtype: + # print "evalsym: rprim listtype" self.icall(self.ufuncall, self.cfun.fcn) yield True else: + # print "evalsym: rprim" self.icall(self.cfun.fcn, *self.arglist) yield True result = None else: + # print "evalsym: not an rprim??" # TODO: find out why stopstack args are mismatched if token.name == 'stopstack': result = self.cfun.fcn() + # print result else: result = self.cfun.fcn(self, *self.arglist) + # print result self.cfun, self.arglist = oldcfun, oldarglist if self.arglist is not None and result == None: raise logoerror("%s didn't output to %s (arglist %s, result %s)" % \ @@ -564,6 +592,10 @@ class LogoCode: # def evalinfix(self, firstarg): token = self.iline.pop(0) + btoken = None + if type(token) == type((1,2)): + (token, btoken) = token + # print "evalinfix %s" % (token) oldcfun, oldarglist = self.cfun, self.arglist self.cfun, self.arglist = token, [firstarg] no_args_check(self) @@ -749,14 +781,14 @@ class LogoCode: def prim_repeat(self, num, list): num = self.an_int(num) self.highlighter("repeat") - bi = self.bi + # bi = self.bi for i in range(num): - self.bi = bi + # self.bi = bi self.icall(self.evline, list[:]) yield True if self.procstop: break - self.bi = bi + # self.bi = bi self.unhighlight("repeat") self.ireturn() yield True @@ -770,41 +802,57 @@ class LogoCode: def prim_forever(self, list): self.highlighter("forever") - bi = self.bi + # bi = self.bi while True: - self.bi = bi + # self.bi = bi self.icall(self.evline, list[:]) yield True if self.procstop: break - self.bi = bi + # self.bi = bi self.unhighlight("forever") self.ireturn() yield True def prim_if(self, bool, list): self.highlighter("if") - bi = self.bi + # bi = self.bi if bool: self.icall(self.evline, list[:]) yield True - self.bi = bi + # self.bi = bi self.unhighlight("if") self.ireturn() yield True def prim_ifelse(self, bool, list1, list2): self.highlighter("ifelse") - bi = self.bi + # bi = self.bi if bool: self.ijmp(self.evline, list1[:]) yield True else: self.ijmp(self.evline, list2[:]) yield True - self.bi = bi + # self.bi = bi self.unhighlight("ifelse") + def prim_equal(self, x, y): + self.highlighter("equal") + self.unhighlight("equal") + try: + return float(x)==float(y) + except: + if type(x) == str or type(x) == unicode: + xx = ord(x[0]) + else: + xx = x + if type(y) == str or type(y) == unicode: + yy = ord(y[0]) + else: + yy = y + return xx==yy + def prim_opar(self, val): self.iline.pop(0) return val @@ -816,42 +864,42 @@ class LogoCode: name.rprim = True def prim_stack(self, str): - self.highlighter("stack") - self.bi = self.bis['stack3'+str] + # self.highlighter("stack") + # self.bi = self.bis['stack3'+str] if (not self.stacks.has_key('stack3'+str)) or\ self.stacks['stack3'+str] is None: raise logoerror("#nostack") self.icall(self.evline, self.stacks['stack3'+str][:]) yield True self.procstop = False - self.bi = bi + # self.bi = bi self.ireturn() yield True def prim_stack1(self): self.highlighter("stack1") - bi = self.bi - self.bi = self.bis['stack1'] + # bi = self.bi + # self.bi = self.bis['stack1'] if self.stacks['stack1'] is None: raise logoerror("#nostack") self.icall(self.evline, self.stacks['stack1'][:]) yield True self.procstop = False - self.bi = bi + # self.bi = bi self.unhighlight("stack1") self.ireturn() yield True def prim_stack2(self): self.highlighter("stack2") - bi = self.bi - self.bi = self.bis['stack2'] + # bi = self.bi + # self.bi = self.bis['stack2'] if self.stacks['stack2'] is None: raise logoerror("#nostack") self.icall(self.evline, self.stacks['stack2'][:]) yield True self.procstop = False - self.bi = bi + # self.bi = bi self.unhighlight("stack2") self.ireturn() yield True @@ -925,7 +973,6 @@ class LogoCode: def kbinput(self): self.highlighter("kbinput") - self.bi += 1 if len(self.tw.keypress) == 1: self.keyboard = ord(self.tw.keypress[0]) else: @@ -999,17 +1046,14 @@ class LogoCode: def highlighter(self, name): if self.tw.step_time == 0: return - b = self.tw.block_list.list[self.blk_index[self.bi-1]] - print ">>> block %s: (%d: %s)" % (name, self.bi, b.name) + # b = self.tw.block_list.list[self.blk_index[self.bi-1]] + # print ">>> block %s: (%d: %s)" % (name, self.bi, b.name) return def unhighlight(self, name): if self.tw.step_time == 0: return - if self.bi > 0: - b = self.tw.block_list.list[self.blk_index[self.bi-1]] - b.unhighlight() - print "<<< block %s: (%d: %s)" % (name, self.bi, b.name) + """ Everything below is related to multimedia commands -- cgit v0.9.1