Skip to content

Commit 417fa9a

Browse files
committed
OnTheFlyIndexing in progress
1 parent 3ac96d1 commit 417fa9a

File tree

1 file changed

+130
-22
lines changed

1 file changed

+130
-22
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 130 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ protected virtual BindingFlags StaticBindingFlag
918918
private int evaluationStackCount;
919919

920920
/// <summary>
921-
/// The Values of the variable use in the expressions
921+
/// The values of the variable use in the expressions
922922
/// </summary>
923923
public IDictionary<string, object> Variables
924924
{
@@ -927,34 +927,47 @@ public IDictionary<string, object> Variables
927927
}
928928

929929
/// <summary>
930-
/// Is Fired before a variable, field or property resolution.
930+
/// Is fired before a variable, field or property resolution.
931931
/// Allow to define a variable and the corresponding value on the fly.
932932
/// Allow also to cancel the evaluation of this variable (consider it does'nt exists)
933933
/// </summary>
934934
public event EventHandler<VariablePreEvaluationEventArg> PreEvaluateVariable;
935935

936936
/// <summary>
937-
/// Is Fired before a function or method resolution.
937+
/// Is fired before a function or method resolution.
938938
/// Allow to define a function or method and the corresponding value on the fly.
939939
/// Allow also to cancel the evaluation of this function (consider it does'nt exists)
940940
/// </summary>
941941
public event EventHandler<FunctionPreEvaluationEventArg> PreEvaluateFunction;
942942

943943
/// <summary>
944-
/// Is Fired if no variable, field or property were found
944+
/// Is fired before a indexing resolution.
945+
/// Allow to define an indexing and the corresponding value on the fly.
946+
/// Allow also to cancel the evaluation of this indexing (consider it does'nt exists)
947+
/// </summary>
948+
public event EventHandler<IndexingPreEvaluationEventArg> PreEvaluateIndexing;
949+
950+
/// <summary>
951+
/// Is fired if no variable, field or property were found
945952
/// Allow to define a variable and the corresponding value on the fly.
946953
/// </summary>
947954
public event EventHandler<VariableEvaluationEventArg> EvaluateVariable;
948955

949956
/// <summary>
950-
/// Is Fired if no function or method when were found.
957+
/// Is fired if no function or method were found.
951958
/// Allow to define a function or method and the corresponding value on the fly.
952959
/// </summary>
953960
public event EventHandler<FunctionEvaluationEventArg> EvaluateFunction;
954961

955962
/// <summary>
956-
/// Is fired when a parameter ist not the correct type for the function.
957-
/// Allow to define a custom parameter cast for the function to work on the fly.
963+
/// Is fired if no indexing were found.
964+
/// Allow to define an indexing and the corresponding value on the fly.
965+
/// </summary>
966+
public event EventHandler<IndexingEvaluationEventArg> EvaluateIndexing;
967+
968+
/// <summary>
969+
/// Is fired when a parameter is not of the correct type for the function.
970+
/// Allow to define a custom parameter cast to make the function call work on the fly.
958971
/// </summary>
959972
public event EventHandler<ParameterCastEvaluationEventArg> EvaluateParameterCast;
960973

@@ -1541,7 +1554,7 @@ public T Evaluate<T>(string expression)
15411554
EvaluateOperators,
15421555
EvaluateChar,
15431556
EvaluateParenthis,
1544-
EvaluateIndexing,
1557+
EvaluateIndexingOperator,
15451558
EvaluateString,
15461559
EvaluateTernaryConditionalOperator,
15471560
});
@@ -1909,7 +1922,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
19091922
}
19101923
else
19111924
{
1912-
FunctionPreEvaluationEventArg functionPreEvaluationEventArg = new FunctionPreEvaluationEventArg(varFuncName, Evaluate, funcArgs, this, obj, genericsTypes, GetConcreteTypes);
1925+
FunctionPreEvaluationEventArg functionPreEvaluationEventArg = new FunctionPreEvaluationEventArg(varFuncName, funcArgs, this, obj, genericsTypes, GetConcreteTypes);
19131926

19141927
PreEvaluateFunction?.Invoke(this, functionPreEvaluationEventArg);
19151928

@@ -2025,7 +2038,7 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
20252038
}
20262039
else
20272040
{
2028-
FunctionEvaluationEventArg functionEvaluationEventArg = new FunctionEvaluationEventArg(varFuncName, Evaluate, funcArgs, this, obj ?? keepObj, genericsTypes, GetConcreteTypes);
2041+
FunctionEvaluationEventArg functionEvaluationEventArg = new FunctionEvaluationEventArg(varFuncName, funcArgs, this, obj ?? keepObj, genericsTypes, GetConcreteTypes);
20292042

20302043
EvaluateFunction?.Invoke(this, functionEvaluationEventArg);
20312044

@@ -2089,7 +2102,7 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
20892102
}
20902103
else
20912104
{
2092-
FunctionPreEvaluationEventArg functionPreEvaluationEventArg = new FunctionPreEvaluationEventArg(varFuncName, Evaluate, funcArgs, this, null, genericsTypes, GetConcreteTypes);
2105+
FunctionPreEvaluationEventArg functionPreEvaluationEventArg = new FunctionPreEvaluationEventArg(varFuncName, funcArgs, this, null, genericsTypes, GetConcreteTypes);
20932106

20942107
PreEvaluateFunction?.Invoke(this, functionPreEvaluationEventArg);
20952108

@@ -2115,7 +2128,7 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
21152128
}
21162129
else
21172130
{
2118-
FunctionEvaluationEventArg functionEvaluationEventArg = new FunctionEvaluationEventArg(varFuncName, Evaluate, funcArgs, this, genericTypes: genericsTypes, evaluateGenericTypes: GetConcreteTypes);
2131+
FunctionEvaluationEventArg functionEvaluationEventArg = new FunctionEvaluationEventArg(varFuncName, funcArgs, this, genericTypes: genericsTypes, evaluateGenericTypes: GetConcreteTypes);
21192132

21202133
EvaluateFunction?.Invoke(this, functionEvaluationEventArg);
21212134

@@ -2631,7 +2644,7 @@ protected virtual void CorrectStackWithUnaryPlusOrMinusBeforeParenthisIfNecessar
26312644
}
26322645
}
26332646

2634-
protected virtual bool EvaluateIndexing(string expression, Stack<object> stack, ref int i)
2647+
protected virtual bool EvaluateIndexingOperator(string expression, Stack<object> stack, ref int i)
26352648
{
26362649
if (!OptionIndexingActive)
26372650
return false;
@@ -2687,6 +2700,10 @@ protected virtual bool EvaluateIndexing(string expression, Stack<object> stack,
26872700
return true;
26882701
}
26892702

2703+
//IndexingPreEvaluationEventArg indexingPreEvaluationEventArg = new IndexingPreEvaluationEventArg(innerExp.ToString(), this, left);
2704+
2705+
//PreEvaluateIndexing?.Invoke(this, indexingPreEvaluationEventArg);
2706+
26902707
dynamic right = Evaluate(innerExp.ToString());
26912708
ExpressionOperator op = indexingBeginningMatch.Length == 2 ? ExpressionOperator.IndexingWithNullConditional : ExpressionOperator.Indexing;
26922709

@@ -4003,7 +4020,7 @@ public enum OptionOnNoReturnKeywordFoundInScriptAction
40034020

40044021
public partial class ExpressionOperator : IEquatable<ExpressionOperator>
40054022
{
4006-
protected static uint indexer = 0;
4023+
protected static uint indexer;
40074024

40084025
protected ExpressionOperator()
40094026
{
@@ -4191,6 +4208,9 @@ public ExpressionEvaluatorSecurityException(string message, Exception innerExcep
41914208
{ }
41924209
}
41934210

4211+
/// <summary>
4212+
/// Infos about the variable, attribut or property that is currently evaluate
4213+
/// </summary>
41944214
public partial class VariableEvaluationEventArg : EventArgs
41954215
{
41964216
private readonly Func<string, Type[]> evaluateGenericTypes;
@@ -4267,6 +4287,9 @@ public Type[] EvaluateGenericTypes()
42674287
}
42684288
}
42694289

4290+
/// <summary>
4291+
/// Infos about the variable, attribut or property that is currently evaluate
4292+
/// </summary>
42704293
public partial class VariablePreEvaluationEventArg : VariableEvaluationEventArg
42714294
{
42724295
public VariablePreEvaluationEventArg(string name, ExpressionEvaluator evaluator = null, object onInstance = null, string genericTypes = null, Func<string, Type[]> evaluateGenericTypes = null)
@@ -4279,17 +4302,18 @@ public VariablePreEvaluationEventArg(string name, ExpressionEvaluator evaluator
42794302
public bool CancelEvaluation { get; set; }
42804303
}
42814304

4305+
/// <summary>
4306+
/// Infos about the function or method that is currently evaluate
4307+
/// </summary>
42824308
public partial class FunctionEvaluationEventArg : EventArgs
42834309
{
4284-
private readonly Func<string, object> evaluateFunc;
42854310
private readonly Func<string, Type[]> evaluateGenericTypes;
42864311
private readonly string genericTypes;
42874312

4288-
public FunctionEvaluationEventArg(string name, Func<string, object> evaluateFunc, List<string> args = null, ExpressionEvaluator evaluator = null, object onInstance = null, string genericTypes = null, Func<string, Type[]> evaluateGenericTypes = null)
4313+
public FunctionEvaluationEventArg(string name, List<string> args = null, ExpressionEvaluator evaluator = null, object onInstance = null, string genericTypes = null, Func<string, Type[]> evaluateGenericTypes = null)
42894314
{
42904315
Name = name;
42914316
Args = args ?? new List<string>();
4292-
this.evaluateFunc = evaluateFunc;
42934317
This = onInstance;
42944318
Evaluator = evaluator;
42954319
this.genericTypes = genericTypes;
@@ -4307,7 +4331,7 @@ public FunctionEvaluationEventArg(string name, Func<string, object> evaluateFunc
43074331
/// <returns></returns>
43084332
public object[] EvaluateArgs()
43094333
{
4310-
return Args.ConvertAll(arg => evaluateFunc(arg)).ToArray();
4334+
return Args.ConvertAll(arg => Evaluator.Evaluate(arg)).ToArray();
43114335
}
43124336

43134337
/// <summary>
@@ -4317,7 +4341,7 @@ public object[] EvaluateArgs()
43174341
/// <returns>The evaluated arg</returns>
43184342
public object EvaluateArg(int index)
43194343
{
4320-
return evaluateFunc(Args[index]);
4344+
return Evaluator.Evaluate(Args[index]);
43214345
}
43224346

43234347
/// <summary>
@@ -4328,7 +4352,7 @@ public object EvaluateArg(int index)
43284352
/// <returns>The evaluated arg casted in the specified type</returns>
43294353
public T EvaluateArg<T>(int index)
43304354
{
4331-
return (T)evaluateFunc(Args[index]);
4355+
return Evaluator.Evaluate<T>(Args[index]);
43324356
}
43334357

43344358
/// <summary>
@@ -4389,10 +4413,94 @@ public Type[] EvaluateGenericTypes()
43894413
}
43904414
}
43914415

4416+
/// <summary>
4417+
/// Infos about the function or method that is currently evaluate
4418+
/// </summary>
43924419
public partial class FunctionPreEvaluationEventArg : FunctionEvaluationEventArg
43934420
{
4394-
public FunctionPreEvaluationEventArg(string name, Func<string, object> evaluateFunc, List<string> args = null, ExpressionEvaluator evaluator = null, object onInstance = null, string genericTypes = null, Func<string, Type[]> evaluateGenericTypes = null)
4395-
: base(name, evaluateFunc, args, evaluator, onInstance, genericTypes, evaluateGenericTypes)
4421+
public FunctionPreEvaluationEventArg(string name, List<string> args = null, ExpressionEvaluator evaluator = null, object onInstance = null, string genericTypes = null, Func<string, Type[]> evaluateGenericTypes = null)
4422+
: base(name, args, evaluator, onInstance, genericTypes, evaluateGenericTypes)
4423+
{ }
4424+
4425+
/// <summary>
4426+
/// If set to true cancel the evaluation of the current function or method and throw an exception that the function does not exists
4427+
/// </summary>
4428+
public bool CancelEvaluation { get; set; }
4429+
}
4430+
4431+
/// <summary>
4432+
/// Infos about the indexing that is currently evaluate
4433+
/// </summary>
4434+
public partial class IndexingEvaluationEventArg : EventArgs
4435+
{
4436+
public IndexingEvaluationEventArg(string arg, ExpressionEvaluator evaluator, object onInstance)
4437+
{
4438+
Arg = arg;
4439+
This = onInstance;
4440+
Evaluator = evaluator;
4441+
}
4442+
4443+
/// <summary>
4444+
/// The not evaluated args of the indexing
4445+
/// </summary>
4446+
public string Arg { get; set; }
4447+
4448+
/// <summary>
4449+
/// The instance of the object on which the indexing is called.
4450+
/// </summary>
4451+
public object This { get; }
4452+
4453+
private object returnValue;
4454+
4455+
/// <summary>
4456+
/// To set the result value of the indexing
4457+
/// </summary>
4458+
public object Value
4459+
{
4460+
get { return returnValue; }
4461+
set
4462+
{
4463+
returnValue = value;
4464+
HasValue = true;
4465+
}
4466+
}
4467+
4468+
/// <summary>
4469+
/// if <c>true</c> the indexing evaluation has been done, if <c>false</c> it means that the indexing does not exist.
4470+
/// </summary>
4471+
public bool HasValue { get; set; }
4472+
4473+
/// <summary>
4474+
/// A reference on the current expression evaluator.
4475+
/// </summary>
4476+
public ExpressionEvaluator Evaluator { get; }
4477+
4478+
/// <summary>
4479+
/// Get the values of the indexing's args.
4480+
/// </summary>
4481+
/// <returns></returns>
4482+
public object EvaluateArg()
4483+
{
4484+
return Evaluator.Evaluate(Arg);
4485+
}
4486+
4487+
/// <summary>
4488+
/// Get the values of the indexing's args.
4489+
/// </summary>
4490+
/// <returns></returns>
4491+
public T EvaluateArg<T>()
4492+
{
4493+
return Evaluator.Evaluate<T>(Arg);
4494+
}
4495+
}
4496+
4497+
/// <summary>
4498+
/// Infos about the indexing that is currently evaluate
4499+
/// </summary>
4500+
public partial class IndexingPreEvaluationEventArg : IndexingEvaluationEventArg
4501+
{
4502+
public IndexingPreEvaluationEventArg(string arg, ExpressionEvaluator evaluator, object onInstance)
4503+
: base(arg, evaluator, onInstance)
43964504
{ }
43974505

43984506
/// <summary>

0 commit comments

Comments
 (0)