public void Execute(IContext context) { BindingEnvironment environment = context as BindingEnvironment; while (!Predicates.IsFalse(this.condition.Evaluate(context))) { this.command.Execute(context); if (environment != null) { if (environment.HasReturnValue()) { return; } if (environment.WasBreak) { environment.WasBreak = false; break; } if (environment.WasContinue) { environment.WasContinue = false; } } } }
public void SetAndGetReturnValue() { BindingEnvironment environment = new BindingEnvironment(); environment.SetReturnValue(1); Assert.IsTrue(environment.HasReturnValue()); Assert.AreEqual(1, environment.GetReturnValue()); }
public void CreateAndEvaluateReturnCommandWithoutValue() { BindingEnvironment environment = new BindingEnvironment(); ICommand command = new ReturnCommand(null); command.Execute(environment); Assert.IsTrue(environment.HasReturnValue()); Assert.IsNull(environment.GetReturnValue()); }
public void CreateAndEvaluateReturnCommand() { BindingEnvironment environment = new BindingEnvironment(); ICommand command = new ReturnCommand(new ConstantExpression(1)); command.Execute(environment); Assert.IsTrue(environment.HasReturnValue()); Assert.AreEqual(1, environment.GetReturnValue()); }
public void CanCreate() { BindingEnvironment environment = new BindingEnvironment(); Assert.IsNotNull(environment); Assert.IsNotNull(environment.GlobalContext); Assert.AreEqual(environment, environment.GlobalContext); Assert.IsNull(environment.Parent); Assert.IsFalse(environment.HasReturnValue()); Assert.IsNull(environment.GetReturnValue()); }
public void Execute(IContext context) { BindingEnvironment environment = context as BindingEnvironment; foreach (ICommand command in this.commands) { command.Execute(context); if (environment != null && (environment.HasReturnValue() || environment.WasContinue || environment.WasBreak)) { break; } } }
public void ExecuteCompositeCommandWithReturn() { SetCommand command1 = new SetCommand("foo", new ConstantExpression("bar")); ReturnCommand command2 = new ReturnCommand(new ConstantExpression("spam")); SetCommand command3 = new SetCommand("one", new ConstantExpression(1)); CompositeCommand command = new CompositeCommand(); command.AddCommand(command1); command.AddCommand(command2); command.AddCommand(command3); Machine machine = new Machine(); BindingEnvironment environment = new BindingEnvironment(machine.Environment); command.Execute(environment); Assert.AreEqual("bar", environment.GetValue("foo")); Assert.IsNull(environment.GetValue("one")); Assert.IsTrue(environment.HasReturnValue()); Assert.AreEqual("spam", environment.GetReturnValue()); Assert.IsNotNull(command.Commands); }
public object Apply(IContext ctx, IList <object> arguments, IDictionary <string, object> namedArguments) { BindingEnvironment newcontext = new BindingEnvironment(this.context); int nargs = 0; if (arguments != null) { nargs = arguments.Count; } if (nargs < this.nminparameters || nargs > this.nmaxparameters) { throw new TypeError(string.Format("{0}() takes {4} {1} positional argument{2} ({3} given)", this.name, this.nminparameters, this.nminparameters == 1 ? string.Empty : "s", nargs, this.hasdefault ? "at least" : "exactly")); } if (namedArguments != null) { foreach (var namarg in namedArguments) { newcontext.SetValue(namarg.Key, namarg.Value); } } if (this.parameters != null) { int k; for (k = 0; k < this.parameters.Count; k++) { if (arguments != null && arguments.Count > k) { if (namedArguments != null && namedArguments.ContainsKey(this.parameters[k].Name)) { throw new TypeError(string.Format("{0}() got multiple values for keyword argument '{1}'", this.name, this.parameters[k].Name)); } if (this.parameters[k].IsList) { newcontext.SetValue(this.parameters[k].Name, GetSublist(arguments, k)); } else { newcontext.SetValue(this.parameters[k].Name, arguments[k]); } } else if (this.parameters[k].IsList) { if (this.parameters[k].DefaultValue == null) { newcontext.SetValue(this.parameters[k].Name, new List <object>()); } else { newcontext.SetValue(this.parameters[k].Name, this.parameters[k].DefaultValue); } break; } else if (namedArguments == null || !namedArguments.ContainsKey(this.parameters[k].Name)) { newcontext.SetValue(this.parameters[k].Name, this.parameters[k].DefaultValue); } } } this.body.Execute(newcontext); if (newcontext.HasReturnValue()) { return(newcontext.GetReturnValue()); } return(null); }
public object Apply(IContext ctx, IList<object> arguments, IDictionary<string, object> namedArguments) { BindingEnvironment newcontext = new BindingEnvironment(this.context); int nargs = 0; if (arguments != null) nargs = arguments.Count; if (nargs < this.nminparameters || nargs > this.nmaxparameters) throw new TypeError(string.Format("{0}() takes {4} {1} positional argument{2} ({3} given)", this.name, this.nminparameters, this.nminparameters == 1 ? string.Empty : "s", nargs, this.hasdefault ? "at least" : "exactly")); if (namedArguments != null) foreach (var namarg in namedArguments) newcontext.SetValue(namarg.Key, namarg.Value); if (this.parameters != null) { int k; for (k = 0; k < this.parameters.Count; k++) if (arguments != null && arguments.Count > k) { if (namedArguments != null && namedArguments.ContainsKey(this.parameters[k].Name)) throw new TypeError(string.Format("{0}() got multiple values for keyword argument '{1}'", this.name, this.parameters[k].Name)); if (this.parameters[k].IsList) newcontext.SetValue(this.parameters[k].Name, GetSublist(arguments, k)); else newcontext.SetValue(this.parameters[k].Name, arguments[k]); } else if (this.parameters[k].IsList) { if (this.parameters[k].DefaultValue == null) newcontext.SetValue(this.parameters[k].Name, new List<object>()); else newcontext.SetValue(this.parameters[k].Name, this.parameters[k].DefaultValue); break; } else if (namedArguments == null || !namedArguments.ContainsKey(this.parameters[k].Name)) newcontext.SetValue(this.parameters[k].Name, this.parameters[k].DefaultValue); } this.body.Execute(newcontext); if (newcontext.HasReturnValue()) return newcontext.GetReturnValue(); return null; }