Nick Meharry avatar Nick Meharry committed bdfed34

Rewriting the gtypes module.

Comments (0)

Files changed (1)

-class Type(object):
-    def __init__(self, type):
-        self.type = type
-    def __str__(self):
-        return self.type
-    __repr__ = __str__
-
-INT = Type('INT')
-STR = Type('STR')
-ARRAY = Type('ARRAY')
-BLOCK = Type('BLOCK')
-
-ANY = Type('ANY')
-
-class GObject(object):
+class Box(object):
     def __init__(self, value, type):
+        assert type in Type.s
         self.value = value
         self.type = type
+    def unbox(self):
+        return self.value
+
+
+types = []
+
+class Type(object):
+    def __init__(self):
+        if not self.__class__.__name__ == 'Type':
+            types.append(self.__class__)
     def __str__(self):
-        if self.type is STR:
-            return repr(self.value)
-        elif self.type is ARRAY:
-            return '[%s]' % ' '.join(str(x) for x in self.value)
-        elif self.type is BLOCK:
-            return '{%s}' % self.value
-        else:
-            return str(self.value)
-    __repr__ = __str__
-    def __eq__(self, other):
-        return self.type == other.type and self.value == other.value
+        return self.__class__.__name__
+    @classmethod
+    def null(cls):
+        raise NotImplementedError
 
-class GFunc(object):
-    '''This should be instantiated and the overriden functions named "run_TYPE1_TYPE2"'''
-    def __init__(self, argCounts, argTypes=[(INT,), (STR,), (BLOCK,), (ARRAY,)]):
-        '''argCounts is a tuple of arg numbers this function accepts,
-           argTypes is a list of tuples of types'''
-        if not isinstance(argCounts, (tuple, list)):
-            argCounts = (argCounts,)
-        self.argCounts = argCounts
-        self.argTypes = argTypes
-    def __repr__(self):
-        return '<GFunc: args=%s, types=%s>' % (self.argCounts, self.argTypes)
-    def __call__(self, stack):
-        if self.argCounts == (0,):
-            return self.run(stack)
-        for c in self.argCounts:
-            # Args is a list of the top of the stack, with order [top, next, ...]
-            args = [stack[x] for x in xrange(c)]
-            for t in self.argTypes:
-                if len(t) != c: continue
-                #print 'c=%i, t=%r, args=%r' % (c, t, args)
-                if all((t[i] is ANY or args[i].type is t[i]) for i in xrange(c)):
-                    [stack.pop() for _ in xrange(c)]
-                    func_name = 'run_%s' % '_'.join(str(x) for x in t)
-                    print 'Running %r' % func_name
-                    return getattr(self, func_name)(stack, *args)
-        raise ValueError('No matching function signiture found')
+class Any(Type):
+    @classmethod
+    def null(cls):
+        return Box(None, Any)
+Any()
+class Number(Type):
+    @classmethod
+    def null(cls):
+        return Box(0, Number)
+Number()
+class Array(Type):
+    def null(cls):
+        return Box([], Array)
+Array()
+
 
 class Stack(object):
     stack = []
     def clear(self):
         self.stack = []
     def push(self, value, type=None):
-        if isinstance(value, GObject):
+        if isinstance(value, Box):
             #print '    Pushing object with value %r and type %s' % (value.value, value.type)
             self.stack.append(value)
         else:
             #print '    Pushing object with value %r and type %s' % (value, type)
-            self.stack.append(GObject(value, type))
+            self.stack.append(Box(value, type))
     append = push
     def pop(self):
+        if self.size() == 0:
+            return Any.null()
         return self.stack.pop()
     def peek(self):
         return self.stack[-1]
         return ' '.join(str(x) for x in self.stack)
     def __repr__(self):
         return "<Stack: %r>" % self.stack
-
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.