Ast editor unit refactoring
This commit is contained in:
@@ -0,0 +1,249 @@
|
||||
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.Core,
|
||||
Myc.Fmx.AstEditor.Handlers;
|
||||
|
||||
type
|
||||
// Inherits from the generic TAstVisitor<TAuraNode>
|
||||
TAstVisualizer = class(TAstVisitor<TAuraNode>, IAstVisualizer)
|
||||
private
|
||||
FExprDepth: Integer;
|
||||
FParentControl: TControl;
|
||||
FWorkspace: TAuraWorkspace;
|
||||
function CallAccept(const Node: IAstNode): TAuraNode;
|
||||
function GetExprDepth: Integer;
|
||||
function GetParentControl: TControl;
|
||||
function GetWorkspace: TAuraWorkspace;
|
||||
procedure SetExprDepth(const Value: Integer);
|
||||
procedure SetParentControl(const Value: TControl);
|
||||
protected
|
||||
// Visitor implementations for each AST node type.
|
||||
function VisitConstant(const Node: IConstantNode): TAuraNode; override;
|
||||
function VisitIdentifier(const Node: IIdentifierNode): TAuraNode; override;
|
||||
function VisitKeyword(const Node: IKeywordNode): TAuraNode; override;
|
||||
function VisitIfExpression(const Node: IIfExpressionNode): TAuraNode; override;
|
||||
function VisitTernaryExpression(const Node: ITernaryExpressionNode): TAuraNode; override;
|
||||
function VisitLambdaExpression(const Node: ILambdaExpressionNode): TAuraNode; override;
|
||||
function VisitFunctionCall(const Node: IFunctionCallNode): TAuraNode; override;
|
||||
function VisitMacroExpansionNode(const Node: IMacroExpansionNode): TAuraNode; override;
|
||||
function VisitBlockExpression(const Node: IBlockExpressionNode): TAuraNode; override;
|
||||
function VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAuraNode; override;
|
||||
function VisitAssignment(const Node: IAssignmentNode): TAuraNode; override;
|
||||
function VisitMacroDefinition(const Node: IMacroDefinitionNode): TAuraNode; override;
|
||||
function VisitQuasiquote(const Node: IQuasiquoteNode): TAuraNode; override;
|
||||
function VisitUnquote(const Node: IUnquoteNode): TAuraNode; override;
|
||||
function VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): TAuraNode; override;
|
||||
function VisitIndexer(const Node: IIndexerNode): TAuraNode; override;
|
||||
function VisitMemberAccess(const Node: IMemberAccessNode): TAuraNode; override;
|
||||
function VisitRecordLiteral(const Node: IRecordLiteralNode): TAuraNode; override;
|
||||
function VisitCreateSeries(const Node: ICreateSeriesNode): TAuraNode; override;
|
||||
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAuraNode; override;
|
||||
function VisitSeriesLength(const Node: ISeriesLengthNode): TAuraNode; override;
|
||||
function VisitRecurNode(const Node: IRecurNode): TAuraNode; override;
|
||||
function VisitNop(const Node: INopNode): TAuraNode; override;
|
||||
|
||||
public
|
||||
constructor Create(AWorkspace: TAuraWorkspace; 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: TAuraWorkspace read GetWorkspace;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TAstVisualizer }
|
||||
|
||||
constructor TAstVisualizer.Create(AWorkspace: TAuraWorkspace; 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): TAuraNode;
|
||||
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<TAuraNode>;
|
||||
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: TAuraWorkspace;
|
||||
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): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TConstantNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitIdentifier(const Node: IIdentifierNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TIdentifierNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitKeyword(const Node: IKeywordNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TKeywordNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitBlockExpression(const Node: IBlockExpressionNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TBlockExpressionNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitIfExpression(const Node: IIfExpressionNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TIfExpressionNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitTernaryExpression(const Node: ITernaryExpressionNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TTernaryExpressionNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitLambdaExpression(const Node: ILambdaExpressionNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TLambdaExpressionNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitFunctionCall(const Node: IFunctionCallNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TFunctionCallNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitMacroExpansionNode(const Node: IMacroExpansionNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TMacroExpansionNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TVariableDeclarationNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitAssignment(const Node: IAssignmentNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TAssignmentNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitMacroDefinition(const Node: IMacroDefinitionNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TMacroDefinitionNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitQuasiquote(const Node: IQuasiquoteNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TQuasiquoteNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitUnquote(const Node: IUnquoteNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TUnquoteNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TUnquoteSplicingNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitIndexer(const Node: IIndexerNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TIndexerNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitMemberAccess(const Node: IMemberAccessNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TMemberAccessNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitRecordLiteral(const Node: IRecordLiteralNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TRecordLiteralNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitCreateSeries(const Node: ICreateSeriesNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TCreateSeriesNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TAddSeriesItemNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitSeriesLength(const Node: ISeriesLengthNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TSeriesLengthNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitRecurNode(const Node: IRecurNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TRecurNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
function TAstVisualizer.VisitNop(const Node: INopNode): TAuraNode;
|
||||
begin
|
||||
Result := TAuraNode.Create(Self, TNopNodeHandler.Create(Node));
|
||||
end;
|
||||
|
||||
end.
|
||||
Reference in New Issue
Block a user