Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
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
parent55024843adee06a07c59e6a59312354583694ba0 (diff)
use the tatype.convert() function to type-convert boxes in exported py code
-rw-r--r--TurtleArt/tatype.py47
-rw-r--r--pyexported/window_setup.py5
2 files changed, 34 insertions, 18 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)
diff --git a/pyexported/window_setup.py b/pyexported/window_setup.py
index 5e9becf..9586f2c 100644
--- a/pyexported/window_setup.py
+++ b/pyexported/window_setup.py
@@ -49,10 +49,13 @@ from TurtleArt.tautils import (magnitude, get_load_name, get_save_name, data_fro
find_block_to_run, find_top_block, journal_check,
find_group, find_blk_below, data_to_string,
find_start_stack, get_hardware, debug_output,
- error_output, convert, find_bot_block,
+ error_output, find_bot_block,
restore_clamp, collapse_clamp, data_from_string,
increment_name, get_screen_dpi)
from TurtleArt.tasprite_factory import (SVG, svg_str_to_pixbuf, svg_from_file)
+from TurtleArt.tatype import (convert, TYPE_BOOL, TYPE_CHAR, TYPE_COLOR,
+ TYPE_FLOAT, TYPE_INT, TYPE_NUMBER, TYPE_OBJECT,
+ TYPE_NUMERIC_STRING, TYPE_STRING)
from TurtleArt.sprites import (Sprites, Sprite)
if _GST_AVAILABLE: