diff options
Diffstat (limited to 'TurtleArt/taprimitive.py')
-rw-r--r-- | TurtleArt/taprimitive.py | 44 |
1 files changed, 27 insertions, 17 deletions
diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index 70762f1..b9e74bc 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -132,6 +132,8 @@ class Primitive(object): func_name = "canvas." elif self.wants_logocode(): func_name = "logo." + elif self.wants_heap(): + func_name = "heap." elif self.wants_tawindow(): func_name = "tw." # get the name of the function directly from the function itself @@ -270,21 +272,23 @@ class Primitive(object): debug_output("end " + repr(self)) # what does this primitive want as its first argument? - if new_prim.wants_turtle(): - first_arg = global_objects["turtles"].get_active_turtle() - elif new_prim.wants_turtles(): - first_arg = global_objects["turtles"] - elif new_prim.wants_canvas(): - first_arg = global_objects["canvas"] - elif new_prim.wants_logocode(): - first_arg = global_objects["logo"] - elif new_prim.wants_tawindow(): - first_arg = global_objects["window"] - else: - first_arg = None + first_arg = None + if not is_bound_method(new_prim.func): + if new_prim.wants_turtle(): + first_arg = global_objects["turtles"].get_active_turtle() + elif new_prim.wants_turtles(): + first_arg = global_objects["turtles"] + elif new_prim.wants_canvas(): + first_arg = global_objects["canvas"] + elif new_prim.wants_logocode(): + first_arg = global_objects["logo"] + elif new_prim.wants_heap(): + first_arg = global_objects["logo"].heap + elif new_prim.wants_tawindow(): + first_arg = global_objects["window"] # execute the actual function - if first_arg is None or is_bound_instancemethod(new_prim.func): + if first_arg is None: return_value = new_prim.func(*new_args, **new_kwargs) else: return_value = new_prim.func(first_arg, *new_args, **new_kwargs) @@ -464,6 +468,9 @@ class Primitive(object): text = ' ' + str(ast_to_value(new_arg_asts[0])) return ast_extensions.Comment(text) + elif self == LogoCode.get_heap: + return TypedName(id_='heap', return_type=self.return_type) + # NORMAL FUNCTION CALL # else: @@ -520,6 +527,12 @@ class Primitive(object): first argument? """ return self._wants(LogoCode) + def wants_heap(self): + """ Does this Primitive want to get the heap as its first argument? """ + return ((hasattr(self.func, '__self__') and + isinstance(self.func.__self__, list)) or + self.func in list.__dict__.values()) + def wants_tawindow(self): """ Does this Primitive want to get the TurtleArtWindow instance as its first argument? """ @@ -532,10 +545,7 @@ class Primitive(object): return not is_instancemethod(self.func) def _wants(self, theClass): - if is_instancemethod(self.func): - return self.func.im_class == theClass - else: - return False + return is_instancemethod(self.func) and self.func.im_class == theClass # treat the following methods in a special way when converting the # Primitive to an AST |