示例#1
0
        private List <KGuard> assignGuards(KObject clauses)
        {
            List <KGuard> result = new List <KGuard>();

            if (clauses is KPair)
            {
                KPair.Foreach(x =>
                {
                    int length = KPair.Length(x);
                    if (length == 2)
                    {
                        KContinuation selector   = First(x) as KContinuation;
                        KApplicative interceptor = Second(x) as KApplicative;
                        if (selector == null || interceptor == null)
                        {
                            throw new RuntimeException("guard-continuation: invalid clause, wrong types");
                        }
                        result.Add(new KGuard {
                            Selector = selector, Interceptor = interceptor
                        });
                    }
                    else
                    {
                        throw new RuntimeException("guard-continuation: invalid clause");
                    }
                }, clauses);
            }
            return(result);
        }
示例#2
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            KEnvironment envir = new KEnvironment();

            try
            {
                KPair.Foreach(x => {
                    if (!(x is KEnvironment))
                    {
                        throw new RuntimeException("make-environment: not an environment");
                    }
                    envir.AddParent((KEnvironment)x);
                }, args);
            }
            catch (Exception e)
            {
                return(CPS.Error(e.Message, cont));
            }
            return(Return(envir, cont));
        }
示例#3
0
        public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            int len = KPair.Length(args);

            if (len == -1)
            {
                return(CPS.Error("encapsulation?: parameter is not a list", cont));
            }
            else
            {
                bool result = true;
                KPair.Foreach(x =>
                {
                    if (!(x is KEncapsulation) || (x as KEncapsulation).Id != id)
                    {
                        result = false;
                    }
                }, args);
                return(ReturnBool(result, cont));
            }
        }
示例#4
0
        public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
        {
            int len = KPair.Length(args);

            if (len == -1)
            {
                throw new RuntimeException("parameter is not a list");
            }
            else
            {
                bool result = true;
                KPair.Foreach(x =>
                {
                    if (!(x is KEncapsulation) || (x as KEncapsulation).Id != id)
                    {
                        result = false;
                    }
                }, args);
                return(result);
            }
        }
示例#5
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));
            }
        }