diff options
author | Marion <marion.zepf@gmail.com> | 2013-08-03 18:44:29 (GMT) |
---|---|---|
committer | Marion <marion.zepf@gmail.com> | 2013-08-03 18:44:29 (GMT) |
commit | 1445dc528d9c03d720918d11424e8ea9b02b5ecf (patch) | |
tree | f9509f210fb0e3b52dbe283e8acf685f3a6298a9 /TurtleArt/taprimitive.py | |
parent | 154625db431ebe252e8ddaeb9e45dbc07d1c4329 (diff) |
add Primitives for the 'xcor' and 'ycor' blocks
Diffstat (limited to 'TurtleArt/taprimitive.py')
-rw-r--r-- | TurtleArt/taprimitive.py | 65 |
1 files changed, 46 insertions, 19 deletions
diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index dc56081..d0431ee 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -185,11 +185,13 @@ class Primitive(object): return (new_args, new_kwargs) def _add_constant_args(self, runtime_args, runtime_kwargs, - raw_to_ast=False): + convert_to_ast=False): """ Add the constant args and kwargs to the given runtime args and kwargs. Return a list containing all args and a dictionary with all kwargs. - raw_to_ast -- convert raw booleans, numbers, strings, etc. to ASTs? """ + PrimitiveCall objects are called and their return value is used as + the argument. + convert_to_ast -- convert all constant arguments to ASTs? """ all_args = [] all_kwargs = runtime_kwargs.copy() @@ -198,9 +200,14 @@ class Primitive(object): def _insert_c_args(i): while i in self.constant_args: c_arg = self.constant_args[i] - if raw_to_ast: - c_arg = value_to_ast(c_arg) - all_args.append(c_arg) + if convert_to_ast: + c_arg_ast = value_to_ast(c_arg) + if c_arg_ast is not None: + all_args.append(c_arg_ast) + elif isinstance(c_arg, PrimitiveCall): + all_args.append(c_arg()) + else: + all_args.append(c_arg) i += 1 return i for arg in runtime_args: @@ -210,11 +217,16 @@ class Primitive(object): i = _insert_c_args(i) # kwargs - for (k, v) in self.constant_args.iteritems(): - if isinstance(k, basestring): - if raw_to_ast: - v = value_to_ast(v) - all_kwargs[k] = v + for (key, value) in self.constant_args.iteritems(): + if isinstance(key, basestring): + if convert_to_ast: + value_ast = value_to_ast(value) + if value_ast is not None: + all_kwargs[key] = value_ast + elif isinstance(value, PrimitiveCall): + all_kwargs[key] = value() + else: + all_kwargs[key] = value return (all_args, all_kwargs) @@ -266,7 +278,7 @@ class Primitive(object): # constant arguments (all_arg_asts, all_kwarg_asts) = self._add_constant_args(arg_asts, - kwarg_asts, raw_to_ast=True) + kwarg_asts, convert_to_ast=True) # slot wrappers (new_arg_asts, new_kwarg_asts) = self._apply_wrappers(all_arg_asts, @@ -550,6 +562,13 @@ class Primitive(object): +class PrimitiveCall(Primitive): + """ Primitive that is called when it is a constant argument to another + Primitive. """ + pass + + + def is_instancemethod(method): # TODO how to access the type `instancemethod` directly? return type(method).__name__ == "instancemethod" @@ -566,11 +585,20 @@ def is_staticmethod(method): def value_to_ast(value): - """ Turn a value into an AST. Supported types: int, float, bool, - basestring, list """ + """ Turn a value into an AST. Supported types: Primitive, int, float, bool, + basestring, list + If the value is already an AST, return it unchanged. + If the value is a non-exportable Primitive, return None. """ # TODO media - if isinstance(value, (int, float)): - return ast.Num(value) + if isinstance(value, ast.AST): + return value + elif isinstance(value, Primitive): + if value.export_me: + return value.get_ast() + 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, basestring): @@ -578,10 +606,9 @@ def value_to_ast(value): elif isinstance(value, list): ast_list = [] for item in value: - if isinstance(item, Primitive): - ast_list.append(item.get_ast()) - else: - ast_list.append(value_to_ast(item)) + item_ast = value_to_ast(item) + if item_ast is not None: + ast_list.append(item_ast) return ast.List(elts=ast_list, ctx=ast.Load) else: raise ValueError("unknown type of raw value: " + repr(type(value))) |