From 5bb8683e7489c240e81f90b9555d7ada47008dfa Mon Sep 17 00:00:00 2001 From: Marion Date: Sun, 01 Sep 2013 14:39:06 +0000 Subject: remove types 'positive', 'negative', and 'zero' - These fine value-dependent distinctions should be made at runtime, not during type checking. - Fix a small bug in the algorithm to find a converter given two types. --- (limited to 'TurtleArt/tatype.py') diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py index 3fa7a83..14f1c9e 100644 --- a/TurtleArt/tatype.py +++ b/TurtleArt/tatype.py @@ -57,12 +57,10 @@ TYPE_CHAR = Type('char', 1) TYPE_COLOR = Type('color', 2) TYPE_FLOAT = Type('float', 3) TYPE_INT = Type('int', 4) -TYPE_NEGATIVE = Type('negative', 5) -TYPE_NUMBER = Type('number', 6) +TYPE_NUMBER = Type('number', 6) # shortcut to avoid a TypeDisjunction between +# TYPE_FLOAT and TYPE_INT TYPE_NUMERIC_STRING = Type('numeric string', 7) -TYPE_POSITIVE = Type('positive', 8) TYPE_STRING = Type('string', 9) -TYPE_ZERO = Type('zero', 10) # TODO add list types @@ -71,13 +69,10 @@ def get_type(x): and a boolean indicating whether x is an AST. If the type cannot be determined, return TYPE_OBJECT as the type. """ # non-AST types - if isinstance(x, (float, int, long)): - if x > 0: - return (TYPE_POSITIVE, False) - elif x == 0: - return (TYPE_ZERO, False) - else: - return (TYPE_NEGATIVE, False) + if isinstance(x, (int, long)): + return (TYPE_INT, False) + elif isinstance(x, float): + return (TYPE_FLOAT, False) elif isinstance(x, basestring): if len(x) == 1: return (TYPE_CHAR, False) @@ -107,8 +102,10 @@ def get_type(x): return (get_type(value)[0], True) elif isinstance(x, ast.Call): if isinstance(x.func, ast.Name): - if x.func.id in ('float', 'int'): - return (x.func.__name__, True) + if x.func.id == 'float': + return (TYPE_FLOAT, True) + if x.func.id == 'int': + return (TYPE_INT, True) elif x.func.id in ('repr', 'str', 'unicode'): return (TYPE_STRING, True) @@ -140,7 +137,6 @@ TYPE_CONVERTERS = { # converting A -> C must yield the same result as converting A -> B -> C. # TYPE_OBJECT is the supertype of everything. TYPE_CHAR: { - TYPE_POSITIVE: ord, # ignore the zero byte, chr(0) TYPE_STRING: identity}, TYPE_COLOR: { TYPE_FLOAT: float, @@ -155,26 +151,13 @@ TYPE_CONVERTERS = { TYPE_FLOAT: float, TYPE_NUMBER: identity, TYPE_STRING: str}, - TYPE_NEGATIVE: { - TYPE_FLOAT: float, - TYPE_INT: int, - TYPE_NUMBER: identity, - TYPE_STRING: str}, TYPE_NUMBER: { - TYPE_STRING: str}, - TYPE_NUMERIC_STRING: { - TYPE_FLOAT: float, - TYPE_STRING: identity}, - TYPE_POSITIVE: { TYPE_FLOAT: float, TYPE_INT: int, - TYPE_NUMBER: identity, TYPE_STRING: str}, - TYPE_ZERO: { + TYPE_NUMERIC_STRING: { TYPE_FLOAT: float, - TYPE_INT: int, - TYPE_NUMBER: identity, - TYPE_STRING: str} + TYPE_STRING: identity} } @@ -251,7 +234,7 @@ def get_converter(old_type, new_type): # form the transitive closure of all types that old_type can be # converted to, and look for new_type there backtrace = converters_from_old.copy() - new_backtrace = backtrace + new_backtrace = backtrace.copy() break_all = False while True: newest_backtrace = {} -- cgit v0.9.1