Ast ternary expr replaced by cond expr

This commit is contained in:
Michael Schimmel
2025-12-07 12:06:29 +01:00
parent 91c4d57aaf
commit 1d13d0cdaa
15 changed files with 449 additions and 180 deletions
+28 -8
View File
@@ -38,7 +38,7 @@ type
// --- Structural Nodes (Replace Logic) ---
function VisitIfExpression(const Node: IIfExpressionNode): IAstNode; override;
function VisitTernaryExpression(const Node: ITernaryExpressionNode): IAstNode; override;
function VisitCondExpression(const Node: ICondExpressionNode): IAstNode; override; // Replaces Ternary
function VisitAssignment(const Node: IAssignmentNode): IAstNode; override;
function VisitVariableDeclaration(const Node: IVariableDeclarationNode): IAstNode; override;
function VisitLambdaExpression(const Node: ILambdaExpressionNode): IAstNode; override;
@@ -279,18 +279,38 @@ begin
Result := TAst.IfExpr(Node.Identity, newCond, newThen, newElse, Node.StaticType);
end;
function TAstNodeRemover.VisitTernaryExpression(const Node: ITernaryExpressionNode): IAstNode;
function TAstNodeRemover.VisitCondExpression(const Node: ICondExpressionNode): IAstNode;
var
newCond, newThen, newElse: IAstNode;
i: Integer;
newPairs: TArray<TCondPair>;
newElse: IAstNode;
newCond, newBranch: IAstNode;
hasChanged: Boolean;
begin
newCond := EnsureNode(Accept(Node.Condition), Node);
newThen := EnsureNode(Accept(Node.ThenBranch), Node);
newElse := EnsureNode(Accept(Node.ElseBranch), Node);
SetLength(newPairs, Length(Node.Pairs));
hasChanged := False;
if (newCond = Node.Condition) and (newThen = Node.ThenBranch) and (newElse = Node.ElseBranch) then
for i := 0 to High(Node.Pairs) do
begin
// Ensure mandatory children exist (replace with Nop if removed)
newCond := EnsureNode(Accept(Node.Pairs[i].Condition), Node);
newBranch := EnsureNode(Accept(Node.Pairs[i].Branch), Node);
newPairs[i] := TCondPair.Create(newCond, newBranch);
if (newCond <> Node.Pairs[i].Condition) or (newBranch <> Node.Pairs[i].Branch) then
hasChanged := True;
end;
// Else branch is effectively mandatory in the structure, even if it's Nop/Void
newElse := EnsureNode(Accept(Node.ElseBranch), Node);
if newElse <> Node.ElseBranch then
hasChanged := True;
if not hasChanged then
Exit(Node);
Result := TAst.TernaryExpr(Node.Identity, newCond, newThen, newElse, Node.StaticType);
Result := TAst.CondExpr(Node.Identity, newPairs, newElse, Node.StaticType);
end;
function TAstNodeRemover.VisitAssignment(const Node: IAssignmentNode): IAstNode;