Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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());
        }
Exemplo n.º 3
0
        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());
        }
Exemplo n.º 4
0
        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);
            }
        }
Exemplo n.º 5
0
        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());
        }
Exemplo n.º 6
0
 public void RaiseIfEndOfInput()
 {
     Parser parser = new Parser("int myfun(");
     parser.ParseCommand();
 }
Exemplo n.º 7
0
 public void RaiseIfBinaryOperator()
 {
     Parser parser = new Parser("==");
     parser.ParseExpression();
 }
Exemplo n.º 8
0
 public void ParseClosingParenthesisAsNoExpression()
 {
     Parser parser = new Parser(")");
     Assert.IsNull(parser.ParseExpression());
 }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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));
        }
Exemplo n.º 11
0
 public void RaiseIfSemicolonIsMissing()
 {
     Parser parser = new Parser("a=1");
     parser.ParseCommand();
 }
Exemplo n.º 12
0
 public void RaiseIfNoCommaToSeparateArguments()
 {
     Parser parser = new Parser("int myfun(int a int b) { return a+b; }");
     parser.ParseCommand();
 }
Exemplo n.º 13
0
        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());
        }
Exemplo n.º 14
0
        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));
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
0
        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));
        }
Exemplo n.º 18
0
        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());
        }
Exemplo n.º 19
0
        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);
        }
Exemplo n.º 20
0
 public void RaiseIfMissingClosingParenthesis()
 {
     Parser parser = new Parser("prints(1");
     parser.ParseExpression();
 }
Exemplo n.º 21
0
 public void RaiseIfMissingParenthesis()
 {
     Parser parser = new Parser("int myfun( { return a+b; }");
     parser.ParseCommand();
 }
Exemplo n.º 22
0
        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());
        }
Exemplo n.º 23
0
 public void RaiseIfNoTypeInArgument()
 {
     Parser parser = new Parser("int myfun(a, b) { return a+b; }");
     parser.ParseCommand();
 }
Exemplo n.º 24
0
        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());
        }
Exemplo n.º 25
0
 public void RaiseNameExpected()
 {
     Parser parser = new Parser("var 1;");
     parser.ParseCommand();
 }
Exemplo n.º 26
0
 public void ParseClosingBraceAsNoCommand()
 {
     Parser parser = new Parser("}");
     Assert.IsNull(parser.ParseCommands());
 }
Exemplo n.º 27
0
        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);
        }
Exemplo n.º 28
0
        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));
        }
Exemplo n.º 29
0
        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());
        }
Exemplo n.º 30
0
        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));
        }