Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarion <marion.zepf@gmail.com>2013-08-15 19:11:09 (GMT)
committer Marion <marion.zepf@gmail.com>2013-08-15 19:11:09 (GMT)
commit5433f0e9ff35f5f3d123e844ea2f0673aa5ff01c (patch)
tree6e7e47f8ed44ca77c148c3839c6ebca2747dacaf
parent63ea8f8170375392e04a7cf2bb3f6802cc8be59e (diff)
add special handling for the 'box' and 'store-in' blocks when exporting them
-rw-r--r--TurtleArt/taprimitive.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py
index c2a4cf6..7c34803 100644
--- a/TurtleArt/taprimitive.py
+++ b/TurtleArt/taprimitive.py
@@ -358,6 +358,17 @@ class Primitive(object):
if_ast = ast.If(test=test, body=body, orelse=orelse)
return if_ast
+ elif self == LogoCode.prim_set_box:
+ id_str = 'BOX[%s]' % (repr(ast_to_value(new_arg_asts[0])))
+ target_ast = ast.Name(id=id_str, ctx=ast.Store)
+ value_ast = new_arg_asts[1]
+ assign_ast = ast.Assign(targets=[target_ast], value=value_ast)
+ return assign_ast
+
+ elif self == LogoCode.prim_get_box:
+ id_str = 'BOX[%s]' % (repr(ast_to_value(new_arg_asts[0])))
+ return ast.Name(id=id_str, ctx=ast.Load)
+
# standard operators
elif self.func.__name__ in Primitive.STANDARD_OPERATORS:
op = Primitive.STANDARD_OPERATORS[self.func.__name__]
@@ -925,7 +936,7 @@ def value_to_ast(value, *args_for_prim, **kwargs_for_prim):
return ast.List(elts=ast_list, ctx=ast.Load)
elif isinstance(value, Color):
if str(value) in CONSTANTS:
- return ast.Name(id='CONSTANTS["%s"]' % str(value), ctx=ast.Load)
+ return ast.Name(id='CONSTANTS[%s]' % repr(value), ctx=ast.Load)
else:
# call to the Color constructor with this object's values,
# e.g., Color('red', 0, 50, 100)
@@ -938,7 +949,26 @@ def value_to_ast(value, *args_for_prim, **kwargs_for_prim):
else:
raise ValueError("unknown type of raw value: " + repr(type(value)))
+# TODO replace every fiddling with a call to this function
+def ast_to_value(ast_object):
+ """ Retrieve the value out of a value AST. Supported AST types:
+ Num, Str, Name, List, Tuple, Set
+ If no value can be extracted, return None. """
+ if isinstance(ast_object, ast.Num):
+ return ast_object.n
+ elif isinstance(ast_object, ast.Str):
+ return ast_object.s
+ elif isinstance(ast_object, (ast.List, ast.Tuple, ast.Set)):
+ return ast_object.elts
+ elif (isinstance(ast_object, ast.Name)):
+ try:
+ return eval(ast_object.id)
+ except NameError:
+ return None
+ else:
+ return None
+# TODO replace every call to ast.Call with a call to this function
def get_call_ast(func_name, args=[], keywords={}):
return ast.Call(func=ast.Name(id=func_name,
ctx=ast.Load),