Skip to content

Commit 26be788

Browse files
committed
Assignation in progress
1 parent 66f1eab commit 26be788

File tree

4 files changed

+49
-15
lines changed

4 files changed

+49
-15
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorScriptEvaluateTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,10 @@ public static void InstancePropertySet()
465465
[Test]
466466
public static void StaticPropertySet()
467467
{
468-
469468
ExpressionEvaluator evaluator = new ExpressionEvaluator();
470469

470+
evaluator.Namespaces.Add("CodingSeb.ExpressionEvaluator.Tests");
471+
471472
ClassForTest1.StaticIntProperty.ShouldEqual(67);
472473

473474
evaluator.ScriptEvaluate("ClassForTest1.StaticIntProperty = 18;");

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,8 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
974974

975975
yield return new TestCaseData("customObject.IntProperty", onInstanceVariables, true).SetCategory("Instance Property").Returns(25);
976976
yield return new TestCaseData("customObject?.IntProperty", onInstanceVariables, true).SetCategory("Instance Property").Returns(25);
977+
yield return new TestCaseData("customObject.intField", onInstanceVariables, true).SetCategory("Instance Field").Returns(12);
978+
yield return new TestCaseData("customObject?.intField", onInstanceVariables, true).SetCategory("Instance Field").Returns(12);
977979
yield return new TestCaseData("customObject.Add3To(9)", onInstanceVariables, true).SetCategory("Instance Method").Returns(12);
978980
yield return new TestCaseData("customObject?.Add3To(5)", onInstanceVariables, true).SetCategory("Instance Method").Returns(8);
979981

@@ -990,6 +992,7 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
990992
yield return new TestCaseData("List(simpleArray[1], simpleList?[0], nullVar?[4] ?? \"Bye\", \"How are you ?\").Find(t => t.Length < 4)", onInstanceVariables, true).SetCategory("Complex expression,Lambda function,Instance method,Instance Property,Null Conditional indexing, Null Coalescing Operator").Returns("Bye");
991993
yield return new TestCaseData("int.Parse(Regex.Match(\"Test 34 Hello / -World\", @\"\\d+\").Value) + simpleArray.ToList().Find(val => val is int)", onInstanceVariables, true).SetCategory("Complex expression,Static Method,Lambda function").Returns(36);
992994
yield return new TestCaseData("otherArray[3].IntProperty", onInstanceVariables, true).SetCategory("Indexing,Instance Property").Returns(18);
995+
yield return new TestCaseData("otherArray[3].intField", onInstanceVariables, true).SetCategory("Indexing,Instance Field").Returns(12);
993996
yield return new TestCaseData("(() => simpleInt + 1)()", onInstanceVariables, true).SetCategory("Complex expression").Returns(43);
994997

995998
yield return new TestCaseData("simpleInt++", onInstanceVariables, true).SetCategory("Postfix operator, ++").Returns(42);

CodingSeb.ExpressionEvaluator.Tests/TestsUtils/ClassForTest1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
{
33
public class ClassForTest1
44
{
5+
public int intField = 12;
6+
57
public int IntProperty { get; set; } = 25;
68

79
public static int StaticIntProperty { get; set; } = 67;

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ExpressionEvaluator
1515
{
1616
#region Regex declarations
1717

18-
private static Regex varOrFunctionRegEx = new Regex(@"^((?<sign>[+-])|(?<inObject>(?<nullConditional>[?])?\.)?)(?<name>[a-zA-Z_][a-zA-Z0-9_]*)\s*((?<postfixOperator>([+][+]|--)(?![a-zA-Z0-9_]))|((?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?(?<isfunction>[(])?))", RegexOptions.IgnoreCase);
18+
private static Regex varOrFunctionRegEx = new Regex(@"^((?<sign>[+-])|(?<inObject>(?<nullConditional>[?])?\.)?)(?<name>[a-zA-Z_][a-zA-Z0-9_]*)\s*((?<assignationOperator>(?<assignmentPrefix>[+\-*/%&|^]|<<|>>)?=(?![=>]))|(?<postfixOperator>([+][+]|--)(?![a-zA-Z0-9_]))|((?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?(?<isfunction>[(])?))", RegexOptions.IgnoreCase);
1919
private static Regex numberRegex = new Regex(@"^(?<sign>[+-])?\d+(?<hasdecimal>\.?\d+(e[+-]?\d+)?)?(?<type>ul|[fdulm])?", RegexOptions.IgnoreCase);
2020
private static Regex stringBeginningRegex = new Regex("^(?<interpolated>[$])?(?<escaped>[@])?[\"]");
2121
private static Regex internalCharRegex = new Regex(@"^['](\\[']|[^'])*[']");
@@ -48,7 +48,7 @@ public class ExpressionEvaluator
4848
private static readonly Regex blockBeginningRegex = new Regex(@"^\s*[{]");
4949
private static readonly Regex returnKeywordRegex = new Regex(@"^return(\s+|\()", RegexOptions.IgnoreCase | RegexOptions.Singleline);
5050

51-
private static readonly string tryToMatchSetPropertyRegexPattern = @"^(?<toEvaluate>([a-zA-Z_][a-zA-Z0-9_]*\s*(?<isgeneric>[(](?>[^()""]+|(?<parenthis>[(])|(?<-parenthis>[)]))*(?(parenthis) (?!))[)])*\.)+)(?<propertyToSet>[a-zA-Z_] [a-zA-Z0-9_]*)";
51+
//private static readonly string tryToMatchSetPropertyRegexPattern = @"^(?<toEvaluate>([a-zA-Z_][a-zA-Z0-9_]*\s*(?<isgeneric>[(](?>[^()""]+|(?<parenthis>[(])|(?<-parenthis>[)]))*(?(parenthis) (?!))[)])*\.)+)(?<propertyToSet>[a-zA-Z_] [a-zA-Z0-9_]*)";
5252

5353
#endregion
5454

@@ -1179,6 +1179,11 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
11791179

11801180
if (varFuncMatch.Groups["isfunction"].Success)
11811181
{
1182+
//if (varFuncMatch.Groups["assignationOperator"].Success)
1183+
//{
1184+
// throw new ExpressionEvaluatorSyntaxErrorException("The left part of an assignation must be a variable, a property or an indexer");
1185+
//}
1186+
11821187
List<string> funcArgs = GetExpressionsBetweenParenthis(expr, ref i, true);
11831188
if (varFuncMatch.Groups["inObject"].Success)
11841189
{
@@ -1337,25 +1342,48 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
13371342
BindingFlags flag = DetermineInstanceOrStatic(ref objType, ref obj);
13381343

13391344
if (!OptionStaticProperiesGetActive && flag.HasFlag(BindingFlags.Static))
1340-
throw new ExpressionEvaluatorSyntaxErrorException($"[{objType.ToString()}] object has no public Property or Member named \"{varFuncName}\".");
1345+
throw new ExpressionEvaluatorSyntaxErrorException($"[{objType.ToString()}] object has no public Property or Field named \"{varFuncName}\".");
13411346
if (!OptionInstanceProperiesGetActive && flag.HasFlag(BindingFlags.Instance))
1342-
throw new ExpressionEvaluatorSyntaxErrorException($"[{objType.ToString()}] object has no public Property or Member named \"{varFuncName}\".");
1347+
throw new ExpressionEvaluatorSyntaxErrorException($"[{objType.ToString()}] object has no public Property or Field named \"{varFuncName}\".");
1348+
1349+
dynamic member = objType?.GetProperty(varFuncName, flag);
1350+
dynamic varValue = null;
1351+
bool assign = true;
1352+
13431353

1344-
PropertyInfo property = objType?.GetProperty(varFuncName, flag);
1354+
if(member == null)
1355+
member = objType.GetField(varFuncName, flag);
1356+
1357+
varValue = member.GetValue(obj);
1358+
1359+
stack.Push(varValue);
13451360

1346-
dynamic varValue = property?.GetValue(obj);
1347-
if (varValue == null)
1361+
if (inScript && varFuncMatch.Groups["assignationOperator"].Success)
13481362
{
1349-
FieldInfo field = objType.GetField(varFuncName, flag);
1350-
varValue = field.GetValue(obj);
1351-
if(varFuncMatch.Groups["postfixOperator"].Success)
1352-
field.SetValue(obj, varFuncMatch.Groups["postfixOperator"].Value.Equals("++") ? varValue + 1 : varValue - 1);
1363+
if (stack.Count > 1)
1364+
throw new ExpressionEvaluatorSyntaxErrorException();
1365+
1366+
if(!varFuncMatch.Groups["assignmentPrefix"].Success)
1367+
{
1368+
string rightExpression = expr.Substring(i);
1369+
i = expr.Length;
1370+
1371+
if (rightExpression.Trim().Equals(string.Empty))
1372+
throw new ExpressionEvaluatorSyntaxErrorException();
1373+
1374+
varValue = Evaluate(rightExpression);
1375+
}
1376+
1377+
stack.Clear();
1378+
stack.Push(varValue);
13531379
}
13541380
else if (varFuncMatch.Groups["postfixOperator"].Success)
1355-
property.SetValue(obj, varFuncMatch.Groups["postfixOperator"].Value.Equals("++") ? varValue + 1 : varValue - 1);
1356-
1381+
varValue = varFuncMatch.Groups["postfixOperator"].Value.Equals("++") ? varValue + 1 : varValue - 1;
1382+
else
1383+
assign = false;
13571384

1358-
stack.Push(varValue);
1385+
if(assign)
1386+
member.SetValue(obj, varValue);
13591387
}
13601388
}
13611389
}

0 commit comments

Comments
 (0)