From 3f1611187221f32ef59d1cd472103e2d8354c495 Mon Sep 17 00:00:00 2001 From: Marion Date: Tue, 27 Aug 2013 10:16:53 +0000 Subject: use Type objects for the types in the type hierarchy --- (limited to 'TurtleArt/tatype.py') 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, -- cgit v0.9.1