Beispiel #1
0
        public void EvaluateConcatenateExpressionUsingStrings()
        {
            IExpression expression = new ConcatenateExpression(new ConstantExpression("foo"), new ConstantExpression("bar"));

            Assert.AreEqual("foobar", expression.Evaluate(null));
        }
Beispiel #2
0
        public void EvaluateConcatenateExpressionUsingIntegers()
        {
            IExpression expression = new ConcatenateExpression(new ConstantExpression(12), new ConstantExpression(34));

            Assert.AreEqual("1234", expression.Evaluate(null));
        }
Beispiel #3
0
        private IExpression ParseSimpleTermExpression()
        {
            if (this.TryParse(TokenType.Name, "default"))
            {
                if (this.TryParse(TokenType.Name, "function") || this.TryParse(TokenType.Name, "sub"))
                    return this.ParseFunctionExpression(true);

                throw new UnexpectedTokenException(this.lexer.NextToken());
            }

            if (this.TryParse(TokenType.Name, "function") || this.TryParse(TokenType.Name, "sub"))
                return this.ParseFunctionExpression(false);

            Token token = this.lexer.NextToken();

            if (token == null)
                return null;

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

                    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:
                    IList<string> parts = StringUtilities.SplitText(token.Value);

                    if (parts.Count == 1)
                        return new ConstantExpression(token.Value);

                    IExpression strexpr = new ConstantExpression(parts[0]);

                    for (int k = 1; k < parts.Count; k++)
                        if ((k % 2) == 0)
                            strexpr = new ConcatenateExpression(strexpr, new ConstantExpression(parts[k]));
                        else
                        {
                            Parser parser = new Parser(parts[k]);
                            strexpr = new ConcatenateExpression(strexpr, parser.ParseExpression());
                        }

                    return strexpr;
                case TokenType.Name:
                    if (this.TryParse(TokenType.Separator, "("))
                    {
                        List<IExpression> arguments = this.ParseArgumentList();
                        return new InvokeExpression(token.Value, arguments);
                    }

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

                        return new VariableVariableExpression(token.Value);
                    }

                    return new VariableExpression(token.Value);
            }

            throw new UnexpectedTokenException(token);
        }