From 836211a549e4928645eac0fb4261509f8947dff3 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Tue, 20 Dec 2022 16:57:13 -0800 Subject: [PATCH] Prep for destructuring, factor out lowerIdentifier() --- compiler/forget/src/HIR/BuildHIR.ts | 48 +++++++++++++++++------------ 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/compiler/forget/src/HIR/BuildHIR.ts b/compiler/forget/src/HIR/BuildHIR.ts index 6d79b759d4..7af5f6717f 100644 --- a/compiler/forget/src/HIR/BuildHIR.ts +++ b/compiler/forget/src/HIR/BuildHIR.ts @@ -723,7 +723,7 @@ function lowerExpression( switch (exprNode.type) { case "Identifier": { const expr = exprPath as NodePath; - return lowerLVal(builder, expr); + return lowerIdentifier(builder, expr); } case "NullLiteral": { return { @@ -1285,30 +1285,38 @@ function lowerExpressionToVoid( }); } +function lowerIdentifier( + builder: HIRBuilder, + exprPath: NodePath +): Place { + const exprNode = exprPath.node; + const exprLoc = exprNode.loc ?? GeneratedSource; + const binding = + exprPath.scope.getBindingIdentifier(exprNode.name) ?? + getOrAddGlobal(exprNode.name); + invariant( + binding != null, + `Expected to find a binding for variable '%s'`, + exprNode.name + ); + const identifier = builder.resolveIdentifier(binding); + const place: Place = { + kind: "Identifier", + identifier: identifier, + memberPath: null, + effect: Effect.Unknown, + loc: exprLoc, + }; + return place; +} + function lowerLVal(builder: HIRBuilder, exprPath: NodePath): Place { const exprNode = exprPath.node; const exprLoc = exprNode.loc ?? GeneratedSource; switch (exprNode.type) { case "Identifier": { - // const expr = exprPath as NodePath; - // const name: string = expr.get("name"); - const binding = - exprPath.scope.getBindingIdentifier(exprNode.name) ?? - getOrAddGlobal(exprNode.name); - invariant( - binding != null, - `Expected to find a binding for variable '%s'`, - exprNode.name - ); - const identifier = builder.resolveIdentifier(binding); - const place: Place = { - kind: "Identifier", - identifier: identifier, - memberPath: null, - effect: Effect.Unknown, - loc: exprLoc, - }; - return place; + const expr = exprPath as NodePath; + return lowerIdentifier(builder, expr); } case "MemberExpression": { const expr = exprPath as NodePath;