[valueblocks] Add InstrId to SeqExperession value
A SequenceExpression currently doesn't store the InstructionId that produced its final `.value`. This PR adds that instruction id, which is then used in the next PR as we compose SequenceExpressions.
Joe Savona committed
Feb 3, 2023 at 14:16 UTC
cc33497c440d9b78372c90df72bfa4421f2da905
5 files changed
+22
-4
compiler/forget/src/HIR/HIR.ts
+1
@@ -109,6 +109,7 @@ export type ReactiveTernaryValue = {
109
export type ReactiveSequenceValue = {
110
kind: "SequenceExpression";
111
instructions: Array<ReactiveInstruction>;
112
+ id: InstructionId;
113
value: ReactiveValue;
114
loc: SourceLocation;
115
};
compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts
+6
-2
@@ -320,6 +320,7 @@ class Driver {
320
initValue = {
321
kind: "SequenceExpression",
322
instructions: [initBlock.instructions.at(-1)!],
323
+ id: terminal.id,
324
loc: terminal.loc,
325
value: {
326
kind: "Primitive",
@@ -518,6 +519,7 @@ class Driver {
519
const sequence: ReactiveSequenceValue = {
520
kind: "SequenceExpression",
521
instructions: instructions.slice(0, -1),
522
+ id: instr.id,
523
value: instr.value,
524
loc: loc,
525
};
@@ -569,7 +571,8 @@ class Driver {
571
})
572
);
573
}
572
- const place = leftInstructions.at(-1)!.lvalue!.place;
574
+ const lastInstruction = leftInstructions.at(-1)!;
575
+ const place = lastInstruction.lvalue!.place;
576
577
let left: ReactiveValue;
578
if (leftInstructions.length === 1) {
@@ -578,7 +581,8 @@ class Driver {
581
const sequence: ReactiveSequenceValue = {
582
kind: "SequenceExpression",
583
instructions: leftInstructions.slice(0, -1),
581
- value: leftInstructions.at(-1)!.value,
584
+ id: lastInstruction.id,
585
+ value: lastInstruction.value,
586
loc: terminal.loc,
587
};
588
left = sequence;
compiler/forget/src/ReactiveScopes/PrintReactiveFunction.ts
+1
-1
@@ -144,7 +144,7 @@ function printReactiveValue(writer: Writer, value: ReactiveValue): void {
144
instruction: instr,
145
})
146
);
147
- writer.write("");
147
+ writer.write(`[${value.id}] `);
148
printReactiveValue(writer, value.value);
149
});
150
});
compiler/forget/src/ReactiveScopes/visitors.ts
+1
-1
@@ -53,7 +53,7 @@ export class ReactiveFunctionVisitor<TState = void> {
53
for (const instr of value.instructions) {
54
this.visitInstruction(instr, state);
55
}
56
- this.visitValue(id, value.value, state);
56
+ this.visitValue(value.id, value.value, state);
57
break;
58
}
59
default: {
compiler/forget/src/__tests__/fixtures/hir/for-logical.js
new
+13
@@ -0,0 +1,13 @@
1
+// @skip
2
+function foo(props) {
3
+ let y = 0;
4
+ for (
5
+ let x = 0;
6
+ x > props.min && x < props.max;
7
+ x += props.cond ? props.increment : 2
8
+ ) {
9
+ x *= 2;
10
+ y += x;
11
+ }
12
+ return y;
13
+}