public void ParseDefineVariable() { Parser parser = new Parser("var a;"); var result = parser.ParseCommand(); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(DefineVariableCommand)); DefineVariableCommand dvcmd = (DefineVariableCommand)result; Assert.AreEqual("a", dvcmd.Name); Assert.IsNull(dvcmd.Expression); }
public void ParseCompositeCommand() { Parser parser = new Parser("{ a = 1; b = 2; }"); var result = parser.ParseCommand(); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(CompositeCommand)); CompositeCommand command = (CompositeCommand)result; Assert.AreEqual(2, command.Commands.Count()); }
public void ParseArrayExpression() { Parser parser = new Parser("[1,2,3]"); var result = parser.ParseExpression(); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(ArrayExpression)); ArrayExpression expr = (ArrayExpression)result; Assert.IsNotNull(expr.Expressions); Assert.AreEqual(3, expr.Expressions.Count()); }
static void Main(string[] args) { System.Console.WriteLine("DartSharp 0.0.0"); Lexer lexer = new Lexer(System.Console.In); Context context = new Context(); context.SetValue("print", new Print(System.Console.Out)); Parser parser = new Parser(lexer); for (ICommand cmd = parser.ParseCommand(); cmd != null; cmd = parser.ParseCommand()) { cmd.Execute(context); } }
public void ParseDefineClassCommand() { Parser parser = new Parser("class MyClass { } "); var result = parser.ParseCommand(); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(DefineClassCommand)); DefineClassCommand command = (DefineClassCommand)result; Assert.AreEqual("MyClass", command.Name); Assert.IsNull(command.Command); Assert.IsNull(parser.ParseCommand()); }
public void RaiseIfEndOfInput() { Parser parser = new Parser("int myfun("); parser.ParseCommand(); }
public void RaiseIfBinaryOperator() { Parser parser = new Parser("=="); parser.ParseExpression(); }
public void ParseClosingParenthesisAsNoExpression() { Parser parser = new Parser(")"); Assert.IsNull(parser.ParseExpression()); }
private static object EvaluateExpression(string text, Context context) { Parser parser = new Parser(text); var result = parser.ParseExpression(); Assert.IsNull(parser.ParseExpression()); return result.Evaluate(context); }
public void ParseSimpleCall() { Parser parser = new Parser("prints(1)"); IExpression expr = parser.ParseExpression(); Assert.IsNotNull(expr); Assert.IsInstanceOfType(expr, typeof(CallExpression)); CallExpression cexpr = (CallExpression)expr; Assert.IsInstanceOfType(cexpr.Expression, typeof(VariableExpression)); Assert.AreEqual(1, cexpr.Arguments.Count()); Assert.IsInstanceOfType(cexpr.Arguments.First(), typeof(ConstantExpression)); }
public void RaiseIfSemicolonIsMissing() { Parser parser = new Parser("a=1"); parser.ParseCommand(); }
public void RaiseIfNoCommaToSeparateArguments() { Parser parser = new Parser("int myfun(int a int b) { return a+b; }"); parser.ParseCommand(); }
public void ParseTwoCommands() { Parser parser = new Parser("a=1;\r\nb=1;"); IList<ICommand> commands = parser.ParseCommands(); Assert.IsNotNull(commands); Assert.AreEqual(2, commands.Count); ICommand command = commands[0]; Assert.IsNotNull(command); Assert.IsInstanceOfType(command, typeof(SetVariableCommand)); SetVariableCommand scommand = (SetVariableCommand)command; Assert.AreEqual("a", scommand.Name); Assert.IsInstanceOfType(scommand.Expression, typeof(ConstantExpression)); ConstantExpression cexpr = (ConstantExpression)scommand.Expression; Assert.AreEqual(1, cexpr.Value); command = commands[1]; Assert.IsNotNull(command); Assert.IsInstanceOfType(command, typeof(SetVariableCommand)); scommand = (SetVariableCommand)command; Assert.AreEqual("b", scommand.Name); Assert.IsInstanceOfType(scommand.Expression, typeof(ConstantExpression)); cexpr = (ConstantExpression)scommand.Expression; Assert.AreEqual(1, cexpr.Value); Assert.IsNull(parser.ParseCommand()); }
public void ParseSimpleCallAsCommandPrecededByNewLine() { Parser parser = new Parser("\r\nprints(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 ParseTrueAsConstant() { Parser parser = new Parser("true"); var result = parser.ParseExpression(); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(ConstantExpression)); ConstantExpression expression = (ConstantExpression)result; Assert.AreEqual(true, expression.Value); }
public void ParseSimpleWhileCommand() { Parser parser = new Parser("while (a) \r\n b = 2;"); var result = parser.ParseCommand(); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(WhileCommand)); WhileCommand command = (WhileCommand)result; Assert.IsNotNull(command.Condition); Assert.IsNotNull(command.Command); }
public void ParseSimpleSum() { Parser parser = new Parser("1+2"); var result = parser.ParseExpression(); Assert.IsNull(parser.ParseExpression()); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(ArithmeticBinaryExpression)); ArithmeticBinaryExpression expression = (ArithmeticBinaryExpression)result; Assert.IsInstanceOfType(expression.LeftExpression, typeof(ConstantExpression)); Assert.IsInstanceOfType(expression.RightExpression, typeof(ConstantExpression)); }
public void ParseSimpleString() { Parser parser = new Parser("\"foo\""); IExpression expr = parser.ParseExpression(); Assert.IsNotNull(expr); Assert.IsInstanceOfType(expr, typeof(ConstantExpression)); ConstantExpression cexpr = (ConstantExpression)expr; Assert.AreEqual("foo", cexpr.Evaluate(null)); Assert.IsNull(parser.ParseExpression()); }
public void ParseSimpleIfCommand() { Parser parser = new Parser("if (a) \r\n b = 2;"); var result = parser.ParseCommand(); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(IfCommand)); IfCommand command = (IfCommand)result; Assert.IsNotNull(command.Condition); Assert.IsNotNull(command.ThenCommand); Assert.IsNull(command.ElseCommand); }
public void RaiseIfMissingClosingParenthesis() { Parser parser = new Parser("prints(1"); parser.ParseExpression(); }
public void RaiseIfMissingParenthesis() { Parser parser = new Parser("int myfun( { return a+b; }"); parser.ParseCommand(); }
public void ParseTwoLineSimpleAssignmentCommands() { Parser parser = new Parser("a=1;\r\nb=1;"); ICommand command = parser.ParseCommand(); Assert.IsNotNull(command); Assert.IsInstanceOfType(command, typeof(SetVariableCommand)); SetVariableCommand scommand = (SetVariableCommand)command; Assert.AreEqual("a", scommand.Name); Assert.IsInstanceOfType(scommand.Expression, typeof(ConstantExpression)); ConstantExpression cexpr = (ConstantExpression)scommand.Expression; Assert.AreEqual(1, cexpr.Value); command = parser.ParseCommand(); Assert.IsNotNull(command); Assert.IsInstanceOfType(command, typeof(SetVariableCommand)); scommand = (SetVariableCommand)command; Assert.AreEqual("b", scommand.Name); Assert.IsInstanceOfType(scommand.Expression, typeof(ConstantExpression)); cexpr = (ConstantExpression)scommand.Expression; Assert.AreEqual(1, cexpr.Value); Assert.IsNull(parser.ParseCommand()); }
public void RaiseIfNoTypeInArgument() { Parser parser = new Parser("int myfun(a, b) { return a+b; }"); parser.ParseCommand(); }
public void ParseTwoSimpleCallsAsCommands() { Parser parser = new Parser("print(1);\r\nprint(2);\r\n"); ICommand cmd = parser.ParseCommand(); Assert.IsNotNull(cmd); Assert.IsInstanceOfType(cmd, typeof(ExpressionCommand)); ExpressionCommand ccmd = (ExpressionCommand)cmd; Assert.IsInstanceOfType(ccmd.Expression, typeof(CallExpression)); cmd = parser.ParseCommand(); Assert.IsNotNull(cmd); Assert.IsInstanceOfType(cmd, typeof(ExpressionCommand)); ccmd = (ExpressionCommand)cmd; Assert.IsInstanceOfType(ccmd.Expression, typeof(CallExpression)); Assert.IsNull(parser.ParseCommand()); }
public void RaiseNameExpected() { Parser parser = new Parser("var 1;"); parser.ParseCommand(); }
public void ParseClosingBraceAsNoCommand() { Parser parser = new Parser("}"); Assert.IsNull(parser.ParseCommands()); }
private static void EvaluateCommands(string text, Context context) { Parser parser = new Parser(text); var result = parser.ParseCommands(); var command = new CompositeCommand(result); command.Execute(context); }
public void ParseVoidFunctionDefinition() { Parser parser = new Parser("void myfun() { a = 1; b = 2; }"); var result = parser.ParseCommand(); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(DefineFunctionCommand)); DefineFunctionCommand command = (DefineFunctionCommand)result; Assert.AreEqual("myfun", command.Name); Assert.IsInstanceOfType(command.Command, typeof(CompositeCommand)); }
public void ParseVariable() { Parser parser = new Parser("foo"); IExpression expr = parser.ParseExpression(); Assert.IsNotNull(expr); Assert.IsInstanceOfType(expr, typeof(VariableExpression)); VariableExpression vexpr = (VariableExpression)expr; Assert.AreEqual("foo", vexpr.Name); Assert.IsNull(parser.ParseExpression()); }
public void ParseSimpleCallWithParenthesisAndTwoArguments() { Parser parser = new Parser("myfunc(a, b)"); IExpression expr = parser.ParseExpression(); Assert.IsNotNull(expr); Assert.IsInstanceOfType(expr, typeof(CallExpression)); CallExpression cexpr = (CallExpression)expr; Assert.IsInstanceOfType(cexpr.Expression, typeof(VariableExpression)); Assert.AreEqual(2, cexpr.Arguments.Count()); Assert.IsInstanceOfType(cexpr.Arguments.First(), typeof(VariableExpression)); Assert.IsInstanceOfType(cexpr.Arguments.Skip(1).First(), typeof(VariableExpression)); }