From dfebf0be36908618c3b23839557dea93810b1198 Mon Sep 17 00:00:00 2001 From: Marion Date: Sun, 08 Sep 2013 14:12:50 +0000 Subject: Primitives for all constant blocks (add screen dimensions, update colors) - Use the get() method of the CONSTANTS dict to retrieve the values of constants, but export it as CONSTANTS['key']. - Introduce the new class TypedSubscript for Subscript ASTs with a type. --- diff --git a/TurtleArt/tabasics.py b/TurtleArt/tabasics.py index fc769b0..d84b169 100644 --- a/TurtleArt/tabasics.py +++ b/TurtleArt/tabasics.py @@ -1422,5 +1422,5 @@ variable')) prim_name=block_name, logo_command=value) self.tw.lc.def_prim(block_name, 0, - Primitive(Primitive.identity, return_type=TYPE_COLOR, - arg_descs=[ConstantArg(constant)])) + Primitive(CONSTANTS.get, return_type=TYPE_COLOR, + arg_descs=[ConstantArg(str(constant))])) diff --git a/TurtleArt/taexportpython.py b/TurtleArt/taexportpython.py index db00add..d41ec98 100644 --- a/TurtleArt/taexportpython.py +++ b/TurtleArt/taexportpython.py @@ -136,6 +136,7 @@ def _walk_action_stack(top_block, lc, convert_me=True): block = top_block # value blocks don't have a primitive + # (but constant blocks (colors, screen dimensions, etc.) do) if block.is_value_block(): raw_value = block.get_value(add_type_prefix=False) if convert_me: diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index 956f50d..f969e96 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -452,6 +452,12 @@ class Primitive(object): raise ValueError("Primitive.identity got unexpected number " "of arguments (%d)" % (len(new_arg_asts))) + # constant + elif self == CONSTANTS.get: + return TypedSubscript(value=ast.Name(id='CONSTANTS', ctx=ast.Load), + slice_=ast.Index(value=new_arg_asts[0]), + return_type=self.return_type) + # group of Primitives or sandwich-clamp block elif self in (Primitive.group, LogoCode.prim_clamp): ast_list = [] @@ -1052,17 +1058,11 @@ def value_to_ast(value, *args_for_prim, **kwargs_for_prim): ast_list.append(item_ast) return ast.List(elts=ast_list, ctx=ast.Load) elif isinstance(value, Color): - if str(value) in CONSTANTS: - # repr(str(value)) is necessary; it first converts the Color to a - # string and then adds appropriate quotes around that string - return ast.Name(id='CONSTANTS[%s]' % repr(str(value)), - ctx=ast.Load) - else: - # call to the Color constructor with this object's values, - # e.g., Color('red', 0, 50, 100) - return get_call_ast('Color', [value.name, value.color, - value.shade, value.gray], - return_type=TYPE_COLOR) + # call to the Color constructor with this object's values, + # e.g., Color('red', 0, 50, 100) + return get_call_ast('Color', [value.name, value.color, + value.shade, value.gray], + return_type=TYPE_COLOR) else: raise ValueError("unknown type of raw value: " + repr(type(value))) diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py index 752f71b..9a8c17d 100644 --- a/TurtleArt/tatype.py +++ b/TurtleArt/tatype.py @@ -350,7 +350,17 @@ def convert(x, new_type, old_type=None, converter=None): return _apply_converter(converter, x) -class TypedCall(ast.Call): +class TypedAST(ast.AST): + + @property + def return_type(self): + if self._return_type is None: + return get_type(self.func)[0] + else: + return self._return_type + + +class TypedCall(ast.Call,TypedAST): """ Like a Call AST, but with a return type """ def __init__(self, func, args=None, keywords=None, starargs=None, @@ -366,12 +376,15 @@ class TypedCall(ast.Call): self._return_type = return_type - @property - def return_type(self): - if self._return_type is None: - return get_type(self.func)[0] - else: - return self._return_type + +class TypedSubscript(ast.Subscript,TypedAST): + """ Like a Subscript AST, but with a return type """ + + def __init__(self, value, slice_, ctx=ast.Load, return_type=None): + + ast.Subscript.__init__(self, value=value, slice=slice_, ctx=ctx) + + self._return_type = return_type def get_call_ast(func_name, args=None, kwargs=None, return_type=None): diff --git a/plugins/turtle_blocks_extras/turtle_blocks_extras.py b/plugins/turtle_blocks_extras/turtle_blocks_extras.py index 041dc24..8538d89 100644 --- a/plugins/turtle_blocks_extras/turtle_blocks_extras.py +++ b/plugins/turtle_blocks_extras/turtle_blocks_extras.py @@ -880,7 +880,9 @@ Journal objects')) prim_name='lpos', logo_command='lpos', help_string=_('xcor of left of screen')) - self.tw.lc.def_prim('lpos', 0, lambda self: CONSTANTS['leftpos']) + self.tw.lc.def_prim('lpos', 0, + Primitive(CONSTANTS.get, return_type=TYPE_INT, + arg_descs=[ConstantArg('leftpos')])) palette.add_block('bottompos', style='box-style', @@ -888,7 +890,9 @@ Journal objects')) prim_name='bpos', logo_command='bpos', help_string=_('ycor of bottom of screen')) - self.tw.lc.def_prim('bpos', 0, lambda self: CONSTANTS['bottompos']) + self.tw.lc.def_prim('bpos', 0, + Primitive(CONSTANTS.get, return_type=TYPE_INT, + arg_descs=[ConstantArg('bottompos')])) palette.add_block('width', style='box-style', @@ -896,7 +900,9 @@ Journal objects')) prim_name='hres', logo_command='width', help_string=_('the canvas width')) - self.tw.lc.def_prim('hres', 0, lambda self: CONSTANTS['width']) + self.tw.lc.def_prim('hres', 0, + Primitive(CONSTANTS.get, return_type=TYPE_INT, + arg_descs=[ConstantArg('width')])) palette.add_block('rightpos', style='box-style', @@ -904,7 +910,9 @@ Journal objects')) prim_name='rpos', logo_command='rpos', help_string=_('xcor of right of screen')) - self.tw.lc.def_prim('rpos', 0, lambda self: CONSTANTS['rightpos']) + self.tw.lc.def_prim('rpos', 0, + Primitive(CONSTANTS.get, return_type=TYPE_INT, + arg_descs=[ConstantArg('rightpos')])) palette.add_block('toppos', style='box-style', @@ -912,7 +920,9 @@ Journal objects')) prim_name='tpos', logo_command='tpos', help_string=_('ycor of top of screen')) - self.tw.lc.def_prim('tpos', 0, lambda self: CONSTANTS['toppos']) + self.tw.lc.def_prim('tpos', 0, + Primitive(CONSTANTS.get, return_type=TYPE_INT, + arg_descs=[ConstantArg('toppos')])) palette.add_block('height', style='box-style', @@ -920,7 +930,9 @@ Journal objects')) prim_name='vres', logo_command='height', help_string=_('the canvas height')) - self.tw.lc.def_prim('vres', 0, lambda self: CONSTANTS['height']) + self.tw.lc.def_prim('vres', 0, + Primitive(CONSTANTS.get, return_type=TYPE_INT, + arg_descs=[ConstantArg('height')])) palette.add_block('titlex', hidden=True, @@ -929,7 +941,9 @@ Journal objects')) label=_('title x'), logo_command='titlex', prim_name='titlex') - self.tw.lc.def_prim('titlex', 0, lambda self: CONSTANTS['titlex']) + self.tw.lc.def_prim('titlex', 0, + Primitive(CONSTANTS.get, return_type=TYPE_INT, + arg_descs=[ConstantArg('titlex')])) palette.add_block('titley', hidden=True, @@ -938,7 +952,9 @@ Journal objects')) label=_('title y'), logo_command='titley', prim_name='titley') - self.tw.lc.def_prim('titley', 0, lambda self: CONSTANTS['titley']) + self.tw.lc.def_prim('titley', 0, + Primitive(CONSTANTS.get, return_type=TYPE_INT, + arg_descs=[ConstantArg('titley')])) palette.add_block('leftx', hidden=True, @@ -947,7 +963,9 @@ Journal objects')) label=_('left x'), prim_name='leftx', logo_command='leftx') - self.tw.lc.def_prim('leftx', 0, lambda self: CONSTANTS['leftx']) + self.tw.lc.def_prim('leftx', 0, + Primitive(CONSTANTS.get, return_type=TYPE_INT, + arg_descs=[ConstantArg('leftx')])) palette.add_block('topy', hidden=True, @@ -956,7 +974,9 @@ Journal objects')) label=_('top y'), prim_name='topy', logo_command='topy') - self.tw.lc.def_prim('topy', 0, lambda self: CONSTANTS['topy']) + self.tw.lc.def_prim('topy', 0, + Primitive(CONSTANTS.get, return_type=TYPE_INT, + arg_descs=[ConstantArg('topy')])) palette.add_block('rightx', hidden=True, @@ -965,7 +985,9 @@ Journal objects')) label=_('right x'), prim_name='rightx', logo_command='rightx') - self.tw.lc.def_prim('rightx', 0, lambda self: CONSTANTS['rightx']) + self.tw.lc.def_prim('rightx', 0, + Primitive(CONSTANTS.get, return_type=TYPE_INT, + arg_descs=[ConstantArg('rightx')])) palette.add_block('bottomy', hidden=True, @@ -974,7 +996,9 @@ Journal objects')) label=_('bottom y'), prim_name='bottomy', logo_command='bottomy') - self.tw.lc.def_prim('bottomy', 0, lambda self: CONSTANTS['bottomy']) + self.tw.lc.def_prim('bottomy', 0, + Primitive(CONSTANTS.get, return_type=TYPE_INT, + arg_descs=[ConstantArg('bottomy')])) def _myblocks_palette(self): ''' User-defined macros are saved as a json-encoded file; diff --git a/util/codegen.py b/util/codegen.py index 5f768f8..6e4d383 100644 --- a/util/codegen.py +++ b/util/codegen.py @@ -477,6 +477,10 @@ class SourceGenerator(NodeVisitor): self.write('[') self.visit(node.slice) self.write(']') + visit_TypedSubscript = visit_Subscript + + def visit_Index(self, node): + self.visit(node.value) def visit_Slice(self, node): if node.lower is not None: -- cgit v0.9.1