示例#1
0
文件: Program.cs 项目: ajlopez/AjLang
        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);
        }
示例#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());
        }
示例#3
0
        public void ParseEqualsOperator()
        {
            Parser parser = new Parser("a == 1");
            IExpression expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(CompareExpression));

            CompareExpression cexpr = (CompareExpression)expr;

            Assert.AreEqual(ComparisonOperator.Equal, cexpr.Operation);
            Assert.IsInstanceOfType(cexpr.LeftExpression, typeof(VariableExpression));
            Assert.IsInstanceOfType(cexpr.RightExpression, typeof(ConstantExpression));

            Assert.IsNull(parser.ParseExpression());
        }
示例#4
0
        public void ParseIfCommandWithMultipleLineAndElse()
        {
            Parser parser = new Parser("if a \r\n b = a\r\n else \r\n b = 1\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.IsNotNull(ifcommand.ElseCommand);

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

            Assert.IsNull(parser.ParseCommand());
        }
示例#5
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());
        }
示例#6
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());
        }
示例#7
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());
        }
示例#8
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());
        }
示例#9
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());
        }
示例#10
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());
        }
示例#11
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());
        }
示例#12
0
        public void ParseSimpleCallWithParenthesisAndTwoArguments()
        {
            Parser parser = new Parser("puts(a, b)");
            IExpression expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(CallExpression));

            CallExpression cexpr = (CallExpression)expr;

            Assert.IsInstanceOfType(cexpr.Expression, typeof(NameExpression));
            Assert.AreEqual(2, cexpr.Arguments.Count());
            Assert.IsInstanceOfType(cexpr.Arguments.First(), typeof(VariableExpression));
            Assert.IsInstanceOfType(cexpr.Arguments.Skip(1).First(), typeof(VariableExpression));
        }
示例#13
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));
        }
示例#14
0
        public void ParseSimpleCall()
        {
            Parser parser = new Parser("puts 1");
            IExpression expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(CallExpression));

            CallExpression cexpr = (CallExpression)expr;

            Assert.IsInstanceOfType(cexpr.Expression, typeof(NameExpression));
            Assert.AreEqual(1, cexpr.Arguments.Count());
            Assert.IsInstanceOfType(cexpr.Arguments.First(), typeof(ConstantExpression));
        }
示例#15
0
        public void ParseInteger()
        {
            Parser parser = new Parser("123");
            IExpression expr = parser.ParseExpression();

            Assert.IsNotNull(expr);
            Assert.IsInstanceOfType(expr, typeof(ConstantExpression));

            ConstantExpression cexpr = (ConstantExpression)expr;

            Assert.AreEqual(123, cexpr.Evaluate(null));

            Assert.IsNull(parser.ParseExpression());
        }
示例#16
0
        public void ParseIfCommandWithOneLineThen()
        {
            Parser parser = new Parser("if a then b = a 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());
        }