From 9be22dea3a5271036ddf4fa772dc8e64bc0ae82b Mon Sep 17 00:00:00 2001 From: Michael Schimmel Date: Fri, 19 Sep 2025 09:23:43 +0200 Subject: [PATCH] Refactoring --- Src/AST/Myc.Ast.Binding.pas | 45 +++++-------------------------------- 1 file changed, 5 insertions(+), 40 deletions(-) diff --git a/Src/AST/Myc.Ast.Binding.pas b/Src/AST/Myc.Ast.Binding.pas index da6aad0..e845b03 100644 --- a/Src/AST/Myc.Ast.Binding.pas +++ b/Src/AST/Myc.Ast.Binding.pas @@ -245,51 +245,16 @@ begin (Node as TIdentifierNode).Address := TResolvedAddress.Create(akUpvalue, 0, upvalueIndex); end else - (Node as TIdentifierNode).Address := TResolvedAddress.Create(akLocalOrParent, depth, idx); - end - else - raise Exception.CreateFmt('Undefined identifier: "%s"', [identNode.Name]); -end; - -(* -function TAstBinder.VisitIdentifier(const Node: IIdentifierNode): TDataValue; -var - depth, idx: Integer; - identNode: TIdentifierNode; - upvalue: TUpvalueMapping; - originalAddress: TResolvedAddress; - upvalueIndex: Integer; -begin - identNode := Node as TIdentifierNode; - if identNode.Address.Kind <> akUnresolved then - Exit; - - if FCurrentDescriptor.FindSymbol(identNode.Name, depth, idx) then - begin - if (depth > 0) and (FUpvalueStack.Count > 0) then begin - upvalue := FUpvalueStack.Peek; - - // Address is relative to the lambda's parent scope. - dec(depth); - originalAddress.Create(akLocalOrParent, depth, idx); - - if not upvalue.Map.TryGetValue(originalAddress, upvalueIndex) then - begin - upvalueIndex := upvalue.Nodes.Count; - upvalue.Map.Add(originalAddress, upvalueIndex); - upvalue.Nodes.Add(identNode); - end; - - (Node as TIdentifierNode).Address.Create(akUpvalue, 0, upvalueIndex); - end - else - (Node as TIdentifierNode).Address.Create(akLocalOrParent, depth, idx); + // 1. case: depth=0 - this is a local var + // 2. case: UpvalueStack is empty - there is no surrounding lambda, we need to reference (and capture) the whole parent scope + (Node as TIdentifierNode).Address := TResolvedAddress.Create(akLocalOrParent, depth, idx); + end; end else raise Exception.CreateFmt('Undefined identifier: "%s"', [identNode.Name]); end; -*) + function TAstBinder.VisitIfExpression(const Node: IIfExpressionNode): TDataValue; begin // The condition is never in a tail position.