private static IDictionary ResolveToDictionary(VariableExpression expression, IContext context)
        {
            string name = expression.Name;

            object obj = context.GetValue(name);

            if (obj == null || obj == Undefined.Instance)
            {
                obj = new Hashtable();

                // TODO Review if Local or not
                context.SetValue(name, obj);
            }

            return (IDictionary)obj;
        }
        public void ResolveVariableExpressionToDictionary()
        {
            Context context = new Context();
            IExpression expression = new VariableExpression("foo");

            object obj = ExpressionUtilities.ResolveToDictionary(expression, context);

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

            Assert.AreEqual(obj, context.GetValue("foo"));
        }
Exemple #3
0
        private IExpression ParseSimpleTermExpression()
        {
            Token token = this.lexer.NextToken();

            if (token == null)
                return null;

            if (token.TokenType == TokenType.Name && token.Value == "function")
                return ParseFunctionExpression();

            switch (token.TokenType)
            {
                case TokenType.Separator:
                    if (token.Value == "(")
                    {
                        IExpression expression = this.ParseExpression();
                        this.Parse(TokenType.Separator, ")");
                        return expression;
                    }

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

                    break;
                case TokenType.Boolean:
                    bool booleanValue = Convert.ToBoolean(token.Value);
                    return new ConstantExpression(booleanValue);
                case TokenType.Integer:
                    int intValue = Int32.Parse(token.Value, System.Globalization.CultureInfo.InvariantCulture);
                    return new ConstantExpression(intValue);
                case TokenType.Real:
                    double realValue = Double.Parse(token.Value, System.Globalization.CultureInfo.InvariantCulture);
                    return new ConstantExpression(realValue);
                case TokenType.String:
                    return new ConstantExpression(token.Value);
                case TokenType.Object:
                    if (token.Value == "null")
                        return new ConstantExpression(null);

                    if (token.Value == "undefined")
                        return new ConstantExpression(Undefined.Instance);

                    break;
                case TokenType.Name:
                    IExpression expr = null;

                    expr = new VariableExpression(token.Value);

                    if (this.TryParse(TokenType.Separator, "("))
                    {
                        IList<IExpression> arguments = this.ParseArgumentList();
                        expr = new InvokeExpression(expr, arguments);
                    }

                    return expr;
            }

            throw new UnexpectedTokenException(token);
        }
Exemple #4
0
        private IExpression ParseSimpleTermExpression()
        {
            Token token = this.lexer.NextToken();

            if (token == null)
                return null;

            if (token.TokenType == TokenType.Name && token.Value == "function")
                return this.ParseFunctionExpression();

            if (token.TokenType == TokenType.Name && token.Value == "typeof")
                return new TypeOfExpression(this.ParseExpression());

            switch (token.TokenType)
            {
                case TokenType.Delimiter:
                    if (token.Value == "(")
                    {
                        IExpression expression = this.ParseExpression();
                        this.Parse(TokenType.Delimiter, ")");
                        return expression;
                    }

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

                    if (token.Value == "[")
                        return this.ParseArrayExpression();

                    break;
                case TokenType.Integer:
                    int intValue = Int32.Parse(token.Value, System.Globalization.CultureInfo.InvariantCulture);
                    return new ConstantExpression(intValue);
                case TokenType.Real:
                    double realValue = Double.Parse(token.Value, System.Globalization.CultureInfo.InvariantCulture);
                    return new ConstantExpression(realValue);
                case TokenType.String:
                    return new ConstantExpression(token.Value);
                case TokenType.Name:
                    if (token.Value == "null")
                        return new ConstantExpression(null);
                    if (token.Value == "undefined")
                        return new ConstantExpression(Undefined.Instance);
                    if (token.Value == "true")
                        return new ConstantExpression(true);
                    if (token.Value == "false")
                        return new ConstantExpression(false);

                    IExpression expr = null;

                    expr = new VariableExpression(token.Value);

                    if (this.TryParse(TokenType.Delimiter, "("))
                    {
                        IList<IExpression> arguments = this.ParseArgumentList();
                        expr = new InvokeExpression(expr, arguments);
                    }

                    return expr;
            }

            throw new ParserException(string.Format("Unexpected '{0}'", token.Value));
        }
Exemple #5
0
        private IExpression ParseQualifiedName()
        {
            string name = this.ParseName();
            IExpression expression = new VariableExpression(name);

            while (this.TryParse(TokenType.Delimiter, "."))
            {
                this.lexer.NextToken();
                expression = new DotExpression(expression, this.ParseName());
            }

            return expression;
        }
 private static void SetValue(VariableExpression expression, object value, IContext context)
 {
     context.SetValue(expression.Name, value);
 }
        private static object ResolveToObject(VariableExpression expression, IContext context)
        {
            string name = expression.Name;

            object obj = context.GetValue(name);

            if (obj == null || obj == Undefined.Instance)
            {
                obj = new DynamicObject();

                // TODO Review if Local or not
                context.SetValue(name, obj);
            }

            return obj;
        }
Exemple #8
0
        public void ExecuteWhileCommand()
        {
            IExpression incrementX = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new ConstantExpression(1), new VariableExpression("a"));
            IExpression decrementY = new ArithmeticBinaryExpression(ArithmeticOperator.Subtract, new VariableExpression("b"), new ConstantExpression(1));
            ICommand setX = new SetVariableCommand("a", incrementX);
            ICommand setY = new SetVariableCommand("b", decrementY);
            List<ICommand> commands = new List<ICommand>();
            commands.Add(setX);
            commands.Add(setY);
            ICommand command = new CompositeCommand(commands);
            IExpression yexpr = new VariableExpression("b");

            WhileCommand whilecmd = new WhileCommand(yexpr, command);

            Context context = new Context();

            context.SetValue("a", 0);
            context.SetValue("b", 5);

            whilecmd.Execute(context);

            Assert.AreEqual(0, context.GetValue("b"));
            Assert.AreEqual(5, context.GetValue("a"));
        }