Skip to content

Commit 348813b

Browse files
committed
Moved processStatements to a better location in cfg.ts
1 parent 1dfa922 commit 348813b

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

src/control-flow/cfg.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,38 @@ export class CFGBuilder {
196196
return child ? child.text : "";
197197
}
198198

199+
private processStatements(statements: Node[]): BasicBlock {
200+
const blockHandler = new BlockHandler();
201+
202+
// Ignore comments
203+
const codeStatements = statements.filter((syntax) => {
204+
if (syntax.type !== "comment") {
205+
return true;
206+
}
207+
208+
return (
209+
this.markerPattern && Boolean(syntax.text.match(this.markerPattern))
210+
);
211+
});
212+
213+
if (codeStatements.length === 0) {
214+
const emptyNode = this.addNode("EMPTY", "empty block");
215+
return { entry: emptyNode, exit: emptyNode };
216+
}
217+
218+
let entry: string | null = null;
219+
let previous: string | null = null;
220+
for (const statement of codeStatements) {
221+
const { entry: currentEntry, exit: currentExit } = blockHandler.update(
222+
this.processBlock(statement),
223+
);
224+
if (!entry) entry = currentEntry;
225+
if (previous && currentEntry) this.addEdge(previous, currentEntry);
226+
previous = currentExit;
227+
}
228+
return blockHandler.update({ entry, exit: previous });
229+
}
230+
199231
private processBlock(node: Node | null): BasicBlock {
200232
if (!node) return { entry: null, exit: null };
201233

@@ -391,37 +423,7 @@ export class CFGBuilder {
391423
return { entry: breakNode, exit: null, breaks: [breakNode] };
392424
}
393425

394-
private processStatements(statements: Node[]): BasicBlock {
395-
const blockHandler = new BlockHandler();
396-
397-
// Ignore comments
398-
const codeStatements = statements.filter((syntax) => {
399-
if (syntax.type !== "comment") {
400-
return true;
401-
}
402-
403-
return (
404-
this.markerPattern && Boolean(syntax.text.match(this.markerPattern))
405-
);
406-
});
407426

408-
if (codeStatements.length === 0) {
409-
const emptyNode = this.addNode("EMPTY", "empty block");
410-
return { entry: emptyNode, exit: emptyNode };
411-
}
412-
413-
let entry: string | null = null;
414-
let previous: string | null = null;
415-
for (const statement of codeStatements) {
416-
const { entry: currentEntry, exit: currentExit } = blockHandler.update(
417-
this.processBlock(statement),
418-
);
419-
if (!entry) entry = currentEntry;
420-
if (previous && currentEntry) this.addEdge(previous, currentEntry);
421-
previous = currentExit;
422-
}
423-
return blockHandler.update({ entry, exit: previous });
424-
}
425427

426428
private processIfStatement(
427429
ifNode: Node,

0 commit comments

Comments
 (0)