Skip to content

Commit 6aa6431

Browse files
committed
Add do while loops + tests
1 parent 7007c0f commit 6aa6431

File tree

8 files changed

+214
-16
lines changed

8 files changed

+214
-16
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@
116116
<ItemGroup>
117117
<None Include="Resources\Script0012.txt" />
118118
</ItemGroup>
119+
<ItemGroup>
120+
<None Include="Resources\Script0013.txt" />
121+
</ItemGroup>
122+
<ItemGroup>
123+
<None Include="Resources\Script0014.txt" />
124+
</ItemGroup>
125+
<ItemGroup>
126+
<None Include="Resources\Script0015.txt" />
127+
</ItemGroup>
119128
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
120129
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
121130
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorScriptEvaluateTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,53 @@ public static IEnumerable<TestCaseData> TestCasesForScriptEvaluateTests
584584
.SetCategory("++")
585585
.SetCategory("+=")
586586
.Returns("0,1,2,3,4");
587+
yield return new TestCaseData(Resources.Script0015, null, null, null)
588+
.SetCategory("Script")
589+
.SetCategory("do while")
590+
.SetCategory("variable assignation")
591+
.SetCategory("++")
592+
.SetCategory("+=")
593+
.Returns(string.Empty);
594+
yield return new TestCaseData(removeAllWhiteSpacesRegex.Replace(Resources.Script0015, string.Empty), null, null, null)
595+
.SetCategory("Script")
596+
.SetCategory("do while")
597+
.SetCategory("variable assignation")
598+
.SetCategory("++")
599+
.SetCategory("+=")
600+
.Returns(string.Empty);
601+
602+
#endregion
603+
604+
#region do while
605+
606+
yield return new TestCaseData(Resources.Script0013, null, null, null)
607+
.SetCategory("Script")
608+
.SetCategory("do while")
609+
.SetCategory("variable assignation")
610+
.SetCategory("++")
611+
.SetCategory("+=")
612+
.Returns("0,1,2,3,4");
613+
yield return new TestCaseData(removeAllWhiteSpacesRegex.Replace(Resources.Script0013, string.Empty), null, null, null)
614+
.SetCategory("Script")
615+
.SetCategory("do while")
616+
.SetCategory("variable assignation")
617+
.SetCategory("++")
618+
.SetCategory("+=")
619+
.Returns("0,1,2,3,4");
620+
yield return new TestCaseData(Resources.Script0014, null, null, null)
621+
.SetCategory("Script")
622+
.SetCategory("do while")
623+
.SetCategory("variable assignation")
624+
.SetCategory("++")
625+
.SetCategory("+=")
626+
.Returns("0");
627+
yield return new TestCaseData(removeAllWhiteSpacesRegex.Replace(Resources.Script0014, string.Empty), null, null, null)
628+
.SetCategory("Script")
629+
.SetCategory("do while")
630+
.SetCategory("variable assignation")
631+
.SetCategory("++")
632+
.SetCategory("+=")
633+
.Returns("0");
587634

588635
#endregion
589636

CodingSeb.ExpressionEvaluator.Tests/Resources.Designer.cs

Lines changed: 66 additions & 13 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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,13 @@
154154
<data name="Script0012" type="System.Resources.ResXFileRef, System.Windows.Forms">
155155
<value>resources\script0012.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
156156
</data>
157+
<data name="Script0013" type="System.Resources.ResXFileRef, System.Windows.Forms">
158+
<value>resources\script0013.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
159+
</data>
160+
<data name="Script0014" type="System.Resources.ResXFileRef, System.Windows.Forms">
161+
<value>resources\script0014.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
162+
</data>
163+
<data name="Script0015" type="System.Resources.ResXFileRef, System.Windows.Forms">
164+
<value>resources\script0015.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
165+
</data>
157166
</root>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Script0013 */
2+
x = 0;
3+
result = "";
4+
5+
do
6+
{
7+
result += $"{x},";
8+
x++;
9+
}
10+
while(x < 5);
11+
12+
result.Remove(result.Length - 1);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Script0014 */
2+
x = 0;
3+
result = "";
4+
5+
do
6+
{
7+
result += $"{x},";
8+
x++;
9+
}
10+
while(false);
11+
12+
result.Remove(result.Length - 1);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Script0015 */
2+
x = 0;
3+
result = "";
4+
5+
while(false)
6+
{
7+
result += $"{x},";
8+
x++;
9+
}
10+
11+
12+
result;

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ public class ExpressionEvaluator
4545

4646
// For script only
4747
private static readonly Regex blockKeywordsBeginningRegex = new Regex(@"^\s*(?<keyword>while|for|if|else\s+if)\s*[(]", RegexOptions.IgnoreCase);
48-
private static readonly Regex elseblockKeywordsBeginningRegex = new Regex(@"^\s*(?<keyword>else)(?![a-zA-Z0-9_])", RegexOptions.IgnoreCase);
48+
private static readonly Regex blockKeywordsWithoutParenthesesBeginningRegex = new Regex(@"^\s*(?<keyword>else|do)(?![a-zA-Z0-9_])", RegexOptions.IgnoreCase);
4949
private static readonly Regex blockBeginningRegex = new Regex(@"^\s*[{]");
5050
private static readonly Regex returnKeywordRegex = new Regex(@"^return(\s+|\()", RegexOptions.IgnoreCase | RegexOptions.Singleline);
51+
private static readonly Regex nextIsEndOfExpressionRegex = new Regex(@"^\s*[;]");
5152

5253
#endregion
5354

@@ -789,12 +790,12 @@ void ExecuteIfList()
789790

790791
while(!isReturn && !isBreak && !isContinue && i < script.Length)
791792
{
792-
Match blockKeywordsBeginingMatch;
793+
Match blockKeywordsBeginingMatch = null;
793794
Match elseBlockKeywordsBeginingMatch = null;
794795

795796
if (script.Substring(startOfExpression, i - startOfExpression).Trim().Equals(string.Empty)
796797
&& ((blockKeywordsBeginingMatch = blockKeywordsBeginningRegex.Match(script.Substring(i))).Success
797-
|| (elseBlockKeywordsBeginingMatch = elseblockKeywordsBeginningRegex.Match(script.Substring(i))).Success))
798+
|| (elseBlockKeywordsBeginingMatch = blockKeywordsWithoutParenthesesBeginningRegex.Match(script.Substring(i))).Success))
798799
{
799800
i += blockKeywordsBeginingMatch.Success ? blockKeywordsBeginingMatch.Length : elseBlockKeywordsBeginingMatch.Length;
800801
string keyword = blockKeywordsBeginingMatch.Success ? blockKeywordsBeginingMatch.Groups["keyword"].Value.Replace(" ", "") : (elseBlockKeywordsBeginingMatch?.Groups["keyword"].Value ?? string.Empty);
@@ -876,6 +877,49 @@ void ExecuteIfList()
876877
ifElseStatementsList.Add(new List<string>() { keywordAttributes[0], subScript });
877878
ifBlockEvaluatedState = IfBlockEvaluatedState.If;
878879
}
880+
else if (keyword.Equals("do"))
881+
{
882+
if((blockKeywordsBeginingMatch = blockKeywordsBeginningRegex.Match(script.Substring(i))).Success
883+
&& blockKeywordsBeginingMatch.Groups["keyword"].Value.ManageCasing(OptionCaseSensitiveEvaluationActive).Equals("while"))
884+
{
885+
i += blockKeywordsBeginingMatch.Length;
886+
keywordAttributes = GetExpressionsBetweenParenthis(script, ref i, true, ";");
887+
888+
i++;
889+
890+
Match nextIsEndOfExpressionMatch = null;
891+
892+
if ((nextIsEndOfExpressionMatch = nextIsEndOfExpressionRegex.Match(script.Substring(i))).Success)
893+
{
894+
i += nextIsEndOfExpressionMatch.Length;
895+
896+
do
897+
{
898+
lastResult = ScriptEvaluate(subScript, ref isReturn, ref isBreak, ref isContinue);
899+
900+
if (isBreak)
901+
{
902+
isBreak = false;
903+
break;
904+
}
905+
if (isContinue)
906+
{
907+
isContinue = false;
908+
continue;
909+
}
910+
}
911+
while (!isReturn && (bool)ManageJumpStatementsOrExpressionEval(keywordAttributes[0]));
912+
}
913+
else
914+
{
915+
throw new ExpressionEvaluatorSyntaxErrorException("A [;] character is missing. (After the do while condition)");
916+
}
917+
}
918+
else
919+
{
920+
throw new ExpressionEvaluatorSyntaxErrorException("No [while] keyword afte the [do] keyword and block");
921+
}
922+
}
879923
else if (keyword.Equals("while"))
880924
{
881925
while (!isReturn && (bool)ManageJumpStatementsOrExpressionEval(keywordAttributes[0]))

0 commit comments

Comments
 (0)