diff options
author | Marion <marion.zepf@gmail.com> | 2013-09-04 09:32:17 (GMT) |
---|---|---|
committer | Marion <marion.zepf@gmail.com> | 2013-09-04 09:32:17 (GMT) |
commit | e18c96fcd0265867757e2d1a09dab608aaa5fab8 (patch) | |
tree | e6353349557e4c305c3a13d008177b95b6fb0b92 | |
parent | 6ee1bb7f1a827d2b6e1d898c63d8017fa95d7222 (diff) |
fix exporting the 'store in' and 'box' blocks
- Use proper ast.Subscript objects to represent `BOX['my box']`
- Enable type guessing for these ASTs
-rw-r--r-- | TurtleArt/taprimitive.py | 27 | ||||
-rw-r--r-- | TurtleArt/tatype.py | 9 |
2 files changed, 24 insertions, 12 deletions
diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index cf15ca5..e0db4fd 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -28,8 +28,8 @@ from tacanvas import TurtleGraphics from taconstants import (Color, CONSTANTS) from talogo import (LogoCode, logoerror) from taturtle import (Turtle, Turtles) -from tatype import (convert, get_call_ast, get_converter, get_type, - is_bound_instancemethod, is_instancemethod, +from tatype import (ACTION_AST, BOX_AST, convert, get_call_ast, get_converter, + get_type, is_bound_instancemethod, is_instancemethod, is_staticmethod, TATypeError, Type, TypeDisjunction, TYPE_FLOAT, TYPE_OBJECT) from tautils import debug_output @@ -352,20 +352,21 @@ class Primitive(object): # 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 + target_ast = ast.Subscript(value=BOX_AST, + slice=ast.Index(value=new_arg_asts[0]), + ctx=ast.Store) + return ast.Assign(targets=[target_ast], value=new_arg_asts[1]) 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) + return ast.Subscript(value=BOX_AST, + slice=ast.Index(value=new_arg_asts[0]), + ctx=ast.Load) # action stacks elif self == LogoCode.prim_define_stack: return elif self == LogoCode.prim_invoke_stack: stack_name = ast_to_value(new_arg_asts[0]) + # TODO use ast.Subscript stack_func_name = 'ACTION[%s]' % (repr(stack_name)) stack_func = ast.Name(id=stack_func_name, ctx=ast.Load) return get_call_ast('logo.icall', [stack_func]) @@ -1004,8 +1005,8 @@ class ArgSlot(object): # 3. check the type and convert the argument if necessary try: - converted_argument = convert(wrapped_argument, new_type, - converter=converter) + converted_argument = convert(wrapped_argument, + new_type, old_type=old_type, converter=converter) except TATypeError as error: # on failure, try next wrapper/ slot/ func bad_value = wrapped_argument @@ -1115,7 +1116,9 @@ 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): + if not isinstance(ast_object, ast.AST): + return ast_object + elif isinstance(ast_object, ast.Num): return ast_object.n elif isinstance(ast_object, ast.Str): return ast_object.s diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py index 86119b6..0fcd74c 100644 --- a/TurtleArt/tatype.py +++ b/TurtleArt/tatype.py @@ -77,6 +77,9 @@ TYPE_STRING = Type('string', 9) # TODO add list types +BOX_AST = ast.Name(id='BOX', ctx=ast.Load) +ACTION_AST = ast.Name(id='ACTION', ctx=ast.Load) + def get_type(x): """ Return the most specific type in the type hierarchy that applies to x and a boolean indicating whether x is an AST. If the type cannot be @@ -113,6 +116,12 @@ def get_type(x): return (TYPE_OBJECT, True) else: return (get_type(value)[0], True) + elif isinstance(x, ast.Subscript): + if x.value == BOX_AST: + return (TypeDisjunction((TYPE_OBJECT, TYPE_STRING, TYPE_NUMBER, + TYPE_FLOAT, TYPE_INT, TYPE_NUMERIC_STRING, TYPE_CHAR, + TYPE_COLOR)), True) + # TODO elif x.value == ACTION_AST: elif isinstance(x, ast.Call): if isinstance(x.func, ast.Name): if x.func.id == 'float': |