private object ResumeWithProcedure(ContextList cList, Procedure proc, Continuation k) { object returnValue; if (cList.Newer == null) { try { returnValue = proc.Apply(this, List.Create(k), null); } catch (CaptureException capEx) { capEx.AppendContext(cList); throw; } } else { returnValue = ResumeWithProcedure(cList.Newer, proc, k); } try { return(cList.Current.Invoke(returnValue)); } catch (CaptureException capEx) { //System.Console.WriteLine("Older: " + cList.Older.ToString()); capEx.AppendContext(cList.Older); throw; } }
public override object Invoke(object val) { Pair vRib = (Pair)val; object fn = _i.Execute(_expr, null, _env); Procedure proc = (Procedure)fn; return(proc.Apply(_i, vRib, _env)); }
public void EvaluateSimpleProcedure() { Procedure procedure = new Procedure("foo", new string[] { "x" }, new ReturnCommand(new NameExpression("x")), new Machine()); object result = procedure.Apply(new object[] { 1 }, null); Assert.AreEqual(1, result); }
public override void Execute(Machine machine, ValueEnvironment environment) { IList <object> values = new List <object>(); foreach (IExpression argument in this.arguments) { values.Add(argument.Evaluate(environment)); } Procedure procedure = (Procedure)environment.GetValue(this.name); procedure.Apply(values, environment); }
public static int[] NextTerms(int count, int term0, int term1) { Procedure nextTerms = (Procedure)CommonOp.Sync(((object[])CommonOp.Sync(Module))[1]); object[] array = (object[])CommonOp.Sync(nextTerms.Apply(count, term0, term1)); int[] res = new int[array.Length]; for (int i = 0; i < array.Length; i++) { res[i] = (int)array[i]; } return(res); }
public object Expand() { object expr = _parser.Read(); object expander = _i.GlobalEnvironment.Lookup(SymbolCache.EXPANDER); Procedure p = expander as Procedure; if (expr != null && p != null) { // _i.Context = new Frame(_i.Context); // _i.Context.Expr = InstructionCache.Apply; // _i.Context.ValueRib = new Pair(expr, Pair.EmptyList); // _i.Context.Value = expander; return(p.Apply(_i, new Pair(expr, Pair.EmptyList), null)); } else { return(expr); } }
public object Evaluate(ValueEnvironment environment) { Procedure procedure = (Procedure)environment.GetValue(this.name); object[] parameters = null; if (this.arguments != null) { List <object> values = new List <object>(); foreach (IExpression argument in this.arguments) { values.Add(argument.Evaluate(environment)); } parameters = values.ToArray(); } return(procedure.Apply(parameters, environment)); }
public object Apply(Interpreter i, Pair args, Environment env) { Procedure p = (Procedure)List.First(args); return(p.Apply(i, List.Flatten((Pair)List.Rest(args)), env)); }
/// <summary> /// Call apply proc evaluator, but do not evaluate arguments. /// In this case, we can avoid having to instantiate an instance. /// </summary> /// <param name="fn">The function to apply.</param> /// <param name="args">The arguments to pass to fn.</param> /// <param name="env">The environment to evaluate the expression in.</param> /// <param name="caller">The caller. Return to this when done.</param> /// <returns>The proc evaluator.</returns> internal static Evaluator CallQuoted(Procedure fn, SchemeObject args, Environment env, Evaluator caller) { return fn.Apply(args, null, caller, caller); }
public object Execute(object expr, Pair valueRib, Environment env) { object instr = List.First(expr); if (instr == Instruction.Constant) { return(List.Second(expr)); } else if (instr == Instruction.Variable) { object sym = List.Second(expr); object varValue = env.Lookup((Symbol)sym); if (varValue == null) { throw new System.Exception( "Error: no value associated with symbol " + sym ); } else { return(varValue); } } else if (instr == Instruction.Conditional) { object cond = null; try { cond = Execute(List.Second(expr), valueRib, env); } catch (CaptureException capEx) { capEx.AddContext(new CondContext(this, List.Third(expr), List.Fourth(expr), env)); throw capEx; } object condResult = Execute(Boolean.IsTrue(cond) ? List.Third(expr) : List.Fourth(expr), valueRib, env); return(condResult); } else if (instr == Instruction.Closure) { return(new Closure((Pair)List.Second(expr), (Pair)List.Third(expr), env)); } else if (instr == Instruction.Assign) { object assignVal = null; Symbol id = (Symbol)List.Second(expr); try { assignVal = Execute(List.Third(expr), valueRib, env); } catch (CaptureException capEx) { capEx.AddContext(new AssignContext(this, id, env)); throw; } if (env.Assign(id, assignVal)) { return(null); } else { throw new System.Exception( "Error: cannot set undefined identifier " + id ); } } else if (instr == Instruction.Define) { object defineVal = null; Symbol id = (Symbol)List.Second(expr); try { defineVal = Execute(List.Third(expr), valueRib, env); } catch (CaptureException capEx) { capEx.AddContext(new DefineContext(this, id, env)); throw; } env.Define(id, defineVal); return(null); } else if (instr == Instruction.Context) { return(Execute(List.Second(expr), Pair.EmptyList, env)); } else if (instr == Instruction.Argument) { Pair p = null; try { object argRest = List.Third(expr); p = (argRest == Pair.EmptyList) ? Pair.EmptyList : (Pair)Execute(argRest, valueRib, env); } catch (CaptureException capEx) { capEx.AddContext(new ArgContext(this, List.Second(expr), env)); throw; } object argVal; try { argVal = Execute(List.Second(expr), valueRib, env); } catch (CaptureException capEx) { capEx.AddContext(new RibContext(p)); throw; } return(new Pair(argVal, p)); } else if (instr == Instruction.Apply) { Pair vRib = null; try { object fnArgs = List.Third(expr); vRib = (fnArgs == Pair.EmptyList) ? Pair.EmptyList : (Pair)Execute(fnArgs, valueRib, env); } catch (CaptureException capEx) { capEx.AddContext(new FuncContext(this, List.Second(expr), env)); throw; } object fn = null; try { fn = Execute(List.Second(expr), valueRib, env); } catch (CaptureException capEx) { capEx.AddContext(new ApplyContext(this, vRib, env)); throw; } Procedure proc = (Procedure)fn; return(proc.Apply(this, vRib, env)); } else if (instr == Instruction.ArgMultiple) { object multArgVal; try { multArgVal = Execute(List.Second(expr), valueRib, env); } catch (CaptureException capEx) { capEx.AddContext(new MultArgContext()); throw; } Pair newRib = (multArgVal is ValueList) ? ((ValueList)multArgVal).Values : (multArgVal != null) ? new Pair(multArgVal, Pair.EmptyList) : Pair.EmptyList; return(newRib); } else if (instr == Instruction.Sequence) { try { object restSeq = List.Third(expr); if (restSeq != Pair.EmptyList) { Execute(restSeq, valueRib, env); } } catch (CaptureException capEx) { capEx.AddContext(new SeqContext(this, List.Second(expr), env)); throw; } return(Execute(List.Second(expr), valueRib, env)); } else { throw new System.Exception("Unexpected value: " + ((instr == null)?"[null]":instr)); } }
/// <summary> /// Force a promise. The promise is a proc: apply it. /// </summary> /// <param name="promise">A proc that will produce the result.</param> /// <param name="caller">The caller.</param> /// <returns>The result of applying the proc.</returns> private static EvaluatorOrObject Force(Procedure promise, Evaluator caller) { return promise.Apply(null, null, caller, caller); }
public override object Invoke(object val) { Procedure proc = (Procedure)val; return(proc.Apply(_i, _valueRib, _env)); }