Пример #1
0
        public void ExecuteDefineObjectCommand()
        {
            DefineObjectCommand command = new DefineObjectCommand("adam", new string[] { "Age", "Name" }, new IExpression[] { new ConstantExpression(800), new ConstantExpression("Adam") });
            BindingEnvironment environment = new BindingEnvironment();

            command.Execute(environment);

            object result = environment.GetValue("adam");

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

            DynamicObject dynobj = (DynamicObject)result;

            Assert.AreEqual(800, dynobj.GetValue("Age"));
            Assert.AreEqual("Adam", dynobj.GetValue("Name"));
        }
Пример #2
0
        private ICommand ParseObjectCommand()
        {
            string name = this.ParseName();
            List<string> memberNames = new List<string>();
            List<IExpression> memberExpressions = new List<IExpression>();

            this.Parse(TokenType.Separator, "{");

            while (this.TryParse(TokenType.Name, "var", "function", "sub", "default"))
            {
                Token token = this.lexer.NextToken();

                if (token.Value == "var")
                    this.ParseMemberVariable(memberNames, memberExpressions);
                else if (token.Value == "default")
                {
                    Token token2 = this.lexer.NextToken();

                    if (token2.Value == "function" || token2.Value == "sub")
                        this.ParseMemberMethod(memberNames, memberExpressions, true);
                    else
                        throw new UnexpectedTokenException(token2);
                }
                else if (token.Value == "function" || token.Value == "sub")
                    this.ParseMemberMethod(memberNames, memberExpressions, false);
                else
                    throw new UnexpectedTokenException(token);
            }

            this.Parse(TokenType.Separator, "}");

            DefineObjectCommand cmd = new DefineObjectCommand(name, memberNames.ToArray(), memberExpressions);

            return cmd;
        }