diff options
author | Marion <marion.zepf@gmail.com> | 2013-09-12 16:37:53 (GMT) |
---|---|---|
committer | Marion <marion.zepf@gmail.com> | 2013-09-12 16:37:53 (GMT) |
commit | 4c4e63bc7928d239a8881990fdae1a2eca4757e2 (patch) | |
tree | de0768c7ad325741b160e7bcad624d3d7f3a48ad | |
parent | 5ebec1534aa4bb59a3ee3261ad44ea1c0c08bd26 (diff) |
add Primitive for 'clear/ empty heap' block
- Use LogoCode's heap in the exported code.
-rw-r--r-- | TurtleArt/taexportpython.py | 1 | ||||
-rw-r--r-- | TurtleArt/talogo.py | 7 | ||||
-rw-r--r-- | TurtleArt/taprimitive.py | 9 | ||||
-rw-r--r-- | plugins/turtle_blocks_extras/turtle_blocks_extras.py | 10 |
4 files changed, 21 insertions, 6 deletions
diff --git a/TurtleArt/taexportpython.py b/TurtleArt/taexportpython.py index 3f3a03e..19cf48e 100644 --- a/TurtleArt/taexportpython.py +++ b/TurtleArt/taexportpython.py @@ -50,7 +50,6 @@ tw = get_tw() BOX = {} ACTION = {} -heap = [] diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 394e4b7..d3410a6 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -810,6 +810,13 @@ class LogoCode: def get_heap(self): return self.heap + def reset_heap(self): + """ Reset heap to an empty list """ + # empty the list rather than setting it to a new empty list object, + # so the object references are preserved + while self.heap: + self.heap.pop() + def clear_value_blocks(self): if not hasattr(self, 'value_blocks_to_update'): return diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index b9e74bc..2272fac 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -133,7 +133,7 @@ class Primitive(object): elif self.wants_logocode(): func_name = "logo." elif self.wants_heap(): - func_name = "heap." + func_name = "logo.heap." elif self.wants_tawindow(): func_name = "tw." # get the name of the function directly from the function itself @@ -468,8 +468,13 @@ class Primitive(object): text = ' ' + str(ast_to_value(new_arg_asts[0])) return ast_extensions.Comment(text) + # heap elif self == LogoCode.get_heap: - return TypedName(id_='heap', return_type=self.return_type) + return TypedName(id_='logo.heap', return_type=self.return_type) + elif self == LogoCode.reset_heap: + target_ast = ast.Name(id='logo.heap', ctx=ast.Store) + value_ast = ast.List(elts=[], ctx=ast.Load) + return ast.Assign(targets=[target_ast], value=value_ast) # NORMAL FUNCTION CALL # diff --git a/plugins/turtle_blocks_extras/turtle_blocks_extras.py b/plugins/turtle_blocks_extras/turtle_blocks_extras.py index 04f83ff..d986b85 100644 --- a/plugins/turtle_blocks_extras/turtle_blocks_extras.py +++ b/plugins/turtle_blocks_extras/turtle_blocks_extras.py @@ -501,8 +501,7 @@ end\n') logo_command='taclearheap', help_string=_('emptys FILO (first-in-last-out \ heap)')) - self.tw.lc.def_prim('clearheap', 0, - lambda self: primitive_dictionary['clearheap']()) + self.tw.lc.def_prim('clearheap', 0, Primitive(self.tw.lc.reset_heap)) define_logo_function('taclearheap', 'to taclearheap\nmake "taheap []\n\ end\n') @@ -530,7 +529,12 @@ make "tmp first :taheap\nmake "taheap butfirst :taheap\noutput :tmp\nend\n') value_block=True, help_string=_('returns True if heap is empty')) self.tw.lc.def_prim('isheapempty', 0, - lambda self: primitive_dictionary['isheapempty']()) + Primitive(int, return_type=TYPE_INT, + arg_descs=[ConstantArg( + Primitive(Primitive.not_, return_type=TYPE_BOOL, + arg_descs=[ConstantArg( + Primitive(self.tw.lc.get_heap, + return_type=TYPE_BOOL))]))])) primitive_dictionary['isheapempty2'] = self._prim_is_heap_empty_bool palette.add_block('isheapempty2', |