private static EvaluateExpressionDelegate Evaluator() { return(FunctionUtils.ApplyWithError( args => { object value = null; string error = null; var min = 0; var max = 0; (min, error) = FunctionUtils.ParseInt32(args[0]); if (error == null) { (max, error) = FunctionUtils.ParseInt32(args[1]); } if (min >= max) { error = $"{min} is not < {max} for rand"; } else { lock (_randomizerLock) { value = Randomizer.Next(min, max); } } return (value, error); }, FunctionUtils.VerifyInteger)); }
private static EvaluateExpressionDelegate Evaluator() { return(FunctionUtils.ApplyWithError( args => { string error = null; IList result = null; var count = 0; (count, error) = FunctionUtils.ParseInt32(args[1]); if (error == null) { if (count <= 0) { error = $"The second parameter {args[1]} should be more than zero"; } else { var start = 0; (start, error) = FunctionUtils.ParseInt32(args[0]); if (error == null) { result = Enumerable.Range(start, count).ToList(); } } } return (result, error); }, FunctionUtils.VerifyInteger)); }
private static (object, string) Evaluator(Expression expression, IMemory state, Options options) { object result = null; object minValue; string error; (minValue, error) = expression.Children[0].TryEvaluate(state, options); if (error != null) { return(result, error); } int minValueInt; (minValueInt, error) = FunctionUtils.ParseInt32(minValue); if (error != null) { return(result, error); } object maxValue; (maxValue, error) = expression.Children[1].TryEvaluate(state, options); if (error != null) { return(result, error); } int maxValueInt; (maxValueInt, error) = FunctionUtils.ParseInt32(maxValue); if (error != null) { return(result, error); } if (minValueInt >= maxValueInt) { error = $"{minValueInt} is not < {maxValueInt} for rand"; } else { result = state.RandomNext(minValueInt, maxValueInt); } return(result, error); }
private static EvaluateExpressionDelegate Evaluator() { return(FunctionUtils.ApplyWithError( args => { object result = null; string error = null; var input = 0; (input, error) = FunctionUtils.ParseInt32(args[0]); if (error == null) { result = EvalAddOrdinal(input); } return (result, error); }, FunctionUtils.VerifyInteger)); }
private static EvaluateExpressionDelegate Evaluator(Func <DateTime, int, DateTime> function) { return((expression, state, options) => { object value = null; string error = null; IReadOnlyList <object> args; var locale = options.Locale != null ? new CultureInfo(options.Locale) : Thread.CurrentThread.CurrentCulture; var format = FunctionUtils.DefaultDateTimeFormat; (args, error) = FunctionUtils.EvaluateChildren(expression, state, options); if (error == null) { (format, locale, error) = FunctionUtils.DetermineFormatAndLocale(args, format, locale, 4); } if (error == null) { if (args[1].IsInteger()) { (value, error) = FunctionUtils.NormalizeToDateTime(args[0], dt => { var result = dt; var(interval, error) = FunctionUtils.ParseInt32(args[1]); if (error == null) { result = function(dt, interval); } return (result, error); }); if (error == null) { value = Convert.ToDateTime(value, CultureInfo.InvariantCulture).ToString(format, locale); } } else { error = $"{expression} should contain an ISO format timestamp and a time interval integer."; } } return (value, error); }); }
private static EvaluateExpressionDelegate Evaluator() { return(FunctionUtils.ApplyWithError( args => { var depth = 100; object result = null; string error = null; if (args.Count > 1) { (depth, error) = FunctionUtils.ParseInt32(args[1]); } if (error == null) { result = EvalFlatten((IEnumerable <object>)args[0], depth); } return (result, error); })); }
private static (object value, string error) Evaluator(Expression expression, IMemory state, Options options) { object value = null; string error; var instance = expression.Children[0]; var index = expression.Children[1]; object inst; (inst, error) = instance.TryEvaluate(state, options); if (error == null) { object idxValue; (idxValue, error) = index.TryEvaluate(state, new Options(options) { NullSubstitution = null }); if (error == null) { if (idxValue.IsInteger()) { var idx = 0; (idx, error) = FunctionUtils.ParseInt32(idxValue); if (error == null) { (value, error) = FunctionUtils.AccessIndex(inst, idx); } } else if (idxValue is string idxStr) { FunctionUtils.TryAccessProperty(inst, idxStr, out value); } else { error = $"Could not coerce {index}<{idxValue?.GetType()}> to an int or string"; } } } return(value, error); }
private static EvaluateExpressionDelegate Evaluator() { return(FunctionUtils.ApplyWithError( args => { string result = null; string error = null; if (!args[0].IsNumber()) { error = $"formatNumber first argument {args[0]} must be number"; } else if (!args[1].IsInteger()) { error = $"formatNumber second argument {args[1]} must be number"; } else if (args.Count == 3 && !(args[2] is string)) { error = $"formatNumber third agument {args[2]} must be a locale"; } else { var precision = 0; (precision, error) = FunctionUtils.ParseInt32(args[1]); try { var number = Convert.ToDouble(args[0], CultureInfo.InvariantCulture); var locale = args.Count == 3 ? new CultureInfo(args[2] as string) : CultureInfo.InvariantCulture; result = number.ToString("N" + precision, locale); } #pragma warning disable CA1031 // Do not catch general exception types (we are capturing the exception and returning it) catch #pragma warning restore CA1031 // Do not catch general exception types { error = $"{args[3]} is not a valid locale for formatNumber"; } } return (result, error); })); }
private static EvaluateExpressionDelegate Evaluator() { return(FunctionUtils.ApplyWithOptionsAndError( (args, options) => { string result = null; string error = null; var locale = options.Locale != null ? new CultureInfo(options.Locale) : Thread.CurrentThread.CurrentCulture; if (!args[0].IsNumber()) { error = $"formatNumber first argument {args[0]} must be number"; } else if (!args[1].IsInteger()) { error = $"formatNumber second argument {args[1]} must be number"; } else if (args.Count == 3 && !(args[2] is string)) { error = $"formatNumber third agument {args[2]} must be a locale"; } if (error == null) { (locale, error) = FunctionUtils.DetermineLocale(args, locale, 3); } if (error == null) { var precision = 0; (precision, error) = FunctionUtils.ParseInt32(args[1]); if (error == null) { var number = Convert.ToDouble(args[0], CultureInfo.InvariantCulture); result = number.ToString("N" + precision.ToString(CultureInfo.InvariantCulture), locale); } } return (result, error); })); }
private static EvaluateExpressionDelegate Evaluator(Func <DateTime, int, DateTime> function) { return((expression, state, options) => { object value = null; string error = null; IReadOnlyList <object> args; (args, error) = FunctionUtils.EvaluateChildren(expression, state, options); if (error == null) { if (args[1].IsInteger()) { var formatString = (args.Count == 3 && args[2] is string string1) ? string1 : FunctionUtils.DefaultDateTimeFormat; (value, error) = FunctionUtils.NormalizeToDateTime(args[0], dt => { var result = dt; var(interval, error) = FunctionUtils.ParseInt32(args[1]); if (error == null) { result = function(dt, interval); } return (result, error); }); if (error == null) { value = Convert.ToDateTime(value, CultureInfo.InvariantCulture).ToString(formatString, CultureInfo.InvariantCulture); } } else { error = $"{expression} should contain an ISO format timestamp and a time interval integer."; } } return (value, error); }); }
private static EvaluateExpressionDelegate Evaluator() { return(FunctionUtils.ApplyWithError( args => { string error = null; object result = null; if (args.Count == 2 && !args[1].IsInteger()) { error = $"The second {args[1]} parameter must be an integer."; } if (error == null) { var digits = 0; if (args.Count == 2) { (digits, error) = FunctionUtils.ParseInt32(args[1]); } if (error == null) { if (digits < 0 || digits > 15) { error = $"The second parameter {args[1]} must be an integer between 0 and 15;"; } else { result = Math.Round(Convert.ToDouble(args[0], CultureInfo.InvariantCulture), digits); } } } return (result, error); }, FunctionUtils.VerifyNumber)); }
private static EvaluateExpressionDelegate Evaluator() { return(FunctionUtils.ApplyWithError(args => FunctionUtils.NormalizeToDateTime(args[0], dt => FunctionUtils.ParseInt32(dt.DayOfWeek)))); }