private static void ExtendGroundEnv(string symbol, KObject value) { init(); GroundEnv.Bind(symbol, value); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 1); return(First(args) is KInert); }
public override bool CompareTo(KObject other) { return(other is KEnvironment && other == this); }
public override bool CompareTo(KObject other) { return(other is KContinuation && other == this); }
public static KObject Eval(KObject datum, KEnvironment env) { return(CPS.Execute <KObject>(() => rceval(datum, env, CPS.RootContinuation <KObject>()))); }
public override bool CompareTo(KObject other) { return(other is KString && (other as KString).Equals(Value)); }
public override bool CompareTo(KObject other) { return(other is KFraction && (other as KFraction).Denominator.Equals(Denominator) && (other as KFraction).Numerator.Equals(Numerator)); }
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)); } }
protected KObject Third(KObject p) { return((((KPair)((KPair)p).Cdr).Cdr as KPair).Car); }
public override bool CompareTo(KObject other) { return(other is KDouble && (other as KDouble).Value.Equals(Value)); }
protected KObject Second(KObject p) { return(((KPair)((KPair)p).Cdr).Car); }
protected KObject First(KObject p) { return(((KPair)p).Car); }
public virtual object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { return(false); }
public KEncapsulation(int id, KObject value) { this.Id = id; Value = value; }
public override bool CompareTo(KObject other) { return(other is KSymbol && (other as KSymbol).Value == Value); }
public override bool CompareTo(KObject other) { return(other is KEncapsulation && other == this); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { return(NumbersModule.Numberp(args, (a, b) => { return a.CompareTo(b); })); }
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()); } }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 1); Console.WriteLine(First(args).Display()); return(new KInert()); }
public override bool CompareTo(KObject other) { return(other is KApplicative && other == this); }
public override bool CompareTo(KObject other) { return(other is KBoolean && (other as KBoolean).Value == Value); }