Refactoring for immutable nodes

This commit is contained in:
Michael Schimmel
2025-11-05 13:21:17 +01:00
parent 9bd2d6f7ab
commit c0a689d2bc
13 changed files with 217 additions and 190 deletions
+4 -8
View File
@@ -117,11 +117,10 @@ end;
function TAstTCO.VisitBlockExpression(const Node: IBlockExpressionNode): IAstNode;
var
isContextTail: Boolean;
newExprs: TArray<IAstNode>; // For CoW
newExprs: TArray<IAstNode>;
begin
isContextTail := FIsTailStack.Peek;
// Call the base implementation which handles CoW
var nTail := High(Node.Expressions);
newExprs :=
AcceptNodes(
@@ -237,20 +236,17 @@ end;
function TAstTCO.VisitMacroExpansionNode(const Node: IMacroExpansionNode): IAstNode;
var
N: TMacroExpansionNode;
newBody: IAstNode;
begin
N := (Node as TMacroExpansionNode);
// Propagate tail call status to the expanded body
FNextIsTail := FIsTailStack.Peek;
newBody := Accept(N.ExpandedBody);
newBody := Accept(Node.ExpandedBody);
if newBody = N.ExpandedBody then
if newBody = Node.ExpandedBody then
Result := Node
else
// Rebuild, preserving the original CallNode metadata
Result := TMacroExpansionNode.Create(N.CallNode, newBody, newBody.AsTypedNode.StaticType);
Result := TMacroExpansionNode.Create(Node.CallNode, newBody.AsTypedNode);
end;
function TAstTCO.VisitFunctionCall(const Node: IFunctionCallNode): IAstNode;