Exemplo n.º 1
0
        /// <summary>
        /// Evaluates the expression by appending all the sub-expressions.
        /// </summary>
        /// <returns></returns>
        public static object EvalInterpolated(InterpolatedExpr expr)
        {
            if (expr.Expressions == null || expr.Expressions.Count == 0)
                return string.Empty;

            string total = "";
            foreach (var exp in expr.Expressions)
            {
                if (exp != null)
                {
                    var val = exp.Evaluate();
                    var text = "";
                    var lobj = (LObject)val;
                    text = lobj.GetValue().ToString();
                    total += text;
                }
            }
            return new LString(total);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Visits the function call expression tree
 /// </summary>
 /// <param name="exp"></param>
 public object VisitInterpolated(InterpolatedExpr exp)
 {
     return null;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Parses an interpolated token into a set of tokens making up an interpolated expression.
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public Expr ParseInterpolatedExpression(Token t)
        {
            var iexp = new InterpolatedExpr();

            // Convert each token in the interpolated string, need to convert it into
            // it's own expression.
            var tokens = t.Value as List<TokenData>;
            foreach (var tokenData in tokens)
            {
                var token = tokenData.Token;
                Expr exp = null;

                // 1. true / false / "name" / 123 / null;
                if (token.IsLiteralAny())
                {
                    exp = token == Tokens.Null
                        ? new ConstantExpr(LObjects.Null)
                        : new ConstantExpr(TokenHelper.ConvertToLangLiteral(token));
                    this.SetupContext(exp, tokenData);
                    _state.ExpressionCount++;
                }
                // 2. ${first + 'abc'} or ${ result / 2 + max }
                else if (token.Kind == TokenKind.Multi)
                {
                    var tokenIterator = new TokenIterator();
                    var tokens2 = token.Value as List<TokenData>;
                    tokenIterator.Init(tokens2, 1, 100);
                    tokenIterator.Advance();
                    var exisiting = _tokenIt;

                    // a. Temporarily set the token iterator for the parser to the one for the interpolation.
                    _tokenIt = tokenIterator;

                    // b. Now parse only the tokens supplied.
                    exp = ParseExpression(null);

                    // c. Reset the token iterator to the global one for the entire script.
                    _tokenIt = exisiting;
                }
                iexp.Add(exp);
            }
            _tokenIt.Advance();
            return iexp;
        }