protected override void Generate(ExpressionCommand cmd) { this.TextRenderer.Write("_Template.Write(() => ", cmd.Begin.Position); this.TextRenderer.Write(cmd.Expression.Text, cmd.Expression.Position, EntryFeatures.ColumnInterpolation); this.TextRenderer.Write($", {this.SourceExpression(cmd.Expression.Position)}, {this.FeatureExpression(EntryFeatures.None)});", cmd.End.Position); this.TextRenderer.WriteLine(cmd.End.Position); }
public ICommand ParseCommand() { Token token = this.lexer.NextToken(); if (token == null) { return(null); } ICommand command; if (token.TokenType == TokenType.Name && this.TryParse(TokenType.Operator, "=")) { command = this.ParseSetCommand(token.Value); this.ParseEndOfCommand(); return(command); } this.lexer.PushToken(token); command = new ExpressionCommand(this.ParseExpression()); this.ParseEndOfCommand(); return(command); }
public void ExecuteConstantExpression() { ConstantExpression expr = new ConstantExpression(1); ExpressionCommand command = new ExpressionCommand(expr); Assert.AreEqual(1, command.Execute(null)); Assert.AreEqual(expr, command.Expression); }
public void CreateExpressionCommand() { IExpression expr = new ConstantExpression(1); ExpressionCommand cmd = new ExpressionCommand(expr); Assert.IsNotNull(cmd.Expression); Assert.AreSame(expr, cmd.Expression); }
public void GetConstant() { ExpressionCommand command = new ExpressionCommand(new ConstantExpression(123)); var result = command.Execute(null); Assert.IsNotNull(result); Assert.AreEqual(123, result); }
public void EvaluateFunctionWithoutReturn() { ICommand body = new ExpressionCommand(new BinaryOperatorExpression(BinaryOperator.Add, new NameExpression("a"), new NameExpression("b"))); Function fn = new Function(new string[] { "a", "b" }, body); var result = fn.Evaluate(null, new object[] { 1, 2 }); Assert.IsNull(result); }
public void ExecuteExpressionCommand() { IExpression expr = new AssignExpression("a", new ConstantExpression(1)); ExpressionCommand cmd = new ExpressionCommand(expr); Context context = new Context(); cmd.Execute(context); Assert.AreEqual(1, context.GetValue("a")); }
public void CreateCompositeCommand() { ICommand cmd1 = new ExpressionCommand(new ConstantExpression(1)); ICommand cmd2 = new ExpressionCommand(new ConstantExpression(2)); IList <ICommand> cmds = new ICommand[] { cmd1, cmd2 }; CompositeCommand cmd = new CompositeCommand(cmds); Assert.IsNotNull(cmd.Commands); Assert.AreSame(cmds, cmd.Commands); }
public void ExecuteIfCommandWithNullCondition() { Context context = new Context(); IExpression cond = new ConstantExpression(null); ICommand thencmd = new ExpressionCommand(new AssignExpression("a", new ConstantExpression(42))); IfCommand cmd = new IfCommand(cond, thencmd); cmd.Execute(context); Assert.IsNull(context.GetValue("a")); }
public void ExecuteIfCommandWithTrueCondition() { Context context = new Context(); IExpression cond = new ConstantExpression(true); ICommand thencmd = new ExpressionCommand(new AssignExpression("a", new ConstantExpression(42))); IfCommand cmd = new IfCommand(cond, thencmd); cmd.Execute(context); Assert.AreEqual(42, context.GetValue("a")); }
public void CreateWhileCommand() { IExpression cond = new ConstantExpression(1); ICommand cmd = new ExpressionCommand(new ConstantExpression(2)); WhileCommand wcmd = new WhileCommand(cond, cmd); Assert.IsNotNull(wcmd.Condition); Assert.AreSame(cond, wcmd.Condition); Assert.IsNotNull(wcmd.Command); Assert.AreSame(cmd, wcmd.Command); }
public void ParseExpressionCommand() { ICommand command = ParseCommand("foo.bar(1,2);"); Assert.IsNotNull(command); Assert.IsInstanceOfType(command, typeof(ExpressionCommand)); ExpressionCommand exprcmd = (ExpressionCommand)command; Assert.IsNotNull(exprcmd.Expression); Assert.IsInstanceOfType(exprcmd.Expression, typeof(DotExpression)); }
public void CreateFunction() { ICommand body = new ExpressionCommand(new ConstantExpression(1)); Function fn = new Function(new string[] { "a", "b" }, body); Assert.IsNotNull(fn.ArgumentNames); Assert.AreEqual(2, fn.ArgumentNames.Count); Assert.AreEqual("a", fn.ArgumentNames[0]); Assert.AreEqual("b", fn.ArgumentNames[1]); Assert.IsNotNull(fn.Body); Assert.AreSame(body, fn.Body); }
public void GetName() { Context context = new Context(); context.SetMember("one", 1); ExpressionCommand command = new ExpressionCommand(new NameExpression("one")); var result = command.Execute(context); Assert.IsNotNull(result); Assert.AreEqual(1, result); }
public void ParseSimpleCallAsCommandPrecededByNewLine() { Parser parser = new Parser("\r\nprints(1);"); ICommand cmd = parser.ParseCommand(); Assert.IsNotNull(cmd); Assert.That(cmd is ExpressionCommand); ExpressionCommand ccmd = (ExpressionCommand)cmd; Assert.That(ccmd.Expression is CallExpression); }
public void ExecuteIfCommandWithNullConditionAndElse() { Context context = new Context(); IExpression cond = new ConstantExpression(null); ICommand thencmd = new ExpressionCommand(new AssignExpression("a", new ConstantExpression(42))); ICommand elsecmd = new ExpressionCommand(new AssignExpression("a", new ConstantExpression(123))); IfCommand cmd = new IfCommand(cond, thencmd, elsecmd); cmd.Execute(context); Assert.AreEqual(123, context.GetValue("a")); }
public void ParseSimpleCallAsCommandPrecededByNewLine() { Parser parser = new Parser("\r\nputs 1"); ICommand cmd = parser.ParseCommand(); Assert.IsNotNull(cmd); Assert.IsInstanceOfType(cmd, typeof(ExpressionCommand)); ExpressionCommand ccmd = (ExpressionCommand)cmd; Assert.IsInstanceOfType(ccmd.Expression, typeof(CallExpression)); }
public void ParseSimpleCallAsCommand() { Parser parser = new Parser("prints(1);"); ICommand cmd = parser.ParseCommand(); Assert.IsNotNull(cmd); Assert.IsInstanceOfType(cmd, typeof(ExpressionCommand)); ExpressionCommand ccmd = (ExpressionCommand)cmd; Assert.IsInstanceOfType(ccmd.Expression, typeof(CallExpression)); }
public void ParseSimpleIncrement() { ICommand command = ParseCommand("k++;"); Assert.IsNotNull(command); Assert.IsInstanceOfType(command, typeof(ExpressionCommand)); ExpressionCommand expcmd = (ExpressionCommand)command; Assert.IsNotNull(expcmd.Expression); Assert.IsInstanceOfType(expcmd.Expression, typeof(IncrementExpression)); }
public void CreateIfCommand() { IExpression cond = new ConstantExpression(1); ICommand thencmd = new ExpressionCommand(new ConstantExpression(2)); IfCommand cmd = new IfCommand(cond, thencmd); Assert.IsNotNull(cmd.Condition); Assert.AreSame(cond, cmd.Condition); Assert.IsNotNull(cmd.ThenCommand); Assert.AreSame(thencmd, cmd.ThenCommand); Assert.IsNull(cmd.ElseCommand); }
public void CreateFunctionCommand() { string name = "foo"; IList <string> argnames = new string[] { "a", "b" }; ICommand body = new ExpressionCommand(new ConstantExpression(2)); FunctionCommand fcmd = new FunctionCommand(name, argnames, body); Assert.AreEqual(name, fcmd.Name); Assert.AreSame(argnames, fcmd.ArgumentNames); Assert.AreSame(body, fcmd.Body); }
public void CreateForCommand() { IExpression fromexpr = new ConstantExpression(1); IExpression toexpr = new ConstantExpression(2); ICommand body = new ExpressionCommand(new ConstantExpression(2)); ForCommand fcmd = new ForCommand("k", fromexpr, toexpr, body); Assert.AreEqual("k", fcmd.Name); Assert.AreSame(fromexpr, fcmd.FromExpression); Assert.AreSame(toexpr, fcmd.ToExpression); Assert.AreSame(body, fcmd.Body); }
public void ExecuteWhileCommandWithTrueCondition() { Context context = new Context(); IExpression cond = new NameExpression("a"); ICommand cmd = new ExpressionCommand(new AssignExpression("a", new ConstantExpression(false))); WhileCommand wcmd = new WhileCommand(cond, cmd); context.SetValue("a", true); wcmd.Execute(context); Assert.AreEqual(false, context.GetValue("a")); }
public void ParseHostedInvocationAsCommand() { ICommand command = ParseCommand("at host function(x,y) {return x+y;} with (1,2);"); Assert.IsNotNull(command); Assert.IsInstanceOfType(command, typeof(ExpressionCommand)); ExpressionCommand expcmd = (ExpressionCommand)command; Assert.IsInstanceOfType(expcmd.Expression, typeof(HostedInvocationExpression)); HostedInvocationExpression hexpr = (HostedInvocationExpression)expcmd.Expression; Assert.IsInstanceOfType(hexpr.HostExpression, typeof(VariableExpression)); Assert.AreEqual(2, hexpr.Arguments.Count); }
public void ExecuteForCommand() { IExpression fromexpr = new ConstantExpression(1); IExpression toexpr = new ConstantExpression(4); ICommand body = new ExpressionCommand(new AssignExpression("a", new BinaryOperatorExpression(BinaryOperator.Multiply, new NameExpression("a"), new NameExpression("k")))); ForCommand fcmd = new ForCommand("k", fromexpr, toexpr, body); Context context = new Context(); context.SetValue("a", 1); fcmd.Execute(context); Assert.AreEqual(24, context.GetValue("a")); }
public void EvaluateFunctionCommand() { string name = "foo"; IList <string> argnames = new string[] { "a", "b" }; ICommand body = new ExpressionCommand(new ConstantExpression(2)); Context context = new Context(); FunctionCommand fcmd = new FunctionCommand(name, argnames, body); fcmd.Execute(context); var result = context.GetValue("foo"); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(Function)); }
public void ExecuteCompositeCommand() { IExpression expr1 = new AssignExpression("a", new ConstantExpression(1)); ExpressionCommand cmd1 = new ExpressionCommand(expr1); IExpression expr2 = new AssignExpression("b", new ConstantExpression(2)); ExpressionCommand cmd2 = new ExpressionCommand(expr2); IList <ICommand> cmds = new ICommand[] { cmd1, cmd2 }; CompositeCommand cmd = new CompositeCommand(cmds); Context context = new Context(); cmd.Execute(context); Assert.AreEqual(1, context.GetValue("a")); Assert.AreEqual(2, context.GetValue("b")); }
private DefCommand ParseDefCommand() { string name = this.ParseName(); IList <ArgumentInfo> arguments = new List <ArgumentInfo>(); if (this.TryParseToken(TokenType.Delimiter, "(")) { while (!this.TryParseToken(TokenType.Delimiter, ")")) { if (arguments.Count > 0) { this.ParseToken(TokenType.Delimiter, ","); } string argname = this.ParseName(); this.ParseToken(TokenType.Delimiter, ":"); TypeInfo ti = this.ParseTypeInfo(); arguments.Add(new ArgumentInfo(argname, ti)); } } TypeInfo typeinfo = null; if (this.TryParseToken(TokenType.Delimiter, ":")) { typeinfo = this.ParseTypeInfo(); } ICommand body = null; if (this.TryParseToken(TokenType.Operator, "=")) { body = new ExpressionCommand(this.ParseExpression()); } if (typeinfo == null && body == null) { throw new ParserException("Expected ':' or '='"); } this.ParseEndOfCommand(); return(new DefCommand(name, arguments, typeinfo, body)); }
public void ExecuteCompositeCommandWithReturn() { IExpression expr1 = new AssignExpression("a", new ConstantExpression(1)); ExpressionCommand cmd1 = new ExpressionCommand(expr1); ICommand cmd2 = new ReturnCommand(new ConstantExpression(3)); IExpression expr3 = new AssignExpression("b", new ConstantExpression(2)); ExpressionCommand cmd3 = new ExpressionCommand(expr3); IList <ICommand> cmds = new ICommand[] { cmd1, cmd2, cmd3 }; CompositeCommand cmd = new CompositeCommand(cmds); Context context = new Context(); cmd.Execute(context); Assert.AreEqual(1, context.GetValue("a")); Assert.IsTrue(context.HasReturn); Assert.AreEqual(3, context.ReturnValue); Assert.IsNull(context.GetValue("b")); }
public void ParseTwoSimpleCallsAsCommands() { Parser parser = new Parser("print(1);\r\nprint(2);\r\n"); ICommand cmd = parser.ParseCommand(); Assert.IsNotNull(cmd); Assert.That(cmd is ExpressionCommand); ExpressionCommand ccmd = (ExpressionCommand)cmd; Assert.That(ccmd.Expression is CallExpression); cmd = parser.ParseCommand(); Assert.IsNotNull(cmd); Assert.That(cmd is ExpressionCommand); ccmd = (ExpressionCommand)cmd; Assert.That(ccmd.Expression is CallExpression); Assert.IsNull(parser.ParseCommand()); }