|
@@ -9,6 +9,7 @@ using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Text.RegularExpressions;
|
|
|
using Expressive.Exceptions;
|
|
|
+using Expressive.Operators;
|
|
|
|
|
|
namespace InABox.Core
|
|
|
{
|
|
@@ -21,14 +22,58 @@ namespace InABox.Core
|
|
|
public abstract string Description { get; }
|
|
|
public abstract string[] Parameters { get; }
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ public interface IExpressionConstant
|
|
|
+ {
|
|
|
+ string Name { get; set; }
|
|
|
+
|
|
|
+ object? GetValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ public class CoreExpressionFunctor : CoreExpressionFunction
|
|
|
+ {
|
|
|
+ public delegate object? EvaluateDelegate(IExpression[] parameters, IDictionary<string, object?> variables, Context context);
|
|
|
+
|
|
|
+ public EvaluateDelegate Function { get; set; }
|
|
|
+
|
|
|
+ private string _name;
|
|
|
+ public override string Name => _name;
|
|
|
+
|
|
|
+ private string _description;
|
|
|
+ public override string Description => _description;
|
|
|
+
|
|
|
+ private string _group;
|
|
|
+ public override string Group => _group;
|
|
|
+
|
|
|
+ private string[] _parameters;
|
|
|
+ public override string[] Parameters => _parameters;
|
|
|
+
|
|
|
+ private int minParameters;
|
|
|
+
|
|
|
+ public CoreExpressionFunctor(EvaluateDelegate function, string name, string description, string group, string[] parameters, int? minParameters = null)
|
|
|
+ {
|
|
|
+ Function = function;
|
|
|
+ _name = name;
|
|
|
+ _description = description;
|
|
|
+ _group = group;
|
|
|
+ _parameters = parameters;
|
|
|
+ minParameters = minParameters ?? parameters.Length;
|
|
|
+ }
|
|
|
+
|
|
|
+ public override object? Evaluate(IExpression[] parameters, Context context)
|
|
|
+ {
|
|
|
+ ValidateParameterCount(parameters, _parameters.Length, minParameters);
|
|
|
+ return Function(parameters, Variables, context);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
internal class FormatFunction : CoreExpressionFunction
|
|
|
{
|
|
|
#region IFunction Members
|
|
|
|
|
|
public override object Evaluate(IExpression[] parameters, Context context)
|
|
|
{
|
|
|
- ValidateParameterCount(parameters,-1, 2);
|
|
|
+ ValidateParameterCount(parameters, -1, 2);
|
|
|
|
|
|
string fmt = parameters.First()?.Evaluate(Variables).ToString() ?? string.Empty;
|
|
|
object[] objects = parameters.Skip(1).Select(x => x.Evaluate(Variables)).ToArray();
|
|
@@ -174,17 +219,27 @@ namespace InABox.Core
|
|
|
|
|
|
private static Context _context = new Context(ExpressiveOptions.None);
|
|
|
|
|
|
- static void RegisterFunction<T>() where T : CoreExpressionFunction, new ()
|
|
|
+ public static void RegisterFunction<T>() where T : CoreExpressionFunction, new ()
|
|
|
{
|
|
|
var function = new T();
|
|
|
Functions.Add(function);
|
|
|
_context.RegisterFunction(function);
|
|
|
}
|
|
|
+
|
|
|
+ public static void RegisterFunction(string name, string description, string group, string[] parameters, CoreExpressionFunctor.EvaluateDelegate function)
|
|
|
+ {
|
|
|
+ var f = new CoreExpressionFunctor(function, name, description, group, parameters);
|
|
|
+ Functions.Add(f);
|
|
|
+ _context.RegisterFunction(f);
|
|
|
+ }
|
|
|
|
|
|
static CoreExpression()
|
|
|
{
|
|
|
RegisterFunction<FormatFunction>();
|
|
|
RegisterFunction<Client_LoadDocumentFunction>();
|
|
|
+
|
|
|
+ DateFunctions.Register();
|
|
|
+ ArrayFunctions.Register();
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
@@ -242,4 +297,13 @@ namespace InABox.Core
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static class CoreExpressionExtensions
|
|
|
+ {
|
|
|
+ public static T Evaluate<T>(this IExpression expression, IDictionary<string, object?>? variables)
|
|
|
+ {
|
|
|
+ var result = expression.Evaluate(variables);
|
|
|
+ return CoreUtils.ChangeType<T>(result);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|