From 78fd1eca34d8c832b223b6da14aa16a3275ff183 Mon Sep 17 00:00:00 2001 From: Marion Date: Sun, 11 Aug 2013 12:45:18 +0000 Subject: add Primitives for the color blocks --- diff --git a/TurtleArt/tabasics.py b/TurtleArt/tabasics.py index 379718d..2b5b1fb 100644 --- a/TurtleArt/tabasics.py +++ b/TurtleArt/tabasics.py @@ -1483,4 +1483,5 @@ variable')) label=label, prim_name=block_name, logo_command=value) - self.tw.lc.def_prim(block_name, 0, lambda self: constant) + self.tw.lc.def_prim(block_name, 0, + Primitive(Primitive.identity, constant_args={0: constant})) diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index 7530aa5..4e0910e 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -24,6 +24,7 @@ from gettext import gettext as _ #from ast_pprint import * # only used for debugging, safe to comment out from tacanvas import TurtleGraphics +from taconstants import (Color, CONSTANTS) from talogo import LogoCode from taturtle import (Turtle, Turtles) from tautils import debug_output @@ -356,6 +357,14 @@ class Primitive(object): % (str(self.func.__func__.__name__), len(new_arg_asts))) + # identity + elif self == Primitive.identity: + if len(new_arg_asts) == 1: + return new_arg_asts[0] + else: + raise ValueError("Primitive.identity got unexpected number " + "of arguments (%d)" % (len(new_arg_asts))) + # tuples elif self == Primitive.make_tuple: if not new_kwarg_asts: @@ -521,6 +530,11 @@ class Primitive(object): pass @staticmethod + def identity(arg): + """ Return the argument unchanged """ + return arg + + @staticmethod def group(prim_list): """ Group together multiple Primitives into one. Treat each Primitive as a separate line of code. """ @@ -651,6 +665,18 @@ def value_to_ast(value, *args_for_prim, **kwargs_for_prim): if item_ast is not None: ast_list.append(item_ast) 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) + 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) else: raise ValueError("unknown type of raw value: " + repr(type(value))) -- cgit v0.9.1