Skip to content

Commit

Permalink
Corrected break detection in while/until block. (ZeraGmbH#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
JMS-1 authored Oct 11, 2024
1 parent 56a29c2 commit 4e3022a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
17 changes: 6 additions & 11 deletions Library/Core/Blocks/Controls/ControlsRepeatExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,14 @@ public class ControlsRepeatExt : Block

await statement.EvaluateAsync(context);

try
{
/* See if a break of the loop is requested. */
if (context.EscapeMode == EscapeMode.Break) break;
}
finally
{
/* Either a continue is requested or the inner block is executed normally. */
context.EscapeMode = EscapeMode.None;
}
/* See if a break of the loop is requested. */
if (context.EscapeMode == EscapeMode.Break) break;

/* Either a continue is requested or the inner block is executed normally. */
context.EscapeMode = EscapeMode.None;
}

/* For safety reasons reset the break mode - should not really be neccessary! */
/* Reset the break mode - just in case. */
context.EscapeMode = EscapeMode.None;

/* Continue with the next block. */
Expand Down
53 changes: 29 additions & 24 deletions Library/Core/Blocks/Controls/ControlsWhileUntil.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


using BlocklyNet.Core.Model;

namespace BlocklyNet.Core.Blocks.Controls;
Expand All @@ -12,36 +10,43 @@ public class ControlsWhileUntil : Block
/// <inheritdoc/>
public override async Task<object?> EvaluateAsync(Context context)
{
var mode = Fields["MODE"];
var value = Values.TryGet("BOOL");
var statement = Statements.TryGet("DO");

if (!Statements.Has("DO") || null == value)
if (statement == null || null == value)
return await base.EvaluateAsync(context);

var statement = Statements["DO"];
var execStatementAndTestForBreak = async () =>
{
context.Cancellation.ThrowIfCancellationRequested();
await statement.EvaluateAsync(context);
if (context.EscapeMode == EscapeMode.Break) return true;
context.EscapeMode = EscapeMode.None;
return false;
};

if (mode == "WHILE")
switch (Fields["MODE"])
{
while ((bool)(await value.EvaluateAsync(context))!)
{
context.Cancellation.ThrowIfCancellationRequested();

if (context.EscapeMode == EscapeMode.Break)
{
context.EscapeMode = EscapeMode.None;
break;
}

await statement.EvaluateAsync(context);
}
case "WHILE":
while ((bool)(await value.EvaluateAsync(context))!)
if (await execStatementAndTestForBreak())
break;

break;
case "UNTIL":
do
if (await execStatementAndTestForBreak())
break;
while (!(bool)(await value.EvaluateAsync(context))!);

break;
}
else
while (!(bool)(await value.EvaluateAsync(context))!)
{
context.Cancellation.ThrowIfCancellationRequested();

await statement.EvaluateAsync(context);
}
context.EscapeMode = EscapeMode.None;

return await base.EvaluateAsync(context);
}
Expand Down

0 comments on commit 4e3022a

Please sign in to comment.