示例#1
0
        public void CreateSetIndexCommand()
        {
            IExpression targetExpression = new ConstantExpression(1);
            IExpression indexExpression  = new ConstantExpression(2);
            IExpression valueExpression  = new ConstantExpression(3);

            var command = new SetIndexCommand(targetExpression, indexExpression, valueExpression);

            Assert.AreEqual(targetExpression, command.TargetExpression);
            Assert.AreEqual(indexExpression, command.IndexExpression);
            Assert.AreEqual(valueExpression, command.Expression);
        }
示例#2
0
        public void ExecuteSetIndexCommandOnDictionary()
        {
            var         dictionary       = new Hashtable();
            IExpression targetExpression = new ConstantExpression(dictionary);
            IExpression indexExpression  = new ConstantExpression("foo");
            IExpression valueExpression  = new ConstantExpression("bar");

            var command = new SetIndexCommand(targetExpression, indexExpression, valueExpression);

            command.Execute(null);

            Assert.AreEqual("bar", dictionary["foo"]);
        }
示例#3
0
        public void ExecuteSetIndexCommandOnArray()
        {
            var         array            = new object[] { 1, 2, 2 };
            IExpression targetExpression = new ConstantExpression(array);
            IExpression indexExpression  = new ConstantExpression(2);
            IExpression valueExpression  = new ConstantExpression(3);

            var command = new SetIndexCommand(targetExpression, indexExpression, valueExpression);

            command.Execute(null);

            Assert.AreEqual(3, array[2]);
        }
示例#4
0
        public void ExecuteSetIndexCommandOnList()
        {
            var list = new List <object>()
            {
                1, 2, 2
            };
            IExpression targetExpression = new ConstantExpression(list);
            IExpression indexExpression  = new ConstantExpression(2);
            IExpression valueExpression  = new ConstantExpression(3);

            var command = new SetIndexCommand(targetExpression, indexExpression, valueExpression);

            command.Execute(null);

            Assert.AreEqual(3, list[2]);
        }
示例#5
0
        private ICommand CompileSimpleCommand()
        {
            Token token = this.TryCompile(TokenType.Name);

            ICommand command;

            if (token == null)
            {
                command = this.CompileExpressionCommand();
                this.CompileEndOfCommand();
                return(command);
            }

            if (token.Value == "import")
            {
                string name = this.CompileName(true).Value;

                while (this.TryCompile(TokenType.Operator, "."))
                {
                    name += "." + this.CompileName(true).Value;
                }

                this.CompileEndOfCommand();

                return(new ImportCommand(name));
            }

            if (token.Value == "from")
            {
                string name = this.CompileName(true).Value;

                while (this.TryCompile(TokenType.Operator, "."))
                {
                    name += "." + this.CompileName(true).Value;
                }

                this.CompileName("import");

                if (this.TryCompile(TokenType.Operator, "*"))
                {
                    return(new ImportFromCommand(name));
                }

                IList <string> names = this.CompileNameList();

                this.CompileEndOfCommand();

                return(new ImportFromCommand(name, names));
            }

            if (token.Value == "if")
            {
                return(this.CompileIfCommand());
            }

            if (token.Value == "class")
            {
                return(this.CompileClassCommand());
            }

            if (token.Value == "for")
            {
                return(this.CompileForCommand());
            }

            if (token.Value == "while")
            {
                return(this.CompileWhileCommand());
            }

            if (token.Value == "break")
            {
                return(new BreakCommand());
            }

            if (token.Value == "continue")
            {
                return(new ContinueCommand());
            }

            if (token.Value == "def")
            {
                return(this.CompileDefCommand());
            }

            if (token.Value == "try")
            {
                return(this.CompileTryCommand());
            }

            if (token.Value == "pass")
            {
                this.CompileEndOfCommand();
                return(new PassCommand());
            }

            if (token.Value == "return")
            {
                return(this.CompileReturnCommand());
            }

            this.lexer.PushToken(token);

            var exprcommand = this.CompileExpressionCommand();

            if (!this.TryCompile(TokenType.Operator, "="))
            {
                this.CompileEndOfCommand();
                return(exprcommand);
            }

            var valueexpr = this.CompileExpression();

            if (exprcommand.Expression is NameExpression)
            {
                command = new SetCommand(((NameExpression)exprcommand.Expression).Name, valueexpr);
                this.CompileEndOfCommand();
                return(command);
            }

            if (exprcommand.Expression is AttributeExpression)
            {
                command = new SetAttributeCommand(((AttributeExpression)exprcommand.Expression).Expression, ((AttributeExpression)exprcommand.Expression).Name, valueexpr);
                this.CompileEndOfCommand();
                return(command);
            }

            if (exprcommand.Expression is IndexedExpression)
            {
                var indexedexpr = (IndexedExpression)exprcommand.Expression;
                command = new SetIndexCommand(indexedexpr.TargetExpression, indexedexpr.IndexExpression, valueexpr);
                return(command);
            }

            throw new SyntaxError("invalid assignment");
        }