Beispiel #1
0
        public void CreateNewInstanceWithConstructor()
        {
            ICommand body = new SetCommand(new DotExpression(new VariableExpression("this"), "Name"), new VariableExpression("n"));
            Function function = new Function(new string[] { "n" }, body);
            this.dynclass.SetMember(this.dynclass.Name, function);
            this.dynclass.SetMember("Name", null);
            this.dynclass.SetMember("Age", 800);

            object instance = this.dynclass.NewInstance(new object[] { "Adam" });

            Assert.IsNotNull(instance);

            Assert.IsInstanceOfType(instance, typeof(IObject));

            IObject obj = (IObject)instance;

            Assert.AreEqual("Adam", obj.GetValue("Name"));
            Assert.AreEqual(800, obj.GetValue("Age"));
        }
Beispiel #2
0
        private ICommand ParseSimpleCommand()
        {
            if (this.TryParse(TokenType.Name, "var"))
            {
                this.lexer.NextToken();
                return this.ParseVarCommand();
            }

            if (this.TryParse(TokenType.Name, "exit"))
            {
                this.lexer.NextToken();
                return new ExitCommand();
            }

            IExpression expression = this.ParseExpression();

            if (expression == null)
                return null;

            if (this.TryParse(TokenType.Operator, "="))
            {
                this.lexer.NextToken();

                ICommand command = null;

                if (expression is ArrayExpression)
                {
                    ArrayExpression aexpr = (ArrayExpression)expression;
                    command = new SetArrayCommand(aexpr.Expression, aexpr.Arguments, this.ParseExpression());
                }
                else
                    command = new SetCommand(expression, this.ParseExpression());

                return command;
            }

            if (this.TryParse(TokenType.Operator, "<-"))
            {
                this.lexer.NextToken();
                return new SetValueCommand(expression, this.ParseExpression());
            }

            return new ExpressionCommand(expression);
        }
Beispiel #3
0
        public void ExecuteSetCommandWithObjectProperty()
        {
            Machine machine = new Machine();
            TextWriter outwriter = new StringWriter();
            machine.Environment.SetValue("machine", machine);
            machine.Environment.SetValue("out", outwriter);

            DotExpression dotexpr = new DotExpression(new VariableExpression("machine"), "Out");
            SetCommand command = new SetCommand(dotexpr, new VariableExpression("out"));

            command.Execute(machine.Environment);

            Assert.AreEqual(machine.Out, outwriter);
        }
Beispiel #4
0
        public void ExecuteSetCommandWithVariable()
        {
            BindingEnvironment environment = new BindingEnvironment();
            SetCommand command = new SetCommand(new VariableExpression("foo"), new ConstantExpression("bar"));

            command.Execute(environment);

            Assert.AreEqual("bar", environment.GetValue("foo"));
        }
Beispiel #5
0
        public void ExecuteSetCommandWithDotExpression()
        {
            BindingEnvironment environment = new BindingEnvironment();
            DotExpression dotexpr = new DotExpression(new VariableExpression("foo"), "FirstName");
            SetCommand command = new SetCommand(dotexpr, new ConstantExpression("bar"));

            command.Execute(environment);

            object obj = environment.GetValue("foo");

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

            DynamicObject dynobj = (DynamicObject)obj;

            Assert.AreEqual("bar", dynobj.GetValue("FirstName"));
        }