diff options
author | Marion <marion.zepf@gmail.com> | 2013-08-27 10:16:53 (GMT) |
---|---|---|
committer | Marion <marion.zepf@gmail.com> | 2013-08-27 10:16:53 (GMT) |
commit | 3f1611187221f32ef59d1cd472103e2d8354c495 (patch) | |
tree | 71f9da444f13babb3f60283a5541fee181977937 | |
parent | 0f7e2912249904219197a58cbd6ad8e82c544e4d (diff) |
use Type objects for the types in the type hierarchy
-rw-r--r-- | TurtleArt/tatype.py | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py index 5ddff02..d4fec2b 100644 --- a/TurtleArt/tatype.py +++ b/TurtleArt/tatype.py @@ -27,19 +27,36 @@ from taconstants import (Color, CONSTANTS) from tautils import debug_output -# types are defined as numbers because they are faster to compare than strings -# TODO make a dict or something to convert the numbers to strings for output -TYPE_OBJECT = 0 -TYPE_CHAR = 1 -TYPE_COLOR = 2 -TYPE_FLOAT = 3 -TYPE_INT = 4 -TYPE_NEGATIVE = 5 -TYPE_NUMBER = 6 -TYPE_NUMERIC_STRING = 7 -TYPE_POSITIVE = 8 -TYPE_STRING = 9 -TYPE_ZERO = 10 +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 + self.value = value + def __eq__(self, other): + if other is None: + return False + if not isinstance(other, Type): + raise TypeError("cannot compare Type to object of type " + + repr(type(other))) + return self.value == other.value + def __repr__(self): + return repr(self.name) + def __str__(self): + return str(self.name) + +TYPE_OBJECT = Type('object', 0) +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_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, |