Exemplo n.º 1
0
        private void ProcessAttributeCommand(SetAttributeCommand command, string s, ref int targetPoints)
        {
            var op           = FindOperation(s);
            var numberString = Regex.Match(s, @"\d+").Value;

            if (string.IsNullOrWhiteSpace(numberString))
            {
                WrmChargenCommon.SendErrorMessage(Session, "No valid number was found.");
                return;
            }

            int n;

            if (!int.TryParse(numberString, out n))
            {
                WrmChargenCommon.SendErrorMessage(Session, "Could not process the number.");
                return;
            }

            if (string.IsNullOrWhiteSpace(op))
            {
                // No operator? Try to set the value directly to the number provided.
                SetAttributeTarget(ref targetPoints, n);
            }
            else if (op == "+")
            {
                SetAttributeTarget(ref targetPoints, targetPoints + n);
            }
            else if (op == "-")
            {
                SetAttributeTarget(ref targetPoints, targetPoints - n);
            }
        }
        public void SetAttributeInNativeObject()
        {
            BindingEnvironment environment = new BindingEnvironment();
            Person adam = new Person();
            environment.SetValue("adam", adam);

            SetAttributeCommand command = new SetAttributeCommand(new NameExpression("adam"), "FirstName", new ConstantExpression("Adam"));

            command.Execute(environment);

            Assert.AreEqual("Adam", adam.FirstName);
        }
        public void SetAttributeInNativeObject()
        {
            BindingEnvironment environment = new BindingEnvironment();
            Person             adam        = new Person();

            environment.SetValue("adam", adam);

            SetAttributeCommand command = new SetAttributeCommand(new NameExpression("adam"), "FirstName", new ConstantExpression("Adam"));

            command.Execute(environment);

            Assert.AreEqual("Adam", adam.FirstName);
        }
        public void SetAttributeInDynamicObject()
        {
            BindingEnvironment environment = new BindingEnvironment();
            DefinedClass klass = new DefinedClass("Spam");
            DynamicObject dynobj = new DynamicObject(klass);
            environment.SetValue("foo", dynobj);

            SetAttributeCommand command = new SetAttributeCommand(new NameExpression("foo"), "one", new ConstantExpression(1));

            command.Execute(environment);

            Assert.IsTrue(dynobj.HasValue("one"));
            Assert.AreEqual(1, dynobj.GetValue("one"));
        }
        public void SetAttributeInDynamicObject()
        {
            BindingEnvironment environment = new BindingEnvironment();
            DefinedClass       klass       = new DefinedClass("Spam");
            DynamicObject      dynobj      = new DynamicObject(klass);

            environment.SetValue("foo", dynobj);

            SetAttributeCommand command = new SetAttributeCommand(new NameExpression("foo"), "one", new ConstantExpression(1));

            command.Execute(environment);

            Assert.IsTrue(dynobj.HasValue("one"));
            Assert.AreEqual(1, dynobj.GetValue("one"));
        }
Exemplo n.º 6
0
        public void CreateInstanceWithConstructor()
        {
            DefinedClass klass = new DefinedClass("Spam");
            ICommand body = new SetAttributeCommand(new NameExpression("self"), "name", new NameExpression("name"));
            DefinedFunction constructor = new DefinedFunction("__init__", new Parameter[] { new Parameter("self", null, false), new Parameter("name", null, false) }, body, null);
            klass.SetMethod(constructor.Name, constructor);
            var result = klass.Apply(null, new object[] { "Adam" }, null);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(DynamicObject));

            var dynobj = (DynamicObject)result;
            Assert.AreEqual(klass, dynobj.Class);
            Assert.IsTrue(dynobj.HasValue("name"));
            var name = dynobj.GetValue("name");
            Assert.IsNotNull(name);
            Assert.AreEqual("Adam", name);
        }
Exemplo n.º 7
0
        public void CreateInstanceWithConstructor()
        {
            DefinedClass    klass       = new DefinedClass("Spam");
            ICommand        body        = new SetAttributeCommand(new NameExpression("self"), "name", new NameExpression("name"));
            DefinedFunction constructor = new DefinedFunction("__init__", new Parameter[] { new Parameter("self", null, false), new Parameter("name", null, false) }, body, null);

            klass.SetMethod(constructor.Name, constructor);
            var result = klass.Apply(null, new object[] { "Adam" }, null);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(DynamicObject));

            var dynobj = (DynamicObject)result;

            Assert.AreEqual(klass, dynobj.Class);
            Assert.IsTrue(dynobj.HasValue("name"));
            var name = dynobj.GetValue("name");

            Assert.IsNotNull(name);
            Assert.AreEqual("Adam", name);
        }
Exemplo n.º 8
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");
        }
Exemplo n.º 9
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");
        }
Exemplo n.º 10
0
        private void ProcessAttributeCommand(SetAttributeCommand command, string s, ref int targetPoints)
        {
            var op = this.FindOperation(s);
            var numberString = Regex.Match(s, @"\d+").Value;
            if (string.IsNullOrWhiteSpace(numberString))
            {
                WrmChargenCommon.SendErrorMessage(this.Session, "No valid number was found.");
                return;
            }

            int n;
            if (!int.TryParse(numberString, out n))
            {
                WrmChargenCommon.SendErrorMessage(this.Session, "Could not process the number.");
                return;
            }

            if (string.IsNullOrWhiteSpace(op))
            {
                // No operator? Try to set the value directly to the number provided.
                this.SetAttributeTarget(ref targetPoints, n);
            }
            else if (op == "+")
            {
                this.SetAttributeTarget(ref targetPoints, targetPoints + n);
            }
            else if (op == "-")
            {
                this.SetAttributeTarget(ref targetPoints, targetPoints - n);
            }
        }