Esempio n. 1
0
 private static void ExtendGroundEnv(string symbol, KObject value)
 {
     init();
     GroundEnv.Bind(symbol, value);
 }
Esempio n. 2
0
 public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     CPara(args, 1);
     return(First(args) is KInert);
 }
Esempio n. 3
0
 public override bool CompareTo(KObject other)
 {
     return(other is KEnvironment && other == this);
 }
Esempio n. 4
0
 public override bool CompareTo(KObject other)
 {
     return(other is KContinuation && other == this);
 }
Esempio n. 5
0
 public static KObject Eval(KObject datum, KEnvironment env)
 {
     return(CPS.Execute <KObject>(() => rceval(datum, env, CPS.RootContinuation <KObject>())));
 }
Esempio n. 6
0
 public override bool CompareTo(KObject other)
 {
     return(other is KString && (other as KString).Equals(Value));
 }
Esempio n. 7
0
 public override bool CompareTo(KObject other)
 {
     return(other is KFraction && (other as KFraction).Denominator.Equals(Denominator) &&
            (other as KFraction).Numerator.Equals(Numerator));
 }
Esempio n. 8
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));
            }
        }
Esempio n. 9
0
 protected KObject Third(KObject p)
 {
     return((((KPair)((KPair)p).Cdr).Cdr as KPair).Car);
 }
Esempio n. 10
0
 public override bool CompareTo(KObject other)
 {
     return(other is KDouble && (other as KDouble).Value.Equals(Value));
 }
Esempio n. 11
0
 protected KObject Second(KObject p)
 {
     return(((KPair)((KPair)p).Cdr).Car);
 }
Esempio n. 12
0
 protected KObject First(KObject p)
 {
     return(((KPair)p).Car);
 }
Esempio n. 13
0
 public virtual object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     return(false);
 }
Esempio n. 14
0
 public KEncapsulation(int id, KObject value)
 {
     this.Id = id;
     Value   = value;
 }
Esempio n. 15
0
 public override bool CompareTo(KObject other)
 {
     return(other is KSymbol && (other as KSymbol).Value == Value);
 }
Esempio n. 16
0
 public override bool CompareTo(KObject other)
 {
     return(other is KEncapsulation && other == this);
 }
Esempio n. 17
0
 public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     return(NumbersModule.Numberp(args, (a, b) => {
         return a.CompareTo(b);
     }));
 }
Esempio n. 18
0
 public static void BindFormalTree(KObject formal, KObject vals, KEnvironment env, KObject ctxt = null)
 {
     if (ctxt == null)
     {
         ctxt = formal;
     }
     if (formal is KSymbol)
     {
         env.Bind(((KSymbol)formal).Value, vals);
     }
     else if (formal is KIgnore)
     {
         return;
     }
     else if (formal is KNil && vals is KNil)
     {
         return;
     }
     else if (formal is KPair && vals is KPair)
     {
         KPair f = formal as KPair;
         KPair v = vals as KPair;
         BindFormalTree(f.Car, v.Car, env, ctxt);
         BindFormalTree(f.Cdr, v.Cdr, env, ctxt);
     }
     else
     {
         throw new RuntimeException("Can't bind formal tree of " + ctxt.Write());
     }
 }
Esempio n. 19
0
 public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     CPara(args, 1);
     Console.WriteLine(First(args).Display());
     return(new KInert());
 }
Esempio n. 20
0
 public override bool CompareTo(KObject other)
 {
     return(other is KApplicative && other == this);
 }
Esempio n. 21
0
 public override bool CompareTo(KObject other)
 {
     return(other is KBoolean && (other as KBoolean).Value == Value);
 }