Example #1
0
        private List <string> EvalExpression(string exp, ParserRuleContext context = null, string errorPrefix = "")
        {
            exp = exp.TrimExpression();
            var(result, error) = EvalByAdaptiveExpression(exp, CurrentTarget().Scope);

            if (error != null || (result == null && lgOptions.StrictMode == true))
            {
                var templateName = CurrentTarget().TemplateName;
                if (evaluationTargetStack.Count > 0)
                {
                    evaluationTargetStack.Pop();
                }

                Evaluator.CheckExpressionResult(exp, error, result, templateName, context, errorPrefix);
            }
            else if (result == null && lgOptions.StrictMode == false)
            {
                result = "null";
            }

            if (result is IList &&
                result.GetType().IsGenericType&&
                result.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>)))
            {
                var listRes = result as List <object>;

                return(listRes.Select(x => x.ToString()).ToList());
            }

            return(new List <string>()
            {
                result.ToString()
            });
        }
Example #2
0
        private List <object> EvalExpression(string exp, string lineContent = "", string errorPrefix = "")
        {
            exp = exp.TrimExpression();
            var(result, error) = EvalByAdaptiveExpression(exp, CurrentTarget().Scope);

            if (error != null || (result == null && _lgOptions.StrictMode == true))
            {
                var currentTemplate = CurrentTemplate();
                if (currentTemplate != null)
                {
                    _evaluationTargetStack.Pop();
                    Evaluator.CheckExpressionResult(exp, error, result, currentTemplate.Name, lineContent, errorPrefix);
                }
            }

            if (result is IList &&
                result.GetType().IsGenericType&&
                result.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>)))
            {
                var listRes = result as List <object>;

                return(listRes.ToList());
            }

            return(new List <object>()
            {
                result
            });
        }
Example #3
0
        private bool EvalExpressionInCondition(string exp, ParserRuleContext context = null, string errorPrefix = "")
        {
            exp = exp.TrimExpression();
            var(result, error) = EvalByAdaptiveExpression(exp, CurrentTarget().Scope);

            if (lgOptions.StrictMode == true && (error != null || result == null))
            {
                var templateName = CurrentTarget().TemplateName;
                if (evaluationTargetStack.Count > 0)
                {
                    evaluationTargetStack.Pop();
                }

                Evaluator.CheckExpressionResult(exp, error, result, templateName, context, errorPrefix);
            }
            else if (error != null ||
                     result == null ||
                     (result is bool r1 && r1 == false) ||
                     (result is int r2 && r2 == 0))
            {
                return(false);
            }

            return(true);
        }
Example #4
0
        private bool EvalExpressionInCondition(ParserRuleContext expressionContext, string contentLine, string errorPrefix = "")
        {
            var exp = expressionContext.GetText().TrimExpression();

            var(result, error) = EvalByAdaptiveExpression(exp, CurrentTarget().Scope);

            if (_lgOptions.StrictMode == true && (error != null || result == null))
            {
                var currentTemplate = CurrentTemplate();
                if (currentTemplate != null)
                {
                    _evaluationTargetStack.Pop();
                    Evaluator.CheckExpressionResult(exp, error, result, currentTemplate.Name, contentLine, errorPrefix);
                }
            }
            else if (error != null ||
                     result == null ||
                     (result is bool r1 && r1 == false) ||
                     (result is int r2 && r2 == 0))
            {
                return(false);
            }

            return(true);
        }