Exemplo n.º 1
0
 private static void ExtendGroundEnv(string symbol, KObject value)
 {
     init();
     GroundEnv.Bind(symbol, value);
 }
Exemplo n.º 2
0
 public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     CPara(args, 1);
     return(First(args) is KInert);
 }
Exemplo n.º 3
0
 public override bool CompareTo(KObject other)
 {
     return(other is KEnvironment && other == this);
 }
Exemplo n.º 4
0
 public override bool CompareTo(KObject other)
 {
     return(other is KContinuation && other == this);
 }
Exemplo n.º 5
0
 public static KObject Eval(KObject datum, KEnvironment env)
 {
     return(CPS.Execute <KObject>(() => rceval(datum, env, CPS.RootContinuation <KObject>())));
 }
Exemplo n.º 6
0
 public override bool CompareTo(KObject other)
 {
     return(other is KString && (other as KString).Equals(Value));
 }
Exemplo 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));
 }
Exemplo 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));
            }
        }
Exemplo n.º 9
0
 protected KObject Third(KObject p)
 {
     return((((KPair)((KPair)p).Cdr).Cdr as KPair).Car);
 }
Exemplo n.º 10
0
 public override bool CompareTo(KObject other)
 {
     return(other is KDouble && (other as KDouble).Value.Equals(Value));
 }
Exemplo n.º 11
0
 protected KObject Second(KObject p)
 {
     return(((KPair)((KPair)p).Cdr).Car);
 }
Exemplo n.º 12
0
 protected KObject First(KObject p)
 {
     return(((KPair)p).Car);
 }
Exemplo n.º 13
0
 public virtual object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     return(false);
 }
Exemplo n.º 14
0
 public KEncapsulation(int id, KObject value)
 {
     this.Id = id;
     Value   = value;
 }
Exemplo n.º 15
0
 public override bool CompareTo(KObject other)
 {
     return(other is KSymbol && (other as KSymbol).Value == Value);
 }
Exemplo n.º 16
0
 public override bool CompareTo(KObject other)
 {
     return(other is KEncapsulation && other == this);
 }
Exemplo n.º 17
0
 public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont)
 {
     return(NumbersModule.Numberp(args, (a, b) => {
         return a.CompareTo(b);
     }));
 }
Exemplo 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());
     }
 }
Exemplo 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());
 }
Exemplo n.º 20
0
 public override bool CompareTo(KObject other)
 {
     return(other is KApplicative && other == this);
 }
Exemplo n.º 21
0
 public override bool CompareTo(KObject other)
 {
     return(other is KBoolean && (other as KBoolean).Value == Value);
 }