예제 #1
0
파일: Functions.cs 프로젝트: IamNaCl/Cell
        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);
        }
예제 #2
0
파일: Functions.cs 프로젝트: IamNaCl/Cell
        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);
        }
예제 #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;
        }
예제 #4
0
파일: Rule.cs 프로젝트: cthibault/Life
        public bool Evaluate(ICell cell, ICellContext context)
        {
            bool condition = Condition(cell, context);
            if (condition)
                context.FutureHealth = Result(cell.Health);

            return condition;
        }
예제 #5
0
파일: Functions.cs 프로젝트: IamNaCl/Cell
        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));
        }
예제 #6
0
파일: Functions.cs 프로젝트: IamNaCl/Cell
        /// <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);
        }
예제 #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);
        }
예제 #8
0
파일: Functions.cs 프로젝트: IamNaCl/Cell
        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]);
        }
예제 #9
0
파일: Functions.cs 프로젝트: IamNaCl/Cell
        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));
        }
예제 #10
0
파일: Functions.cs 프로젝트: IamNaCl/Cell
        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));
        }
예제 #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));
        }
예제 #12
0
파일: Functions.cs 프로젝트: IamNaCl/Cell
        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));
        }
예제 #13
0
파일: Functions.cs 프로젝트: IamNaCl/Cell
        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]);
        }
예제 #14
0
파일: Functions.cs 프로젝트: IamNaCl/Cell
 private static object NotEqual(ICellContext ctx, IList <IExpression> args, out string error)
 {
     return(!(bool)Equal(ctx, args, out error));
 }
예제 #15
0
파일: Functions.cs 프로젝트: IamNaCl/Cell
 private static object Inspect(ICellContext ctx, IList <IExpression> args, out string error)
 {
     error = null;
     return(args[0].Inspect());
 }