Skip to content

Commit

Permalink
Learned to handled labeled statements
Browse files Browse the repository at this point in the history
Previously we treated labels as placeholders. But they actually hold the next statement inside them!
  • Loading branch information
tmr232 committed Sep 5, 2024
1 parent ed7cfa5 commit 7aad64e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/control-flow/cfg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,13 @@ export class CFGBuilder {
return { entry: gotoNode, exit: null, gotos: [{ node: gotoNode, label: name }] }
}
private processLabeledStatement(labelSyntax: Parser.SyntaxNode): BasicBlock {
let blockHandler = new BlockHandler();
let name = this.getChildFieldText(labelSyntax, "label");
let labelNode = this.addNode("LABEL", name);
return { entry: labelNode, exit: labelNode, labels: new Map([[name, labelNode]]) }
console.log("label", labelSyntax.namedChildCount);
const { entry: labeledEntry, exit: labeledExit } = blockHandler.update(this.processBlock(labelSyntax.namedChildren[1]))
if (labeledEntry) this.addEdge(labelNode, labeledEntry)
return blockHandler.update({ entry: labelNode, exit: labeledExit, labels: new Map([[name, labelNode]]) });
}
private processContinueStatement(continueSyntax: Parser.SyntaxNode): BasicBlock {
const continueNode = this.addNode("CONTINUE", "CONTINUE");
Expand Down
17 changes: 17 additions & 0 deletions src/frontend/src/assets/samples/anotherLabelTestCase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
func anotherLabelTest() {
switch x {
case 1:
case 2:
goto label
case 3:
case 4:
}
return

label:
if x {
f()
} else {
g()
}
}
5 changes: 5 additions & 0 deletions src/frontend/src/assets/samples/labeledStatement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
func labeledStatement() {
label:
if x {
}
}

0 comments on commit 7aad64e

Please sign in to comment.