diff options
author | Marion <marion.zepf@gmail.com> | 2013-08-29 13:12:10 (GMT) |
---|---|---|
committer | Marion <marion.zepf@gmail.com> | 2013-08-29 13:12:10 (GMT) |
commit | b645b5525d78cee4e348255c8b25d9867a2427c0 (patch) | |
tree | 1a653c5f507374adb5f25acb443927ecb8483cde /TurtleArt/tatype.py | |
parent | f58dc4d4cb86c0085a542301a05ba77443c2c543 (diff) |
get rid of the tuple containing all types of the hierarchy
- To check whether something is a type, use isinstance(x, Type).
Diffstat (limited to 'TurtleArt/tatype.py')
-rw-r--r-- | TurtleArt/tatype.py | 19 |
1 files changed, 7 insertions, 12 deletions
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) |