From b645b5525d78cee4e348255c8b25d9867a2427c0 Mon Sep 17 00:00:00 2001 From: Marion Date: Thu, 29 Aug 2013 13:12:10 +0000 Subject: get rid of the tuple containing all types of the hierarchy - To check whether something is a type, use isinstance(x, Type). --- (limited to 'TurtleArt/tatype.py') diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py index 6b0acd6..2c78c92 100644 --- a/TurtleArt/tatype.py +++ b/TurtleArt/tatype.py @@ -57,12 +57,7 @@ TYPE_NUMERIC_STRING = Type('numeric string', 7) TYPE_POSITIVE = Type('positive', 8) TYPE_STRING = Type('string', 9) TYPE_ZERO = Type('zero', 10) -ARG_TYPES = ( - # possible types of arguments to Primitives - TYPE_CHAR, TYPE_COLOR, TYPE_FLOAT, TYPE_INT, TYPE_NEGATIVE, TYPE_NUMBER, - TYPE_NUMERIC_STRING, TYPE_OBJECT, TYPE_POSITIVE, TYPE_STRING, TYPE_ZERO - # TODO add list types -) +# TODO add list types def get_type(x): @@ -105,9 +100,9 @@ def get_type(x): else: return (get_type(value)[0], True) elif isinstance(x, ast.Call): - if x.func.__name__ in (TYPE_FLOAT, TYPE_INT): + if x.func.__name__ in ('float', 'int'): return (x.func.__name__, True) - elif x.func.__name__ in ('str', 'unicode'): + elif x.func.__name__ in ('repr', 'str', 'unicode'): return (TYPE_STRING, True) return (TYPE_OBJECT, isinstance(x, ast.AST)) @@ -268,7 +263,7 @@ def get_converter(old_type, new_type): if new_type in backtrace: converter_chain = [] t = new_type - while t in backtrace and backtrace[t] in ARG_TYPES: + while t in backtrace and isinstance(backtrace[t], Type): converter_chain.insert(0, TYPE_CONVERTERS[backtrace[t]][t]) t = backtrace[t] return converter_chain @@ -278,13 +273,13 @@ def get_converter(old_type, new_type): def convert(x, new_type, old_type=None, converter=None): """ Convert x to the new type if possible. old_type -- the type of x. If not given, it is computed. """ - if new_type not in ARG_TYPES: + if not isinstance(new_type, Type): raise ValueError('%s is not a type in the type hierarchy' % (repr(new_type))) # every type can be converted to TYPE_OBJECT if new_type == TYPE_OBJECT: return x - if old_type not in ARG_TYPES: + if not isinstance(old_type, Type): (old_type, is_an_ast) = get_type(x) else: is_an_ast = isinstance(x, ast.AST) @@ -324,7 +319,7 @@ def convert(x, new_type, old_type=None, converter=None): # apply the converter chain recursively result = x for conv in converter: - result = _apply_converter(converter, result) + result = _apply_converter(conv, result) return result elif converter is not None: return _apply_converter(converter, x) -- cgit v0.9.1