diff options
author | Marion <marion.zepf@gmail.com> | 2013-09-04 09:55:39 (GMT) |
---|---|---|
committer | Marion <marion.zepf@gmail.com> | 2013-09-04 09:55:39 (GMT) |
commit | 795c89415ea2f79ddb52ca287c682717a6781829 (patch) | |
tree | ac002840d05ce7881c82b15c8520e7ae88670e20 | |
parent | e18c96fcd0265867757e2d1a09dab608aaa5fab8 (diff) |
update Primitives for 'hat' (top of an action stack) and 'stack' blocks
- Insert 'yield True' after every call to logo.icall(...) in the exported
code.
-rw-r--r-- | TurtleArt/tabasics.py | 18 | ||||
-rw-r--r-- | TurtleArt/taexportpython.py | 10 | ||||
-rw-r--r-- | TurtleArt/taprimitive.py | 18 | ||||
-rw-r--r-- | TurtleArt/tatype.py | 1 |
4 files changed, 23 insertions, 24 deletions
diff --git a/TurtleArt/tabasics.py b/TurtleArt/tabasics.py index ed7c337..e71b226 100644 --- a/TurtleArt/tabasics.py +++ b/TurtleArt/tabasics.py @@ -980,9 +980,12 @@ buttons')) default=_('action'), logo_command='to action', help_string=_('top of nameable action stack')) - self.tw.lc.def_prim('nop3', 1, Primitive(self.tw.lc.prim_define_stack)) + self.tw.lc.def_prim('nop3', 1, + Primitive(self.tw.lc.prim_define_stack, + arg_descs=[ArgSlot(TYPE_STRING)])) - primitive_dictionary['stack'] = Primitive(self.tw.lc.prim_invoke_stack) + primitive_dictionary['stack'] = Primitive(self.tw.lc.prim_invoke_stack, + arg_descs=[ArgSlot(TYPE_STRING)]) palette.add_block('stack', style='basic-style-1arg', label=_('action'), @@ -991,8 +994,7 @@ buttons')) logo_command='action', default=_('action'), help_string=_('invokes named action stack')) - self.tw.lc.def_prim('stack', 1, - Primitive(self.tw.lc.prim_invoke_stack), True) + self.tw.lc.def_prim('stack', 1, primitive_dictionary['stack'], True) palette.add_block('storeinbox1', hidden=True, @@ -1082,7 +1084,7 @@ variable')) help_string=_('top of Action 1 stack')) self.tw.lc.def_prim('nop1', 0, Primitive(self.tw.lc.prim_define_stack, - constant_args={0: 'stack1'})) + arg_descs=[ConstantArg('stack1')])) palette.add_block('hat2', hidden=True, @@ -1093,7 +1095,7 @@ variable')) help_string=_('top of Action 2 stack')) self.tw.lc.def_prim('nop2', 0, Primitive(self.tw.lc.prim_define_stack, - constant_args={0: 'stack2'})) + arg_descs=[ConstantArg('stack2')])) palette.add_block('stack1', hidden=True, @@ -1104,7 +1106,7 @@ variable')) help_string=_('invokes Action 1 stack')) self.tw.lc.def_prim('stack1', 0, Primitive(self.tw.lc.prim_invoke_stack, - constant_args={0: 'stack1'}), + arg_descs=[ConstantArg('stack1')]), True) palette.add_block('stack2', @@ -1116,7 +1118,7 @@ variable')) help_string=_('invokes Action 2 stack')) self.tw.lc.def_prim('stack2', 0, Primitive(self.tw.lc.prim_invoke_stack, - constant_args={0: 'stack2'}), + arg_descs=[ConstantArg('stack2')]), True) def _trash_palette(self): diff --git a/TurtleArt/taexportpython.py b/TurtleArt/taexportpython.py index 71c4f9b..405e683 100644 --- a/TurtleArt/taexportpython.py +++ b/TurtleArt/taexportpython.py @@ -30,7 +30,8 @@ import util.codegen as codegen #from ast_pprint import * # only used for debugging, safe to comment out from talogo import LogoCode -from taprimitive import (Primitive, PyExportError, value_to_ast) +from taprimitive import (ast_yield_true, Primitive, PyExportError, + value_to_ast) from tautils import (debug_output, find_group, find_top_block, get_stack_name) @@ -106,7 +107,7 @@ def _action_stack_to_python(block, lc, name="start"): name -- the name of the action stack (defaults to "start") """ # traverse the block stack and get the AST for every block ast_list = _walk_action_stack(block, lc) - ast_list.append(_ast_yield_true()) + ast_list.append(ast_yield_true()) action_stack_ast = ast.Module(body=ast_list) #debug_output(str(action_stack_ast)) @@ -208,7 +209,7 @@ def _walk_action_stack(top_block, lc, convert_me=True): new_arg_asts = _walk_action_stack(conn, lc, convert_me=convert_me) if prim == LogoCode.prim_loop: - new_arg_asts.append(_ast_yield_true()) + new_arg_asts.append(ast_yield_true()) arg_asts.append(new_arg_asts) else: # argument block @@ -239,7 +240,4 @@ def _indent(code, num_levels=1): new_line_list.append(indentation + line) return linesep.join(new_line_list) -def _ast_yield_true(): - return ast.Yield(value=ast.Name(id='True', ctx=ast.Load)) - diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index e0db4fd..f303910 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -353,23 +353,20 @@ class Primitive(object): # boxes elif self == LogoCode.prim_set_box: target_ast = ast.Subscript(value=BOX_AST, - slice=ast.Index(value=new_arg_asts[0]), - ctx=ast.Store) + slice=ast.Index(value=new_arg_asts[0]), ctx=ast.Store) return ast.Assign(targets=[target_ast], value=new_arg_asts[1]) elif self == LogoCode.prim_get_box: return ast.Subscript(value=BOX_AST, - slice=ast.Index(value=new_arg_asts[0]), - ctx=ast.Load) + slice=ast.Index(value=new_arg_asts[0]), ctx=ast.Load) # action stacks elif self == LogoCode.prim_define_stack: return elif self == LogoCode.prim_invoke_stack: - stack_name = ast_to_value(new_arg_asts[0]) - # TODO use ast.Subscript - stack_func_name = 'ACTION[%s]' % (repr(stack_name)) - stack_func = ast.Name(id=stack_func_name, ctx=ast.Load) - return get_call_ast('logo.icall', [stack_func]) + stack_func = ast.Subscript(value=ACTION_AST, + slice=ast.Index(value=new_arg_asts[0]), ctx=ast.Load) + call_ast = get_call_ast('logo.icall', [stack_func]) + return [call_ast, ast_yield_true()] # standard operators elif self.func.__name__ in Primitive.STANDARD_OPERATORS: @@ -1132,6 +1129,9 @@ def ast_to_value(ast_object): else: return None +def ast_yield_true(): + return ast.Yield(value=ast.Name(id='True', ctx=ast.Load)) + def export_me(something): """ Return True iff this is not a Primitive or its export_me attribute diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py index 0fcd74c..b2b14f4 100644 --- a/TurtleArt/tatype.py +++ b/TurtleArt/tatype.py @@ -121,7 +121,6 @@ def get_type(x): return (TypeDisjunction((TYPE_OBJECT, TYPE_STRING, TYPE_NUMBER, TYPE_FLOAT, TYPE_INT, TYPE_NUMERIC_STRING, TYPE_CHAR, TYPE_COLOR)), True) - # TODO elif x.value == ACTION_AST: elif isinstance(x, ast.Call): if isinstance(x.func, ast.Name): if x.func.id == 'float': |