From 29a95d2dea298b7ba06bbbb55dd781b32d306364 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Fri, 3 Feb 2023 12:59:13 -0800 Subject: [PATCH] [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! --- .../forget/src/TypeInference/InferTypes.ts | 50 +++++++++---------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/compiler/forget/src/TypeInference/InferTypes.ts b/compiler/forget/src/TypeInference/InferTypes.ts index 9f949a5963..a7c496d23e 100644 --- a/compiler/forget/src/TypeInference/InferTypes.ts +++ b/compiler/forget/src/TypeInference/InferTypes.ts @@ -63,77 +63,73 @@ type TypeEquation = { right: Type; }; +function equation(left: Type, right: Type): TypeEquation { + return { + left, + right, + }; +} + function* generate( func: HIRFunction ): Generator { for (const [_, block] of func.body.blocks) { for (const phi of block.phis) { - yield { - left: phi.type, - right: { - kind: "Phi", - operands: [...phi.operands.values()].map((id) => id.type), - }, - }; + yield equation(phi.type, { + kind: "Phi", + operands: [...phi.operands.values()].map((id) => id.type), + }); } for (const instr of block.instructions) { - yield* generateTypeEquation(instr); + yield* generateInstructionTypes(instr); } } } -function generateTypeEquation(instr: Instruction): Array { - const equations: Array = []; - - function add(left: Type, right: Type) { - equations.push({ - left, - right, - }); - } - +function* generateInstructionTypes( + instr: Instruction +): Generator { const { lvalue, value } = instr; const left = lvalue.place.identifier.type; switch (value.kind) { case "JSXText": case "Primitive": { - add(left, { kind: "Primitive" }); + yield equation(left, { kind: "Primitive" }); break; } case "UnaryExpression": { - add(left, { kind: "Primitive" }); + yield equation(left, { kind: "Primitive" }); break; } case "Identifier": { - add(left, value.identifier.type); + yield equation(left, value.identifier.type); break; } case "BinaryExpression": { if (isPrimitiveBinaryOp(value.operator)) { - add(value.left.identifier.type, { kind: "Primitive" }); - add(value.right.identifier.type, { kind: "Primitive" }); + yield equation(value.left.identifier.type, { kind: "Primitive" }); + yield equation(value.right.identifier.type, { kind: "Primitive" }); } - add(left, { kind: "Primitive" }); + yield equation(left, { kind: "Primitive" }); break; } case "CallExpression": { - add(value.callee.identifier.type, { kind: "Function" }); + yield equation(value.callee.identifier.type, { kind: "Function" }); break; } case "ObjectExpression": { invariant(left !== null, "invald object expression"); - add(left, { kind: "Object" }); + yield equation(left, { kind: "Object" }); break; } } - return equations; } type Substitution = Map;