public KOperative(KObject formals, KObject eformal, KObject expr, KEnvironment env) { Formals = formals; EFormal = eformal; Expr = expr; if (!(eformal is KIgnore || eformal is KSymbol)) { throw new RuntimeException("Can't construct operative, type mismatch of eformal"); } var lst = CheckFormalTree(Formals); if (eformal is KSymbol) { KSymbol s = eformal as KSymbol; if (lst.Contains(s.Value)) { throw new RuntimeException("Distinct eformal needed"); } } if (!(env is KEnvironment)) { throw new RuntimeException("Operative: not an environment!"); } else { staticenv = env; } }
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 RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont) { var res = CheckParameter(args, 3, "guard-continuation"); if (res != null) { return(CPS.Error(res, cont)); } KContinuation c = Second(args) as KContinuation; KObject entry = First(args); KObject exit = Third(args); try { if (null == c) { throw new RuntimeException("guard-continuation: not a continution"); } c.Value.EntryGuard = assignGuards(entry); c.Value.ExitGuard = assignGuards(exit); return(CPS.Return(c, cont)); } catch (Exception e) { return(CPS.Error(e.Message, cont)); } }
public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont) { var res = CheckParameter(args, 1, "load"); if (res != null) { return(CPS.Error(res, cont)); } KObject val = First(args) as KString; if (!(val is KString)) { return(CPS.Error("load: not a string", cont)); } string path = (val as KString).Value; try { List <KObject> tokens = Parser.ParseAll(File.ReadAllText(path)); foreach (var token in tokens) { Evaluator.Eval(token, env); } return(CPS.Return(new KInert(), cont)); } catch (Exception e) { return(CPS.Error("Failed to load file: " + e.Message, cont)); } }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 3); KObject formalt = First(args), eformal = Second(args), expr = Third(args); return(new KOperative(formalt, eformal, expr, env)); }
public virtual RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont) { object result = null; try { result = Do(args, env, cont); } catch (RuntimeException e) { return(CPS.Error <KObject>(getName() + ": " + e.Message, cont)); } if (result is KObject) { return(CPS.Return(result as KObject, cont)); } else if (result is bool) { return(CPS.Return(new KBoolean((bool)result), cont)); } else if (result is RecursionResult <KObject> ) { return(result as RecursionResult <KObject>); } else { return(CPS.Return(new KInert(), cont)); } }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 1); KFraction input = First(args) as KFraction; Check(input); return(new KDouble(input.ToDouble())); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 1); var inex = First(args) as KDouble; Check(inex); return(KFraction.fromDouble(inex.Value)); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 1); KContinuation c = First(args) as KContinuation; Check(c, "expected continuation"); return(new KApplicative(new PPassCont(c))); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 1); KCombiner comb = First(args) as KCombiner; Check(comb, "not a combiner"); return(Evaluator.rceval(new KPair(comb, new KPair(new KContinuation(cont), new KNil())), env, cont)); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 1); KCombiner op = First(args) as KCombiner; Check(op, "not a combiner"); return(new KApplicative(op)); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 2); KObject a = First(args); KObject b = Second(args); return(CompareEqual(a, b)); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 2); KObject expr = First(args); KEnvironment envir = Second(args) as KEnvironment; Check(envir, "not an enviroment"); return(CPS.PassTo <KObject>(() => Evaluator.rceval(expr, envir, cont))); }
public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont) { var res = CheckParameter(args, 1, "copy-es-immutable"); if (res != null) { return(CPS.Error(res, cont)); } return(Return(KPair.CopyEs(First(args)), cont)); }
public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont) { var res = CheckParameter(args, 1, "number?"); if (res != null) { return(CPS.Error(res, cont)); } return(ReturnBool(First(args) is KDouble || First(args) is KInteger, cont)); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 1); Continuation <KObject> result = myc.Value; if (result.Context == null) { return(CPS.Return(First(args), CPS.RootContinuation <KObject>())); } return(result.Call(First(args))); }
private static void init() { if (initialized) { return; } GroundEnv = new KEnvironment(null); env = new KEnvironment(GroundEnv); loadedModules = new List <string>(); initialized = true; }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 0); KObject datum = null; while (null == datum) { datum = Interpreter.readDatum(); } return(datum); }
public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont) { var res = CheckParameter(args, 1, "encapsulate"); if (res != null) { return(CPS.Error(res, cont)); } KObject datum = First(args); return(Return(new KEncapsulation(id, datum), cont)); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 1); KEncapsulation datum = First(args) as KEncapsulation; Check(datum, "not an encap"); if (datum.Id != id) { throw new RuntimeException("wrong id"); } return(datum.Value); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 2); KPair p = args as KPair; Continuation <KObject> c = null; c = new Continuation <KObject>((x) => { return(CPS.PassTo(() => Evaluator.rceval( new KPair(p.Car, new KPair(new KString(c.Context.ToString()), new KNil())), env, cont))); }, cont, "error-handler"); c.isHandler = true; return(CPS.PassTo(() => Evaluator.rceval(Second(p), env, c))); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 0); this.id = counter++; KApplicative e = new KApplicative(new PEncapE(id)); KApplicative p = new KApplicative(new PEncapP(id)); KApplicative d = new KApplicative(new PEncapD(id)); KPair p3 = new KPair(d, new KNil()); KPair p2 = new KPair(p, p3); KPair p1 = new KPair(e, p2); return(p1); }
public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont) { var res = CheckParameter(args, 1, "display"); if (res != null) { return(CPS.Error(res, cont)); } KObject datum = First(args); Console.WriteLine(datum.Display()); return(Return(new KInert(), cont)); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { CPara(args, 2); KObject definand = First(args), expr = Second(args); var cc = new Continuation <KObject>((e) => { try { Evaluator.BindFormalTree(definand, e, env); } catch (Exception ex) { return(CPS.Error <KObject>(ex.Message, cont)); } return(CPS.Return <KObject>(new KInert(), cont)); }, cont, "define"); return(CPS.PassTo(() => Evaluator.rceval(expr, env, cc))); }
public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont) { var res = CheckParameter(args, 1, "continuation->applicative"); if (res != null) { return(CPS.Error(res, cont)); } KObject c = First(args); if (c is KContinuation) { return(CPS.Return(new KApplicative(new PPassCont(c as KContinuation)), cont)); } return(CPS.Error("continuation->applicative: not a continuation given", cont)); }
public override object Do(KObject args, KEnvironment env, Continuation <KObject> cont) { int length = KPair.Length(args); if (length == -1) { throw new RuntimeException("improper list"); } if (length == 0) { throw new RuntimeException("at least one arg expected"); } else if (length == 1) { KObject a = NumbersModule.Check(First(args)); if (a is KFraction) { return(new KFraction((a as KFraction).Numerator * -1, (a as KFraction).Denominator)); } else { return(new KDouble((a as KDouble).Value * -1)); } } else { KObject dif = NumbersModule.Check(First(args)); KObject head = (args as KPair).Cdr; while (head is KPair) { KObject nextNumber = NumbersModule.Check(First(head)); if (dif is KFraction && nextNumber is KFraction) { dif = (dif as KFraction).Subtract(nextNumber as KFraction); } else { KDouble a = NumbersModule.ToInexact(dif); KDouble b = NumbersModule.ToInexact(nextNumber); dif = new KDouble(a.Value - b.Value); } head = (head as KPair).Cdr; } return(dif); } }
public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont) { var res = CheckParameter(args, 2, "*"); if (res != null) { return(CPS.Error(res, cont)); } KObject a = First(args), b = Second(args); var result = KNumber.DoMath(a, b, (x, y) => x * y, (x, y) => x * y); if (result == null) { return(CPS.Error("*: wrong types", cont)); } return(Return(result, cont)); }
public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont) { var res = CheckParameter(args, 0, "make-encapsulation-type"); if (res != null) { return(CPS.Error(res, cont)); } this.id = counter++; KApplicative e = new KApplicative(new PEncapE(id)); KApplicative p = new KApplicative(new PEncapP(id)); KApplicative d = new KApplicative(new PEncapD(id)); KPair p3 = new KPair(d, new KNil(), true); KPair p2 = new KPair(p, p3, true); KPair p1 = new KPair(e, p2, true); return(Return(p1, cont)); }
public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont) { var res = CheckParameter(args, 1, "unwrap"); if (res != null) { return(CPS.Error(res, cont)); } KObject ap = First(args); if (!(ap is KApplicative)) { return(CPS.Error("unwrap: not an applicative", cont)); } else { return(Return((ap as KApplicative).Combiner, cont)); } }
public override RecursionResult <KObject> Combine(KObject args, KEnvironment env, Continuation <KObject> cont) { var res = CheckParameter(args, 1, "wrap"); if (res != null) { return(CPS.Error(res, cont)); } KObject op = First(args); if (!(op is KOperative || op is KApplicative)) { return(CPS.Error("wrap: not a operative", cont)); } else { return(Return(new KApplicative(op as KCombiner), cont)); } }