@samitouri / QOS-React-2 / commits / 7a29405d68

[be] Rename some types for ReactiveFunction

Joe Savona committed Dec 16, 2022 at 08:45 UTC 7a29405d68bceba680c812f33074201516ab2668
7 files changed +65 -65
compiler/forget/src/HIR/BuildReactiveFunction.ts
+27 -27
@@ -14,11 +14,11 @@ import {
14 InstructionId,
15 InstructionValue,
16 Place,
17 - ReactiveBasicBlock,
17 ReactiveBlock,
18 ReactiveFunction,
20 - ReactiveInstruction,
19 ReactiveScope,
20 + ReactiveScopeBlock,
21 + ReactiveStatement,
22 ReactiveTerminal,
23 ReactiveValueBlock,
24 ScopeId,
@@ -41,23 +41,23 @@ export function buildReactiveFunction(fn: HIRFunction): ReactiveFunction {
41 }
42
43 type BlockKind =
44 - | { kind: "block"; block: ReactiveBasicBlock }
45 - | { kind: "scope"; block: ReactiveBasicBlock; scope: ReactiveScope };
44 + | { kind: "block"; block: ReactiveBlock }
45 + | { kind: "scope"; block: ReactiveBlock; scope: ReactiveScope };
46
47 class Builder {
48 - #instructions: ReactiveBasicBlock;
48 + #instructions: ReactiveBlock;
49 #stack: Array<
50 - | { kind: "scope"; block: ReactiveBlock }
51 - | { kind: "block"; block: ReactiveBasicBlock }
50 + | { kind: "scope"; block: ReactiveScopeBlock }
51 + | { kind: "block"; block: ReactiveBlock }
52 >;
53
54 constructor() {
55 - const block: ReactiveBasicBlock = [];
55 + const block: ReactiveBlock = [];
56 this.#instructions = block;
57 this.#stack = [{ kind: "block", block }];
58 }
59
60 - append(item: ReactiveInstruction, label: BlockId | undefined): void {
60 + append(item: ReactiveStatement, label: BlockId | undefined): void {
61 if (label !== undefined) {
62 invariant(item.kind === "terminal", "Only terminals may have a label");
63 item.label = label;
@@ -66,8 +66,8 @@ class Builder {
66 }
67
68 startScope(scope: ReactiveScope): void {
69 - const block: ReactiveBlock = {
70 - kind: "block",
69 + const block: ReactiveScopeBlock = {
70 + kind: "scope",
71 scope,
72 instructions: [],
73 };
@@ -92,7 +92,7 @@ class Builder {
92 }
93 }
94
95 - complete(): ReactiveBasicBlock {
95 + complete(): ReactiveBlock {
96 // TODO @josephsavona: debug two failures of this
97 // invariant(
98 // this.#stack.length === 1,
@@ -111,12 +111,12 @@ class ReactiveFunctionBuilder
111 implements
112 Visitor<
113 Builder,
114 - ReactiveBasicBlock,
114 + ReactiveBlock,
115 ReactiveValueBlock,
116 ReactiveValueBlock,
117 InstructionValue | ReactiveValueBlock,
118 - ReactiveInstruction,
119 - { test: InstructionValue | null; block: ReactiveBasicBlock }
118 + ReactiveStatement,
119 + { test: InstructionValue | null; block: ReactiveBlock }
120 >
121 {
122 #builders: Array<Builder> = [];
@@ -134,12 +134,12 @@ class ReactiveFunctionBuilder
134 }
135 appendBlock(
136 block: Builder,
137 - item: ReactiveInstruction,
137 + item: ReactiveStatement,
138 label?: BlockId | undefined
139 ): void {
140 block.append(item, label);
141 }
142 - leaveBlock(block: Builder): ReactiveBasicBlock {
142 + leaveBlock(block: Builder): ReactiveBlock {
143 const builder = this.#builders.pop();
144 invariant(
145 builder === block,
@@ -155,7 +155,7 @@ class ReactiveFunctionBuilder
155 value: null,
156 };
157 }
158 - appendValueBlock(block: ReactiveValueBlock, item: ReactiveInstruction): void {
158 + appendValueBlock(block: ReactiveValueBlock, item: ReactiveStatement): void {
159 block.instructions.push(item);
160 }
161 leaveValueBlock(
@@ -175,7 +175,7 @@ class ReactiveFunctionBuilder
175 enterInitBlock(block: Builder): ReactiveValueBlock {
176 return this.enterValueBlock(block);
177 }
178 - appendInitBlock(block: ReactiveValueBlock, item: ReactiveInstruction): void {
178 + appendInitBlock(block: ReactiveValueBlock, item: ReactiveStatement): void {
179 this.appendValueBlock(block, item);
180 }
181 leaveInitBlock(block: ReactiveValueBlock): ReactiveValueBlock {
@@ -191,7 +191,7 @@ class ReactiveFunctionBuilder
191 visitInstruction(
192 instruction: Instruction,
193 value: InstructionValue | ReactiveValueBlock
194 - ): ReactiveInstruction {
194 + ): ReactiveStatement {
195 this.visitId(instruction.id);
196 const scope = getInstructionScope(instruction);
197 if (scope !== null && !this.#scopes.has(scope.id)) {
@@ -204,17 +204,17 @@ class ReactiveFunctionBuilder
204 visitTerminalId(id: InstructionId): void {
205 this.visitId(id);
206 }
207 - visitImplicitTerminal(): ReactiveInstruction | null {
207 + visitImplicitTerminal(): ReactiveStatement | null {
208 return null;
209 }
210 visitTerminal(
211 terminal: BlockTerminal<
212 ReactiveValueBlock,
213 InstructionValue | ReactiveValueBlock,
214 - ReactiveBasicBlock,
215 - { test: InstructionValue | null; block: ReactiveBasicBlock }
214 + ReactiveBlock,
215 + { test: InstructionValue | null; block: ReactiveBlock }
216 >
217 - ): ReactiveInstruction {
217 + ): ReactiveStatement {
218 let result: ReactiveTerminal;
219 switch (terminal.kind) {
220 case "break": {
@@ -259,7 +259,7 @@ class ReactiveFunctionBuilder
259 test: terminal.test as Place,
260 cases: terminal.cases as Array<{
261 test: Place | null;
262 - block: ReactiveBasicBlock | void;
262 + block: ReactiveBlock | void;
263 }>,
264 };
265 break;
@@ -291,8 +291,8 @@ class ReactiveFunctionBuilder
291 }
292 visitCase(
293 test: InstructionValue | ReactiveValueBlock | null,
294 - block: ReactiveBasicBlock
295 - ): { test: InstructionValue | null; block: ReactiveBasicBlock } {
294 + block: ReactiveBlock
295 + ): { test: InstructionValue | null; block: ReactiveBlock } {
296 if (test !== null && test.kind !== "Identifier") {
297 invariant(false, "Expected a Place");
298 }
compiler/forget/src/HIR/CodegenReactiveFunction.ts
+4 -7
@@ -21,7 +21,7 @@ import {
21 Identifier,
22 Instruction,
23 InstructionKind,
24 - ReactiveBasicBlock,
24 + ReactiveBlock,
25 ReactiveFunction,
26 ReactiveScope,
27 ReactiveTerminal,
@@ -84,10 +84,7 @@ class Context {
84 }
85 }
86
87 -function codegenBlock(
88 - cx: Context,
89 - block: ReactiveBasicBlock
90 -): t.BlockStatement {
87 +function codegenBlock(cx: Context, block: ReactiveBlock): t.BlockStatement {
88 const statements: Array<t.Statement> = [];
89 for (const item of block) {
90 switch (item.kind) {
@@ -102,7 +99,7 @@ function codegenBlock(
99 }
100 break;
101 }
105 - case "block": {
102 + case "scope": {
103 codegenReactiveScope(cx, statements, item.scope, item.instructions);
104 break;
105 }
@@ -132,7 +129,7 @@ function codegenReactiveScope(
129 cx: Context,
130 statements: Array<t.Statement>,
131 scope: ReactiveScope,
135 - block: ReactiveBasicBlock
132 + block: ReactiveBlock
133 ): void {
134 const cacheStoreStatements: Array<t.Statement> = [];
135 const cacheLoadStatements: Array<t.Statement> = [];
compiler/forget/src/HIR/FlattenReactiveLoops.ts
+5 -5
@@ -6,7 +6,7 @@
6 */
7
8 import { assertExhaustive } from "../Common/utils";
9 -import { ReactiveBasicBlock, ReactiveBlock, ReactiveFunction } from "./HIR";
9 +import { ReactiveBlock, ReactiveFunction, ReactiveScopeBlock } from "./HIR";
10
11 /**
12 * Given a reactive function, flattens any scopes contained within a loop construct.
@@ -16,12 +16,12 @@ export function flattenReactiveLoops(fn: ReactiveFunction): void {
16 visit(fn.body, false);
17 }
18
19 -function visit(block: ReactiveBasicBlock, shouldFlatten: boolean): void {
19 +function visit(block: ReactiveBlock, shouldFlatten: boolean): void {
20 let i = 0;
21 while (i < block.length) {
22 const item = block[i]!;
23 switch (item.kind) {
24 - case "block": {
24 + case "scope": {
25 if (shouldFlatten) {
26 const successors = block.splice(i + 1);
27 block.pop(); // remove the current element
@@ -87,10 +87,10 @@ function visit(block: ReactiveBasicBlock, shouldFlatten: boolean): void {
87 }
88 }
89
90 -function flatten(scope: ReactiveBlock, block: ReactiveBasicBlock): void {
90 +function flatten(scope: ReactiveScopeBlock, block: ReactiveBlock): void {
91 for (const item of scope.instructions) {
92 switch (item.kind) {
93 - case "block": {
93 + case "scope": {
94 flatten(item, block);
95 break;
96 }
compiler/forget/src/HIR/HIR.ts
+14 -14
@@ -14,7 +14,7 @@ import { invariant } from "../CompilerError";
14 // *******************************************************************************************
15 // *******************************************************************************************
16
17 -// AST -> (lowering) -> HIR -> (analysis) -> Reactive Scopes -> (scheduling?) -> HIR -> (codegen) -> AST
17 +// AST -> (lowering) -> HIR -> (analysis) -> Reactive Scopes -> (codegen) -> AST
18
19 /**
20 * A location in a source file, intended to be used for providing diagnostic information and
@@ -47,27 +47,27 @@ export type ReactiveFunction = {
47 params: Array<Place>;
48 generator: boolean;
49 async: boolean;
50 - body: ReactiveBasicBlock;
50 + body: ReactiveBlock;
51 };
52
53 -export type ReactiveBlock = {
54 - kind: "block";
53 +export type ReactiveScopeBlock = {
54 + kind: "scope";
55 scope: ReactiveScope;
56 - instructions: ReactiveBasicBlock;
56 + instructions: ReactiveBlock;
57 };
58
59 -export type ReactiveBasicBlock = Array<ReactiveInstruction>;
59 +export type ReactiveBlock = Array<ReactiveStatement>;
60
61 export type ReactiveValueBlock = {
62 kind: "value-block";
63 - instructions: ReactiveBasicBlock;
63 + instructions: ReactiveBlock;
64 value: InstructionValue | null;
65 };
66
67 -export type ReactiveInstruction =
67 +export type ReactiveStatement =
68 | { kind: "instruction"; instruction: Instruction }
69 | { kind: "terminal"; terminal: ReactiveTerminal; label: BlockId | null }
70 - | ReactiveBlock;
70 + | ReactiveScopeBlock;
71
72 export type ReactiveTerminal =
73 | { kind: "break"; label: BlockId | null }
@@ -79,22 +79,22 @@ export type ReactiveTerminal =
79 test: Place;
80 cases: Array<{
81 test: Place | null;
82 - block: ReactiveBasicBlock | void;
82 + block: ReactiveBlock | void;
83 }>;
84 }
85 - | { kind: "while"; test: ReactiveValueBlock; loop: ReactiveBasicBlock }
85 + | { kind: "while"; test: ReactiveValueBlock; loop: ReactiveBlock }
86 | {
87 kind: "for";
88 init: ReactiveValueBlock;
89 test: ReactiveValueBlock;
90 update: ReactiveValueBlock;
91 - loop: ReactiveBasicBlock;
91 + loop: ReactiveBlock;
92 }
93 | {
94 kind: "if";
95 test: Place;
96 - consequent: ReactiveBasicBlock;
97 - alternate: ReactiveBasicBlock | null;
96 + consequent: ReactiveBlock;
97 + alternate: ReactiveBlock | null;
98 };
99
100 /**
compiler/forget/src/HIR/PrintReactiveFunction.ts
+9 -6
@@ -8,9 +8,9 @@
8 import invariant from "invariant";
9 import { assertExhaustive } from "../Common/utils";
10 import {
11 - ReactiveBlock,
11 ReactiveFunction,
13 - ReactiveInstruction,
12 + ReactiveScopeBlock,
13 + ReactiveStatement,
14 ReactiveTerminal,
15 ReactiveValueBlock,
16 } from "./HIR";
@@ -35,7 +35,10 @@ export function printReactiveFunction(fn: ReactiveFunction): string {
35 return writer.complete();
36 }
37
38 -export function printReactiveBlock(writer: Writer, block: ReactiveBlock): void {
38 +export function printReactiveBlock(
39 + writer: Writer,
40 + block: ReactiveScopeBlock
41 +): void {
42 writer.writeLine(
43 `scope @${block.scope.id} [${block.scope.range.start}:${
44 block.scope.range.end
@@ -51,7 +54,7 @@ export function printReactiveBlock(writer: Writer, block: ReactiveBlock): void {
54
55 export function printReactiveInstructions(
56 writer: Writer,
54 - instructions: Array<ReactiveInstruction>
57 + instructions: Array<ReactiveStatement>
58 ): void {
59 writer.indented(() => {
60 for (const instr of instructions) {
@@ -62,14 +65,14 @@ export function printReactiveInstructions(
65
66 function printReactiveInstruction(
67 writer: Writer,
65 - instr: ReactiveInstruction
68 + instr: ReactiveStatement
69 ): void {
70 switch (instr.kind) {
71 case "instruction": {
72 writer.writeLine(printInstruction(instr.instruction));
73 break;
74 }
72 - case "block": {
75 + case "scope": {
76 printReactiveBlock(writer, instr);
77 break;
78 }
compiler/forget/src/HIR/PropagateScopeDependencies.ts
+3 -3
@@ -15,7 +15,7 @@ import {
15 makeInstructionId,
16 MutableRange,
17 Place,
18 - ReactiveBasicBlock,
18 + ReactiveBlock,
19 ReactiveFunction,
20 ReactiveValueBlock,
21 } from "./HIR";
@@ -50,14 +50,14 @@ enum DeclKind {
50 type DeclMap = Map<Identifier, { kind: DeclKind; id: InstructionId }>;
51
52 function visit(
53 - block: ReactiveBasicBlock,
53 + block: ReactiveBlock,
54 dependencies: Set<Place>,
55 declarations: DeclMap,
56 scopeRange: MutableRange | null
57 ): void {
58 for (const item of block) {
59 switch (item.kind) {
60 - case "block": {
60 + case "scope": {
61 const scopeDependencies: Set<Place> = new Set();
62 // TODO: it would be sufficient to use a single mapping of declarations
63 const scopeDeclarations: DeclMap = new Map(declarations);
compiler/forget/src/HIR/PruneUnusedLabels.ts
+3 -3
@@ -8,7 +8,7 @@
8 import { assertExhaustive } from "../Common/utils";
9 import {
10 BlockId,
11 - ReactiveBasicBlock,
11 + ReactiveBlock,
12 ReactiveFunction,
13 ReactiveTerminal,
14 } from "./HIR";
@@ -23,7 +23,7 @@ export function pruneUnusedLabels(fn: ReactiveFunction): void {
23
24 type Labels = Set<BlockId>;
25
26 -function visitBlock(labels: Labels, block: ReactiveBasicBlock): void {
26 +function visitBlock(labels: Labels, block: ReactiveBlock): void {
27 for (const item of block) {
28 if (item.kind === "terminal") {
29 // first visit the terminal's contents, which is the only place that can
@@ -33,7 +33,7 @@ function visitBlock(labels: Labels, block: ReactiveBasicBlock): void {
33 if (item.label !== null && !labels.has(item.label)) {
34 item.label = null;
35 }
36 - } else if (item.kind === "block") {
36 + } else if (item.kind === "scope") {
37 visitBlock(labels, item.instructions);
38 }
39 }