public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args) { var argArray = DatumHelpers.enumerate(args).ToArray(); Array.Reverse(argArray); c = c.PushTask(new InvokeFunction(function, argArray.Length)); return argArray.Aggregate(c, (current, arg) => current.Evaluate(env, arg)); }
public static LexicalEnvironment Extend(LexicalEnvironment env) { env.Define("symbol->string", new SymbolToString().ToStack()); env.Define("string->symbol", new StringToSymbol().ToStack()); env.Define("gensym", new GenSym().ToStack()); return env; }
public static LexicalEnvironment AddTo(LexicalEnvironment env) { env.Define("read", Read.Instance); env.Define("open-input-string", OpenInputString.Instance); env.Define("eof-object?", IsEof.Instance); env.Define("open-input-stream", OpenInputStream.Instance); return env; }
public void setupFixture() { e = new Evaluator(); env = StandardEnvironment.Create(); var setupDatum = getLispFromResource("setup"); if (setupDatum != nil) foreach(var f in setupDatum.Enumerate()) e.Evaluate(env, f); }
public static LexicalEnvironment AddTo(LexicalEnvironment env) { return env.Define("task-descriptions", MakeDatumFunction(getTaskDescriptions, ",task-descriptions")) .Define("execute-with-error-translator", ExecuteWithErrorTranslator.Instance) .Define("env-stack", MakeDatumFunction(getEnvironments, ",env-stack")) .Define("pending-results", MakeDatumFunction(getPendingResults, ",pending-results")) .Define("throw", MakeDatumFunction(throwMsg, ",throw")) .Define("get-env", MakeDatumFunction(getEnv, ",get-env")); }
public Datum Evaluate(Statistics statistics, LexicalEnvironment env, Datum datum) { env.Statistics = statistics; var c = Continuation.Create(statistics) .PushTask(null) .PushResult(null) .Evaluate(env, datum); return Evaluate(c); }
public static LexicalEnvironment AddTo(LexicalEnvironment env) { env = Arithmetic.Extend(env); env.Define("append", Append.Instance); env = SymbolFunctions.Extend(env); ResourceLoader.ExecuteResource(env, "LispEngine.Bootstrap.Builtins.lisp"); ResourceLoader.ExecuteResource(env, "LispEngine.Bootstrap.Library.lisp"); env = Reader.AddTo(env); return env; }
public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args) { var argList = args.ToArray(); if (argList.Length != 2) throw c.error("Expected 2 arguments: (define <symbol> <expression>). Got {0} instead", argList.Length); var name = argList[0].CastSymbol(); var expression = argList[1]; c = c.PushTask( tc => { env.Define(name, tc.Result); return tc;}, "define '{0}'", name); return c.Evaluate(env, expression); }
public static LexicalEnvironment Extend(LexicalEnvironment env) { return env .Define("+", makeOperation("+", (x, y) => x + y)) .Define("-", makeOperation("-", (x, y) => x - y)) .Define("*", makeOperation("*", (x, y) => x*y)) .Define("/", makeOperation("/", (x, y) => x/y)) .Define("<", makeOperation("<", (x, y) => x < y)) .Define(">", makeOperation(">", (x, y) => x > y)) .Define("bit-and", makeOperation("bit-and", (x , y) => x & y)) .Define("bit-or", makeOperation("bit-or", (x, y) => x | y)) .Define("bit-shift", makeOperation("bit-shift", (x, y) => x << y)); }
public static LexicalEnvironment AddTo(LexicalEnvironment env) { // Invoke a given instance method on an object env.Define("make-instance-method", DelegateFunctions.MakeDatumFunction(MakeInstanceMethod, ",make-instance-method")); env.Define("get-static-method", DelegateFunctions.MakeDatumFunction(GetStaticMethod, ",get-static-method")); env.Define("get-type", new GetTypeFunction().ToStack()); env.Define("new", new New().ToStack()); env.Define("atom", new WrapAtom().ToStack()); // Define "dot" and "slash" as a macros which allow us to use // Clojure-style syntax for invoking and referring to methods. ResourceLoader.ExecuteResource(env, "LispEngine.ReflectionBinding.ReflectionBuiltins.lisp"); return env; }
private static Datum evaluate(Continuation c, LexicalEnvironment env, Datum args) { var macroArgs = args.ToArray(); if (macroArgs.Length % 2 != 0) throw c.error("Invalid macro syntax for lambda. Argument count for '{0}' is not even ({1}). Syntax is (lambda [args body]+)", args, macroArgs.Length); var argBodies = new List<ArgBody>(); for (var i = 0; i < macroArgs.Length; i += 2) { var closureArgs = macroArgs[i]; var body = macroArgs[i + 1]; argBodies.Add(new ArgBody(closureArgs, body)); } return new Closure(env, argBodies); }
public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args) { var argList = args.ToArray(); if (argList.Length < 1) throw c.error("Expected at least 1 expression for begin. Got none."); // Scope any local definitions. var localEnv = env.NewFrame(); var remaining = argList.Reverse().ToArray(); for (var i = 0; i < remaining.Length; ++i) { if (i > 0) c = c.PushTask(popResult, "Discard result"); c = c.Evaluate(localEnv, remaining[i]); } return c; }
public static LexicalEnvironment AddTo(LexicalEnvironment env) { env = env .Define("log", Log.Instance) .Define("lambda", Lambda.Instance) .Define("cons", DelegateFunctions.MakeDatumFunction(DatumHelpers.cons, ",cons")) .Define("set-car!", DelegateFunctions.MakeDatumFunction(DatumHelpers.setCar, ",set-car!")) .Define("set-cdr!", DelegateFunctions.MakeDatumFunction(DatumHelpers.setCdr, ",set-cdr!")) .Define("apply", Apply.Instance) .Define("eq?", EqualFunctions.Eq) .Define("equal?", EqualFunctions.Equal) .Define("quote", Quote.Instance) .Define("define", Define.Instance) .Define("set!", Set.Instance) .Define("begin", Begin.Instance) .Define("call-with-current-continuation", CallCC.Instance) .Define("eval", Eval.Instance) .Define("env", Env.Instance); env = DebugFunctions.AddTo(env); env = Macro.AddTo(env); env = VectorFunctions.AddTo(env); return env; }
private static Binding findAndCache(LexicalEnvironment e, Symbol symbol) { if (e.statistics != null) e.statistics.Lookups++; var id = symbol.ID; while(e != null) { var b = e.findInFrame(id); if (b != null) { symbol.Env = e; return (symbol.CachedBinding = b); } e = e.parent; } throw undefined(symbol); }
/** * Used for bootstrapping various .lisp files into the environment. */ public static void ExecuteResource(Statistics statistics, Assembly assembly, LexicalEnvironment env, string resourceFile) { var evaluator = new Evaluator(); foreach (var d in ReadDatums(assembly, resourceFile)) evaluator.Evaluate(statistics, env, d); }
public Datum Evaluate(LexicalEnvironment env, Datum datum) { return(Evaluate(new Statistics(), env, datum)); }
public EvaluateFExpression(Datum args, LexicalEnvironment env) { this.args = args; this.env = env; }
public static void ExecuteResource(LexicalEnvironment env, string resourceFile) { ExecuteResource(new Statistics(), Assembly.GetCallingAssembly(), env, resourceFile); }
private static Binding checkCached(LexicalEnvironment e, Symbol symbol) { if (symbol.Env == null) return null; while(e != null) { if (ReferenceEquals(symbol.Env, e)) return symbol.CachedBinding; e = e.parent; } return null; }
public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args) { var p = args as Pair; c = c.PushEnv(env).PushTask(new EvaluateExpansion(this, p)); // Optimization - if this macro has been expanded on this Datum before, // use the same expansion. // See "macro-cache-in-datum" unit test for a demonstration of why // we need to check against "this" also. if(p != null) { var cachedPair = p.Cache as Pair; if(cachedPair != null && ReferenceEquals(cachedPair.First, this)) return c.PushResult(cachedPair.Second); } c.Statistics.Expansions++; return argFunction.Evaluate(c, args); }
public abstract Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args);
public Continuation Evaluate(LexicalEnvironment e, Datum expression) { return PushEnv(e).PushTask(new EvaluateTask(expression)); }
public Continuation PushEnv(LexicalEnvironment env) { return(SetEnvs(envs.Push(env))); }
// This is a bit hacky - can't figure out a better way to "supply" the statistics // object yet. Basically, I want to expose "get-counter" to Lisp, but not "statistics" // itself... I guess. public LexicalEnvironment AddTo(LexicalEnvironment env) { env.Define("!get-statistics", DelegateFunctions.MakeFunction(Snapshot, "!get-statistics")); env.Define("!get-statistics-delta", DelegateFunctions.MakeFunction <Statistics, Statistics>(Delta, "!get-statistics-delta")); return(env); }
private static LexicalEnvironment newFrame(LexicalEnvironment parent, FrameBindings bindings) { return(new LexicalEnvironment(parent, bindings)); }
public Datum Evaluate(LexicalEnvironment env, Datum datum) { return Evaluate(new Statistics(), env, datum); }
public static LexicalEnvironment AddTo(LexicalEnvironment env) { env.Define("unmacro", Unmacro); env.Define("macro", Instance); return env; }
private static LexicalEnvironment newFrame(LexicalEnvironment parent, FrameBindings bindings) { return new LexicalEnvironment(parent, bindings); }
public Continuation PushEnv(LexicalEnvironment env) { return SetEnvs(envs.Push(env)); }
private LexicalEnvironment(LexicalEnvironment parent, IStack<Binding> bindings) { this.statistics = parent == null ? null : parent.statistics; this.parent = parent; this.bindings = bindings; }
public Closure(LexicalEnvironment env, IEnumerable<ArgBody> argBodies) { this.env = env; this.argBodies = argBodies; }
public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args) { return c.PushResult(evaluate(c, args)); }
// This is a bit hacky - can't figure out a better way to "supply" the statistics // object yet. Basically, I want to expose "get-counter" to Lisp, but not "statistics" // itself... I guess. public LexicalEnvironment AddTo(LexicalEnvironment env) { env.Define("!get-statistics", DelegateFunctions.MakeFunction(Snapshot, "!get-statistics")); env.Define("!get-statistics-delta", DelegateFunctions.MakeFunction<Statistics, Statistics>(Delta, "!get-statistics-delta")); return env; }
public Continuation Evaluate(LexicalEnvironment e, Datum expression) { return(PushEnv(e).PushTask(new EvaluateTask(expression))); }
private LexicalEnvironment(LexicalEnvironment parent, IStack <Binding> bindings) { this.statistics = parent == null ? null : parent.statistics; this.parent = parent; this.bindings = bindings; }