示例#1
0
        public static RecursionResult <KObject> rceval(KObject datum, KEnvironment env, Continuation <KObject> cont)
        {
            // useful for debugging
            //Console.WriteLine(datum.Display());

            if (datum is KPair)
            {
                KPair p = datum as KPair;

                // this function get called when the operator is evaluated to f
                var childCont = new Continuation <KObject>((f) =>
                {
                    if (f is KOperative)
                    {
                        return(combineOp(f as KOperative, p.Cdr, env, cont));
                    }
                    else if (f is KApplicative && (p.Cdr is KPair || p.Cdr is KNil))
                    {
                        if (p.Cdr is KNil)
                        {
                            return(combineApp(f as KApplicative, p.Cdr, env, cont));
                        }
                        KPair ops = p.Cdr as KPair;
                        LinkedList <KObject> input = new LinkedList <KObject>();
                        KPair.Foreach(x =>
                        {
                            input.AddLast(x);
                        }, ops);
                        LinkedList <KObject> pairs = new LinkedList <KObject>();
                        Func <KObject, RecursionResult <KObject> > recursion = null;
                        bool firstRun = true;

                        // this continuation is called with the next argument evaled to x. Place next value
                        recursion = (x) =>
                        {
                            if (CPS.getContext() is int && !firstRun)
                            {
                                // restore elements when reentering continuation
                                int oldInputCount = (int)CPS.getContext() + 1;
                                input             = new LinkedList <KObject>();
                                KPair.Foreach(e =>
                                {
                                    input.AddLast(e);
                                }, ops);
                                int leaveOutputs = input.Count - oldInputCount;
                                while (input.Count > oldInputCount)
                                {
                                    input.RemoveFirst();
                                }
                                while (pairs.Count >= leaveOutputs)
                                {
                                    pairs.RemoveFirst();
                                }
                                firstRun = true;
                            }
                            pairs.AddFirst(x);
                            if (input.Count == 0)
                            {
                                // we are finished
                                KObject output = new KNil();
                                foreach (var el in pairs)
                                {
                                    output = new KPair(el, output);
                                }
                                firstRun = false;
                                return(combineApp(f as KApplicative, output, env, cont));
                            }
                            else
                            {
                                // do something with the next Head argument
                                KObject next = input.First.Value;
                                input.RemoveFirst();
                                var cc2 = new Continuation <KObject>(recursion, cont, input.Count);
                                return(CPS.PassTo(() => rceval(next, env, cc2)));
                            }
                        };
                        KObject next2 = input.First.Value;
                        input.RemoveFirst();
                        var cc = new Continuation <KObject>(recursion, cont, input.Count);
                        return(CPS.PassTo(() => rceval(next2, env, cc)));
                    }
                    return(CPS.Error <KObject>("Unsuitable operation of " + f.Write(), cont));
                }, cont, "eval op/app");
                return(CPS.PassTo(() => rceval(p.Car, env, childCont)));
            }
            else if (datum is KSymbol)
            {
                KObject val = env.Lookup(((KSymbol)datum).Value);
                if (null == val)
                {
                    return(CPS.Error <KObject>("Unbound variable " + ((KSymbol)datum).Value, cont));
                }
                return(CPS.Return(val, cont));
            }
            else
            {
                return(CPS.Return(datum, cont));
            }
        }