@samitouri / QOS-React-2 / commits / 8c920cfa90

Factor out assignment expression cases

Splits handling of simple assignment updates (`=`) from update-assignments (`+=` etc). This unblocks starting to support destructuring for the simple case in the subsequent PR.

Joe Savona committed Dec 21, 2022 at 10:02 UTC 8c920cfa9049d00f3c1400132f4ca10ba903914a
1 file changed +13 -10
compiler/forget/src/HIR/BuildHIR.ts
+13 -10
@@ -943,21 +943,22 @@ function lowerExpression(
943 }
944 case "AssignmentExpression": {
945 const expr = exprPath as NodePath<t.AssignmentExpression>;
946 - const left = lowerLVal(builder, expr.get("left"));
946 const operator = expr.node.operator;
947
948 if (operator === "=") {
949 + const left = expr.get("left");
950 + // const left = lowerLVal(builder, expr.get("left"));
951 const right =
951 - left.memberPath === null
952 + left.node.type === "Identifier"
953 ? lowerExpression(builder, expr.get("right"))
954 : lowerExpressionToPlace(builder, expr.get("right"));
954 - builder.push({
955 - id: makeInstructionId(0),
956 - lvalue: { place: left, kind: InstructionKind.Reassign },
957 - value: right,
958 - loc: exprLoc,
959 - });
960 - return left;
955 + return lowerAssignment(
956 + builder,
957 + expr.node.loc ?? GeneratedSource,
958 + InstructionKind.Reassign,
959 + left,
960 + right
961 + );
962 }
963
964 const operators: { [key: string]: t.BinaryExpression["operator"] } = {
@@ -980,6 +981,7 @@ function lowerExpression(
981 `Unhandled assignment operator '${operator}'`
982 );
983
984 + const left = lowerLVal(builder, expr.get("left"));
985 const right = lowerExpressionToPlace(builder, expr.get("right"));
986 builder.push({
987 id: makeInstructionId(0),
@@ -1351,7 +1353,7 @@ function lowerAssignment(
1353 kind: InstructionKind,
1354 lvalue: NodePath<t.LVal>,
1355 value: InstructionValue
1354 -): void {
1356 +): InstructionValue {
1357 const id = lowerLVal(builder, lvalue);
1358 builder.push({
1359 id: makeInstructionId(0),
@@ -1359,4 +1361,5 @@ function lowerAssignment(
1361 value,
1362 loc,
1363 });
1364 + return id;
1365 }