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 override Continuation Evaluate(Continuation oldContinuation, Datum args) { // Replace the old continuation with the new continuation - but pass in the // supplied argument as the 'return value' of the new continuation. var returnValue = args.ToArray()[0]; return c.PushResult(returnValue); }
public override Continuation Evaluate(Continuation s, Datum args) { var argArray = args.ToArray(); var expression = argArray[0]; var environment = (Environment) argArray[1].CastObject(); return s.Evaluate(environment, expression); }
private static Datum evaluate(Continuation c, Datum args) { var argList = args.ToArray(); if (argList.Length != 1) throw c.error("invalid syntax '{0}'", args); return argList[0]; }
public Datum Evaluate(Continuation c) { try { while (c.Task != null) { if (debug) { Console.WriteLine("{0}", c.GetStackTrace()); Console.Write("Press enter for next step."); Console.ReadLine(); } c = c.Task.Perform(c.PopTask()); } c = c.PopTask(); var result = c.Result; c = c.PopResult(); if (c.Result != null) throw new Exception(string.Format("Additional '{0}' on result stack", c.Result)); return result; } catch (EvaluationException) { throw; } catch (Exception ex) { throw c.error(ex, "EvaluationError", ex.Message); } }
public override Continuation Evaluate(Continuation c, Datum args) { var argArray = args.ToArray(); if (argArray.Length != 2) throw DatumHelpers.error("Invalid syntax. ArgCount ({0}) != 2. Usage: (execute-with-error-handler <error-function> <fn>)", argArray.Length); var errorHandler = makeErrorHandler(c.ErrorHandler, (StackFunction)argArray[0]); var fn = (StackFunction)argArray[1]; return fn.Evaluate(c.NewErrorHandler(errorHandler), DatumHelpers.compound()); }
public override Continuation Evaluate(Continuation c, Environment env, Datum args) { var argList = args.ToArray(); if (argList.Length != 2) throw c.error("Expected 2 arguments: (set! <symbol> <expression>). Got {0} instead", argList.Length); var name = argList[0].CastIdentifier(); var expression = argList[1]; c = c.PushTask(new SetName(env, name)); return c.Evaluate(env, expression); }
public override Continuation Evaluate(Continuation c, Datum args) { var datumArgs = args.ToArray(); if (datumArgs.Length != 2) throw c.error("Apply expects 2 arguments. {0} passed", datumArgs.Length); var function = datumArgs[0] as StackFunction; if (function == null) throw c.error("'{0}' is not a function", datumArgs[0]); return function.Evaluate(c, datumArgs[1]); }
public Continuation Perform(Continuation c) { var argResults = DatumHelpers.nil; for (var i = 0; i < argCount; ++i) { argResults = DatumHelpers.cons(c.Result, argResults); c = c.PopResult(); } return function.Evaluate(c, argResults); }
public Continuation Perform(Continuation c) { var args = new Datum[argCount]; for (var i = 0; i < argCount; ++i) { args[i] = c.Result; c = c.PopResult(); } return function.Evaluate(c, DatumHelpers.compound(args)); }
public override Continuation Evaluate(Continuation c, Datum args) { var argArray = args.ToArray(); if(argArray.Length != 1) throw DatumHelpers.error("call/cc: expect a single function as an argument. Got {0}", argArray.Length); var arg = argArray[0]; var function = arg as StackFunction; if(function == null) throw DatumHelpers.error("call/cc: {0} must be a function", arg); return function.Evaluate(c, DatumHelpers.compound(new ContinuationFunction(c))); }
public override Continuation Evaluate(Continuation oldContinuation, Datum args) { // Replace the old continuation with the new continuation - but pass in the // supplied argument as the 'return value' of the new continuation. var argArray = args.ToArray(); // We allow calling a "continuation" with 0 args. Such a continuation // only arises from the error function. // TODO: we should differentiate the two with an "expected args" member which we can error check. if (argArray.Length == 0) return c; return c.PushResult(argArray[0]); }
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 Continuation Perform(Continuation c) { // The result datum may be a graph. This makes certain // optimizations risky. var expansion = c.Result; if(macroDatum != null) { // Cache macro expansions. In the extremely // common case of the same macro being used on the // same Datum, re-use the expansion. macroDatum.Cache = cons(macro, expansion); } return c.PopResult().PopEnv().Evaluate(c.Env, expansion); }
private static Datum evaluate(Continuation c, Environment 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; }
private static Datum Evaluate(Continuation c) { while (c.Task != null) { try { c = c.Task.Perform(c.PopTask()); c.Statistics.Steps++; } catch (Exception ex) { c = c.ErrorHandler(c, ex); } } return(c.Result); }
public override Continuation Evaluate(Continuation c, Environment 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 = new Environment(env); var remaining = argList.Reverse().ToArray(); for (var i = 0; i < remaining.Length; ++i) { if (i > 0) c = c.PushTask(Ignore.Instance); c = c.Evaluate(localEnv, remaining[i]); } return c; }
private static Datum Evaluate(Continuation c) { while (c.Task != null) { try { c = c.Task.Perform(c.PopTask()); c.Statistics.Steps++; } catch (Exception ex) { c = c.ErrorHandler(c, ex); } } return c.Result; }
public static Datum Lookup(this Continuation c, string name) { var env = GetEnvironment(c); if (env == null) { return(null); } Datum datum; if (env.TryLookup(name, out datum)) { return(datum); } return(DatumHelpers.atom("undefined")); }
private static FExpression toFExpression(Continuation c) { return(c.Result.accept(new FExpressionConverter(c))); }
public static Continuation Unhandled(Continuation c, Exception ex) { throw new EvaluationException(c, ex); }
public override Continuation Evaluate(Continuation c, Environment env, Datum args) { c = c.PushTask(new EvaluateExpansion(env)); return argFunction.Evaluate(c, args); }
public Continuation Perform(Continuation c) { var expansion = c.Result; c = c.PopResult(); return c.Evaluate(env, expansion); }
public ContinuationFunction(Continuation c) { this.c = c; }
public override Continuation Evaluate(Continuation c, Environment env, Datum args) { return c.PushResult(env.ToAtom()); }
public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args) { return c.PushResult(evaluate(c, args)); }
public EvaluationException(string msg, Continuation c, Exception cause) : base(msg, cause) { this.c = c; }
public static StackFunction MakeContinuationFunction(Continuation c) { return new ContinuationFunction(c); }
public Continuation Perform(Continuation c) { var fexpression = toFExpression(c); return(fexpression.Evaluate(c.PopResult(), env, args)); }
public Continuation Perform(Continuation c) { return c.PopResult(); }
public Continuation Perform(Continuation c) { return(taskDelegate(c)); }
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 static EvaluationException error(this Continuation c, Exception cause, string msg, params object[] args) { return(new EvaluationException(string.Format(msg, args), c, cause)); }
public FExpressionConverter(Continuation c) { this.c = c; }
public abstract Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args);
public override Continuation Evaluate(Continuation c, Datum args) { foreach (var ab in argBodies) { var closureEnv = ab.binding(env, args); if (closureEnv == null) continue; return c.Evaluate(closureEnv, ab.body); } throw bindError(args); }
public Continuation Perform(Continuation c) { env.Set(name, c.Result); return c; }
public Continuation Perform(Continuation c) { return(datum.accept(new Visitor(c, env))); }
public EvaluationException(Continuation continuation, Exception ex) : base("Evaluation failed", ex) { this.Continuation = continuation; }
public abstract Continuation Evaluate(Continuation c, Datum args);
public static EvaluationException error(this Continuation c, string msg, params object[] args) { return(error(c, null, msg, args)); }
public override Continuation Evaluate(Continuation c, Datum args) { return c.PushResult(function.Evaluate(args)); }
public Continuation Perform(Continuation c) { return(datum.accept(new Visitor(c.PopEnv(), c.Env))); }
public static Continuation Evaluate(this Continuation s, Environment e, Datum expression) { return(s.PushTask(new EvaluateTask(e, expression))); }
public static Continuation Invoke(this Continuation s, Function f, Datum args) { return(toStack(f).Evaluate(s, args)); }
public override Continuation Evaluate(Continuation c, Datum args) { return(c.PushResult(function.Evaluate(args))); }