def APPLY(next, n): app = stack.pop() args = [stack.pop() for x in range(n)] #print app #print args #print app.arity, n if isinstance( app, Application) and app.args is not None and len(app.args + args) >= app.arity: #print "apply type 1" missing = app.arity - len(app.args) app.args.extend(args[:missing]) apply = lookup(FakeFullyQualifId("", "_apply")) #print args, missing, args[:missing], args[missing:] for arg in args[missing:]: app = Application(apply, [app, arg]) elif isinstance(app, Application): #print "apply type 2" app.args.extend(args) elif not isinstance(app, Application) and app.arity < n: #print "apply type 3" app = Application(app, args[:app.arity]) apply = lookup(FakeFullyQualifId("", "_apply")) for arg in args[app.arity:]: app = Application(apply, [app, arg]) else: #print "apply type 4" app = Application(app, args) stack.push(app) return next
def PUSH_CONST(next, val): const = get_app().func.consts[val] if hasattr(const, "module"): const = lookup(const) stack.push(Application(const, [])) else: stack.push(Value(const)) return next
def STRING(next): s = stack.pop() assert isinstance(s.value, str) if len(s.value) > 1: cstring = lookup(FakeFullyQualifId("", "_primCString")) stack.push( Application(lookup(FakeFullyQualifId("", "Cons")), [ Value(s.value[0]), Application(cstring, [Value(s.value[1:])]) ])) else: stack.push( Application(lookup(FakeFullyQualifId("", "Cons")), [ Value(s.value[0]), Application(lookup(FakeFullyQualifId("", "Nil")), []) ])) return next
def MK_AP(next, val): func = lookup(get_app().func.consts[val]) if isinstance(func, tuple): func = func[1] if len(stack) < func.arity: raise SystemError, "MK_AP tried to pop %i arguments from a stack with %i elements for function %s. Stack contents = %s" % ( func.arity, len(stack), str(func), str(stack)) args = [stack.pop() for x in range(func.arity)] stack.push(Application(func, args)) return next
def MK_PAP(next, val, arity): #print lookup(get_app().func.consts[val]) args = [stack.pop() for x in range(arity)] stack.push(Application(lookup(get_app().func.consts[val]), args)) return next
def ZAP_ARG(next, val): get_app().args[val] = Application( lookup(FakeFullyQualifId("Prelude", "_zap_arg")), []) return next
def ZAP_STACK(next, val): stack[val] = Application( lookup(FakeFullyQualifId("Prelude", "_zap_stack")), []) return next
def MK_CON(next, val): func = lookup(get_app().func.consts[val]) args = [stack.pop() for x in range(func.arity)] app = Application(func, args) stack.push(app) return next
def evaluate(func): toplevel = lookup(FakeFullyQualifId("", "_toplevel")) driver = lookup(FakeFullyQualifId("", "_driver")) driver.eval([toplevel, func])