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
+69 -13
View File
@@ -32,12 +32,20 @@ type
function ReconstructAst(OwnerNode: TAstViewNode): IAstNode; override;
end;
TTernaryExpressionNodeHandler = class(TBaseNodeHandler<ITernaryExpressionNode>)
// Handles the 'cond' expression (multi-branch if/else)
TCondExpressionNodeHandler = class(TBaseNodeHandler<ICondExpressionNode>)
private
FConditionNode: TAstViewNode;
FThenNode: TAstViewNode;
type
TBranchView = record
Condition: TAstViewNode;
Branch: TAstViewNode;
end;
private
FBranchNodes: TList<TBranchView>;
FElseNode: TAstViewNode;
public
constructor Create(const ANode: ICondExpressionNode);
destructor Destroy; override;
procedure BuildUI(OwnerNode: TAstViewNode); override;
function ReconstructAst(OwnerNode: TAstViewNode): IAstNode; override;
end;
@@ -152,26 +160,74 @@ begin
Result := TAst.IfExpr(FNode.Identity, FConditionNode.CreateAst, FThenNode.CreateAst, elseAst, FNode.StaticType);
end;
{ TTernaryExpressionNodeHandler }
{ TCondExpressionNodeHandler }
procedure TTernaryExpressionNodeHandler.BuildUI(OwnerNode: TAstViewNode);
constructor TCondExpressionNodeHandler.Create(const ANode: ICondExpressionNode);
begin
inherited Create(ANode);
FBranchNodes := TList<TBranchView>.Create;
end;
destructor TCondExpressionNodeHandler.Destroy;
begin
FBranchNodes.Free;
inherited;
end;
procedure TCondExpressionNodeHandler.BuildUI(OwnerNode: TAstViewNode);
var
visu: IAstVisualizer;
pair: TCondPair;
i: Integer;
row: TAutoFitLayout;
bv: TBranchView;
begin
visu := OwnerNode.Visualizer.Clone(OwnerNode, OwnerNode.Visualizer.ExprDepth + 1);
OwnerNode.Frameless := True;
OwnerNode.Orientation := loHorizontal;
FConditionNode := visu.CallAccept(FNode.Condition);
OwnerNode.AddLabel(OwnerNode, '?');
FThenNode := visu.CallAccept(FNode.ThenBranch);
OwnerNode.AddLabel(OwnerNode, ':');
OwnerNode.Frameless := False;
OwnerNode.Orientation := loVertical;
OwnerNode.Alignment := laFlush;
var title := OwnerNode.AddLabel(OwnerNode, '?');
title.FontSettings.Font.Style := [TFontStyle.fsBold];
for i := 0 to High(FNode.Pairs) do
begin
pair := FNode.Pairs[i];
row := OwnerNode.AddContainer(OwnerNode, loHorizontal, laCenter);
row.Margins := TMarginRect.Create(0, 2, 0, 2);
bv.Condition := visu.CallAccept(pair.Condition);
row.AddChild(bv.Condition);
OwnerNode.AddLabel(row, '->');
bv.Branch := visu.CallAccept(pair.Branch);
row.AddChild(bv.Branch);
FBranchNodes.Add(bv);
end;
// Else Branch
var elseRow := OwnerNode.AddContainer(OwnerNode, loHorizontal, laCenter);
elseRow.Margins := TMarginRect.Create(0, 4, 0, 0); // Extra spacing
OwnerNode.AddLabel(elseRow, 'else ->');
FElseNode := visu.CallAccept(FNode.ElseBranch);
// FIX: Use AddChild instead of assigning Parent directly
elseRow.AddChild(FElseNode);
end;
function TTernaryExpressionNodeHandler.ReconstructAst(OwnerNode: TAstViewNode): IAstNode;
function TCondExpressionNodeHandler.ReconstructAst(OwnerNode: TAstViewNode): IAstNode;
var
newPairs: TArray<TCondPair>;
i: Integer;
begin
Result := TAst.TernaryExpr(FNode.Identity, FConditionNode.CreateAst, FThenNode.CreateAst, FElseNode.CreateAst, FNode.StaticType);
SetLength(newPairs, FBranchNodes.Count);
for i := 0 to FBranchNodes.Count - 1 do
begin
newPairs[i] := TCondPair.Create(FBranchNodes[i].Condition.CreateAst, FBranchNodes[i].Branch.CreateAst);
end;
Result := TAst.CondExpr(FNode.Identity, newPairs, FElseNode.CreateAst, FNode.StaticType);
end;
{ TLambdaExpressionNodeHandler }