public override LispObject Eval(Cons body, LispEnvironment env) { var varForms = body.Car.As<Cons>(); var varSymbols = varForms.Select(x => x.As<Cons>().Car.As<Symbol>()).ToList(); var varInitForms = varForms.Select(x => x.As<Cons>().Cdr.As<Cons>().Car).ToList(); var varStepForms = varForms.Select(x => x.As<Cons>().Cdr.As<Cons>().Cdr.As<Cons>().Car).ToList(); var cadr = body.Cdr.As<Cons>().Car.As<Cons>(); var testForm = cadr.Car; var resultForms = cadr.Cdr.As<Cons>(); var doBody = body.Cdr.As<Cons>().Cdr.As<Cons>(); var doEnv = new LispEnvironment(env); SetInitValues(doEnv, varSymbols, varInitForms); while (doEnv.Eval(testForm).IsNil) { new Progn().Eval(doBody, doEnv); SetStepValues(doEnv, varSymbols, varStepForms); } return new Progn().Eval(resultForms, doEnv); }
public override LispObject Eval(Cons body, LispEnvironment env) { var lambdaList = body.Car.As<Cons>(); var progn = Progn.Wrap(body.Cdr.As<Cons>(), env); return new Lambda(lambdaList, progn, env); }
protected override void SetInitValues(LispEnvironment doEnv, List<Symbol> varSymbols, List<LispObject> varInitForms) { for (int i = 0; i < varSymbols.Count; i++) { var val = doEnv.Eval(varInitForms[i]); doEnv.AddBinding(varSymbols[i], val); } }
protected override void SetStepValues(LispEnvironment doEnv, List<Symbol> varSymbols, List<LispObject> varStepForms) { for (int i = 0; i < varSymbols.Count; i++) { var val = doEnv.Eval(varStepForms[i]); doEnv.SetValue(varSymbols[i], val); } }
public override LispObject Apply(Cons argumentValues, LispEnvironment env) { var fun = argumentValues.Car.As<Function>(); var args = argumentValues.Cdr.As<Cons>(); var spread = Spread(args); return fun.Apply(spread, env); }
protected virtual void SetStepValues(LispEnvironment doEnv, List<Symbol> varSymbols, List<LispObject> varStepForms) { var stepValues = varStepForms.Select(doEnv.Eval).ToList(); for (int i = 0; i < varSymbols.Count; i++) { doEnv.SetValue(varSymbols[i], stepValues[i]); } }
public Repl(TextReader input, TextWriter output) { this.environment = new LibraryEnvironment(reader); this.stars = InternStars(); this.input = input; this.output = output; }
public override LispObject Eval(Cons body, LispEnvironment env) { var symbol = Car.Of(body).As<Symbol>(); var value = env.Eval(Car.Of(Cdr.Of(body))); env.SetValue(symbol, value); return value; }
public override LispObject Apply(Cons arguments, LispEnvironment env) { if (arguments.Car.IsAtom) { return T.True; } return LispNull.Nil; }
protected virtual void SetInitValues(LispEnvironment doEnv, List<Symbol> varSymbols, List<LispObject> varInitForms) { var varInitValues = varInitForms.Select(doEnv.Eval).ToList(); for (int i = 0; i < varSymbols.Count; i++) { doEnv.AddBinding(varSymbols[i], varInitValues[i]); } }
public LispEnvironment(LispEnvironment parent) { this.parent = parent; this.symbols = parent.symbols; this.specials = parent.specials; if (Level >= 256) { throw new EnvironmentException("Stack overflow!"); } }
public override LispObject Apply(Cons argumentsValues, LispEnvironment env) { LispNumber sum = new LispInteger(0); foreach (var addend in argumentsValues) { var num = addend.As<LispNumber>(); sum = sum.Add(num); } return sum; }
public override LispObject Eval(Cons body, LispEnvironment env) { var testForm = Car.Of(body); var thenForm = Car.Of(Cdr.Of(body)); var elseForm = Car.Of(Cdr.Of(Cdr.Of(body))); if (env.Eval(testForm).IsTrue) { return env.Eval(thenForm); } return env.Eval(elseForm); }
public override LispObject Eval(Cons defmacroBody, LispEnvironment env) { var symbol = defmacroBody.Car.As<Symbol>(); var lambdaList = Car.Of(Cdr.Of(defmacroBody)); var macroBody = Cdr.Of(Cdr.Of(defmacroBody)).As<Cons>(); var progn = Progn.Wrap(macroBody, env); var macro = new Macro(symbol.Name, lambdaList, progn, env); env.AddBinding(symbol, macro); return macro; }
public override LispObject Apply(Cons arguments, LispEnvironment env) { var argList = new List<LispObject>(arguments); if (argList.Count < 2) { throw new EvaluateException("Too few arguments to " + this.Name); } if (argList.Count > 2) { throw new EvaluateException("Too many arguments to " + this.Name); } return argList[0].As<LispNumber>().DivideBy(argList[1].As<LispNumber>()); }
public override LispObject Apply(Cons arguments, LispEnvironment env) { if (arguments.IsNil) { throw new EvaluationException("Too few arguments to " + Name); } if (arguments.Cdr.IsNil) { return arguments.Car.As<LispNumber>().Negate(); } var difference = arguments.Car.As<LispNumber>(); foreach (var subtrahend in arguments.Cdr.As<Cons>()) { difference = difference.Add(subtrahend.As<LispNumber>().Negate()); } return difference; }
public override LispObject Apply(Cons arguments, LispEnvironment env) { if (arguments.IsNil) { throw new EvaluationException("Too few arguments to " + Name); } var previous = arguments.Car.As<LispNumber>(); foreach (var arg in arguments.Cdr.As<Cons>()) { var num = arg.As<LispNumber>(); if (!test(previous, num)) { return LispNull.Nil; } previous = num; } return T.True; }
public Cons Expand(Cons form, LispEnvironment env) { var argumentValues = form.Cdr.As<Cons>(); return this.expander.Apply(argumentValues, this.macroEnv).As<Cons>(); }
public Macro(string name, LispObject lambdaList, Cons macroBody, LispEnvironment macroEnv) { this.name = name; this.expander = new Lambda(lambdaList, macroBody, macroEnv); this.macroEnv = macroEnv; }
public override LispObject Apply(Cons argumentValues, LispEnvironment env) { return Car.Of(argumentValues.Car); }
public override LispObject Apply(Cons arguments, LispEnvironment env) { return env.Eval(arguments.Car); }
public IntegrationTest() { this.reader = new Reader(); this.env = new LibraryEnvironment(this.reader); }
public override LispObject Apply(Cons arguments, LispEnvironment env) { return fun(); }
private LispEnvironment GetLoopEnvironment(Action action) { var symbol = environment.Symbols.Intern("QUIT"); var builtin = new DelegateBuiltin(symbol.Name, () => { action(); return T.True; }); var loopEnv = new LispEnvironment(environment); loopEnv.AddBinding(symbol, builtin); for (int i = 0; i < stars.Length; i++) { loopEnv.AddBinding(stars[i], LispNull.Nil); } return loopEnv; }
public override LispObject Eval(Cons body, LispEnvironment env) { return Car.Of(body); }
public abstract LispObject Eval(Cons body, LispEnvironment env);