Refactoring

This commit is contained in:
Michael Schimmel
2025-11-02 22:44:01 +01:00
parent 92cfe94463
commit eb42a4ef3b
6 changed files with 29 additions and 23 deletions
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -4,7 +4,7 @@
<ProjectVersion>20.3</ProjectVersion>
<FrameworkType>FMX</FrameworkType>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
<Config Condition="'$(Config)'==''">Release</Config>
<Platform Condition="'$(Platform)'==''">Win64</Platform>
<ProjectName Condition="'$(ProjectName)'==''">ASTPlayground</ProjectName>
<TargetedPlatforms>2</TargetedPlatforms>
+3 -3
View File
@@ -197,7 +197,7 @@ begin
// Arguments are not in tail position
FNextIsTail := False;
N.Arguments := AcceptNodes<IAstNode>(N.Arguments, function(Node: IAstNode): IAstNode begin exit(Node) end);
N.Arguments := AcceptNodes(N.Arguments);
Result := N;
end;
@@ -215,7 +215,7 @@ begin
// Also visit the original call nodes, though they are not in tail pos
FNextIsTail := False;
N.Callee := Accept(N.Callee);
N.Arguments := AcceptNodes<IAstNode>(N.Arguments, function(Node: IAstNode): IAstNode begin exit(Node) end);
N.Arguments := AcceptNodes(N.Arguments);
Result := N;
end;
@@ -231,7 +231,7 @@ begin
// Arguments are not in tail position
FNextIsTail := False;
N.Callee := Accept(N.Callee);
N.Arguments := AcceptNodes<IAstNode>(N.Arguments, function(Node: IAstNode): IAstNode begin exit(Node) end);
N.Arguments := AcceptNodes(N.Arguments);
// Mutate the node with the TCO status
N.IsTailCall := isTailCall;
+23 -14
View File
@@ -96,7 +96,8 @@ type
TAstTransformer = class abstract(TAstVisitor<IAstNode>)
protected
function AcceptNodes<T: IAstNode>(const Nodes: TArray<T>; const Map: TFunc<IAstNode, T>): TArray<T>;
function AcceptParameters(const Nodes: TArray<IIdentifierNode>): TArray<IIdentifierNode>;
function AcceptNodes(const Nodes: TArray<IAstNode>): TArray<IAstNode>;
function VisitConstant(const Node: IConstantNode): IAstNode; override;
function VisitIdentifier(const Node: IIdentifierNode): IAstNode; override;
@@ -340,25 +341,33 @@ begin
Result := TDataValue.FromGeneric<T>(VisitRecurNode(Node));
end;
function TAstTransformer.AcceptNodes<T>(const Nodes: TArray<T>; const Map: TFunc<IAstNode, T>): TArray<T>;
function TAstTransformer.AcceptParameters(const Nodes: TArray<IIdentifierNode>): TArray<IIdentifierNode>;
var
i: Integer;
newNode: T; // Changed from TDataValue
newList: TList<T>; // Used if nodes are removed (e.g. defmacro)
begin
SetLength(Result, Length(Nodes));
for i := 0 to High(Nodes) do
Result[i] := Accept(Nodes[i]).AsIdentifier;
end;
function TAstTransformer.AcceptNodes(const Nodes: TArray<IAstNode>): TArray<IAstNode>;
var
i: Integer;
newNode: IAstNode; // Changed from TDataValue
newList: TList<IAstNode>; // Used if nodes are removed (e.g. defmacro)
begin
Result := Nodes; // Assume no changes
newList := nil;
for i := 0 to High(Nodes) do
begin
if Assigned(Map) then
newNode := Map(Accept(Nodes[i])); // Calls new Accept, returns IAstNode (or nil)
newNode := Accept(Nodes[i]); // Calls new Accept, returns IAstNode (or nil)
if not Assigned(newNode) then // Node was removed (e.g. defmacro)
begin
if newList = nil then // First change
begin
newList := TList<T>.Create;
newList := TList<IAstNode>.Create;
for var j := 0 to i - 1 do
newList.Add(Nodes[j]);
end;
@@ -370,7 +379,7 @@ begin
begin
if newList = nil then // First change
begin
newList := TList<T>.Create;
newList := TList<IAstNode>.Create;
for var j := 0 to i - 1 do
newList.Add(Nodes[j]);
end;
@@ -455,7 +464,7 @@ var
N: TLambdaExpressionNode;
begin
N := (Node as TLambdaExpressionNode);
N.Parameters := AcceptNodes<IIdentifierNode>(N.Parameters, function(Node: IAstNode): IIdentifierNode begin exit(Node.AsIdentifier) end);
N.Parameters := AcceptParameters(N.Parameters);
N.Body := Accept(N.Body);
Result := Node;
end;
@@ -466,7 +475,7 @@ var
begin
N := (Node as TFunctionCallNode);
N.Callee := Accept(N.Callee);
N.Arguments := AcceptNodes<IAstNode>(N.Arguments, function(Node: IAstNode): IAstNode begin exit(Node) end);
N.Arguments := AcceptNodes(N.Arguments);
Result := Node;
end;
@@ -477,7 +486,7 @@ begin
N := (Node as TMacroExpansionNode);
// Visit original call parts
N.Callee := Accept(N.Callee);
N.Arguments := AcceptNodes<IAstNode>(N.Arguments, function(Node: IAstNode): IAstNode begin exit(Node) end);
N.Arguments := AcceptNodes(N.Arguments);
// Visit expanded body
N.ExpandedBody := Accept(N.ExpandedBody);
Result := Node;
@@ -488,7 +497,7 @@ var
N: TBlockExpressionNode;
begin
N := (Node as TBlockExpressionNode);
N.Expressions := AcceptNodes<IAstNode>(N.Expressions, function(Node: IAstNode): IAstNode begin exit(Node) end);
N.Expressions := AcceptNodes(N.Expressions);
Result := Node;
end;
@@ -518,7 +527,7 @@ var
begin
N := (Node as TMacroDefinitionNode);
N.Name := Accept(N.Name).AsIdentifier;
N.Parameters := AcceptNodes<IIdentifierNode>(N.Parameters, function(Node: IAstNode): IIdentifierNode begin exit(Node.AsIdentifier) end);
N.Parameters := AcceptParameters(N.Parameters);
N.Body := Accept(N.Body);
Result := Node;
end;
@@ -618,7 +627,7 @@ var
N: TRecurNode;
begin
N := (Node as TRecurNode);
N.Arguments := AcceptNodes<IAstNode>(N.Arguments, function(Node: IAstNode): IAstNode begin exit(Node) end);
N.Arguments := AcceptNodes(N.Arguments);
Result := Node;
end;
-4
View File
@@ -108,10 +108,6 @@ type
property Kind: TAstNodeKind read GetKind;
end;
// ---
// --- "God Node" Implementations (Alle Child-Properties sind jetzt writeable)
// ---
TConstantNode = class(TAstNode, IConstantNode)
private
FValue: TDataValue;
+1
View File
@@ -90,6 +90,7 @@ type
implementation
uses
WinApi.Windows,
System.Generics.Defaults; // For TComparer<Integer>
{ TKeywordRegistry.TKeyword }