Skip to content

Commit

Permalink
Fix bug in 'if' evaluation where else blocks wouldn't be skipped if t…
Browse files Browse the repository at this point in the history
…he first 'if' condition was true
  • Loading branch information
ethanmoffat committed Feb 7, 2022
1 parent 3f3ace5 commit 6bbc99c
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions EOBot/Interpreter/States/IfEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ public override async Task<(EvalResult, string, BotToken)> EvaluateAsync(Program
if (result == EvalResult.Ok)
{
if (bool.TryParse(token.TokenValue, out var conditionValue) && conditionValue)
return await EvaluateBlockAsync(input);
{
var ifRes = await EvaluateBlockAsync(input);
if (ifRes.Item1 == EvalResult.Ok)
SkipElseBlocks(input);

return ifRes;
}

SkipBlock(input);

Expand All @@ -37,17 +43,7 @@ public override async Task<(EvalResult, string, BotToken)> EvaluateAsync(Program
return elseIfRes;
else if (elseIfRes.Result == EvalResult.Ok)
{
while (input.Expect(BotTokenType.NewLine)) ;

// skip the rest of the following blocks if evaluated
while (IsElse(input))
{
input.Expect(BotTokenType.Keyword);
SkipBlock(input);
while (input.Expect(BotTokenType.NewLine)) ;
}

RestoreLastNewline(input);
SkipElseBlocks(input);
return elseIfRes;
}

Expand All @@ -65,5 +61,20 @@ private bool IsElse(ProgramState input)
var current = input.Current();
return current.TokenType == BotTokenType.Keyword && current.TokenValue == "else";
}

private void SkipElseBlocks(ProgramState input)
{
while (input.Expect(BotTokenType.NewLine)) ;

// skip the rest of the following blocks if evaluated
while (IsElse(input))
{
input.Expect(BotTokenType.Keyword);
SkipBlock(input);
while (input.Expect(BotTokenType.NewLine)) ;
}

RestoreLastNewline(input);
}
}
}

0 comments on commit 6bbc99c

Please sign in to comment.