Fixed some glitches from last refactoring

This commit is contained in:
Michael Schimmel
2025-11-21 17:49:06 +01:00
parent 58c44079f7
commit 61b6a1742b
+18 -8
View File
@@ -34,7 +34,7 @@ type
FFunctionRegistry: IFunctionDefinitionRegistry;
FBoxedDeclarations: THashSet<IVariableDeclarationNode>;
// 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);