Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/TurtleArt/tatype.py
diff options
context:
space:
mode:
authorMarion <marion.zepf@gmail.com>2013-09-08 11:20:20 (GMT)
committer Marion <marion.zepf@gmail.com>2013-09-08 11:20:20 (GMT)
commit6f2d7e4ce822ec2bb2a932a1cb7297919eb5eb6a (patch)
treef89132b32f4d57b277241a41c9591f692a341e1e /TurtleArt/tatype.py
parent55024843adee06a07c59e6a59312354583694ba0 (diff)
use the tatype.convert() function to type-convert boxes in exported py code
Diffstat (limited to 'TurtleArt/tatype.py')
-rw-r--r--TurtleArt/tatype.py47
1 files changed, 30 insertions, 17 deletions
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)