Пример #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Equal"/> class.
 /// </summary>
 public Equal()
     : base(
         ExpressionType.Equal,
         (args) => FunctionUtils.CommonEquals(args[0], args[1]),
         FunctionUtils.ValidateBinary)
 {
 }
Пример #2
0
        private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            var found = false;

            var(args, error) = FunctionUtils.EvaluateChildren(expression, state, options);
            if (error == null)
            {
                if (args[0] is string string0 && args[1] is string string1)
                {
                    found = string0.Contains(string1);
                }
                else if (FunctionUtils.TryParseList(args[0], out IList ilist))
                {
                    // list to find a value
                    var operands = FunctionUtils.ResolveListValue(ilist);

                    foreach (var item in operands)
                    {
                        if (FunctionUtils.CommonEquals(item, args[1]))
                        {
                            found = true;
                            break;
                        }
                    }
                }
                else if (args[1] is string string2)
                {
                    found = FunctionUtils.TryAccessProperty((object)args[0], string2, out var _);
                }
            }
Пример #3
0
        /// <inheritdoc/>
        public override object VisitSwitchCaseBody([NotNull] LGTemplateParser.SwitchCaseBodyContext context)
        {
            var switchCaseNodes   = context.switchCaseTemplateBody().switchCaseRule();
            var length            = switchCaseNodes.Length;
            var switchExprs       = switchCaseNodes[0].switchCaseStat().expression();
            var switchErrorPrefix = "Switch '" + switchExprs[0].GetText() + "': ";
            var switchExprResult  = EvalExpression(switchExprs[0].GetText(), switchExprs[0], switchCaseNodes[0].switchCaseStat().GetText(), switchErrorPrefix);
            var idx = 0;

            foreach (var switchCaseNode in switchCaseNodes)
            {
                if (idx == 0)
                {
                    idx++;
                    continue;   // skip the first node, which is switch statement
                }

                if (idx == length - 1 && switchCaseNode.switchCaseStat().DEFAULT() != null)
                {
                    var defaultBody = switchCaseNode.normalTemplateBody();
                    if (defaultBody != null)
                    {
                        return(Visit(defaultBody));
                    }
                    else
                    {
                        return(null);
                    }
                }

                var caseExprs = switchCaseNode.switchCaseStat().expression();

                var caseErrorPrefix = "Case '" + caseExprs[0].GetText() + "': ";
                var caseExprResult  = EvalExpression(caseExprs[0].GetText(), caseExprs[0], switchCaseNode.switchCaseStat().GetText(), caseErrorPrefix);
                if (FunctionUtils.CommonEquals(switchExprResult, caseExprResult))
                {
                    return(Visit(switchCaseNode.normalTemplateBody()));
                }

                idx++;
            }

            return(null);
        }