Commits

Nick Meharry committed d3827be

Fix nested evaluation.

Comments (0)

Files changed (3)

-from gtypes import builtin, Box, Number, String
+from gtypes import builtin, Box, Number, String, Block, Array
 
 from sys import stderr
 
+@builtin(' ', 0, ())
+def Space(stack, vars):
+    pass
+
 @builtin('~', 1, Number)
 def Tilde(stack, vars):
     a = stack.pop(Number)
     stack.push(Box(result, Number))
 @builtin('~', 1, String)
 def Tilde(stack, vars):
-    a = stack.pop(Number)
+    a = stack.pop(String)
     stack.eval(a.unbox(), stack, vars)
+@builtin('~', 1, Block)
+def Tilde(stack, vars):
+    a = stack.pop(Block)
+    stack.eval(a.unbox(), stack, vars)
+@builtin('~', 1, Array)
+def Tilde(stack, vars):
+    a = stack.pop(Array)
+    [stack.push(box) for box in a.unbox()]
             vars[token].run(stack, vars)
         # Literals
         elif token.replace('-','').isdigit():
+            print '  '*eval_depth + 'pushing number to stack'
             stack.push(int(token), gtypes.Number)
         
         elif token.startswith('"') and token.endswith('"'):
         return self.value
     def __str__(self):
         return str(self.value)
+    def run(self, stack, vars):
+        return self
 
 
 types = []
 vars = FallthroughDict()
 
 class Builtin(Type):
-    def __init__(self, type='native'):
+    def __init__(self, var, type='native'):
         self.is_native = type == 'native'
+        self.var = var
         self.funcs = (
             defaultdict(dict),
             defaultdict(dict),
         for tuple in arg_types:
             self.funcs[arg_count][tuple] = function
     def run(self, stack, vars):
-        print self.funcs
+        print 'var = %r' % self.var
         for arg_count in xrange(len(self.funcs)):
             args = [stack.peek(i+1) for i in xrange(arg_count)]
             arg_types = [arg.type for arg in args]
-            print 'arg_count = %i, args = %s' % (arg_count, args)
+            print 'arg_count = %i, args = %s' % (arg_count, [a.unbox() for a in args])
             for requested_args, func in self.funcs[arg_count].iteritems():
+                if hasattr(requested_args, '__iter__'):
+                    requested_args = [ra for ra in requested_args]
+                if not isinstance(requested_args, list):
+                    requested_args = [requested_args]
                 print 'requested_args = %s, arg_types = %s' % (requested_args, arg_types)
-                if not isinstance(requested_args, (list, tuple)):
-                    requested_args = [requested_args]
                 if requested_args == arg_types:
                     return func(stack, vars)
         raise AttributeError("No matching functions found.")
-Builtin()
+Builtin(None)
 
 def builtin(var, arg_count, *arg_types):
     if arg_count == 'block':
         type = 'native'
 
     if var not in vars:
-        vars[var] = Builtin(type)
+        vars[var] = Builtin(var, type)
 
     def middle(func):
         vars[var].add_func(func, arg_count, arg_types)
             self.stack.append(Box(value, type))
     append = push
     def pop(self, type=Any):
-        if self.size() == 0:
+        try:
+            return self.stack.pop()
+        except IndexError:
             return type.null()
-        return self.stack.pop()
-    def peek(self, depth=1):
-        return self.stack[-1]
+    def peek(self, depth=1, type=Any):
+        try:
+            return self.stack[-depth]
+        except IndexError:
+            return type.null()
     def size(self):
         return len(self.stack)
     __len__ = size