From 6f2d7e4ce822ec2bb2a932a1cb7297919eb5eb6a Mon Sep 17 00:00:00 2001 From: Marion Date: Sun, 08 Sep 2013 11:20:20 +0000 Subject: use the tatype.convert() function to type-convert boxes in exported py code --- (limited to 'TurtleArt/tatype.py') diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py index 346eb23..752f71b 100644 --- a/TurtleArt/tatype.py +++ b/TurtleArt/tatype.py @@ -28,12 +28,16 @@ from tautils import debug_output class Type(object): - """ A type in the type hierarchy. The `name` attribute is only for - pretty-printing. The `value` attribute should be of a type that is fast - to compare, like e.g., int. """ - def __init__(self, name, value): - self.name = name + """ A type in the type hierarchy. """ + + def __init__(self, constant_name, value): + """ constant_name -- the name of the constant that points to this Type + object + value -- an arbitrary integer that is different from the values of + all other Types. The order of the integers doesn't matter. """ + self.constant_name = constant_name self.value = value + def __eq__(self, other): if other is None: return False @@ -41,16 +45,20 @@ class Type(object): raise TypeError("cannot compare Type to object of type " + repr(type(other))) return self.value == other.value + def __repr__(self): - return repr(self.name) + return repr(self.constant_name) + def __str__(self): - return str(self.name) + return str(self.constant_name) class TypeDisjunction(tuple,Type): """ Disjunction of two or more Types (from the type hierarchy) """ + def __init__(self, iterable): self = tuple(iterable) + def __str__(self): s = ["("] for disj in self: @@ -61,17 +69,17 @@ class TypeDisjunction(tuple,Type): return "".join(s) -TYPE_OBJECT = Type('object', 0) -TYPE_BOOL = Type('bool', 5) -TYPE_BOX = Type('box', 8) # special type for the unknown content of a box -TYPE_CHAR = Type('char', 1) -TYPE_COLOR = Type('color', 2) -TYPE_FLOAT = Type('float', 3) -TYPE_INT = Type('int', 4) -TYPE_NUMBER = Type('number', 6) # shortcut to avoid a TypeDisjunction +TYPE_OBJECT = Type('TYPE_OBJECT', 0) +TYPE_BOOL = Type('TYPE_BOOL', 5) +TYPE_BOX = Type('TYPE_BOX', 8) # special type for the unknown content of a box +TYPE_CHAR = Type('TYPE_CHAR', 1) +TYPE_COLOR = Type('TYPE_COLOR', 2) +TYPE_FLOAT = Type('TYPE_FLOAT', 3) +TYPE_INT = Type('TYPE_INT', 4) +TYPE_NUMBER = Type('TYPE_NUMBER', 6) # shortcut to avoid a TypeDisjunction # between TYPE_FLOAT and TYPE_INT -TYPE_NUMERIC_STRING = Type('numeric string', 7) -TYPE_STRING = Type('string', 9) +TYPE_NUMERIC_STRING = Type('TYPE_NUMERIC_STRING', 7) +TYPE_STRING = Type('TYPE_STRING', 9) # TODO add list types @@ -303,6 +311,11 @@ def convert(x, new_type, old_type=None, converter=None): if old_type == new_type: return x + # special case: 'box' block as an AST + if isinstance(x, ast.Subscript) and x.value is BOX_AST: + new_type_ast = ast.Name(id=new_type.constant_name) + return get_call_ast('convert', [x, new_type_ast], return_type=new_type) + # if the converter is not given, try to find one if converter is None: converter = get_converter(old_type, new_type) -- cgit v0.9.1