Files
MycLib/Src/AST/Myc.Fmx.AstEditor.Visualizer.pas
T
2025-12-01 13:19:07 +01:00

287 lines
10 KiB
ObjectPascal

unit Myc.Fmx.AstEditor.Visualizer;
interface
uses
System.SysUtils,
System.Classes,
FMX.Controls,
Myc.Data.Value,
Myc.Ast.Nodes,
Myc.Ast.Visitor,
Myc.Fmx.AstEditor.Workspace,
Myc.Fmx.AstEditor.Node,
Myc.Fmx.AstEditor.Handlers;
type
// Inherits from the generic TAstVisitor<TAstViewNode>
TAstVisualizer = class(TAstVisitor<TAstViewNode>, IAstVisualizer)
private
FExprDepth: Integer;
FParentControl: TControl;
FWorkspace: TWorkspace; // Changed from TAstWorkspace to TWorkspace
function CallAccept(const Node: IAstNode): TAstViewNode;
function GetExprDepth: Integer;
function GetParentControl: TControl;
function GetWorkspace: TWorkspace; // Changed signature
procedure SetExprDepth(const Value: Integer);
procedure SetParentControl(const Value: TControl);
protected
// Visitor implementations for each AST node type.
function VisitConstant(const Node: IConstantNode): TAstViewNode; override;
function VisitIdentifier(const Node: IIdentifierNode): TAstViewNode; override;
function VisitKeyword(const Node: IKeywordNode): TAstViewNode; override;
// --- List Visitors (NEW) ---
function VisitParameterList(const Node: IParameterList): TAstViewNode; override;
function VisitArgumentList(const Node: IArgumentList): TAstViewNode; override;
function VisitExpressionList(const Node: IExpressionList): TAstViewNode; override;
function VisitRecordFieldList(const Node: IRecordFieldList): TAstViewNode; override;
function VisitRecordField(const Node: IRecordFieldNode): TAstViewNode; override;
function VisitIfExpression(const Node: IIfExpressionNode): TAstViewNode; override;
function VisitTernaryExpression(const Node: ITernaryExpressionNode): TAstViewNode; override;
function VisitLambdaExpression(const Node: ILambdaExpressionNode): TAstViewNode; override;
function VisitFunctionCall(const Node: IFunctionCallNode): TAstViewNode; override;
function VisitMacroExpansionNode(const Node: IMacroExpansionNode): TAstViewNode; override;
function VisitBlockExpression(const Node: IBlockExpressionNode): TAstViewNode; override;
function VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstViewNode; override;
function VisitAssignment(const Node: IAssignmentNode): TAstViewNode; override;
function VisitMacroDefinition(const Node: IMacroDefinitionNode): TAstViewNode; override;
function VisitQuasiquote(const Node: IQuasiquoteNode): TAstViewNode; override;
function VisitUnquote(const Node: IUnquoteNode): TAstViewNode; override;
function VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): TAstViewNode; override;
function VisitIndexer(const Node: IIndexerNode): TAstViewNode; override;
function VisitMemberAccess(const Node: IMemberAccessNode): TAstViewNode; override;
function VisitRecordLiteral(const Node: IRecordLiteralNode): TAstViewNode; override;
function VisitCreateSeries(const Node: ICreateSeriesNode): TAstViewNode; override;
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstViewNode; override;
function VisitSeriesLength(const Node: ISeriesLengthNode): TAstViewNode; override;
function VisitRecurNode(const Node: IRecurNode): TAstViewNode; override;
function VisitNop(const Node: INopNode): TAstViewNode; override;
public
constructor Create(AWorkspace: TWorkspace; AParentControl: TControl; AExprDepth: Integer);
destructor Destroy; override;
function Clone(ParentControl: TControl; ExprDepth: Integer): IAstVisualizer;
property ExprDepth: Integer read GetExprDepth write SetExprDepth;
property ParentControl: TControl read GetParentControl write SetParentControl;
property Workspace: TWorkspace read GetWorkspace;
end;
implementation
{ TAstVisualizer }
constructor TAstVisualizer.Create(AWorkspace: TWorkspace; AParentControl: TControl; AExprDepth: Integer);
begin
inherited Create;
FWorkspace := AWorkspace;
FParentControl := AParentControl;
FExprDepth := AExprDepth;
end;
destructor TAstVisualizer.Destroy;
begin
inherited;
end;
function TAstVisualizer.CallAccept(const Node: IAstNode): TAstViewNode;
var
dataValue: TDataValue;
begin
if not Assigned(Node) then
exit(nil);
dataValue := Node.Accept(Self);
if dataValue.Kind = TDataValueKind.vkGeneric then
begin
Result := dataValue.AsGeneric<TAstViewNode>;
end
else
begin
Result := nil;
end;
end;
function TAstVisualizer.Clone(ParentControl: TControl; ExprDepth: Integer): IAstVisualizer;
begin
Result := TAstVisualizer.Create(FWorkspace, ParentControl, ExprDepth);
end;
function TAstVisualizer.GetExprDepth: Integer;
begin
Result := FExprDepth;
end;
function TAstVisualizer.GetParentControl: TControl;
begin
Result := FParentControl;
end;
function TAstVisualizer.GetWorkspace: TWorkspace;
begin
Result := FWorkspace;
end;
procedure TAstVisualizer.SetExprDepth(const Value: Integer);
begin
FExprDepth := Value;
end;
procedure TAstVisualizer.SetParentControl(const Value: TControl);
begin
FParentControl := Value;
end;
// --- Visitor Implementations ---
function TAstVisualizer.VisitConstant(const Node: IConstantNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TConstantNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitIdentifier(const Node: IIdentifierNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TIdentifierNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitKeyword(const Node: IKeywordNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TKeywordNodeHandler.Create(Node));
end;
// --- List Visitors ---
function TAstVisualizer.VisitParameterList(const Node: IParameterList): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TParameterListHandler.Create(Node));
end;
function TAstVisualizer.VisitArgumentList(const Node: IArgumentList): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TArgumentListHandler.Create(Node));
end;
function TAstVisualizer.VisitExpressionList(const Node: IExpressionList): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TExpressionListHandler.Create(Node));
end;
function TAstVisualizer.VisitRecordFieldList(const Node: IRecordFieldList): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TRecordFieldListHandler.Create(Node));
end;
function TAstVisualizer.VisitRecordField(const Node: IRecordFieldNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TRecordFieldHandler.Create(Node));
end;
// --- Node Visitors ---
function TAstVisualizer.VisitBlockExpression(const Node: IBlockExpressionNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TBlockExpressionNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitIfExpression(const Node: IIfExpressionNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TIfExpressionNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitTernaryExpression(const Node: ITernaryExpressionNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TTernaryExpressionNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitLambdaExpression(const Node: ILambdaExpressionNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TLambdaExpressionNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitFunctionCall(const Node: IFunctionCallNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TFunctionCallNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitMacroExpansionNode(const Node: IMacroExpansionNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TMacroExpansionNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TVariableDeclarationNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitAssignment(const Node: IAssignmentNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TAssignmentNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitMacroDefinition(const Node: IMacroDefinitionNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TMacroDefinitionNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitQuasiquote(const Node: IQuasiquoteNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TQuasiquoteNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitUnquote(const Node: IUnquoteNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TUnquoteNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TUnquoteSplicingNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitIndexer(const Node: IIndexerNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TIndexerNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitMemberAccess(const Node: IMemberAccessNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TMemberAccessNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitRecordLiteral(const Node: IRecordLiteralNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TRecordLiteralNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitCreateSeries(const Node: ICreateSeriesNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TCreateSeriesNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TAddSeriesItemNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitSeriesLength(const Node: ISeriesLengthNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TSeriesLengthNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitRecurNode(const Node: IRecurNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TRecurNodeHandler.Create(Node));
end;
function TAstVisualizer.VisitNop(const Node: INopNode): TAstViewNode;
begin
Result := TAstViewNode.Create(Self, TNopNodeHandler.Create(Node));
end;
end.