From 7bdbe864ade7916bedfc1c40c9f59ea355c4a00a Mon Sep 17 00:00:00 2001 From: Marion Date: Wed, 21 Aug 2013 21:37:18 +0000 Subject: pass arguments to Primitive objects without calling the args first --- diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 33d6834..ef482a1 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -276,7 +276,7 @@ class LogoCode: return ['%nothing%', '%nothing%'] code = [] dock = blk.docks[0] - if len(dock) > 4: # There could be a '(', ')', '[' or ']'. + if len(dock) > 4 and dock[4] in ('[', ']', ']['): # There could be a '(', ')', '[' or ']'. code.append(dock[4]) if blk.primitive is not None: # make a tuple (prim, blk) if blk in self.tw.block_list.list: @@ -296,7 +296,7 @@ class LogoCode: for i in range(1, len(blk.connections)): b = blk.connections[i] dock = blk.docks[i] - if len(dock) > 4: # There could be a '(', ')', '[' or ']'. + if len(dock) > 4 and dock[4] in ('[', ']', ']['): # There could be a '(', ')', '[' or ']'. for c in dock[4]: code.append(c) if b is not None: @@ -380,7 +380,7 @@ class LogoCode: self.istack.append(self.step) self.step = fcn(*(args)) - def evline(self, blklist): + def evline(self, blklist, call_me=True): """ Evaluate a line of code from the list. """ oldiline = self.iline self.iline = blklist[:] @@ -411,7 +411,7 @@ class LogoCode: (token, self.bindex) = self.iline[1] # Process the token and any arguments. - self.icall(self._eval) + self.icall(self._eval, call_me) yield True # Time to unhighlight the current block. @@ -434,7 +434,7 @@ class LogoCode: self.tw.display_coordinates() yield True - def _eval(self): + def _eval(self, call_me=True): """ Evaluate the next token on the line of code we are processing. """ token = self.iline.pop(0) bindex = None @@ -446,7 +446,7 @@ class LogoCode: # We highlight blocks here in case an error occurs... if not self.tw.hide and bindex is not None: self.tw.block_list.list[bindex].highlight() - self.icall(self._evalsym, token) + self.icall(self._evalsym, token, call_me) yield True # and unhighlight if everything was OK. if not self.tw.hide and bindex is not None: @@ -458,7 +458,7 @@ class LogoCode: self.ireturn(res) yield True - def _evalsym(self, token): + def _evalsym(self, token, call_me): """ Process primitive associated with symbol token """ self._undefined_check(token) oldcfun, oldarglist = self.cfun, self.arglist @@ -468,35 +468,50 @@ class LogoCode: self.tw.showblocks() self.tw.display_coordinates() raise logoerror("#noinput") + call_args = type(self.cfun.fcn).__name__ != 'Primitive' for i in range(token.nargs): self._no_args_check() - self.icall(self._eval) + self.icall(self._eval, call_args) yield True self.arglist.append(self.iresult) + need_to_pop_istack = False if self.cfun.rprim: if isinstance(self.cfun.fcn, list): # debug_output('evalsym rprim list: %s' % (str(token)), # self.tw.running_sugar) - self.icall(self._ufuncall, self.cfun.fcn) + self.icall(self._ufuncall, self.cfun.fcn, call_args) yield True + need_to_pop_istack = True + result = None else: - self.icall(self.cfun.fcn, *self.arglist) - yield True - result = None + if call_me: + self.icall(self.cfun.fcn, *self.arglist) + yield True + need_to_pop_istack = True + result = None + else: + result = (self.cfun.fcn, ) + tuple(self.arglist) else: - result = self.cfun.fcn(self, *self.arglist) + need_to_pop_istack = True + if call_me: + result = self.cfun.fcn(self, *self.arglist) + else: + result = (self.cfun.fcn, self) + tuple(self.arglist) self.cfun, self.arglist = oldcfun, oldarglist if self.arglist is not None and result is None: self.tw.showblocks() raise logoerror("%s %s %s" % (oldcfun.name, _("did not output to"), self.cfun.name)) - self.ireturn(result) - yield True + if need_to_pop_istack: + self.ireturn(result) + yield True + else: + self.iresult = result - def _ufuncall(self, body): + def _ufuncall(self, body, call_me): """ ufuncall """ - self.ijmp(self.evline, body) + self.ijmp(self.evline, body, call_me) yield True def doevalstep(self): diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index 48972ae..bac9f7b 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -268,6 +268,14 @@ class Primitive(object): if runtime_args and isinstance(runtime_args[0], LogoCode): runtime_args = runtime_args[1:] + runtime_args_copy = runtime_args[:] + runtime_args = [] + for arg in runtime_args_copy: + if isinstance(arg, tuple) and arg and callable(arg[0]): + runtime_args.append(arg[0](*arg[1:])) + else: + runtime_args.append(arg) + # what does this primitive want as its first argument? if self.wants_turtle(): first_arg = global_objects["turtles"].get_active_turtle() -- cgit v0.9.1