예제 #1
0
        /// <summary>
        /// Gets a value from the object context, and appends it to the string builder.
        /// </summary>
        /// <param name="model"></param>
        /// <param name="stringBuilder"></param>
        public override void GetContents(object model, StringBuilder stringBuilder)
        {
            foreach (var child in _token.Children)
            {
                if (!child.CanHandle(model))
                {
                    continue;
                }

                stringBuilder.Append(child.Evaluate(model));
                return;
            }

            var token = _token;

            do
            {
                if (!token.CanHandle(model))
                {
                    token = token.Parent;
                }
            } while (token != null && !token.CanHandle(model));

            if (token != null && token.CanHandle(model))
            {
                stringBuilder.Append(_token.Evaluate(model));
                return;
            }
        }
예제 #2
0
        public override IToken Evaluate(IToken first, IToken last, TokenTreeList parameters, bool isFinal)
        {
            if (first == null)
                throw new Exception($"Operation {Text} can not be unary.");

            IToken evaluated = last.Evaluate(parameters, isFinal);
            if (evaluated == null)
                throw new Exception($"Second element of Operation {Text} is not unique.");

            ListToken evaluatedList = evaluated as ListToken;
            if (evaluatedList != null)
            {
                ListToken list = new ListToken();
                foreach (IToken item in evaluatedList.Tokens)
                    list.Tokens.Add(Evaluate(first, item, parameters, isFinal));
                return list;
            }

            IntToken intToken = evaluated as IntToken;
            if (intToken == null)
            {
                if (isFinal)
                    throw new Exception($"Operation {Text} must have integer second element.");
                return new ExpressionToken(first, new IndexOperator(), last);
            }

            IToken tokenList = first.Evaluate(parameters, isFinal);
            ListToken listToken = tokenList as ListToken;
            int index = intToken.Value;
            return listToken == null
                ? (index == 0 && tokenList is ITypeToken ? tokenList : new ExpressionToken(first, new IndexOperator(), intToken))
                : (listToken.Tokens.Count > index ? listToken.Tokens[index] : new NullToken());
        }
예제 #3
0
        public override IToken Evaluate(IToken first, IToken last, TokenTreeList parameters, bool isFinal)
        {
            IToken firstList = first.Evaluate(parameters, isFinal);
            IToken lastList = last.Evaluate(parameters, isFinal);

            if (firstList == null || lastList == null)
                throw new Exception($"Operation {Text} is a binary operation.");

            return AddToList(firstList, lastList);
        }
예제 #4
0
        public override TokenList Evaluate(IToken first, IToken last, TokenTreeList parameters)
        {
            if (first == null)
                throw new Exception($"Operation {Text} can not be unary.");

            TokenList lastList = last.Evaluate(parameters);
            if (lastList == null || lastList.Count != 1)
                throw new Exception($"Second element of Operation {Text} is not unique.");

            IntToken intToken = lastList[0] as IntToken;
            if (intToken == null)
                throw new Exception($"Operation {Text} must have integer second element.");

            return new TokenList(first.Evaluate(parameters)[intToken.Value]);
        }
        public override IToken Evaluate(IToken first, IToken last, TokenTreeList parameters, bool isFinal)
        {
            if (parameters == null)
            {
                if (isFinal)
                    throw new Exception($"Operation {Text} must have parameters if final.");
                return new ExpressionToken(first, this, last);
            }

            if (first != null)
                throw new Exception($"Operation {Text} is unary.");
            if (last == null)
                throw new Exception($"Operation {Text} needs a variable.");

            IToken evaluated = last.Evaluate(parameters, isFinal);
            if (evaluated is ExpressionToken)
                return new ExpressionToken(null, this, evaluated);
            ListToken listToken = evaluated as ListToken;
            if (listToken != null && listToken.Tokens.Exists(x => x is ExpressionToken))
                return new ExpressionToken(null, this, evaluated);

            string text = evaluated.Text;
            TokenTreeList found = parameters.FindMatches(text, true);
            ListToken result = new ListToken();
            foreach (TokenTree tokenTree in found)
            {
                bool debug = bool.Parse(tokenTree["Debug"] ?? "False");
                if (debug)
                    LogControl?.SetLogging(true);
                IToken token = tokenTree.Value.Evaluate(parameters, isFinal);
                if (!(token is NullToken))
                    result.Add(token);
                if (debug)
                    LogControl?.ResetLoggingToDefault();
            }

            if (result.Tokens.Count == 0)
                return new ExpressionToken(null, this, evaluated);
            return result.Tokens.Count == 1 ? result.Tokens[0] : result;
        }
예제 #6
0
        public override IToken Evaluate(IToken first, IToken last, TokenTreeList parameters, bool isFinal)
        {
            if (_function == null)
            {
                if (first == null)
                    throw new Exception($"Operation {Text} can not be unary.");

                IToken functionToken = first.Evaluate(parameters, isFinal);
                if (functionToken == null || functionToken is ListToken)
                    throw new Exception($"First element of Operation {Text} is not unique.");

                string function = functionToken.Text;
                switch (function)
                {
                    case AndFunction.ID:
                        _function = new AndFunction();
                        break;
                    case AggregateFunction.ID:
                        _function = new AggregateFunction();
                        break;
                    case CaseFunction.ID:
                        _function = new CaseFunction();
                        break;
                    case ComparisonFunction.ID:
                        _function = new ComparisonFunction();
                        break;
                    case ContainsFunction.ID:
                        _function = new ContainsFunction();
                        break;
                    case CountFunction.ID:
                        _function = new CountFunction();
                        break;
                    case DoubleFunction.ID:
                        _function = new DoubleFunction();
                        break;
                    case IfFunction.ID:
                        _function = new IfFunction();
                        break;
                    case IntFunction.ID:
                        _function = new IntFunction();
                        break;
                    case JoinFunction.ID:
                        _function = new JoinFunction();
                        break;
                    case OrFunction.ID:
                        _function = new OrFunction();
                        break;
                    case OverFunction.ID:
                        _function = new OverFunction();
                        break;
                    case RangeFunction.ID:
                        _function = new RangeFunction();
                        break;
                    case RegexFunction.ID:
                        _function = new RegexFunction();
                        break;
                    case ReverseFunction.ID:
                        _function = new ReverseFunction();
                        break;
                    case SplitFunction.ID:
                        _function = new SplitFunction();
                        break;
                    case SumFunction.ID:
                        _function = new SumFunction();
                        break;
                    case UserFunction.ID:
                        _function = new UserFunction();
                        break;
                    default:
                        ListToken newList = new ListToken
                        {
                            new ExpressionToken(null, new SubstitutionOperator(), new StringToken(function))
                        };
                        ListToken oldList = last.Evaluate(parameters, isFinal) as ListToken;
                        if (oldList != null)
                        {
                            foreach (IToken token in oldList.Tokens)
                                newList.Add(token);
                        }
                        else
                        {
                            newList.Add(last);
                        }
                        ExpressionToken expression = new ExpressionToken(null, new FunctionOperator(new UserFunction()), newList);
                        return expression.Evaluate(parameters, isFinal);
                }
            }

            IToken parameterList;
            if (isFinal && _function is UserFunction)
            {
                parameterList = PrepareUserFunction(last, parameters);
            }
            else if (_function.IsComparisonFunction)
            {
                parameterList = last.EvaluateList();
            }
            else
            {
                parameterList = last.Evaluate(parameters, !_function.FinalCanBeExpression && isFinal);
                ExpressionToken expression = parameterList as ExpressionToken;
                if (expression != null)
                {
                    if (isFinal)
                    {
                        IToken substitute = _function.ValueIfFinalValueIsExpression(expression);
                        if (substitute is NullToken)
                            return new ExpressionToken(null, new FunctionOperator(_function), parameterList);
                        parameterList = substitute;
                    }
                    else
                    {
                        return new ExpressionToken(null, new FunctionOperator(_function), parameterList);
                    }
                }
            }

            return _function.Perform(parameterList, parameters, isFinal);
        }
예제 #7
0
 /// <summary>
 /// Evaluates a token.
 /// </summary>
 /// <param name="value">The token to process.</param>
 /// <param name="parameters">Calculation parameters.</param>
 /// <returns>The processed token.</returns>
 private static string ProcessTokens(IToken value, TokenTreeList parameters)
 {
     return value.Evaluate(parameters, false).Text;
 }