Example #1
0
        static void Main(string[] args)
        {
            Lexer lexer = new Lexer(System.Console.In);
            Context context = new Context();
            context.SetValue("puts", new Puts(System.Console.Out));
            Parser parser = new Parser(lexer);

            for (ICommand cmd = parser.ParseCommand(); cmd != null; cmd = parser.ParseCommand())
                cmd.Execute(context);
        }
Example #2
0
        public void ParseForCommandMultiline()
        {
            Parser parser = new Parser("for k in a \r\n b = k\r\nend");
            ICommand command = parser.ParseCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ForCommand));

            ForCommand forcommand = (ForCommand)command;

            Assert.AreEqual("k", forcommand.VariableName);
            Assert.IsNotNull(forcommand.Expression);
            Assert.IsNotNull(forcommand.Command);

            Assert.IsNull(parser.ParseCommand());
        }
Example #3
0
        public void ParseIfCommandWithMultipleLine()
        {
            Parser parser = new Parser("if a \r\n b = a\r\n end");
            ICommand command = parser.ParseCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(IfCommand));

            IfCommand ifcommand = (IfCommand)command;

            Assert.IsNotNull(ifcommand.Condition);
            Assert.IsNotNull(ifcommand.ThenCommand);
            Assert.IsNull(ifcommand.ElseCommand);

            Assert.IsInstanceOfType(ifcommand.Condition, typeof(VariableExpression));

            Assert.IsNull(parser.ParseCommand());
        }
Example #4
0
        public void ParseWhileCommandMultiline()
        {
            Parser parser = new Parser("while a \r\n a = b\r\nend");
            ICommand command = parser.ParseCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(WhileCommand));

            WhileCommand whilecommand = (WhileCommand)command;

            Assert.IsNotNull(whilecommand.Condition);
            Assert.IsNotNull(whilecommand.Command);

            Assert.IsInstanceOfType(whilecommand.Condition, typeof(VariableExpression));

            Assert.IsNull(parser.ParseCommand());
        }
Example #5
0
        public void ParseTwoSimpleCallsAsCommands()
        {
            Parser parser = new Parser("puts 1\r\nputs 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());
        }
Example #6
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());
        }
Example #7
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());
        }
Example #8
0
        public void ParseSimpleDefineCommandWithArguments()
        {
            Parser parser = new Parser("def foo(x, y)\r\na=x\r\nb=y\r\nend\r\n");
            ICommand command = parser.ParseCommand();

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(DefineCommand));

            DefineCommand defcommand = (DefineCommand)command;

            Assert.AreEqual("foo", defcommand.Name);
            Assert.IsNotNull(defcommand.ArgumentNames);
            Assert.AreEqual(2, defcommand.ArgumentNames.Count());

            Assert.IsNotNull(defcommand.Command);
            Assert.IsInstanceOfType(defcommand.Command, typeof(CompositeCommand));

            CompositeCommand commands = (CompositeCommand) defcommand.Command;

            Assert.IsNotNull(commands.Commands);
            Assert.AreEqual(2, commands.Commands.Count());

            Assert.IsNull(parser.ParseCommand());
        }
Example #9
0
        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));
        }