diff options
author | Marion <marion.zepf@gmail.com> | 2013-08-07 11:31:43 (GMT) |
---|---|---|
committer | Marion <marion.zepf@gmail.com> | 2013-08-07 11:31:43 (GMT) |
commit | 5fc1c13ca41bbb1832b4257d9e9b032b3abd287d (patch) | |
tree | 5baa433ae3e85f5f4291034f0c185f61fcc3df9a /TurtleArt | |
parent | d7da80ca0f67617d5c305aed1ef66e12e4d43e69 (diff) |
add Primitives for the 'pen state', 'while', and 'until' blocks
- The Primitives for the 'while' and 'until' blocks are only used to export
the blocks, not for running them.
Diffstat (limited to 'TurtleArt')
-rw-r--r-- | TurtleArt/tabasics.py | 2 | ||||
-rw-r--r-- | TurtleArt/taprimitive.py | 36 |
2 files changed, 31 insertions, 7 deletions
diff --git a/TurtleArt/tabasics.py b/TurtleArt/tabasics.py index 0181109..995481e 100644 --- a/TurtleArt/tabasics.py +++ b/TurtleArt/tabasics.py @@ -464,7 +464,7 @@ used in place of a number block)'), self.tw.lc.def_prim( 'penstate', 0, - lambda self: self.tw.turtles.get_active_turtle().get_pen_state()) + Primitive(Turtle.get_pen_state)) palette.add_block('setpensize', style='basic-style-1arg', diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index 29458ea..3bbfe0d 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -306,12 +306,21 @@ class Primitive(object): body=new_arg_asts[1:], orelse=[]) return loop_ast - elif controller == Primitive.controller_forever: - loop_ast = ast.While(test=ast.Name(id="True", ctx=ast.Load), + else: + if controller == Primitive.controller_forever: + condition_ast = ast.Name(id="True", ctx=ast.Load) + elif controller == Primitive.controller_while: + condition_ast = new_arg_asts[0].args[0] + elif controller == Primitive.controller_until: + condition_ast = ast.UnaryOp(op=ast.Not, + operand=new_arg_asts[0].args[0]) + else: + raise ValueError("unknown loop controller: " + + repr(controller)) + loop_ast = ast.While(test=condition_ast, body=new_arg_asts[1:], orelse=[]) return loop_ast - # TODO other loop blocks # standard operators elif self.func.__name__ in Primitive.STANDARD_OPERATORS: @@ -453,7 +462,22 @@ class Primitive(object): while True: yield True - LOOP_CONTROLLERS = [controller_repeat, controller_forever] + @staticmethod + def controller_while(boolean): + """ Loop controller for the 'while' block """ + while boolean: + yield True + yield False + + @staticmethod + def controller_until(boolean): + """ Loop controller for the 'until' block """ + while not boolean: + yield True + yield False + + LOOP_CONTROLLERS = [controller_repeat, controller_forever, + controller_while, controller_until] def _get_loop_controller(self): """ Return the controller for this loop Primitive. Raise a @@ -597,10 +621,10 @@ def value_to_ast(value, *args_for_prim, **kwargs_for_prim): return value.get_ast(*args_for_prim, **kwargs_for_prim) else: return None - elif isinstance(value, (int, float)): - return ast.Num(n=value) elif isinstance(value, bool): return ast.Name(id=str(value), ctx=ast.Load) + elif isinstance(value, (int, float)): + return ast.Num(n=value) elif isinstance(value, basestring): return ast.Str(value) elif isinstance(value, list): |