Files
MycLib/ASTPlayground/Myc.Fmx.AstEditor.pas
T
2025-09-16 13:14:33 +02:00

1104 lines
40 KiB
ObjectPascal

unit Myc.Fmx.AstEditor;
interface
uses
System.SysUtils,
System.Classes,
System.UITypes,
System.Types,
System.Math.Vectors,
System.Generics.Collections,
FMX.Types,
FMX.Controls,
FMX.Objects,
FMX.Graphics,
Myc.Data.Value,
Myc.Ast.Nodes,
Myc.Fmx.AstEditor.Node,
Myc.Fmx.AstEditor.Workspace;
const
// Pin Visuals
cPinSize = 10;
cDataPinColor = TAlphaColors.Dodgerblue;
cExecPinColor = TAlphaColors.Lightgreen;
type
TPinShape = (psCircle, psTriangle);
TPinAlignment = (paLeft, paRight);
TAstToAuraNodeVisitor = class(TInterfacedObject, IAstVisitor)
public
type
// Holds the result of a node visitation, separating layout and connection concerns.
TAuraNodeResult = record
LayoutNode: TAuraNode;
OutputPin: TControl;
end;
TAuraNodeResultList = TList<TAuraNodeResult>;
TChildVisitorProc = reference to procedure(const Visitor: IAstVisitor);
// The lambda now returns the full result, including the output pin reference.
TCreateParentNodeProc = reference to function(const InputNodes: TAuraNodeResultList): TAuraNodeResult;
TVerticalAlignment = (vaCenter, vaTop);
private
const
// Node Layout
cHorizontalPadding = 25; // Padding inside the node title bar
cPinOffsetY = 18; // Vertical distance between pins
cVerticalPadding = 10; // General vertical padding inside container nodes
// Specific Node Heights
cDefaultNodeHeight = 25; // Default height for simple nodes like BinaryExpr
cIfNodeHeight = 40; // Taller height for If nodes with more pins
private
FWorkspace: TAuraWorkspace;
FParentControl: TControl;
FCurrentPos: TPointF;
FSpacing: TPointF;
FLastResult: TAuraNodeResult;
FConnections: TList<TPinConnection>;
FCurrentExec: TList<TControl>;
FMode: TVisualizationMode;
FResolvedIdentifierCache: TDictionary<TResolvedAddress, TAuraNodeResult>;
FOwnsCaches: Boolean;
procedure FinalizeNodeLayout(const Node: TAuraNode);
function VisitContainerNode(
const Title, Details: string;
const InPin, OutPin: String;
IsExec: Boolean;
HasResult: Boolean;
const ChildVisitorProc: TChildVisitorProc
): TAuraNode;
function VisitOperatorNode(
const InputExpressions: TArray<IAstNode>;
const CreateParentProc: TCreateParentNodeProc;
Alignment: TVerticalAlignment = vaCenter
): TAuraNode;
function BuildNodeControl(const Title, Details: string): TAuraNode;
function CreateNodeControl(const Title, Details: string): TAuraNode;
function CreatePin(
ParentNode: TAuraNode;
Shape: TPinShape;
Alignment: TPinAlignment;
Color: TAlphaColor;
const Tag: string
): TShape;
function CreateInput(ParentNode: TAuraNode; const Caption: string): TControl;
function CreateOutput(ParentNode: TAuraNode): TControl;
function CreateEntry(ParentNode: TAuraNode; connect: Boolean = true): TControl;
function CreateExit(ParentNode: TAuraNode; const Caption: string): TControl;
function TryGetDescr(const Node: IAstNode; out Text: String): Boolean;
public
constructor Create(
AWorkspace: TAuraWorkspace;
AParentControl: TControl;
const AStartPosition: TPointF;
AConnections: TList<TPinConnection>;
const ACurrentExec: TArray<TControl>;
AMode: TVisualizationMode;
AResolvedIdentifierCache: TDictionary<TResolvedAddress, TAuraNodeResult> = nil
);
destructor Destroy; override;
// Public access to the collected connections
property Connections: TList<TPinConnection> read FConnections;
{ IAstVisitor }
function VisitConstant(const Node: IConstantNode): TDataValue;
function VisitIdentifier(const Node: IIdentifierNode): TDataValue;
function VisitBinaryExpression(const Node: IBinaryExpressionNode): TDataValue;
function VisitUnaryExpression(const Node: IUnaryExpressionNode): TDataValue;
function VisitIfExpression(const Node: IIfExpressionNode): TDataValue;
function VisitTernaryExpression(const Node: ITernaryExpressionNode): TDataValue;
function VisitLambdaExpression(const Node: ILambdaExpressionNode): TDataValue;
function VisitFunctionCall(const Node: IFunctionCallNode): TDataValue;
function VisitBlockExpression(const Node: IBlockExpressionNode): TDataValue;
function VisitVariableDeclaration(const Node: IVariableDeclarationNode): TDataValue;
function VisitAssignment(const Node: IAssignmentNode): TDataValue;
function VisitIndexer(const Node: IIndexerNode): TDataValue;
function VisitMemberAccess(const Node: IMemberAccessNode): TDataValue;
function VisitCreateSeries(const Node: ICreateSeriesNode): TDataValue;
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TDataValue;
function VisitSeriesLength(const Node: ISeriesLengthNode): TDataValue;
end;
implementation
uses
System.Math,
System.StrUtils,
FMX.Platform,
Myc.Data.Scalar,
Myc.Fmx.AstEditor.Text;
{ TAstToAuraNodeVisitor }
constructor TAstToAuraNodeVisitor.Create(
AWorkspace: TAuraWorkspace;
AParentControl: TControl;
const AStartPosition: TPointF;
AConnections: TList<TPinConnection>;
const ACurrentExec: TArray<TControl>;
AMode: TVisualizationMode;
AResolvedIdentifierCache: TDictionary<TResolvedAddress, TAuraNodeResult>
);
begin
inherited Create;
FWorkspace := AWorkspace;
FParentControl := AParentControl;
FCurrentPos := AStartPosition;
FSpacing := TPointF.Create(20, 10);
FConnections := AConnections;
FCurrentExec := TList<TControl>.Create(ACurrentExec);
FLastResult.LayoutNode := nil;
FLastResult.OutputPin := nil;
FMode := AMode;
if Assigned(AResolvedIdentifierCache) then
begin
FResolvedIdentifierCache := AResolvedIdentifierCache;
FOwnsCaches := False;
end
else
begin
FResolvedIdentifierCache := TDictionary<TResolvedAddress, TAuraNodeResult>.Create;
FOwnsCaches := True;
end;
end;
destructor TAstToAuraNodeVisitor.Destroy;
begin
if FOwnsCaches then
begin
FResolvedIdentifierCache.Free;
end;
FCurrentExec.Free;
inherited;
end;
procedure TAstToAuraNodeVisitor.FinalizeNodeLayout(const Node: TAuraNode);
var
control: TControl;
leftPins, rightPins: TList<TControl>;
numLeftPins, numRightPins, maxPins: Integer;
reqHeight: Single;
i: Integer;
totalHeight, startY: Single;
begin
leftPins := TList<TControl>.Create;
rightPins := TList<TControl>.Create;
try
for control in Node.Controls do
begin
if (Pos('exec.', control.TagString) = 1) or (Pos('data.', control.TagString) = 1) then
begin
if Pos('.in', control.TagString) > 0 then
begin
leftPins.Add(control);
control.Position.X := 0;
end
else
begin
rightPins.Add(control);
control.Position.X := Node.Width - cPinSize;
end;
end;
end;
numLeftPins := leftPins.Count;
numRightPins := rightPins.Count;
maxPins := Max(numLeftPins, numRightPins);
if maxPins = 0 then
exit;
reqHeight := cVerticalPadding + (maxPins - 1) * cPinOffsetY + cVerticalPadding;
Node.Height := Max(Node.Height, reqHeight);
Node.Height := Max(cDefaultNodeHeight, Node.Height);
if numLeftPins > 0 then
begin
totalHeight := (numLeftPins - 1) * cPinOffsetY;
startY := (Node.Height / 2) - (totalHeight / 2.0);
for i := 0 to numLeftPins - 1 do
leftPins[i].Position.Y := startY + i * cPinOffsetY - (cPinSize / 2);
end;
if numRightPins > 0 then
begin
totalHeight := (numRightPins - 1) * cPinOffsetY;
startY := (Node.Height / 2) - (totalHeight / 2.0);
for i := 0 to numRightPins - 1 do
rightPins[i].Position.Y := startY + i * cPinOffsetY - (cPinSize / 2);
end;
finally
leftPins.Free;
rightPins.Free;
end;
end;
function TAstToAuraNodeVisitor.BuildNodeControl(const Title, Details: string): TAuraNode;
var
fullTitle: string;
textWidth: Single;
newWidth: Single;
measureCanvas: TCanvas;
begin
Result := TAuraNode.Create(FParentControl);
Result.Parent := FParentControl;
fullTitle := Title;
if not Details.IsEmpty then
fullTitle := fullTitle + ': ' + Details;
Result.Title := fullTitle;
measureCanvas := TCanvasManager.MeasureCanvas;
if Assigned(measureCanvas) then
begin
measureCanvas.Font.Assign(Result.TitleFont);
textWidth := measureCanvas.TextWidth(Result.Title);
newWidth := textWidth + (2 * cHorizontalPadding);
Result.Width := Max(Result.Width, newWidth);
end;
end;
function TAstToAuraNodeVisitor.CreateInput(ParentNode: TAuraNode; const Caption: string): TControl;
begin
var cap := Caption;
if cap <> '' then
cap := '.' + cap;
Result := CreatePin(ParentNode, psCircle, paLeft, cDataPinColor, 'data.in' + cap);
Result.Hint := Caption;
Result.ShowHint := Caption <> '';
end;
function TAstToAuraNodeVisitor.CreateNodeControl(const Title, Details: string): TAuraNode;
begin
Result := BuildNodeControl(Title, Details);
Result.Position.Point := FCurrentPos;
FCurrentPos.Y := FCurrentPos.Y + Result.Height + FSpacing.Y;
FLastResult.LayoutNode := Result;
FLastResult.OutputPin := nil;
end;
function TAstToAuraNodeVisitor.CreateOutput(ParentNode: TAuraNode): TControl;
begin
Result := CreatePin(ParentNode, psCircle, paRight, cDataPinColor, 'data.out');
end;
function TAstToAuraNodeVisitor.CreateEntry(ParentNode: TAuraNode; connect: Boolean = true): TControl;
begin
Result := CreatePin(ParentNode, psTriangle, paLeft, cExecPinColor, 'exec.in');
if connect then
begin
for var curr in FCurrentExec do
FConnections.Add(TPinConnection.Create(curr, Result));
FCurrentExec.Clear;
end;
end;
function TAstToAuraNodeVisitor.CreateExit(ParentNode: TAuraNode; const Caption: string): TControl;
begin
var cap := Caption;
if cap <> '' then
cap := '.' + cap;
Result := CreatePin(ParentNode, psTriangle, paRight, cExecPinColor, 'exec.out' + cap);
FCurrentExec.Add(Result);
end;
function TAstToAuraNodeVisitor.CreatePin(
ParentNode: TAuraNode;
Shape: TPinShape;
Alignment: TPinAlignment;
Color: TAlphaColor;
const Tag: string
): TShape;
var
posX, posY: Single;
begin
posY := 0;
if Alignment = paLeft then
posX := 0
else // paRight
posX := ParentNode.Width - cPinSize;
case Shape of
psCircle:
begin
var circle := TEllipse.Create(ParentNode);
circle.Fill.Color := Color;
circle.Stroke.Kind := TBrushKind.None;
Result := circle;
end;
psTriangle:
begin
var triangle := TPath.Create(ParentNode);
triangle.Data.Data := Format('M 0 0 L %d %d L 0 %d Z', [cPinSize, cPinSize div 2, cPinSize]);
triangle.Fill.Color := Color;
triangle.Stroke.Kind := TBrushKind.None;
Result := triangle;
end;
else
Result := nil;
exit;
end;
Result.Parent := ParentNode;
Result.SetBounds(posX, posY, cPinSize, cPinSize);
Result.HitTest := true;
Result.TagString := Tag;
end;
function TAstToAuraNodeVisitor.TryGetDescr(const Node: IAstNode; out Text: String): Boolean;
begin
if FMode <> vmControlFlow then
exit(false);
var textVisitor: IAstVisitor := TAstToTextVisitor.Create;
Text := Node.Accept(textVisitor).AsText;
Result := Pos('{', Text) = 0;
end;
function TAstToAuraNodeVisitor.VisitAddSeriesItem(const Node: IAddSeriesItemNode): TDataValue;
var
details: string;
inputs: TArray<IAstNode>;
begin
if (FMode = vmControlFlow) and TryGetDescr(Node, details) then
begin
var addNode := CreateNodeControl('Add to Series', details);
CreateEntry(addNode);
CreateExit(addNode, '');
FinalizeNodeLayout(addNode);
end
else
begin
if Assigned(Node.Lookback) then
inputs := [Node.Series, Node.Value, Node.Lookback]
else
inputs := [Node.Series, Node.Value];
VisitOperatorNode(
inputs,
function(const InputResults: TAuraNodeResultList): TAuraNodeResult
var
addNode: TAuraNode;
seriesPin, valuePin, lookbackPin: TControl;
begin
addNode := BuildNodeControl('Add to Series', '');
CreateEntry(addNode);
seriesPin := CreateInput(addNode, 'Series');
valuePin := CreateInput(addNode, 'Value');
if Assigned(InputResults[0].OutputPin) then
FConnections.Add(TPinConnection.Create(InputResults[0].OutputPin, seriesPin));
if Assigned(InputResults[1].OutputPin) then
FConnections.Add(TPinConnection.Create(InputResults[1].OutputPin, valuePin));
if InputResults.Count > 2 then
begin
lookbackPin := CreateInput(addNode, 'Lookback');
if Assigned(InputResults[2].OutputPin) then
FConnections.Add(TPinConnection.Create(InputResults[2].OutputPin, lookbackPin));
end;
CreateExit(addNode, '');
FinalizeNodeLayout(addNode);
Result.LayoutNode := addNode;
Result.OutputPin := nil; // No data output
end,
vaTop
);
end;
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitAssignment(const Node: IAssignmentNode): TDataValue;
var
details: string;
assignmentNode: TAuraNode;
control: TControl;
begin
if (FMode = vmControlFlow) and TryGetDescr(Node, details) then
begin
assignmentNode := CreateNodeControl('Assignment', details);
CreateEntry(assignmentNode);
CreateExit(assignmentNode, '');
FinalizeNodeLayout(assignmentNode);
end
else
begin
assignmentNode := CreateNodeControl('Assignment', Node.Identifier.Name);
var childStartPos := TPointF.Create(FSpacing.X, assignmentNode.TitleFont.Size * 1.8 + FSpacing.Y);
var childVisitor :=
TAstToAuraNodeVisitor.Create(FWorkspace, assignmentNode, childStartPos, FConnections, [], FMode, FResolvedIdentifierCache);
Node.Value.Accept(childVisitor);
var maxRight: Single := 0;
var maxBottom: Single := 0;
for control in assignmentNode.Controls do
begin
if control is TAuraNode then
begin
maxRight := Max(maxRight, control.Position.X + control.Width);
maxBottom := Max(maxBottom, control.Position.Y + control.Height);
end;
end;
assignmentNode.Width := Max(assignmentNode.Width, maxRight + FSpacing.X);
assignmentNode.Height := Max(assignmentNode.Height, maxBottom + FSpacing.Y);
CreateEntry(assignmentNode);
CreateExit(assignmentNode, '');
FLastResult.OutputPin := CreateOutput(assignmentNode);
FinalizeNodeLayout(assignmentNode);
FCurrentPos.Y := assignmentNode.Position.Y + assignmentNode.Height + FSpacing.Y;
end;
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitBinaryExpression(const Node: IBinaryExpressionNode): TDataValue;
begin
var exprStr: String;
if (FMode = vmControlFlow) and TryGetDescr(Node, exprStr) then
begin
var exprNode := CreateNodeControl('Expression', exprStr);
FLastResult.OutputPin := CreateOutput(exprNode);
FinalizeNodeLayout(exprNode);
end
else
begin
VisitOperatorNode(
[Node.Left, Node.Right],
function(const InputResults: TAuraNodeResultList): TAuraNodeResult
var
binaryExprNode: TAuraNode;
inputPin1, inputPin2: TControl;
leftResult, rightResult: TAuraNodeResult;
outPin: TControl;
begin
leftResult := InputResults[0];
rightResult := InputResults[1];
binaryExprNode := BuildNodeControl(Node.Operator.ToString, '');
binaryExprNode.TitleFont.Size := 20;
inputPin1 := CreateInput(binaryExprNode, '');
inputPin2 := CreateInput(binaryExprNode, '');
outPin := CreateOutput(binaryExprNode);
if Assigned(leftResult.OutputPin) then
FConnections.Add(TPinConnection.Create(leftResult.OutputPin, inputPin1));
if Assigned(rightResult.OutputPin) then
FConnections.Add(TPinConnection.Create(rightResult.OutputPin, inputPin2));
FinalizeNodeLayout(binaryExprNode);
Result.LayoutNode := binaryExprNode;
Result.OutputPin := outPin;
end
);
end;
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitBlockExpression(const Node: IBlockExpressionNode): TDataValue;
var
blockNode: TAuraNode;
childLastResult: TAuraNodeResult;
begin
childLastResult.LayoutNode := nil;
childLastResult.OutputPin := nil;
blockNode :=
VisitContainerNode(
'',
'',
'',
'',
true,
false,
procedure(const Visitor: IAstVisitor)
var
expression: IAstNode;
begin
for expression in Node.Expressions do
expression.Accept(Visitor);
childLastResult := (Visitor as TAstToAuraNodeVisitor).FLastResult;
end
);
blockNode.Frameless := true;
FCurrentPos.Y := blockNode.Position.Y + blockNode.Height + FSpacing.Y;
FLastResult.LayoutNode := blockNode;
FLastResult.OutputPin := childLastResult.OutputPin;
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitConstant(const Node: IConstantNode): TDataValue;
var
constantNode: TAuraNode;
begin
constantNode := CreateNodeControl('Constant', Node.Value.ToString);
FLastResult.OutputPin := CreateOutput(constantNode);
FinalizeNodeLayout(constantNode);
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitContainerNode(
const Title, Details: string;
const InPin, OutPin: String;
IsExec: Boolean;
HasResult: Boolean;
const ChildVisitorProc: TChildVisitorProc
): TAuraNode;
var
containerNode: TAuraNode;
childVisitor: IAstVisitor;
childStartPos: TPointF;
maxRight: Single;
maxBottom: Single;
control: TControl;
childLastResult: TAuraNodeResult;
begin
const pinNodeHeight = cPinSize + cVerticalPadding;
containerNode := BuildNodeControl(Title, Details);
containerNode.Position.Point := FCurrentPos;
Result := containerNode;
childStartPos :=
TPointF.Create(FSpacing.X, FSpacing.Y + max(IfThen(InPin <> '', pinNodeHeight, 0), IfThen(Title <> '', cVerticalPadding, 0)));
var currExec := FCurrentExec.ToArray;
if InPin <> '' then
begin
var entryNode := BuildNodeControl(InPin, '');
entryNode.Parent := containerNode;
entryNode.Position.Point := TPointF.Create(0, 0);
entryNode.Height := pinNodeHeight;
if IsExec then
begin
var entryPin := CreateEntry(containerNode);
var entryExitPin := CreateExit(entryNode, '');
FinalizeNodeLayout(entryNode);
entryPin.Position.Y := containerNode.AbsoluteToLocal(entryNode.LocalToAbsolute(entryExitPin.Position.Point)).Y;
end
else
begin
FCurrentExec.Clear;
CreateExit(entryNode, '');
FinalizeNodeLayout(entryNode);
end;
end;
childVisitor :=
TAstToAuraNodeVisitor
.Create(FWorkspace, containerNode, childStartPos, FConnections, FCurrentExec.ToArray, FMode, FResolvedIdentifierCache);
ChildVisitorProc(childVisitor);
childLastResult := (childVisitor as TAstToAuraNodeVisitor).FLastResult;
FCurrentExec.Clear;
FCurrentExec.AddRange((childVisitor as TAstToAuraNodeVisitor).FCurrentExec);
maxRight := 0;
maxBottom := 0;
for control in containerNode.Controls do
begin
if control is TAuraNode then
begin
maxRight := Max(maxRight, control.Position.X + control.Width);
maxBottom := Max(maxBottom, control.Position.Y + control.Height);
end;
end;
containerNode.Width := Max(containerNode.Width, maxRight + FSpacing.X);
containerNode.Height := Max(containerNode.Height, maxBottom + FSpacing.Y);
if OutPin <> '' then
begin
var exitNode := BuildNodeControl(OutPin, '');
exitNode.Parent := containerNode;
exitNode.Height := pinNodeHeight;
var exitPin := CreateEntry(exitNode);
if HasResult and Assigned(childLastResult.OutputPin) then
begin
var dataResultPin := CreateInput(exitNode, 'Result');
FConnections.Add(TPinConnection.Create(childLastResult.OutputPin, dataResultPin));
end;
FinalizeNodeLayout(exitNode);
containerNode.Height := Max(containerNode.Height, maxBottom + FSpacing.Y + exitNode.Height);
exitNode.Position.Point := TPointF.Create(containerNode.Width - exitNode.Width, containerNode.Height - exitNode.Height);
if not IsExec then
begin
FCurrentExec.Clear;
FCurrentExec.AddRange(currExec);
end
else
CreateExit(containerNode, '').Position.Y := containerNode.AbsoluteToLocal(exitNode.LocalToAbsolute(exitPin.Position.Point)).Y;
end;
FinalizeNodeLayout(containerNode);
end;
function TAstToAuraNodeVisitor.VisitCreateSeries(const Node: ICreateSeriesNode): TDataValue;
var
seriesNode: TAuraNode;
begin
seriesNode := CreateNodeControl('Create Series', Node.Definition);
FLastResult.OutputPin := CreateOutput(seriesNode);
FinalizeNodeLayout(seriesNode);
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitFunctionCall(const Node: IFunctionCallNode): TDataValue;
var
inputExpressions: TArray<IAstNode>;
begin
var details: String;
if (FMode = vmControlFlow) and TryGetDescr(Node, details) then
begin
var funcCallNode := CreateNodeControl('FunctionCall', details);
CreateEntry(funcCallNode);
FLastResult.OutputPin := CreateOutput(funcCallNode);
CreateExit(funcCallNode, '');
FinalizeNodeLayout(funcCallNode);
end
else
begin
SetLength(inputExpressions, 1 + Length(Node.Arguments));
inputExpressions[0] := Node.Callee;
for var i := 0 to High(Node.Arguments) do
inputExpressions[i + 1] := Node.Arguments[i];
VisitOperatorNode(
inputExpressions,
function(const InputResults: TAuraNodeResultList): TAuraNodeResult
var
funcCallNode: TAuraNode;
calleePin: TControl;
argPin: array of TControl;
i: Integer;
outPin: TControl;
begin
funcCallNode := BuildNodeControl('FunctionCall', '');
CreateEntry(funcCallNode);
calleePin := CreateInput(funcCallNode, 'Callee');
SetLength(argPin, Length(Node.Arguments));
for i := 0 to High(argPin) do
argPin[i] := CreateInput(funcCallNode, 'Arg' + i.ToString);
CreateExit(funcCallNode, '');
outPin := CreateOutput(funcCallNode);
if Assigned(InputResults[0].OutputPin) then
FConnections.Add(TPinConnection.Create(InputResults[0].OutputPin, calleePin));
for i := 0 to High(Node.Arguments) do
if Assigned(InputResults[i + 1].OutputPin) then
FConnections.Add(TPinConnection.Create(InputResults[i + 1].OutputPin, argPin[i]));
FinalizeNodeLayout(funcCallNode);
Result.LayoutNode := funcCallNode;
Result.OutputPin := outPin;
end
);
end;
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitIdentifier(const Node: IIdentifierNode): TDataValue;
var
existingResult: TAuraNodeResult;
identifierNode: TAuraNode;
begin
if Node.IsResolved then
begin
if FResolvedIdentifierCache.TryGetValue(Node.Address, existingResult) then
begin
FLastResult := existingResult;
end
else
begin
identifierNode := CreateNodeControl('Identifier', Node.Name);
FLastResult.LayoutNode := identifierNode;
FLastResult.OutputPin := CreateOutput(identifierNode);
FinalizeNodeLayout(identifierNode);
FResolvedIdentifierCache.Add(Node.Address, FLastResult);
end;
end
else
begin
identifierNode := CreateNodeControl('Identifier', Node.Name);
FLastResult.LayoutNode := identifierNode;
FLastResult.OutputPin := CreateOutput(identifierNode);
FinalizeNodeLayout(identifierNode);
end;
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitIfExpression(const Node: IIfExpressionNode): TDataValue;
var
startPosition: TPointF;
ifNode: TAuraNode;
conditionEndY: Single;
branchStartX: Single;
execPathes: TList<TControl>;
thenEndY, elseEndY: Single;
begin
startPosition := FCurrentPos;
var condStr: String;
if (FMode = vmControlFlow) and TryGetDescr(Node, condStr) then
begin
ifNode := CreateNodeControl('If', condStr);
ifNode.Height := cIfNodeHeight;
end
else
begin
ifNode :=
VisitOperatorNode(
[Node.Condition],
function(const InputResults: TAuraNodeResultList): TAuraNodeResult
var
conditionInputPin: TControl;
condResult: TAuraNodeResult;
ifNode: TAuraNode;
begin
condResult := InputResults[0];
ifNode := BuildNodeControl('If', '');
conditionInputPin := CreateInput(ifNode, 'Condition');
if Assigned(condResult.OutputPin) then
FConnections.Add(TPinConnection.Create(condResult.OutputPin, conditionInputPin));
Result.LayoutNode := ifNode;
Result.OutputPin := nil;
end
);
end;
CreateEntry(ifNode);
conditionEndY := FCurrentPos.Y;
branchStartX := ifNode.Position.X + ifNode.Width + FSpacing.X;
execPathes := TList<TControl>.Create;
try
FCurrentExec.Clear;
CreateExit(ifNode, 'Then');
FCurrentPos.X := branchStartX;
FCurrentPos.Y := startPosition.Y;
Node.ThenBranch.Accept(Self);
thenEndY := FCurrentPos.Y;
FLastResult.LayoutNode := ifNode;
execPathes.AddRange(FCurrentExec);
elseEndY := thenEndY;
FCurrentExec.Clear;
CreateExit(ifNode, 'Else');
if Assigned(Node.ElseBranch) then
begin
FCurrentPos.X := branchStartX;
FCurrentPos.Y := thenEndY;
Node.ElseBranch.Accept(Self);
elseEndY := FCurrentPos.Y;
end;
execPathes.AddRange(FCurrentExec);
FinalizeNodeLayout(ifNode);
FCurrentPos.X := startPosition.X;
FCurrentPos.Y := Max(conditionEndY, elseEndY);
FLastResult.LayoutNode := ifNode;
FLastResult.OutputPin := nil;
FCurrentExec.Clear;
FCurrentExec.AddRange(execPathes);
finally
execPathes.Free;
end;
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitIndexer(const Node: IIndexerNode): TDataValue;
var
details: String;
begin
if (FMode = vmControlFlow) and TryGetDescr(Node, details) then
begin
var indexerNode := CreateNodeControl('Expression', details);
FLastResult.OutputPin := CreateOutput(indexerNode);
FinalizeNodeLayout(indexerNode);
end
else
begin
VisitOperatorNode(
[Node.Base, Node.Index],
function(const InputResults: TAuraNodeResultList): TAuraNodeResult
var
indexerNode: TAuraNode;
basePin, indexPin, outPin: TControl;
begin
indexerNode := BuildNodeControl('[]', '');
indexerNode.TitleFont.Size := 20;
basePin := CreateInput(indexerNode, 'Base');
indexPin := CreateInput(indexerNode, 'Index');
outPin := CreateOutput(indexerNode);
if Assigned(InputResults[0].OutputPin) then
FConnections.Add(TPinConnection.Create(InputResults[0].OutputPin, basePin));
if Assigned(InputResults[1].OutputPin) then
FConnections.Add(TPinConnection.Create(InputResults[1].OutputPin, indexPin));
FinalizeNodeLayout(indexerNode);
Result.LayoutNode := indexerNode;
Result.OutputPin := outPin;
end
);
end;
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitLambdaExpression(const Node: ILambdaExpressionNode): TDataValue;
var
paramStr: String;
lambdaNode: TAuraNode;
i: Integer;
begin
paramStr := '(';
if Length(Node.Parameters) > 0 then
begin
paramStr := paramStr + Node.Parameters[0].Name;
for i := 1 to High(Node.Parameters) do
paramStr := paramStr + ', ' + Node.Parameters[i].Name;
end;
paramStr := paramStr + ')';
lambdaNode :=
VisitContainerNode(
#$03BB,
paramStr,
'call',
'return',
false,
true,
procedure(const Visitor: IAstVisitor) begin Node.Body.Accept(Visitor); end
);
FCurrentPos.Y := lambdaNode.Position.Y + lambdaNode.Height + FSpacing.Y;
FLastResult.LayoutNode := lambdaNode;
FLastResult.OutputPin := CreateOutput(lambdaNode);
FinalizeNodeLayout(lambdaNode);
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitMemberAccess(const Node: IMemberAccessNode): TDataValue;
var
details: String;
begin
if (FMode = vmControlFlow) and TryGetDescr(Node, details) then
begin
var memberNode := CreateNodeControl('Expression', details);
FLastResult.OutputPin := CreateOutput(memberNode);
FinalizeNodeLayout(memberNode);
end
else
begin
VisitOperatorNode(
[Node.Base],
function(const InputResults: TAuraNodeResultList): TAuraNodeResult
var
memberAccessNode: TAuraNode;
basePin, outPin: TControl;
begin
memberAccessNode := BuildNodeControl('Get ' + Node.Member.Name, '');
basePin := CreateInput(memberAccessNode, 'Base');
outPin := CreateOutput(memberAccessNode);
if Assigned(InputResults[0].OutputPin) then
FConnections.Add(TPinConnection.Create(InputResults[0].OutputPin, basePin));
FinalizeNodeLayout(memberAccessNode);
Result.LayoutNode := memberAccessNode;
Result.OutputPin := outPin;
end
);
end;
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitOperatorNode(
const InputExpressions: TArray<IAstNode>;
const CreateParentProc: TCreateParentNodeProc;
Alignment: TVerticalAlignment
): TAuraNode;
var
inputResults: TAuraNodeResultList;
inputNode: IAstNode;
startPosition: TPointF;
endY, maxChildWidth: Single;
parentNode: TAuraNode;
createResult: TAuraNodeResult;
begin
startPosition := FCurrentPos;
inputResults := TAuraNodeResultList.Create;
try
for inputNode in InputExpressions do
begin
inputNode.Accept(Self);
inputResults.Add(FLastResult);
end;
endY := FCurrentPos.Y;
maxChildWidth := 0;
for var res in inputResults do
if Assigned(res.LayoutNode) then
maxChildWidth := Max(maxChildWidth, res.LayoutNode.Position.X + res.LayoutNode.Width);
createResult := CreateParentProc(inputResults);
parentNode := createResult.LayoutNode;
Result := parentNode;
var parentY: Single;
if Alignment = vaCenter then
begin
var totalInputHeight := endY - startPosition.Y;
var nodeCenterY := startPosition.Y + (totalInputHeight / 2);
parentY := nodeCenterY - (parentNode.Height / 2);
end
else // vaTop
begin
parentY := startPosition.Y;
end;
parentNode.Position.Point := TPointF.Create(maxChildWidth + FSpacing.X, parentY);
FCurrentPos.Y := Max(endY, parentNode.Position.Y + parentNode.Height + FSpacing.Y);
FLastResult.LayoutNode := parentNode;
FLastResult.OutputPin := createResult.OutputPin;
finally
inputResults.Free;
end;
end;
function TAstToAuraNodeVisitor.VisitSeriesLength(const Node: ISeriesLengthNode): TDataValue;
begin
VisitOperatorNode(
[Node.Series],
function(const InputResults: TAuraNodeResultList): TAuraNodeResult
var
lengthNode: TAuraNode;
seriesPin, outPin: TControl;
begin
lengthNode := BuildNodeControl('Series Length', '');
seriesPin := CreateInput(lengthNode, 'Series');
outPin := CreateOutput(lengthNode);
if Assigned(InputResults[0].OutputPin) then
FConnections.Add(TPinConnection.Create(InputResults[0].OutputPin, seriesPin));
FinalizeNodeLayout(lengthNode);
Result.LayoutNode := lengthNode;
Result.OutputPin := outPin;
end
);
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitTernaryExpression(const Node: ITernaryExpressionNode): TDataValue;
begin
var exprStr: String;
if (FMode = vmControlFlow) and TryGetDescr(Node, exprStr) then
begin
var ternaryNode := CreateNodeControl('Expression', exprStr);
FLastResult.OutputPin := CreateOutput(ternaryNode);
FinalizeNodeLayout(ternaryNode);
end
else
begin
VisitOperatorNode(
[Node.Condition, Node.ThenBranch, Node.ElseBranch],
function(const InputResults: TAuraNodeResultList): TAuraNodeResult
var
ternaryNode: TAuraNode;
condPin, thenPin, elsePin, outPin: TControl;
begin
ternaryNode := BuildNodeControl('?', '');
ternaryNode.TitleFont.Size := 20;
condPin := CreateInput(ternaryNode, 'Condition');
thenPin := CreateInput(ternaryNode, 'Then');
elsePin := CreateInput(ternaryNode, 'Else');
outPin := CreateOutput(ternaryNode);
if Assigned(InputResults[0].OutputPin) then
FConnections.Add(TPinConnection.Create(InputResults[0].OutputPin, condPin));
if Assigned(InputResults[1].OutputPin) then
FConnections.Add(TPinConnection.Create(InputResults[1].OutputPin, thenPin));
if Assigned(InputResults[2].OutputPin) then
FConnections.Add(TPinConnection.Create(InputResults[2].OutputPin, elsePin));
FinalizeNodeLayout(ternaryNode);
Result.LayoutNode := ternaryNode;
Result.OutputPin := outPin;
end
);
end;
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitUnaryExpression(const Node: IUnaryExpressionNode): TDataValue;
begin
var exprStr: String;
if (FMode = vmControlFlow) and TryGetDescr(Node, exprStr) then
begin
var exprNode := CreateNodeControl('Expression', exprStr);
FLastResult.OutputPin := CreateOutput(exprNode);
FinalizeNodeLayout(exprNode);
end
else
begin
VisitOperatorNode(
[Node.Right],
function(const InputResults: TAuraNodeResultList): TAuraNodeResult
var
unaryExprNode: TAuraNode;
inputPin: TControl;
rightResult: TAuraNodeResult;
outPin: TControl;
begin
rightResult := InputResults[0];
unaryExprNode := BuildNodeControl(Node.Operator.ToString, '');
unaryExprNode.TitleFont.Size := 20;
inputPin := CreateInput(unaryExprNode, '');
outPin := CreateOutput(unaryExprNode);
if Assigned(rightResult.OutputPin) then
FConnections.Add(TPinConnection.Create(rightResult.OutputPin, inputPin));
FinalizeNodeLayout(unaryExprNode);
Result.LayoutNode := unaryExprNode;
Result.OutputPin := outPin;
end
);
end;
Result := TDataValue.Void;
end;
function TAstToAuraNodeVisitor.VisitVariableDeclaration(const Node: IVariableDeclarationNode): TDataValue;
var
details: string;
varDeclNode: TAuraNode;
control: TControl;
begin
if (FMode = vmControlFlow) and TryGetDescr(Node, details) then
begin
varDeclNode := CreateNodeControl('VarDecl', details);
CreateEntry(varDeclNode);
CreateExit(varDeclNode, '');
FinalizeNodeLayout(varDeclNode);
end
else
begin
if Assigned(Node.Initializer) then
begin
varDeclNode := CreateNodeControl('VarDecl', Node.Identifier.Name);
var childStartPos := TPointF.Create(FSpacing.X, varDeclNode.TitleFont.Size * 1.8 + FSpacing.Y);
var childVisitor :=
TAstToAuraNodeVisitor.Create(FWorkspace, varDeclNode, childStartPos, FConnections, [], FMode, FResolvedIdentifierCache);
Node.Initializer.Accept(childVisitor);
var maxRight: Single := 0;
var maxBottom: Single := 0;
for control in varDeclNode.Controls do
begin
if control is TAuraNode then
begin
maxRight := Max(maxRight, control.Position.X + control.Width);
maxBottom := Max(maxBottom, control.Position.Y + control.Height);
end;
end;
varDeclNode.Width := Max(varDeclNode.Width, maxRight + FSpacing.X);
varDeclNode.Height := Max(varDeclNode.Height, maxBottom + FSpacing.Y);
CreateEntry(varDeclNode);
CreateExit(varDeclNode, '');
FLastResult.OutputPin := CreateOutput(varDeclNode);
FinalizeNodeLayout(varDeclNode);
FCurrentPos.Y := varDeclNode.Position.Y + varDeclNode.Height + FSpacing.Y;
end
else
begin
varDeclNode := CreateNodeControl('VarDecl', Node.Identifier.Name);
CreateEntry(varDeclNode);
CreateExit(varDeclNode, '');
FLastResult.OutputPin := CreateOutput(varDeclNode);
FinalizeNodeLayout(varDeclNode);
end;
end;
Result := TDataValue.Void;
end;
end.