Skip to content

Commit ccc2c52

Browse files
committed
generic-cfg-builder: Move the statement handlers into this file
They very well belong there, and it makes reading the code easier.
1 parent 1625730 commit ccc2c52

File tree

6 files changed

+39
-36
lines changed

6 files changed

+39
-36
lines changed

src/control-flow/cfg-c.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import {
55
type CFGBuilder,
66
} from "./cfg-defs";
77
import { Match } from "./block-matcher.ts";
8-
import type { Context, StatementHandlers } from "./statement-handlers.ts";
9-
import { GenericCFGBuilder } from "./generic-cfg-builder.ts";
8+
import {
9+
GenericCFGBuilder,
10+
type Context,
11+
type StatementHandlers,
12+
} from "./generic-cfg-builder.ts";
1013
import { pairwise, zip } from "./zip.ts";
1114
import { buildSwitch, collectCases } from "./switch-utils.ts";
1215

src/control-flow/cfg-go.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import {
44
type BuilderOptions,
55
type CFGBuilder,
66
} from "./cfg-defs";
7-
import type { Context, StatementHandlers } from "./statement-handlers";
8-
import { GenericCFGBuilder } from "./generic-cfg-builder";
7+
import {
8+
GenericCFGBuilder,
9+
type Context,
10+
type StatementHandlers,
11+
} from "./generic-cfg-builder";
912
import { buildSwitch, collectCases, type SwitchOptions } from "./switch-utils";
1013

1114
function getChildFieldText(node: Parser.SyntaxNode, fieldName: string): string {

src/control-flow/cfg-python.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import {
44
type BuilderOptions,
55
type CFGBuilder,
66
} from "./cfg-defs";
7-
import type { Context, StatementHandlers } from "./statement-handlers.ts";
8-
import { GenericCFGBuilder } from "./generic-cfg-builder.ts";
7+
import {
8+
GenericCFGBuilder,
9+
type Context,
10+
type StatementHandlers,
11+
} from "./generic-cfg-builder.ts";
912
import { matchExistsIn } from "./block-matcher.ts";
1013
import { maybe, zip } from "./zip.ts";
1114

src/control-flow/generic-cfg-builder.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,33 @@ import {
66
type BuilderOptions,
77
type CFG,
88
} from "./cfg-defs";
9-
import type { StatementHandlers } from "./statement-handlers";
109
import { BlockMatcher } from "./block-matcher";
1110
import { NodeMapper } from "./node-mapper";
1211
import { pairwise } from "./zip";
1312

13+
interface Dispatch {
14+
single(syntax: Parser.SyntaxNode | null): BasicBlock;
15+
many(statements: Parser.SyntaxNode[]): BasicBlock;
16+
}
17+
interface Link {
18+
syntaxToNode: InstanceType<typeof NodeMapper>["linkSytaxToNode"];
19+
offsetToSyntax: InstanceType<typeof NodeMapper>["linkOffsetToSyntax"];
20+
}
21+
export interface Context {
22+
builder: Builder;
23+
options: BuilderOptions;
24+
matcher: BlockMatcher;
25+
dispatch: Dispatch;
26+
state: BlockHandler;
27+
link: Link;
28+
}
29+
30+
type StatementHandler = (syntax: Parser.SyntaxNode, ctx: Context) => BasicBlock;
31+
export type StatementHandlers = {
32+
named: { [key: string]: StatementHandler };
33+
default: StatementHandler;
34+
};
35+
1436
export class GenericCFGBuilder {
1537
private builder: Builder = new Builder();
1638
private readonly options: BuilderOptions;

src/control-flow/statement-handlers.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/control-flow/switch-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type Parser from "web-tree-sitter";
22
import type { Case, EdgeType } from "./cfg-defs";
3-
import type { Context } from "./statement-handlers";
43
import { pairwise } from "./zip";
4+
import type { Context } from "./generic-cfg-builder";
55
export interface SwitchOptions {
66
/// A Go `select` blocks until one of the branches matches.
77
/// This means that we never add an alternative edge from the

0 commit comments

Comments
 (0)