eliminate unreachable code

This commit is contained in:
nub31
2025-05-05 22:29:50 +02:00
parent df6e926bae
commit c3502fe68a
2 changed files with 13 additions and 3 deletions

View File

@@ -1,7 +1,12 @@
import "c";
global func main() {
while true {
continue;
let x = true;
while x {
if (true) {
puts("level 1");
}
puts("level 2");
break;
}
}

View File

@@ -12,6 +12,7 @@ public class Generator
private readonly List<string> _strings = [];
private readonly Stack<string> _breakLabels = new();
private readonly Stack<string> _continueLabels = new();
private bool _codeIsReachable = true;
public Generator(List<DefinitionNode> definitions)
{
@@ -80,10 +81,12 @@ public class Generator
private void GenerateBlock(BlockNode block)
{
foreach (var statement in block.Statements)
foreach (var statement in block.Statements.Where(_ => _codeIsReachable))
{
GenerateStatement(statement);
}
_codeIsReachable = true;
}
private void GenerateStatement(StatementNode statement)
@@ -122,11 +125,13 @@ public class Generator
private void GenerateBreak()
{
_builder.AppendLine($" jmp @{_breakLabels.Peek()}");
_codeIsReachable = false;
}
private void GenerateContinue()
{
_builder.AppendLine($" jmp @{_continueLabels.Peek()}");
_codeIsReachable = false;
}
private void GenerateStatementFuncCall(FuncCallStatementNode funcCall)