eliminate unreachable code
This commit is contained in:
@@ -1,7 +1,12 @@
|
|||||||
import "c";
|
import "c";
|
||||||
|
|
||||||
global func main() {
|
global func main() {
|
||||||
while true {
|
let x = true;
|
||||||
continue;
|
while x {
|
||||||
|
if (true) {
|
||||||
|
puts("level 1");
|
||||||
|
}
|
||||||
|
puts("level 2");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ public class Generator
|
|||||||
private readonly List<string> _strings = [];
|
private readonly List<string> _strings = [];
|
||||||
private readonly Stack<string> _breakLabels = new();
|
private readonly Stack<string> _breakLabels = new();
|
||||||
private readonly Stack<string> _continueLabels = new();
|
private readonly Stack<string> _continueLabels = new();
|
||||||
|
private bool _codeIsReachable = true;
|
||||||
|
|
||||||
public Generator(List<DefinitionNode> definitions)
|
public Generator(List<DefinitionNode> definitions)
|
||||||
{
|
{
|
||||||
@@ -80,10 +81,12 @@ public class Generator
|
|||||||
|
|
||||||
private void GenerateBlock(BlockNode block)
|
private void GenerateBlock(BlockNode block)
|
||||||
{
|
{
|
||||||
foreach (var statement in block.Statements)
|
foreach (var statement in block.Statements.Where(_ => _codeIsReachable))
|
||||||
{
|
{
|
||||||
GenerateStatement(statement);
|
GenerateStatement(statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_codeIsReachable = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GenerateStatement(StatementNode statement)
|
private void GenerateStatement(StatementNode statement)
|
||||||
@@ -122,11 +125,13 @@ public class Generator
|
|||||||
private void GenerateBreak()
|
private void GenerateBreak()
|
||||||
{
|
{
|
||||||
_builder.AppendLine($" jmp @{_breakLabels.Peek()}");
|
_builder.AppendLine($" jmp @{_breakLabels.Peek()}");
|
||||||
|
_codeIsReachable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GenerateContinue()
|
private void GenerateContinue()
|
||||||
{
|
{
|
||||||
_builder.AppendLine($" jmp @{_continueLabels.Peek()}");
|
_builder.AppendLine($" jmp @{_continueLabels.Peek()}");
|
||||||
|
_codeIsReachable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GenerateStatementFuncCall(FuncCallStatementNode funcCall)
|
private void GenerateStatementFuncCall(FuncCallStatementNode funcCall)
|
||||||
|
|||||||
Reference in New Issue
Block a user