From 991b4f4697eeb14f1a0c55fd714863cf4bbf90de Mon Sep 17 00:00:00 2001 From: Marion Date: Thu, 15 Aug 2013 19:28:44 +0000 Subject: use utility function for creating Call ASTs instead of ast.Call --- (limited to 'TurtleArt/taprimitive.py') diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index 7c34803..e05460b 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -320,11 +320,7 @@ class Primitive(object): num_repetitions = new_arg_asts[0] if num_repetitions.func.id == 'controller_repeat': num_repetitions = num_repetitions.args[0] - repeat_iter = ast.Call(func=ast.Name(id="range", ctx=ast.Load), - args=[num_repetitions], - keywords={}, - starargs=None, - kwargs=None) + repeat_iter = get_call_ast("range", [num_repetitions]) # TODO use new variable name in nested loops loop_ast = ast.For(target=ast.Name(id="i", ctx=ast.Store), iter=repeat_iter, @@ -358,13 +354,13 @@ class Primitive(object): if_ast = ast.If(test=test, body=body, orelse=orelse) return if_ast + # boxes 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) @@ -442,11 +438,7 @@ class Primitive(object): # get the name of the function directly from the function itself func_name += self.func.__name__ - return ast.Call(func=ast.Name(id=func_name, ctx=ast.Load), - args=new_arg_asts, - keywords=new_kwarg_asts, - starargs=None, - kwargs=None) + return get_call_ast(func_name, new_arg_asts, new_kwarg_asts) def __eq__(self, other): """ Two Primitives are equal iff their all their properties are equal. @@ -731,12 +723,7 @@ class Primitive(object): elif isinstance(value, Color): converted = float(value) if convert_to_ast: - conversion_ast = ast.Call(func=ast.Name(id='float', - ctx=ast.Load), - args=[value_ast], - keywords={}, - starargs=None, - kwargs=None) + conversion_ast = get_call_ast('float', [value_ast]) else: return None @@ -940,12 +927,8 @@ def value_to_ast(value, *args_for_prim, **kwargs_for_prim): else: # call to the Color constructor with this object's values, # e.g., Color('red', 0, 50, 100) - return ast.Call(func=ast.Name(id='Color', ctx=ast.Load), - args=[value.name, value.color, value.shade, - value.gray], - keywords={}, - starargs=None, - kwargs=None) + return get_call_ast('Color', [value.name, value.color, + value.shade, value.gray]) else: raise ValueError("unknown type of raw value: " + repr(type(value))) @@ -968,7 +951,7 @@ def ast_to_value(ast_object): 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