コード例 #1
0
        public void SetVariable()
        {
            Context context = new Context();
            ConstantExpression expr = new ConstantExpression(1);
            SetVariableCommand command = new SetVariableCommand("One", expr);

            object result = command.Execute(context);

            Assert.AreEqual(1, result);
            Assert.AreEqual(1, context.GetValue("One"));
            Assert.AreEqual(expr, command.Expression);
            Assert.AreEqual("One", command.Name);
        }
コード例 #2
0
        public void WhileWithAddExpression()
        {
            var expression = new CompareExpression(ComparisonOperator.Less, new VariableExpression("a"), new ConstantExpression(10));
            var inccommand = new SetVariableCommand("k", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("k"), new ConstantExpression(1)));
            var addcommand = new SetVariableCommand("a", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("k"), new VariableExpression("a")));
            var command = new CompositeCommand(new ICommand[] { inccommand, addcommand });
            var whilecommand = new WhileCommand(expression, command);

            Context context = new Context();

            context.SetValue("a", 0);
            context.SetValue("k", 0);

            var result = whilecommand.Execute(context);

            Assert.IsNull(result);
            Assert.IsNotNull(whilecommand.Condition);
            Assert.IsNotNull(whilecommand.Command);
            Assert.AreEqual(4, context.GetValue("k"));
            Assert.AreEqual(10, context.GetValue("a"));
        }
コード例 #3
0
ファイル: Parser.cs プロジェクト: ajlopez/DartSharp
        public ICommand ParseCommand()
        {
            Token token = this.NextToken();

            if (token == null)
                return null;

            if (token.Type == TokenType.Separator)
            {
                if (token.Value == ";")
                    return NullCommand.Instance;

                if (token.Value == "{")
                {
                    ICommand commands = new CompositeCommand(this.ParseCommands());
                    this.ParseToken("}", TokenType.Separator);
                    return commands;
                }
            }

            if (token.Type == TokenType.Name)
            {
                if (token.Value == "if")
                    return this.ParseIfCommand();

                if (token.Value == "while")
                    return this.ParseWhileCommand();

                if (token.Value == "class")
                    return this.ParseDefineClassCommand();

                if (token.Value == "return")
                {
                    var expr = this.ParseExpression();
                    this.ParseToken(";", TokenType.Separator);
                    return new ReturnCommand(expr);
                }

                if (token.Value == "void")
                    return this.ParseDefineFunction(this.ParseName());

                if (token.Value == "main")
                    return this.ParseDefineFunction(token.Value);
            }

            this.PushToken(token);

            IExpression expression = this.ParseExpression();

            if (expression == null)
                return null;

            token = this.NextToken();

            if (token != null && token.Type == TokenType.Name && this.IsTypeExpression(expression))
            {
                if (this.TryPeekToken("(", TokenType.Separator))
                    return this.ParseDefineFunction(token.Value);

                return this.ParseDefineVariableCommand(expression, token.Value);
            }

            ICommand command = null;

            if (expression is VariableExpression && token.Type == TokenType.Operator && token.Value == "=")
            {
                string name = ((VariableExpression)expression).Name;
                IExpression expr = this.ParseExpression();

                command = new SetVariableCommand(name, expr);

                token = this.NextToken();
            }
            else
                command = new ExpressionCommand(expression);

            if (token == null || token.Type != TokenType.Separator || token.Value != ";")
                throw new ParserException("Expected ';'");

            return command;
        }