Exemple #1
0
        private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            object result = false;
            string error  = null;

            foreach (var child in expression.Children)
            {
                (result, error) = child.TryEvaluate(state, new Options(options)
                {
                    NullSubstitution = null
                });
                if (error == null)
                {
                    if (FunctionUtils.IsLogicTrue(result))
                    {
                        result = true;
                        break;
                    }
                }
                else
                {
                    // Interpret error as false and swallow errors
                    error = null;
                }
            }

            return(result, error);
        }
Exemple #2
0
        private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            var    result = false;
            string error;

            object instance;

            (instance, error) = expression.Children[0].TryEvaluate(state, options);
            if (error == null)
            {
                var list = FunctionUtils.ConvertToList(instance);
                if (list == null)
                {
                    error = $"{expression.Children[0]} is not a collection or structure object to run Any";
                }
                else
                {
                    FunctionUtils.LambdaEvaluator(expression, state, options, list, (object currentItem, object r, string e) =>
                    {
                        if (FunctionUtils.IsLogicTrue(r) && e == null)
                        {
                            result = true;
                            return(true);
                        }

                        return(false);
                    });
                }
            }

            return(result, error);
        }
Exemple #3
0
        private static bool Function(IReadOnlyList <object> args)
        {
            var arg = args[0];

            if (arg is int @int)
            {
                arg = @int != 0;
            }

            return(FunctionUtils.IsLogicTrue(arg));
        }
        private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            object result = null;
            string error;

            object instance;

            (instance, error) = expression.Children[0].TryEvaluate(state, options);
            if (error == null)
            {
                var list = FunctionUtils.ConvertToList(instance);
                if (list == null)
                {
                    error = $"{expression.Children[0]} is not a collection or structure object to run Where";
                }
                else
                {
                    result = new List <object>();
                    FunctionUtils.LambdaEvaluator(expression, state, options, list, (object currentItem, object r, string e) =>
                    {
                        if (FunctionUtils.IsLogicTrue(r) && e == null)
                        {
                            // add if only if it evaluates to true
                            ((List <object>)result).Add(currentItem);
                        }

                        return(false);
                    });

                    if (!FunctionUtils.TryParseList(instance, out IList _))
                    {
                        // re-construct object
                        var jobjResult = new JObject();
                        foreach (var item in (List <object>)result)
                        {
                            FunctionUtils.TryAccessProperty(item, "key", out var keyVal);
                            FunctionUtils.TryAccessProperty(item, "value", out var val);
                            jobjResult.Add(keyVal as string, FunctionUtils.ConvertToJToken(val));
                        }

                        result = jobjResult;
                    }
                }
            }

            return(result, error);
        }
        private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            object result;
            string error;

            (result, error) = expression.Children[0].TryEvaluate(state, new Options(options)
            {
                NullSubstitution = null
            });
            if (error == null && FunctionUtils.IsLogicTrue(result))
            {
                (result, error) = expression.Children[1].TryEvaluate(state, options);
            }
            else
            {
                // Swallow error and treat as false
                (result, error) = expression.Children[2].TryEvaluate(state, options);
            }

            return(result, error);
        }
Exemple #6
0
        private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            object result;
            string error;

            (result, error) = expression.Children[0].TryEvaluate(state, new Options(options)
            {
                NullSubstitution = null
            });
            if (error == null)
            {
                result = !FunctionUtils.IsLogicTrue(result);
            }
            else
            {
                error  = null;
                result = true;
            }

            return(result, error);
        }
Exemple #7
0
        private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options)
        {
            object result = null;
            string error;

            object instance;

            (instance, error) = expression.Children[0].TryEvaluate(state, options);
            if (error == null)
            {
                var   isInstanceList = false;
                IList list           = null;
                if (FunctionUtils.TryParseList(instance, out IList ilist))
                {
                    isInstanceList = true;
                    list           = ilist;
                }
                else if (instance is JObject jobj)
                {
                    list = FunctionUtils.Object2KVPairList(jobj);
                }
                else if (FunctionUtils.ConvertToJToken(instance) is JObject jobject)
                {
                    list = FunctionUtils.Object2KVPairList(jobject);
                }
                else
                {
                    error = $"{expression.Children[0]} is not a collection or structure object to run foreach";
                }

                if (error == null)
                {
                    var iteratorName  = (string)(expression.Children[1].Children[0] as Constant).Value;
                    var stackedMemory = StackedMemory.Wrap(state);
                    result = new List <object>();
                    for (var idx = 0; idx < list.Count; idx++)
                    {
                        var local = new Dictionary <string, object>
                        {
                            { iteratorName, FunctionUtils.AccessIndex(list, idx).value },
                        };

                        // the local iterator is pushed as one memory layer in the memory stack
                        stackedMemory.Push(new SimpleObjectMemory(local));
                        var(r, e) = expression.Children[2].TryEvaluate(stackedMemory, new Options(options)
                        {
                            NullSubstitution = null
                        });
                        stackedMemory.Pop();

                        if (FunctionUtils.IsLogicTrue(r) && e == null)
                        {
                            // add if only if it evaluates to true
                            ((List <object>)result).Add(local[iteratorName]);
                        }
                    }

                    if (!isInstanceList)
                    {
                        // re-construct object
                        var jobjResult = new JObject();
                        foreach (var item in (List <object>)result)
                        {
                            FunctionUtils.TryAccessProperty(item, "key", out var keyVal);
                            FunctionUtils.TryAccessProperty(item, "value", out var val);
                            jobjResult.Add(keyVal as string, FunctionUtils.ConvertToJToken(val));
                        }

                        result = jobjResult;
                    }
                }
            }

            return(result, error);
        }
Exemple #8
0
 private static bool Function(IReadOnlyList <object> args)
 {
     return(FunctionUtils.IsLogicTrue(args[0]));
 }