From 41e58dde24b5a93496edf30b35ff963fc53d48b9 Mon Sep 17 00:00:00 2001 From: Marion Date: Sun, 11 Aug 2013 22:25:20 +0000 Subject: construct proper ASTs for type conversion --- diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index 8171c43..baaa9e0 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -361,15 +361,9 @@ class Primitive(object): % (str(self.func.__func__.__name__), len(new_arg_asts))) + # type conversion elif self == Primitive.convert_for_cmp: - # remove the 'convert_to_ast' keyword argument - my_kwarg_asts = {} - for (key, value) in new_kwarg_asts.iteritems(): - if key != 'convert_to_ast': - my_kwarg_asts[key] = value - return Primitive.convert_for_cmp(*new_arg_asts, - convert_to_ast=True, - **my_kwarg_asts) + return Primitive.convert_for_cmp(*new_arg_asts, **new_kwarg_asts) # identity elif self == Primitive.identity: @@ -630,15 +624,25 @@ class Primitive(object): return not arg @staticmethod - def convert_for_cmp(value, decimal_point='.', convert_to_ast=False): - """ Convert value such that it can be compared to something else. + def convert_for_cmp(value, decimal_point='.'): + """ Convert value such that it can be compared to something else. If + value is an AST, another AST is wrapped around it to represent the + conversion, e.g., + Str(s='a') -> Call(func=Name('ord'), args=[Str(s='a')]) 1. Convert a string containing a number into a float. 2. Convert a single character to its ASCII integer value. 3. Return all other values unchanged. """ converted = None conversion_ast = None - if convert_to_ast: - value_ast = value_to_ast(value) + convert_to_ast = False + if isinstance(value, ast.AST): + convert_to_ast = True + value_ast = value + value = None + if isinstance(value_ast, ast.Str): + value = value_ast.s + if isinstance(decimal_point, ast.Str): + decimal_point = decimal_point.s if isinstance(value, basestring): # 1. string containing a number -- cgit v0.9.1