From 5433f0e9ff35f5f3d123e844ea2f0673aa5ff01c Mon Sep 17 00:00:00 2001 From: Marion Date: Thu, 15 Aug 2013 19:11:09 +0000 Subject: add special handling for the 'box' and 'store-in' blocks when exporting them --- 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), -- cgit v0.9.1