public void SetVariable() { IExpression expression = new ConstantExpression(1); BindingEnvironment environment = new BindingEnvironment(); ICommand command = new SetCommand("spam", expression); command.Execute(environment); Assert.AreEqual(1, environment.GetValue("spam")); }
public void ExecuteSetCommand() { SetCommand command = new SetCommand("foo", new ConstantExpression("bar")); Machine machine = new Machine(); command.Execute(machine.Environment); Assert.AreEqual("bar", machine.Environment.GetValue("foo")); }
public void CreateSimpleDefinedFunction() { IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, false) }; ICommand body = new SetCommand("c", new NameExpression("a")); DefinedFunction func = new DefinedFunction("foo", parameters, body, null); Assert.AreEqual(parameters, func.Parameters); Assert.AreEqual(body, func.Body); }
public void CreateAndExecuteTryCommand() { BindingEnvironment environment = new BindingEnvironment(); ICommand body = new SetCommand("a", new ConstantExpression(1)); TryCommand command = new TryCommand(body); command.Execute(environment); Assert.AreEqual(1, environment.GetValue("a")); }
public void CreateSimpleDefCommand() { IList<ParameterExpression> parameters = new ParameterExpression[] { new ParameterExpression("a", null, false), new ParameterExpression("b", null, false) }; ICommand body = new SetCommand("c", new ConstantExpression(1)); DefCommand command = new DefCommand("foo", parameters, body); Assert.AreEqual("foo", command.Name); Assert.AreEqual(parameters, command.ParameterExpressions); Assert.AreEqual(body, command.Body); }
public void CreateSetCommand() { IExpression expression = new ConstantExpression("bar"); SetCommand command = new SetCommand("foo", expression); Assert.IsNotNull(command); Assert.IsNotNull(command.Target); Assert.IsNotNull(command.Expression); Assert.AreEqual("foo", command.Target); Assert.AreEqual(expression, command.Expression); }
public void ExecuteSimpleForOnEmptyList() { ICommand body = new SetCommand("b", new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add)); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("b", 0); ForCommand command = new ForCommand("a", new ConstantExpression(new object[] { }), body); command.Execute(environment); Assert.AreEqual(0, environment.GetValue("b")); }
public void CreateAndEvaluateSimpleWhileCommand() { BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("a", 1); IExpression condition = new CompareExpression(ComparisonOperator.Less, new NameExpression("a"), new ConstantExpression(10)); ICommand body = new SetCommand("a", new BinaryOperatorExpression(new NameExpression("a"), new ConstantExpression(1), BinaryOperator.Add)); WhileCommand command = new WhileCommand(condition, body); command.Execute(environment); Assert.AreEqual(condition, command.Condition); Assert.AreEqual(body, command.Command); Assert.AreEqual(10, environment.GetValue("a")); }
public void ExecuteSimpleForWithContinue() { ICommand ifcmd = new IfCommand(new CompareExpression(ComparisonOperator.Equal, new NameExpression("a"), new ConstantExpression(2)), new ContinueCommand()); ICommand setcmd = new SetCommand("b", new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add)); ICommand body = new CompositeCommand(new ICommand[] { ifcmd, setcmd }); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("b", 0); ForCommand command = new ForCommand("a", new ConstantExpression(new object[] { 1, 2, 3 }), body); command.Execute(environment); Assert.AreEqual(4, environment.GetValue("b")); }
public void RaiseWhenNonDefaultArgumentFollowsDefaultArgument() { IList<ParameterExpression> parameters = new ParameterExpression[] { new ParameterExpression("a", new ConstantExpression(1), false), new ParameterExpression("b", null, false) }; ICommand body = new SetCommand("c", new ConstantExpression(1)); try { DefCommand command = new DefCommand("foo", parameters, body); Assert.Fail("Exception expected"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(SyntaxError)); Assert.AreEqual("non-default argument follows default argument", ex.Message); } }
public void ExecuteCompositeCommand() { SetCommand command1 = new SetCommand("foo", new ConstantExpression("bar")); SetCommand command2 = new SetCommand("one", new ConstantExpression(1)); CompositeCommand command = new CompositeCommand(); command.AddCommand(command1); command.AddCommand(command2); Machine machine = new Machine(); command.Execute(machine.Environment); Assert.AreEqual("bar", machine.Environment.GetValue("foo")); Assert.AreEqual(1, machine.Environment.GetValue("one")); Assert.IsNotNull(command.Commands); }
public void ExecuteSimpleDefCommand() { IList<ParameterExpression> parameters = new ParameterExpression[] { new ParameterExpression("a", null, false), new ParameterExpression("b", null, false) }; ICommand body = new SetCommand("c", new ConstantExpression(1)); DefCommand command = new DefCommand("foo", parameters, body); Machine machine = new Machine(); command.Execute(machine.Environment); var func = machine.Environment.GetValue("foo"); Assert.IsNotNull(func); Assert.IsInstanceOfType(func, typeof(IFunction)); Assert.IsInstanceOfType(func, typeof(DefinedFunction)); var dfunc = (DefinedFunction)func; Assert.AreEqual(parameters.Count, dfunc.Parameters.Count); Assert.AreEqual(body, dfunc.Body); }
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 void ExecuteFinallyEvenWhenRaiseException() { BindingEnvironment environment = new BindingEnvironment(); ICommand body = new SetCommand("a", new NameExpression("c")); ICommand @finally = new SetCommand("b", new ConstantExpression(2)); TryCommand command = new TryCommand(body); command.SetFinally(@finally); try { command.Execute(environment); Assert.Fail("Exception expected"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NameError)); Assert.AreEqual("name 'c' is not defined", ex.Message); } Assert.IsNull(environment.GetValue("a")); Assert.AreEqual(2, environment.GetValue("b")); }
public void RaiseIfExpressionIsNullForSetCommand() { SetCommand command = new SetCommand("foo", null); }
public void RaiseIfNameIsNullForSetCommand() { IExpression expression = new ConstantExpression("bar"); SetCommand command = new SetCommand(null, expression); }
private ICommand CompileSimpleCommand() { Token token = this.TryCompile(TokenType.Name); ICommand command; if (token == null) { command = this.CompileExpressionCommand(); this.CompileEndOfCommand(); return command; } if (token.Value == "import") { string name = this.CompileName(true).Value; while (this.TryCompile(TokenType.Operator, ".")) name += "." + this.CompileName(true).Value; this.CompileEndOfCommand(); return new ImportCommand(name); } if (token.Value == "from") { string name = this.CompileName(true).Value; while (this.TryCompile(TokenType.Operator, ".")) name += "." + this.CompileName(true).Value; this.CompileName("import"); if (this.TryCompile(TokenType.Operator, "*")) return new ImportFromCommand(name); IList<string> names = this.CompileNameList(); this.CompileEndOfCommand(); return new ImportFromCommand(name, names); } if (token.Value == "if") return this.CompileIfCommand(); if (token.Value == "class") return this.CompileClassCommand(); if (token.Value == "for") return this.CompileForCommand(); if (token.Value == "while") return this.CompileWhileCommand(); if (token.Value == "break") return new BreakCommand(); if (token.Value == "continue") return new ContinueCommand(); if (token.Value == "def") return this.CompileDefCommand(); if (token.Value == "try") return this.CompileTryCommand(); if (token.Value == "pass") { this.CompileEndOfCommand(); return new PassCommand(); } if (token.Value == "return") return this.CompileReturnCommand(); this.lexer.PushToken(token); var exprcommand = this.CompileExpressionCommand(); if (!this.TryCompile(TokenType.Operator, "=")) { this.CompileEndOfCommand(); return exprcommand; } var valueexpr = this.CompileExpression(); if (exprcommand.Expression is NameExpression) { command = new SetCommand(((NameExpression)exprcommand.Expression).Name, valueexpr); this.CompileEndOfCommand(); return command; } if (exprcommand.Expression is AttributeExpression) { command = new SetAttributeCommand(((AttributeExpression)exprcommand.Expression).Expression, ((AttributeExpression)exprcommand.Expression).Name, valueexpr); this.CompileEndOfCommand(); return command; } if (exprcommand.Expression is IndexedExpression) { var indexedexpr = (IndexedExpression)exprcommand.Expression; command = new SetIndexCommand(indexedexpr.TargetExpression, indexedexpr.IndexExpression, valueexpr); return command; } throw new SyntaxError("invalid assignment"); }