Skip to content

Commit 66f1eab

Browse files
committed
inscript only multi line lambda + try to interprete property set assignation (work in progress)
1 parent b3b3fcb commit 66f1eab

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorScriptEvaluateTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Text.RegularExpressions;
5+
using Should;
56

67
namespace CodingSeb.ExpressionEvaluator.Tests
78
{
@@ -439,6 +440,45 @@ public void ExceptionThrowingScriptEvaluation(ExpressionEvaluator evaluator, str
439440

440441
#endregion
441442

443+
#region properties set assignation
444+
445+
[Test]
446+
public static void InstancePropertySet()
447+
{
448+
ClassForTest1 obj = new ClassForTest1();
449+
450+
ExpressionEvaluator evaluator = new ExpressionEvaluator()
451+
{
452+
Variables = new Dictionary<string, object>()
453+
{
454+
{"obj", obj }
455+
}
456+
};
457+
458+
obj.IntProperty.ShouldEqual(25);
459+
460+
evaluator.ScriptEvaluate("obj.IntProperty = 3;");
461+
462+
obj.IntProperty.ShouldEqual(3);
463+
}
464+
465+
[Test]
466+
public static void StaticPropertySet()
467+
{
468+
469+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
470+
471+
ClassForTest1.StaticIntProperty.ShouldEqual(67);
472+
473+
evaluator.ScriptEvaluate("ClassForTest1.StaticIntProperty = 18;");
474+
475+
ClassForTest1.StaticIntProperty.ShouldEqual(18);
476+
477+
ClassForTest1.StaticIntProperty = 67;
478+
}
479+
480+
#endregion
481+
442482
#region Remove Comments
443483

444484
public static IEnumerable<TestCaseData> TestCasesForRemoveCommentsTests

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ 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_]*)";
52+
5153
#endregion
5254

5355
#region enums (Operators, if else blocks states)
@@ -632,6 +634,8 @@ public ExpressionEvaluator(Dictionary<string, object> variables) : this()
632634

633635
#region Main evaluate methods (Expressions and scripts ==> public)
634636

637+
private bool inScript = false;
638+
635639
/// <summary>
636640
/// Evaluate a script (multiple expressions separated by semicolon)
637641
/// Support Assignation with [=] (for simple variable write in the Variables dictionary)
@@ -654,18 +658,26 @@ public T ScriptEvaluate<T>(string script)
654658
/// <returns>The result of the last evaluated expression</returns>
655659
public object ScriptEvaluate(string script)
656660
{
657-
bool isReturn = false;
658-
bool isBreak = false;
659-
bool isContinue = false;
661+
inScript = true;
662+
try
663+
{
664+
bool isReturn = false;
665+
bool isBreak = false;
666+
bool isContinue = false;
660667

661-
object result = ScriptEvaluate(script, ref isReturn, ref isBreak, ref isContinue);
668+
object result = ScriptEvaluate(script, ref isReturn, ref isBreak, ref isContinue);
662669

663-
if (isBreak)
664-
throw new ExpressionEvaluatorSyntaxErrorException("[break] keyword executed outside a loop");
665-
else if (isContinue)
666-
throw new ExpressionEvaluatorSyntaxErrorException("[continue] keyword executed outside a loop");
667-
else
668-
return result;
670+
if (isBreak)
671+
throw new ExpressionEvaluatorSyntaxErrorException("[break] keyword executed outside a loop");
672+
else if (isContinue)
673+
throw new ExpressionEvaluatorSyntaxErrorException("[continue] keyword executed outside a loop");
674+
else
675+
return result;
676+
}
677+
finally
678+
{
679+
inScript = false;
680+
}
669681
}
670682

671683
private object ScriptEvaluate(string script, ref bool valueReturned, ref bool breakCalled, ref bool continueCalled)
@@ -948,6 +960,8 @@ void forAction(int index)
948960
breakCalled = isBreak;
949961
continueCalled = isContinue;
950962

963+
inScript = false;
964+
951965
if (isReturn || OptionOnNoReturnKeywordFoundInScriptAction == OptionOnNoReturnKeywordFoundInScriptAction.ReturnAutomaticallyLastEvaluatedExpression)
952966
return lastResult;
953967
else if (OptionOnNoReturnKeywordFoundInScriptAction == OptionOnNoReturnKeywordFoundInScriptAction.ReturnNull)
@@ -1813,7 +1827,7 @@ private bool GetLambdaExpression(string expr, Stack<object> stack)
18131827

18141828
string lambdaBody = lambdaExpressionMatch.Groups["expression"].Value.Trim();
18151829

1816-
if (lambdaBody.StartsWith("{") && lambdaBody.EndsWith("}"))
1830+
if (inScript && lambdaBody.StartsWith("{") && lambdaBody.EndsWith("}"))
18171831
return expressionEvaluator.ScriptEvaluate(lambdaBody.Substring(1, lambdaBody.Length - 2));
18181832
else
18191833
return expressionEvaluator.Evaluate(lambdaExpressionMatch.Groups["expression"].Value);

0 commit comments

Comments
 (0)