Skip to content

Commit ee5c1a5

Browse files
authored
Merge pull request #11 from codingseb/dev
Dev
2 parents 14ffef6 + ea45006 commit ee5c1a5

File tree

8 files changed

+105
-13
lines changed

8 files changed

+105
-13
lines changed

CodingSeb.ExpressionEvaluator.Tests/CodingSeb.ExpressionEvaluator.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@
122122
<ItemGroup>
123123
<None Include="Resources\Script0017.txt" />
124124
</ItemGroup>
125+
<ItemGroup>
126+
<None Include="Resources\Script0018.txt" />
127+
</ItemGroup>
125128
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
126129
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
127130
<PropertyGroup>

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorScriptEvaluateTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,17 @@ public static IEnumerable<TestCaseData> TestCasesForScriptEvaluateTests
667667

668668
#endregion
669669

670+
#region foreach
671+
672+
yield return new TestCaseData(Resources.Script0018, null, null, null)
673+
.SetCategory("Script")
674+
.SetCategory("foreach")
675+
.SetCategory("variable assignation")
676+
.SetCategory("+=")
677+
.Returns("This is a splitted text");
678+
679+
#endregion
680+
670681
#region if, else if, else
671682

672683
yield return new TestCaseData(Resources.Script0004.Replace("[valx]", "0").Replace("[valy]", "1"), null, null, null)

CodingSeb.ExpressionEvaluator.Tests/Resources.Designer.cs

Lines changed: 27 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodingSeb.ExpressionEvaluator.Tests/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,7 @@
169169
<data name="Script0017" type="System.Resources.ResXFileRef, System.Windows.Forms">
170170
<value>resources\script0017.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
171171
</data>
172+
<data name="Script0018" type="System.Resources.ResXFileRef, System.Windows.Forms">
173+
<value>resources\script0018.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
174+
</data>
172175
</root>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Script0018 */
2+
3+
result = string.Empty;
4+
elements = List("This", "is", "a", "splitted", "text");
5+
6+
foreach(element in elements)
7+
{
8+
result += element;
9+
result += " ";
10+
}
11+
12+
result.Remove(result.Length - 1);

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public class ExpressionEvaluator
4444
private static readonly Regex newLineCharsRegex = new Regex(@"\r\n|\r|\n");
4545

4646
// For script only
47-
private static readonly Regex blockKeywordsBeginningRegex = new Regex(@"^\s*(?<keyword>while|for|if|else\s+if)\s*[(]", RegexOptions.IgnoreCase);
47+
private static readonly Regex blockKeywordsBeginningRegex = new Regex(@"^\s*(?<keyword>while|for|foreach|if|else\s+if)\s*[(]", RegexOptions.IgnoreCase);
48+
private static readonly Regex foreachParenthisEvaluationRegex = new Regex(@"^\s*(?<variableName>[a-zA-Z_][a-zA-Z0-9_]*)\s+(?<in>in)\s+(?<collection>.*)", RegexOptions.IgnoreCase);
4849
private static readonly Regex blockKeywordsWithoutParenthesesBeginningRegex = new Regex(@"^\s*(?<keyword>else|do)(?![a-zA-Z0-9_])", RegexOptions.IgnoreCase);
4950
private static readonly Regex blockBeginningRegex = new Regex(@"^\s*[{]");
5051
private static readonly Regex returnKeywordRegex = new Regex(@"^return(\s+|\()", RegexOptions.IgnoreCase | RegexOptions.Singleline);
@@ -946,7 +947,10 @@ void ExecuteIfList()
946947
else if (keyword.Equals("for"))
947948
{
948949
void forAction(int index)
949-
{ if (keywordAttributes.Count > index && !keywordAttributes[index].Trim().Equals(string.Empty)) ManageJumpStatementsOrExpressionEval(keywordAttributes[index]); }
950+
{
951+
if (keywordAttributes.Count > index && !keywordAttributes[index].Trim().Equals(string.Empty))
952+
ManageJumpStatementsOrExpressionEval(keywordAttributes[index]);
953+
}
950954

951955
for (forAction(0); !isReturn && (bool)ManageJumpStatementsOrExpressionEval(keywordAttributes[1]); forAction(2))
952956
{
@@ -964,6 +968,41 @@ void forAction(int index)
964968
}
965969
}
966970
}
971+
else if(keyword.Equals("foreach"))
972+
{
973+
Match foreachParenthisEvaluationMatch = foreachParenthisEvaluationRegex.Match(keywordAttributes[0]);
974+
975+
if(!foreachParenthisEvaluationMatch.Success)
976+
{
977+
throw new ExpressionEvaluatorSyntaxErrorException("wrong foreach syntax");
978+
}
979+
else if(!foreachParenthisEvaluationMatch.Groups["in"].Value.ManageCasing(OptionCaseSensitiveEvaluationActive).Equals("in"))
980+
{
981+
throw new ExpressionEvaluatorSyntaxErrorException("no [in] keyword found in foreach");
982+
}
983+
else
984+
{
985+
dynamic collection = Evaluate(foreachParenthisEvaluationMatch.Groups["collection"].Value);
986+
987+
foreach(dynamic foreachValue in collection)
988+
{
989+
Variables[foreachParenthisEvaluationMatch.Groups["variableName"].Value] = foreachValue;
990+
991+
lastResult = ScriptEvaluate(subScript, ref isReturn, ref isBreak, ref isContinue);
992+
993+
if (isBreak)
994+
{
995+
isBreak = false;
996+
break;
997+
}
998+
if (isContinue)
999+
{
1000+
isContinue = false;
1001+
continue;
1002+
}
1003+
}
1004+
}
1005+
}
9671006
}
9681007

9691008
startOfExpression = i;

CodingSeb.ExpressionEvaluator/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
3333
// en utilisant '*', comme indiqué ci-dessous :
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.2.0.0")]
36-
[assembly: AssemblyFileVersion("1.2.0.0")]
35+
[assembly: AssemblyVersion("1.2.1.0")]
36+
[assembly: AssemblyFileVersion("1.2.1.0")]

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ It is largely based on and inspired by the following resources [this post on st
2929
* Classes like File, Directory, Regex, List ... available ([You can extend the list of Namespaces](#namespaces))
3030
* Create instance with [new(MyClassName, constructorArgs)](#standard-functions) or [new MyClassName(constructorArgs)](#operators)
3131
* [Call void methods with fluid prefix convention to chain operations](#go-fluid-with-a-simple-methods-prefixing-convention)
32-
* Manage now assignation operators like =, +=, -=, *= ... (On variables and sub properties)
33-
* Manage now postfix operators ++ and -- (On variables and sub properties)
32+
* Manage now [assignation operators](#assignation-operators) like =, +=, -=, *= ...
33+
* Manage now postfix [operators](#operators) ++ and --
3434

3535
## And with [ScriptEvaluate](#scripts) method
3636
* Small C# like script evaluation (Multi expressions separated by ;)
37-
* Some conditional and loop blocks [keywords](#script-keywords) (if, while, for ...)
37+
* Some conditional and loop blocks [keywords](#script-keywords) (if, while, for, foreach ...)
3838
* Multi-line (multi expression) Lambda expressions.
3939

4040
## Getting started
@@ -376,7 +376,7 @@ The following functions are internally defined. (Most of these are [System.Math
376376
377377
## On the fly variables and functions evaluation
378378
In addition to custom variables, you can add variables and/or functions with on the fly evaluation.
379-
2 C# events are provided that are fired when variables or functions are not fund as standard ones in evaluation time.
379+
2 C# events are provided that are fired when variables or functions are not found as standard ones in evaluation time.
380380

381381
*Remark : Can be use to define or redefine on object instances methods or properties*
382382
```C#
@@ -526,7 +526,7 @@ Here is a list of which operators are supported in ExpressionEvaluator or not
526526
|Conditional AND|[x && y](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-and-operator)|Supported|
527527
|Conditional OR|[x &#124;&#124; y](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-or-operator)|Supported|
528528
|Null-coalescing|[x ?? y](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operator)|Supported|
529-
|Conditional|[t ? x : y](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator)|Supported equivalent to the [if() function](#standard-functions)|
529+
|Conditional|[t ? x : y](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator)|Supported|
530530
|Lambda|[=>](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-operator)|Supported|
531531
532532
### Assignation operators
@@ -596,7 +596,7 @@ Currently the following script keywords are supported
596596
|Selection|[switch case](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch)|Not yet supported|
597597
|Iteration|[do ... while](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/do)|Supported|
598598
|Iteration|[for](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/for)|Supported|
599-
|Iteration|[foreach, in](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in)|Not yet supported|
599+
|Iteration|[foreach, in](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in)|Supported|
600600
|Iteration|[while](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/while)|Supported|
601601
|Jump|[break](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/break)|Supported in do, for and while blocks|
602602
|Jump|[continue](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/continue)|Supported in do, for and while blocks|

0 commit comments

Comments
 (0)