From 61b6a1742bc9be318a5f05e910bcf815516b38c3 Mon Sep 17 00:00:00 2001 From: Michael Schimmel Date: Fri, 21 Nov 2025 17:49:06 +0100 Subject: [PATCH] Fixed some glitches from last refactoring --- Src/AST/Myc.Ast.Compiler.Binder.pas | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/Src/AST/Myc.Ast.Compiler.Binder.pas b/Src/AST/Myc.Ast.Compiler.Binder.pas index 7aed155..0dd0748 100644 --- a/Src/AST/Myc.Ast.Compiler.Binder.pas +++ b/Src/AST/Myc.Ast.Compiler.Binder.pas @@ -34,7 +34,7 @@ type FFunctionRegistry: IFunctionDefinitionRegistry; FBoxedDeclarations: THashSet; - // Fix for TExecutionScope parent chain optimization + // Counts lambdas encountered to determine if a lambda has nested children FLambdaCounter: Integer; function IsValidIdentifier(const Name: string): Boolean; @@ -157,12 +157,17 @@ var begin if Name.IsEmpty then exit(False); - c := Name[1]; - if not (c.IsLetter or (c = '_')) then - exit(False); + + // Support standard identifiers and hygienic macro names (e.g. loop#1) for c in Name do - if not (c.IsLetterOrDigit or (c = '_') or (c = '-')) then + if not (c.IsLetterOrDigit or (c = '_') or (c = '-') or (c = '#')) then exit(False); + + // First char check (cannot be digit unless it's a pure number which is tokenized differently) + c := Name[1]; + if not (c.IsLetter or (c = '_') or (c = '#')) then + exit(False); + Result := True; end; @@ -172,6 +177,7 @@ var depth: Integer; slot: Integer; begin + // Start search in current builder layout := FCurrentBuilder; depth := 0; @@ -230,10 +236,12 @@ begin if physAddr.ScopeDepth > 0 then begin + // It's an upvalue (from a parent scope). Capture it. upvalueIndex := CaptureUpvalue(physAddr); Result := TAst.Identifier(Node.Name, TResolvedAddress.Create(akUpvalue, 0, upvalueIndex), TTypes.Unknown); end else + // It's local. Result := TAst.Identifier(Node.Name, physAddr, TTypes.Unknown); end else @@ -301,10 +309,11 @@ var startCount: Integer; hasNested: Boolean; begin - // Count logic: Measure how many lambdas are visited inside this one. + // 1. Count logic to determine if this lambda has nested lambdas startCount := FLambdaCounter; - Inc(FLambdaCounter); // Count self + Inc(FLambdaCounter); // Count myself + // 2. Push Scope parentBuilder := FCurrentBuilder; FCurrentBuilder := TScope.CreateBuilder(parentBuilder); @@ -355,7 +364,8 @@ begin FUpvalueStack.Pop; end; - // Calculate HasNested: If counter increased by more than 1 (self), we have children. + // 3. Calculate hasNested + // If FLambdaCounter increased by more than just 1 (myself), children were visited. hasNested := FLambdaCounter > (startCount + 1); Result := TAst.LambdaExpr(newParams, newBody, finalLayout, nil, upvaluesList, hasNested);