Exemple #1
0
        private static object Or(ICellContext ctx, IList <IExpression> args, out string error)
        {
            var realArgs = args.Eval(ctx, out error);
            var l        = realArgs[0];
            var r        = realArgs[1];

            if (l is null || l.Equals(string.Empty) || l.Equals(0.0))
            {
                l = false;
            }
            if (r is null || r.Equals(string.Empty) || r.Equals(0.0))
            {
                r = false;
            }

            // This is in case of array or range.
            if (l is object && !(l is bool))
            {
                l = true;
            }
            if (r is object && !(r is bool))
            {
                r = true;
            }

            return((bool)l || (bool)r);
        }
Exemple #2
0
        private static object Concat(ICellContext ctx, IList <IExpression> args, out string error)
        {
            var realArgs = args.Eval(ctx, out error);

            if (error is null)
            {
                var result = new System.Text.StringBuilder();
                foreach (var arg in realArgs)
                {
                    // Arg is actually a range.
                    if (arg is Dictionary <int, object> dict)
                    {
                        result.AppendJoin("", dict.Values);
                    }
                    // Arg is an array defined in the language, arrays should always be IList<object>
                    else if (arg is IList <object> arr)
                    {
                        result.AppendJoin("", arr);
                    }
                    // Arg is: null, string, double or bool.
                    else
                    {
                        result.Append(arg?.ToString() ?? "");
                    }
                }
                return(result.ToString());
            }
            return(null);
        }
Exemple #3
0
        public virtual bool Evaluate(ICell cell, ICellContext context)
        {
            bool evaluated = false;
            Parallel.ForEach<IRule>(Rules, r => { evaluated |= r.Evaluate(cell, context); });

            return evaluated;
        }
Exemple #4
0
        public bool Evaluate(ICell cell, ICellContext context)
        {
            bool condition = Condition(cell, context);
            if (condition)
                context.FutureHealth = Result(cell.Health);

            return condition;
        }
Exemple #5
0
        private static object Abs(ICellContext ctx, IList <IExpression> args, out string error)
        {
            var realArgs = args.Eval(ctx, out error);
            var l        = realArgs[0];

            if (!(l is double))
            {
                error = BuildError("PLUS", "first argument is not a number.");
            }

            return((error is object)? null: (object)Math.Abs((double)l));
        }
Exemple #6
0
        /// <summary>
        /// Evaluates the arguments of a function, call this before sending the arguments to the underlying function.
        /// </summary>
        /// <param name="args">Arguments to evaluate.</param>
        /// <param name="context">Cell context to operate on.</param>
        /// <param name="error">Output string with any error.</param>
        /// <returns>Instance of List`1 with the evaluated arguments.</returns>
        public static IList <object> Eval(this IList <IExpression> args, ICellContext context, out string error)
        {
            error = null;
            var input = args ?? new List <IExpression>();
            var list  = new List <object>(input.Count);

            for (int i = 0; i < args.Count; i++)
            {
                list.Add(input[i].Evaluate(context, out error));
            }

            return(list);
        }
Exemple #7
0
        /// <inheritdoc/>
        public object Evaluate(ICellContext context, out string error)
        {
            error = null;
            object result = null;

            foreach (var statement in Body)
            {
                result = statement.Evaluate(context, out error);
                if (error is object)
                {
                    return(null);
                }
            }

            return(result);
        }
Exemple #8
0
        private static object GetCell(ICellContext ctx, IList <IExpression> args, out string error)
        {
            var realArgs = args.Eval(ctx, out error);
            var index    = realArgs[0];

            if (index is double)
            {
                index = (int)((double)index);
            }

            if (!(index is int))
            {
                error = BuildError("GET_CELL", "argument is not a number.");
            }

            return(error is object?null : ctx[(int)index]);
        }
Exemple #9
0
        private static object GreaterEqual(ICellContext ctx, IList <IExpression> args, out string error)
        {
            var realArgs = args.Eval(ctx, out error);
            var l        = realArgs[0];
            var r        = realArgs[1];

            if (!(l is double))
            {
                error = BuildError("GREATER_EQUAL", "first argument is not a number.");
            }

            if (!(r is double))
            {
                error = BuildError("GREATER_EQUAL", "second argument is not a number.");
            }

            return(error is object?null : (object)((double)l >= (double)r));
        }
Exemple #10
0
        private static object Equal(ICellContext ctx, IList <IExpression> args, out string error)
        {
            var realArgs = args.Eval(ctx, out error);
            var l        = realArgs[0];
            var r        = realArgs[1];

            if (l is null || l.Equals(string.Empty) || l.Equals(0.0))
            {
                l = false;
            }

            if (r is null || r.Equals(string.Empty) || r.Equals(0.0))
            {
                r = false;
            }

            return(l.Equals(r));
        }
Exemple #11
0
        /// <inheritdoc/>
        public object Invoke(ICellContext ctx, IList <IExpression> arguments, out string error)
        {
            error = null;
            if (arguments?.Count != ParameterCount && !IsVariadic)
            {
                error = $"{Name}: argument count differs from parameter count.";
            }

            if (IsVariadic && arguments?.Count < ParameterCount)
            {
                error = $"{Name}: function requires {(IsVariadic? "at least": "")} {ParameterCount} arguments.";
            }

            if (error is object)
            {
                return(null);
            }

            return(_func(ctx, arguments ?? new List <IExpression>(), out error));
        }
Exemple #12
0
        private static object Divide(ICellContext ctx, IList <IExpression> args, out string error)
        {
            var realArgs = args.Eval(ctx, out error);
            var l        = realArgs[0];
            var r        = realArgs[1];

            if (!(l is double))
            {
                error = BuildError("DIVIDE", "first argument is not a number.");
            }

            if (!(r is double))
            {
                error = BuildError("DIVIDE", "second argument is not a number.");
            }

            if ((double)r == 0.0)
            {
                error = BuildError("DIVIDE", "did you even try to divide by zero?");
            }

            return(error is object?null : (object)((double)l / (double)r));
        }
Exemple #13
0
        private static object GetRange(ICellContext ctx, IList <IExpression> args, out string error)
        {
            var realArgs = args.Eval(ctx, out error);
            var start    = realArgs[0];
            var end      = realArgs[1];

            if (start is double)
            {
                start = (int)((double)start);
            }

            if (end is double)
            {
                end = (int)((double)end);
            }

            if (!(start is int) || !(end is int))
            {
                error = BuildError("GET_RANGE", "argument is not a number.");
            }

            return(error is object?null : ctx[(int)start, (int)end]);
        }
Exemple #14
0
 private static object NotEqual(ICellContext ctx, IList <IExpression> args, out string error)
 {
     return(!(bool)Equal(ctx, args, out error));
 }
Exemple #15
0
 private static object Inspect(ICellContext ctx, IList <IExpression> args, out string error)
 {
     error = null;
     return(args[0].Inspect());
 }