[be] Use generator in InferTypes, extract helper
Just small things I noticed when looking at InferTypes. I was thinking about how we'd adjust this pass to account for hooks, i'll probably pause that for now but putting this up in case you like the changes. If not no big deal!
Joe Savona committed
Feb 3, 2023 at 12:59 UTC
29a95d2dea298b7ba06bbbb55dd781b32d306364
1 file changed
+23
-27
compiler/forget/src/TypeInference/InferTypes.ts
+23
-27
@@ -63,77 +63,73 @@ type TypeEquation = {
63
right: Type;
64
};
65
66
+function equation(left: Type, right: Type): TypeEquation {
67
+ return {
68
+ left,
69
+ right,
70
+ };
71
+}
72
+
73
function* generate(
74
func: HIRFunction
75
): Generator<TypeEquation, void, undefined> {
76
for (const [_, block] of func.body.blocks) {
77
for (const phi of block.phis) {
71
- yield {
72
- left: phi.type,
73
- right: {
74
- kind: "Phi",
75
- operands: [...phi.operands.values()].map((id) => id.type),
76
- },
77
- };
78
+ yield equation(phi.type, {
79
+ kind: "Phi",
80
+ operands: [...phi.operands.values()].map((id) => id.type),
81
+ });
82
}
83
84
for (const instr of block.instructions) {
81
- yield* generateTypeEquation(instr);
85
+ yield* generateInstructionTypes(instr);
86
}
87
}
88
}
89
86
-function generateTypeEquation(instr: Instruction): Array<TypeEquation> {
87
- const equations: Array<TypeEquation> = [];
88
-
89
- function add(left: Type, right: Type) {
90
- equations.push({
91
- left,
92
- right,
93
- });
94
- }
95
-
90
+function* generateInstructionTypes(
91
+ instr: Instruction
92
+): Generator<TypeEquation, void, undefined> {
93
const { lvalue, value } = instr;
94
const left = lvalue.place.identifier.type;
95
96
switch (value.kind) {
97
case "JSXText":
98
case "Primitive": {
102
- add(left, { kind: "Primitive" });
99
+ yield equation(left, { kind: "Primitive" });
100
break;
101
}
102
103
case "UnaryExpression": {
107
- add(left, { kind: "Primitive" });
104
+ yield equation(left, { kind: "Primitive" });
105
break;
106
}
107
108
case "Identifier": {
112
- add(left, value.identifier.type);
109
+ yield equation(left, value.identifier.type);
110
break;
111
}
112
113
case "BinaryExpression": {
114
if (isPrimitiveBinaryOp(value.operator)) {
118
- add(value.left.identifier.type, { kind: "Primitive" });
119
- add(value.right.identifier.type, { kind: "Primitive" });
115
+ yield equation(value.left.identifier.type, { kind: "Primitive" });
116
+ yield equation(value.right.identifier.type, { kind: "Primitive" });
117
}
121
- add(left, { kind: "Primitive" });
118
+ yield equation(left, { kind: "Primitive" });
119
break;
120
}
121
122
case "CallExpression": {
126
- add(value.callee.identifier.type, { kind: "Function" });
123
+ yield equation(value.callee.identifier.type, { kind: "Function" });
124
break;
125
}
126
127
case "ObjectExpression": {
128
invariant(left !== null, "invald object expression");
132
- add(left, { kind: "Object" });
129
+ yield equation(left, { kind: "Object" });
130
break;
131
}
132
}
136
- return equations;
133
}
134
135
type Substitution = Map<TypeId, Type>;