Beginning Editor

This commit is contained in:
Michael Schimmel
2025-09-08 18:59:04 +02:00
parent 7e4ecb2ff9
commit a9cc9633a2
14 changed files with 1582 additions and 288 deletions
+6 -6
View File
@@ -90,11 +90,11 @@ type
TClosureValue = class(TInterfacedObject, TAstValue.IClosure)
private
FBody: IExpressionNode;
FBody: IAstNode;
FClosureScope: IExecutionScope;
FLambdaNode: ILambdaExpressionNode;
FUpvalues: TArray<IValueCell>;
function GetBody: IExpressionNode;
function GetBody: IAstNode;
function GetParameters: TArray<IIdentifierNode>;
function GetClosureScope: IExecutionScope;
function GetUpvalues: TArray<IValueCell>;
@@ -113,7 +113,7 @@ type
TNativeClosure = class(TInterfacedObject, TAstValue.IClosure)
private
FMethod: TNativeFunction;
function GetBody: IExpressionNode;
function GetBody: IAstNode;
function GetParameters: TArray<IIdentifierNode>;
function GetClosureScope: IExecutionScope;
function GetUpvalues: TArray<IValueCell>;
@@ -167,7 +167,7 @@ begin
FUpvalues := AUpvalues; // Store the captured cells
end;
function TClosureValue.GetBody: IExpressionNode;
function TClosureValue.GetBody: IAstNode;
begin
Result := FBody;
end;
@@ -195,7 +195,7 @@ begin
FMethod := AMethod;
end;
function TNativeClosure.GetBody: IExpressionNode;
function TNativeClosure.GetBody: IAstNode;
begin
Result := nil;
end;
@@ -639,7 +639,7 @@ end;
function TEvaluatorVisitor.VisitBlockExpression(const Node: IBlockExpressionNode): TAstValue;
var
expression: IExpressionNode;
expression: IAstNode;
begin
// The result of a block is the result of its last expression.
Result := TAstValue.Void;
+58 -62
View File
@@ -27,7 +27,6 @@ type
// --- Forward Declarations to break cycles ---
IAstVisitor = interface;
IAstNode = interface;
IExpressionNode = interface;
IIdentifierNode = interface;
IConstantNode = interface;
IBinaryExpressionNode = interface;
@@ -54,12 +53,12 @@ type
type
IClosure = interface
{$region 'private'}
function GetBody: IExpressionNode;
function GetBody: IAstNode;
function GetClosureScope: IExecutionScope;
function GetParameters: TArray<IIdentifierNode>;
function GetUpvalues: TArray<IValueCell>;
{$endregion}
property Body: IExpressionNode read GetBody;
property Body: IAstNode read GetBody;
property ClosureScope: IExecutionScope read GetClosureScope;
property Parameters: TArray<IIdentifierNode> read GetParameters;
property Upvalues: TArray<IValueCell> read GetUpvalues;
@@ -170,17 +169,14 @@ type
function Accept(const Visitor: IAstVisitor): TAstValue;
end;
IExpressionNode = interface(IAstNode)
end;
IConstantNode = interface(IExpressionNode)
IConstantNode = interface(IAstNode)
{$region 'private'}
function GetValue: TScalar;
{$endregion}
property Value: TScalar read GetValue;
end;
IIdentifierNode = interface(IExpressionNode)
IIdentifierNode = interface(IAstNode)
{$region 'private'}
function GetIsResolved: Boolean;
function GetAddress: TResolvedAddress;
@@ -191,119 +187,119 @@ type
property Address: TResolvedAddress read GetAddress;
end;
IBinaryExpressionNode = interface(IExpressionNode)
IBinaryExpressionNode = interface(IAstNode)
{$region 'private'}
function GetLeft: IExpressionNode;
function GetLeft: IAstNode;
function GetOperator: TBinaryOperator;
function GetRight: IExpressionNode;
function GetRight: IAstNode;
{$endregion}
property Left: IExpressionNode read GetLeft;
property Left: IAstNode read GetLeft;
property Operator: TBinaryOperator read GetOperator;
property Right: IExpressionNode read GetRight;
property Right: IAstNode read GetRight;
end;
IUnaryExpressionNode = interface(IExpressionNode)
IUnaryExpressionNode = interface(IAstNode)
{$region 'private'}
function GetOperator: TUnaryOperator;
function GetRight: IExpressionNode;
function GetRight: IAstNode;
{$endregion}
property Operator: TUnaryOperator read GetOperator;
property Right: IExpressionNode read GetRight;
property Right: IAstNode read GetRight;
end;
// Represents an if-statement for control flow.
IIfExpressionNode = interface(IExpressionNode)
IIfExpressionNode = interface(IAstNode)
{$region 'private'}
function GetCondition: IExpressionNode;
function GetThenBranch: IExpressionNode;
function GetElseBranch: IExpressionNode;
function GetCondition: IAstNode;
function GetThenBranch: IAstNode;
function GetElseBranch: IAstNode;
{$endregion}
property Condition: IExpressionNode read GetCondition;
property ThenBranch: IExpressionNode read GetThenBranch;
property ElseBranch: IExpressionNode read GetElseBranch;
property Condition: IAstNode read GetCondition;
property ThenBranch: IAstNode read GetThenBranch;
property ElseBranch: IAstNode read GetElseBranch;
end;
// Represents a ternary expression (e.g., condition ? true_expr : false_expr) for value selection.
ITernaryExpressionNode = interface(IExpressionNode)
ITernaryExpressionNode = interface(IAstNode)
{$region 'private'}
function GetCondition: IExpressionNode;
function GetThenBranch: IExpressionNode;
function GetElseBranch: IExpressionNode;
function GetCondition: IAstNode;
function GetThenBranch: IAstNode;
function GetElseBranch: IAstNode;
{$endregion}
property Condition: IExpressionNode read GetCondition;
property ThenBranch: IExpressionNode read GetThenBranch;
property ElseBranch: IExpressionNode read GetElseBranch;
property Condition: IAstNode read GetCondition;
property ThenBranch: IAstNode read GetThenBranch;
property ElseBranch: IAstNode read GetElseBranch;
end;
ILambdaExpressionNode = interface(IExpressionNode)
ILambdaExpressionNode = interface(IAstNode)
{$region 'private'}
function GetParameters: TArray<IIdentifierNode>;
function GetBody: IExpressionNode;
function GetBody: IAstNode;
function GetScopeDescriptor: IScopeDescriptor;
function GetUpvalues: TArray<TResolvedAddress>;
{$endregion}
property Parameters: TArray<IIdentifierNode> read GetParameters;
property Body: IExpressionNode read GetBody;
property Body: IAstNode read GetBody;
property ScopeDescriptor: IScopeDescriptor read GetScopeDescriptor;
property Upvalues: TArray<TResolvedAddress> read GetUpvalues;
end;
IFunctionCallNode = interface(IExpressionNode)
IFunctionCallNode = interface(IAstNode)
{$region 'private'}
function GetCallee: IExpressionNode;
function GetArguments: TList<IExpressionNode>;
function GetCallee: IAstNode;
function GetArguments: TList<IAstNode>;
{$endregion}
property Callee: IExpressionNode read GetCallee;
property Arguments: TList<IExpressionNode> read GetArguments;
property Callee: IAstNode read GetCallee;
property Arguments: TList<IAstNode> read GetArguments;
end;
IBlockExpressionNode = interface(IExpressionNode)
IBlockExpressionNode = interface(IAstNode)
{$region 'private'}
function GetExpressions: TList<IExpressionNode>;
function GetExpressions: TList<IAstNode>;
{$endregion}
property Expressions: TList<IExpressionNode> read GetExpressions;
property Expressions: TList<IAstNode> read GetExpressions;
end;
IVariableDeclarationNode = interface(IExpressionNode)
IVariableDeclarationNode = interface(IAstNode)
{$region 'private'}
function GetIdentifier: IIdentifierNode;
function GetInitializer: IExpressionNode;
function GetInitializer: IAstNode;
{$endregion}
property Identifier: IIdentifierNode read GetIdentifier;
property Initializer: IExpressionNode read GetInitializer;
property Initializer: IAstNode read GetInitializer;
end;
IAssignmentNode = interface(IExpressionNode)
IAssignmentNode = interface(IAstNode)
{$region 'private'}
function GetIdentifier: IIdentifierNode;
function GetValue: IExpressionNode;
function GetValue: IAstNode;
{$endregion}
property Identifier: IIdentifierNode read GetIdentifier;
property Value: IExpressionNode read GetValue;
property Value: IAstNode read GetValue;
end;
// Represents an index access expression (e.g., series[index]).
IIndexerNode = interface(IExpressionNode)
IIndexerNode = interface(IAstNode)
{$region 'private'}
function GetBase: IExpressionNode;
function GetIndex: IExpressionNode;
function GetBase: IAstNode;
function GetIndex: IAstNode;
{$endregion}
property Base: IExpressionNode read GetBase;
property Index: IExpressionNode read GetIndex;
property Base: IAstNode read GetBase;
property Index: IAstNode read GetIndex;
end;
// Represents a member access expression (e.g., series.field or record.field).
IMemberAccessNode = interface(IExpressionNode)
IMemberAccessNode = interface(IAstNode)
{$region 'private'}
function GetBase: IExpressionNode;
function GetBase: IAstNode;
function GetMember: IIdentifierNode;
{$endregion}
property Base: IExpressionNode read GetBase;
property Base: IAstNode read GetBase;
property Member: IIdentifierNode read GetMember;
end;
// Represents creating a new, empty series (e.g., new series(int)).
ICreateSeriesNode = interface(IExpressionNode)
ICreateSeriesNode = interface(IAstNode)
{$region 'private'}
function GetDefinition: String;
{$endregion}
@@ -311,18 +307,18 @@ type
end;
// Represents adding a value to a series (e.g., my_series.add(value, 100)).
IAddSeriesItemNode = interface(IExpressionNode)
IAddSeriesItemNode = interface(IAstNode)
{$region 'private'}
function GetSeries: IIdentifierNode;
function GetValue: IExpressionNode;
function GetLookback: IExpressionNode;
function GetValue: IAstNode;
function GetLookback: IAstNode;
{$endregion}
property Series: IIdentifierNode read GetSeries;
property Value: IExpressionNode read GetValue;
property Lookback: IExpressionNode read GetLookback;
property Value: IAstNode read GetValue;
property Lookback: IAstNode read GetLookback;
end;
ISeriesLengthNode = interface(IExpressionNode)
ISeriesLengthNode = interface(IAstNode)
{$region 'private'}
function GetSeries: IIdentifierNode;
{$endregion}
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -182,7 +182,7 @@ end;
function TPrettyPrintVisitor.VisitFunctionCall(const Node: IFunctionCallNode): TAstValue;
var
arg: IExpressionNode;
arg: IAstNode;
begin
AppendLine('FunctionCall');
Indent;
@@ -201,7 +201,7 @@ end;
function TPrettyPrintVisitor.VisitBlockExpression(const Node: IBlockExpressionNode): TAstValue;
var
expr: IExpressionNode;
expr: IAstNode;
begin
AppendLine('Block');
Indent;
+102
View File
@@ -0,0 +1,102 @@
unit Myc.Ast.ViewModel;
interface
uses
System.SysUtils,
System.Types,
System.Generics.Collections,
Myc.Ast.Nodes;
type
// An enum to identify the specific type of an AST node without relying on RTTI or GUIDs.
// This allows the renderer to depend only on the ViewModel layer.
TAstNodeType = (
antUndefined,
antConstant,
antIdentifier,
antBinaryExpression,
antUnaryExpression,
antIfExpression,
antTernaryExpression,
antLambdaExpression,
antFunctionCall,
antBlockExpression,
antVariableDeclaration,
antAssignment,
antIndexer,
antMemberAccess,
antCreateSeries,
antAddSeriesItem,
antSeriesLength
);
// A unique, stable identifier for a visual node instance.
TViewModelID = NativeInt;
// Defines how a view model displays its children.
TVisualizationMode = (vmSyntactic, vmSemantic);
// Metadata tied to a logical IAstNode. All visual instances of this node will share this data.
TLogicalMetadata = record
// The concrete type of the IAstNode, used for type-safe operations in the renderer.
NodeType: TAstNodeType;
Description: String;
end;
// Metadata tied to a specific visual instance of a node, identified by its TViewModelID.
TVisualInstanceMetadata = record
PositionOverride: TPointF;
IsCollapsed: Boolean;
VisualizationMode: TVisualizationMode;
end;
// The ViewModel for a node in the visual graph.
TVisualNodeViewModel = class
private
FId: TViewModelID;
FNode: IAstNode;
FLogicalMetadata: TLogicalMetadata;
FParents: TList<TVisualNodeViewModel>;
FChildren: TList<TVisualNodeViewModel>;
public
constructor Create(const ANode: IAstNode; const AId: TViewModelID; const ALogicalMetadata: TLogicalMetadata);
destructor Destroy; override;
procedure AddChild(const AChild: TVisualNodeViewModel);
property Id: TViewModelID read FId;
property Node: IAstNode read FNode;
property LogicalMetadata: TLogicalMetadata read FLogicalMetadata;
property Children: TList<TVisualNodeViewModel> read FChildren;
property Parents: TList<TVisualNodeViewModel> read FParents;
end;
implementation
{ TVisualNodeViewModel }
constructor TVisualNodeViewModel.Create(const ANode: IAstNode; const AId: TViewModelID; const ALogicalMetadata: TLogicalMetadata);
begin
inherited Create;
FNode := ANode;
FId := AId;
FLogicalMetadata := ALogicalMetadata;
FParents := TList<TVisualNodeViewModel>.Create;
FChildren := TList<TVisualNodeViewModel>.Create;
end;
destructor TVisualNodeViewModel.Destroy;
begin
FParents.Free;
FChildren.Free;
inherited;
end;
procedure TVisualNodeViewModel.AddChild(const AChild: TVisualNodeViewModel);
begin
FChildren.Add(AChild);
AChild.FParents.Add(Self);
end;
end.
+120 -134
View File
@@ -17,38 +17,28 @@ type
// --- Existing factory functions ---
class function Constant(AValue: TScalar): IConstantNode; static;
class function Identifier(AName: string): IIdentifierNode; static;
class function BinaryExpr(
ALeft: IExpressionNode;
AOperator: TBinaryOperator;
ARight: IExpressionNode
): IBinaryExpressionNode; static;
class function UnaryExpr(const AOperator: TUnaryOperator; const ARight: IExpressionNode): IUnaryExpressionNode; static;
class function IfExpr(
const ACondition: IExpressionNode;
const AThenBranch, AElseBranch: IExpressionNode
): IIfExpressionNode; static;
class function TernaryExpr(
const ACondition: IExpressionNode;
const AThenBranch, AElseBranch: IExpressionNode
): ITernaryExpressionNode; static;
class function LambdaExpr(const AParameters: TArray<IIdentifierNode>; const ABody: IExpressionNode): ILambdaExpressionNode; static;
class function FunctionCall(const ACallee: IExpressionNode; const AArguments: array of IExpressionNode): IFunctionCallNode; static;
class function Block(const AExpressions: array of IExpressionNode): IBlockExpressionNode; static;
class function VarDecl(const AIdentifier: IIdentifierNode; AInitializer: IExpressionNode): IVariableDeclarationNode; static;
class function Assign(const AIdentifier: IIdentifierNode; const AValue: IExpressionNode): IAssignmentNode; static;
class function AssignResult(const AValue: IExpressionNode): IAssignmentNode; static; deprecated;
class function Indexer(const ABase: IExpressionNode; const AIndex: IExpressionNode): IIndexerNode; static;
class function MemberAccess(const ABase: IExpressionNode; const AMember: IIdentifierNode): IMemberAccessNode; static;
class function BinaryExpr(ALeft: IAstNode; AOperator: TBinaryOperator; ARight: IAstNode): IBinaryExpressionNode; static;
class function UnaryExpr(const AOperator: TUnaryOperator; const ARight: IAstNode): IUnaryExpressionNode; static;
class function IfExpr(const ACondition: IAstNode; const AThenBranch, AElseBranch: IAstNode): IIfExpressionNode; static;
class function TernaryExpr(const ACondition: IAstNode; const AThenBranch, AElseBranch: IAstNode): ITernaryExpressionNode; static;
class function LambdaExpr(const AParameters: TArray<IIdentifierNode>; const ABody: IAstNode): ILambdaExpressionNode; static;
class function FunctionCall(const ACallee: IAstNode; const AArguments: array of IAstNode): IFunctionCallNode; static;
class function Block(const AExpressions: array of IAstNode): IBlockExpressionNode; static;
class function VarDecl(const AIdentifier: IIdentifierNode; AInitializer: IAstNode): IVariableDeclarationNode; static;
class function Assign(const AIdentifier: IIdentifierNode; const AValue: IAstNode): IAssignmentNode; static;
class function AssignResult(const AValue: IAstNode): IAssignmentNode; static; deprecated;
class function Indexer(const ABase: IAstNode; const AIndex: IAstNode): IIndexerNode; static;
class function MemberAccess(const ABase: IAstNode; const AMember: IIdentifierNode): IMemberAccessNode; static;
class function CreateSeries(const ADefinition: String): ICreateSeriesNode; static;
class function AddSeriesItem(
const ASeries: IIdentifierNode;
const AValue: IExpressionNode;
const ALookback: IExpressionNode = nil
const AValue: IAstNode;
const ALookback: IAstNode = nil
): IAddSeriesItemNode; static;
class function SeriesLength(const ASeries: IIdentifierNode): ISeriesLengthNode; static;
class function CreateDescriptor(const Scope: IExecutionScope): IScopeDescriptor; static;
class function Bind(const RootNode: IExpressionNode; const ParentScope: IExecutionScope): IExecutionScope; static;
class function Bind(const RootNode: IAstNode; const ParentScope: IExecutionScope): IExecutionScope; static;
end;
// TAstTraverser provides a default AST traversal implementation.
@@ -94,7 +84,7 @@ type
{ TAstNode }
// Common base class for AST nodes to reduce boilerplate.
TAstNode = class(TInterfacedObject, IExpressionNode)
TAstNode = class(TInterfacedObject, IAstNode)
public
function Accept(const Visitor: IAstVisitor): TAstValue; virtual; abstract;
end;
@@ -130,14 +120,14 @@ type
{ TBinaryExpressionNode }
TBinaryExpressionNode = class(TAstNode, IBinaryExpressionNode)
private
FLeft: IExpressionNode;
FLeft: IAstNode;
FOperator: TBinaryOperator;
FRight: IExpressionNode;
function GetLeft: IExpressionNode;
FRight: IAstNode;
function GetLeft: IAstNode;
function GetOperator: TBinaryOperator;
function GetRight: IExpressionNode;
function GetRight: IAstNode;
public
constructor Create(ALeft: IExpressionNode; AOperator: TBinaryOperator; ARight: IExpressionNode);
constructor Create(ALeft: IAstNode; AOperator: TBinaryOperator; ARight: IAstNode);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
@@ -145,39 +135,39 @@ type
TUnaryExpressionNode = class(TAstNode, IUnaryExpressionNode)
private
FOperator: TUnaryOperator;
FRight: IExpressionNode;
FRight: IAstNode;
function GetOperator: TUnaryOperator;
function GetRight: IExpressionNode;
function GetRight: IAstNode;
public
constructor Create(const AOperator: TUnaryOperator; const ARight: IExpressionNode);
constructor Create(const AOperator: TUnaryOperator; const ARight: IAstNode);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
{ TIfExpressionNode }
TIfExpressionNode = class(TAstNode, IIfExpressionNode)
private
FCondition: IExpressionNode;
FThenBranch: IExpressionNode;
FElseBranch: IExpressionNode;
function GetCondition: IExpressionNode;
function GetThenBranch: IExpressionNode;
function GetElseBranch: IExpressionNode;
FCondition: IAstNode;
FThenBranch: IAstNode;
FElseBranch: IAstNode;
function GetCondition: IAstNode;
function GetThenBranch: IAstNode;
function GetElseBranch: IAstNode;
public
constructor Create(const ACondition, AThenBranch, AElseBranch: IExpressionNode);
constructor Create(const ACondition, AThenBranch, AElseBranch: IAstNode);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
{ TTernaryExpressionNode }
TTernaryExpressionNode = class(TAstNode, ITernaryExpressionNode)
private
FCondition: IExpressionNode;
FThenBranch: IExpressionNode;
FElseBranch: IExpressionNode;
function GetCondition: IExpressionNode;
function GetThenBranch: IExpressionNode;
function GetElseBranch: IExpressionNode;
FCondition: IAstNode;
FThenBranch: IAstNode;
FElseBranch: IAstNode;
function GetCondition: IAstNode;
function GetThenBranch: IAstNode;
function GetElseBranch: IAstNode;
public
constructor Create(const ACondition, AThenBranch, AElseBranch: IExpressionNode);
constructor Create(const ACondition, AThenBranch, AElseBranch: IAstNode);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
@@ -185,15 +175,15 @@ type
TLambdaExpressionNode = class(TAstNode, ILambdaExpressionNode)
private
FParameters: TArray<IIdentifierNode>;
FBody: IExpressionNode;
FBody: IAstNode;
FScopeDescriptor: IScopeDescriptor;
FUpvalues: TArray<TResolvedAddress>;
function GetParameters: TArray<IIdentifierNode>;
function GetBody: IExpressionNode;
function GetBody: IAstNode;
function GetScopeDescriptor: IScopeDescriptor;
function GetUpvalues: TArray<TResolvedAddress>;
public
constructor Create(const AParameters: TArray<IIdentifierNode>; const ABody: IExpressionNode);
constructor Create(const AParameters: TArray<IIdentifierNode>; const ABody: IAstNode);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
property ScopeDescriptor: IScopeDescriptor read GetScopeDescriptor;
end;
@@ -201,12 +191,12 @@ type
{ TFunctionCallNode }
TFunctionCallNode = class(TAstNode, IFunctionCallNode)
private
FCallee: IExpressionNode;
FArguments: TList<IExpressionNode>;
function GetCallee: IExpressionNode;
function GetArguments: TList<IExpressionNode>;
FCallee: IAstNode;
FArguments: TList<IAstNode>;
function GetCallee: IAstNode;
function GetArguments: TList<IAstNode>;
public
constructor Create(const ACallee: IExpressionNode; const AArguments: array of IExpressionNode);
constructor Create(const ACallee: IAstNode; const AArguments: array of IAstNode);
destructor Destroy; override;
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
@@ -214,10 +204,10 @@ type
{ TBlockExpressionNode }
TBlockExpressionNode = class(TAstNode, IBlockExpressionNode)
private
FExpressions: TList<IExpressionNode>;
function GetExpressions: TList<IExpressionNode>;
FExpressions: TList<IAstNode>;
function GetExpressions: TList<IAstNode>;
public
constructor Create(const AExpressions: array of IExpressionNode);
constructor Create(const AExpressions: array of IAstNode);
destructor Destroy; override;
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
@@ -226,11 +216,11 @@ type
TVariableDeclarationNode = class(TAstNode, IVariableDeclarationNode)
private
FIdentifier: IIdentifierNode;
FInitializer: IExpressionNode;
FInitializer: IAstNode;
function GetIdentifier: IIdentifierNode;
function GetInitializer: IExpressionNode;
function GetInitializer: IAstNode;
public
constructor Create(const AIdentifier: IIdentifierNode; AInitializer: IExpressionNode);
constructor Create(const AIdentifier: IIdentifierNode; AInitializer: IAstNode);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
@@ -238,35 +228,35 @@ type
TAssignmentNode = class(TAstNode, IAssignmentNode)
private
FIdentifier: IIdentifierNode;
FValue: IExpressionNode;
FValue: IAstNode;
function GetIdentifier: IIdentifierNode;
function GetValue: IExpressionNode;
function GetValue: IAstNode;
public
constructor Create(const AIdentifier: IIdentifierNode; const AValue: IExpressionNode);
constructor Create(const AIdentifier: IIdentifierNode; const AValue: IAstNode);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
{ TIndexerNode }
TIndexerNode = class(TAstNode, IIndexerNode)
private
FBase: IExpressionNode;
FIndex: IExpressionNode;
function GetBase: IExpressionNode;
function GetIndex: IExpressionNode;
FBase: IAstNode;
FIndex: IAstNode;
function GetBase: IAstNode;
function GetIndex: IAstNode;
public
constructor Create(const ABase: IExpressionNode; const AIndex: IExpressionNode);
constructor Create(const ABase: IAstNode; const AIndex: IAstNode);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
{ TMemberAccessNode }
TMemberAccessNode = class(TAstNode, IMemberAccessNode)
private
FBase: IExpressionNode;
FBase: IAstNode;
FMember: IIdentifierNode;
function GetBase: IExpressionNode;
function GetBase: IAstNode;
function GetMember: IIdentifierNode;
public
constructor Create(const ABase: IExpressionNode; const AMember: IIdentifierNode);
constructor Create(const ABase: IAstNode; const AMember: IIdentifierNode);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
@@ -284,13 +274,13 @@ type
TAddSeriesItemNode = class(TAstNode, IAddSeriesItemNode)
private
FSeries: IIdentifierNode;
FValue: IExpressionNode;
FLookback: IExpressionNode;
FValue: IAstNode;
FLookback: IAstNode;
function GetSeries: IIdentifierNode;
function GetValue: IExpressionNode;
function GetLookback: IExpressionNode;
function GetValue: IAstNode;
function GetLookback: IAstNode;
public
constructor Create(const ASeries: IIdentifierNode; const AValue: IExpressionNode; const ALookback: IExpressionNode);
constructor Create(const ASeries: IIdentifierNode; const AValue: IAstNode; const ALookback: IAstNode);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
@@ -459,7 +449,7 @@ end;
{ TBinaryExpressionNode }
constructor TBinaryExpressionNode.Create(ALeft: IExpressionNode; AOperator: TBinaryOperator; ARight: IExpressionNode);
constructor TBinaryExpressionNode.Create(ALeft: IAstNode; AOperator: TBinaryOperator; ARight: IAstNode);
begin
inherited Create;
FLeft := ALeft;
@@ -472,7 +462,7 @@ begin
Result := Visitor.VisitBinaryExpression(Self);
end;
function TBinaryExpressionNode.GetLeft: IExpressionNode;
function TBinaryExpressionNode.GetLeft: IAstNode;
begin
Result := FLeft;
end;
@@ -482,14 +472,14 @@ begin
Result := FOperator;
end;
function TBinaryExpressionNode.GetRight: IExpressionNode;
function TBinaryExpressionNode.GetRight: IAstNode;
begin
Result := FRight;
end;
{ TUnaryExpressionNode }
constructor TUnaryExpressionNode.Create(const AOperator: TUnaryOperator; const ARight: IExpressionNode);
constructor TUnaryExpressionNode.Create(const AOperator: TUnaryOperator; const ARight: IAstNode);
begin
inherited Create;
FOperator := AOperator;
@@ -506,14 +496,14 @@ begin
Result := FOperator;
end;
function TUnaryExpressionNode.GetRight: IExpressionNode;
function TUnaryExpressionNode.GetRight: IAstNode;
begin
Result := FRight;
end;
{ TIfExpressionNode }
constructor TIfExpressionNode.Create(const ACondition, AThenBranch, AElseBranch: IExpressionNode);
constructor TIfExpressionNode.Create(const ACondition, AThenBranch, AElseBranch: IAstNode);
begin
inherited Create;
FCondition := ACondition;
@@ -526,24 +516,24 @@ begin
Result := Visitor.VisitIfExpression(Self);
end;
function TIfExpressionNode.GetCondition: IExpressionNode;
function TIfExpressionNode.GetCondition: IAstNode;
begin
Result := FCondition;
end;
function TIfExpressionNode.GetElseBranch: IExpressionNode;
function TIfExpressionNode.GetElseBranch: IAstNode;
begin
Result := FElseBranch;
end;
function TIfExpressionNode.GetThenBranch: IExpressionNode;
function TIfExpressionNode.GetThenBranch: IAstNode;
begin
Result := FThenBranch;
end;
{ TTernaryExpressionNode }
constructor TTernaryExpressionNode.Create(const ACondition, AThenBranch, AElseBranch: IExpressionNode);
constructor TTernaryExpressionNode.Create(const ACondition, AThenBranch, AElseBranch: IAstNode);
begin
inherited Create;
FCondition := ACondition;
@@ -556,24 +546,24 @@ begin
Result := Visitor.VisitTernaryExpression(Self);
end;
function TTernaryExpressionNode.GetCondition: IExpressionNode;
function TTernaryExpressionNode.GetCondition: IAstNode;
begin
Result := FCondition;
end;
function TTernaryExpressionNode.GetElseBranch: IExpressionNode;
function TTernaryExpressionNode.GetElseBranch: IAstNode;
begin
Result := FElseBranch;
end;
function TTernaryExpressionNode.GetThenBranch: IExpressionNode;
function TTernaryExpressionNode.GetThenBranch: IAstNode;
begin
Result := FThenBranch;
end;
{ TLambdaExpressionNode }
constructor TLambdaExpressionNode.Create(const AParameters: TArray<IIdentifierNode>; const ABody: IExpressionNode);
constructor TLambdaExpressionNode.Create(const AParameters: TArray<IIdentifierNode>; const ABody: IAstNode);
begin
inherited Create;
FBody := ABody;
@@ -591,7 +581,7 @@ begin
Result := Visitor.VisitLambdaExpression(Self);
end;
function TLambdaExpressionNode.GetBody: IExpressionNode;
function TLambdaExpressionNode.GetBody: IAstNode;
begin
Result := FBody;
end;
@@ -608,13 +598,13 @@ end;
{ TFunctionCallNode }
constructor TFunctionCallNode.Create(const ACallee: IExpressionNode; const AArguments: array of IExpressionNode);
constructor TFunctionCallNode.Create(const ACallee: IAstNode; const AArguments: array of IAstNode);
var
arg: IExpressionNode;
arg: IAstNode;
begin
inherited Create;
FCallee := ACallee;
FArguments := TList<IExpressionNode>.Create;
FArguments := TList<IAstNode>.Create;
for arg in AArguments do
FArguments.Add(arg);
end;
@@ -630,24 +620,24 @@ begin
Result := Visitor.VisitFunctionCall(Self);
end;
function TFunctionCallNode.GetArguments: TList<IExpressionNode>;
function TFunctionCallNode.GetArguments: TList<IAstNode>;
begin
Result := FArguments;
end;
function TFunctionCallNode.GetCallee: IExpressionNode;
function TFunctionCallNode.GetCallee: IAstNode;
begin
Result := FCallee;
end;
{ TBlockExpressionNode }
constructor TBlockExpressionNode.Create(const AExpressions: array of IExpressionNode);
constructor TBlockExpressionNode.Create(const AExpressions: array of IAstNode);
var
expr: IExpressionNode;
expr: IAstNode;
begin
inherited Create;
FExpressions := TList<IExpressionNode>.Create;
FExpressions := TList<IAstNode>.Create;
for expr in AExpressions do
FExpressions.Add(expr);
end;
@@ -663,14 +653,14 @@ begin
Result := Visitor.VisitBlockExpression(Self);
end;
function TBlockExpressionNode.GetExpressions: TList<IExpressionNode>;
function TBlockExpressionNode.GetExpressions: TList<IAstNode>;
begin
Result := FExpressions;
end;
{ TVariableDeclarationNode }
constructor TVariableDeclarationNode.Create(const AIdentifier: IIdentifierNode; AInitializer: IExpressionNode);
constructor TVariableDeclarationNode.Create(const AIdentifier: IIdentifierNode; AInitializer: IAstNode);
begin
inherited Create;
FIdentifier := AIdentifier;
@@ -687,14 +677,14 @@ begin
Result := FIdentifier;
end;
function TVariableDeclarationNode.GetInitializer: IExpressionNode;
function TVariableDeclarationNode.GetInitializer: IAstNode;
begin
Result := FInitializer;
end;
{ TAssignmentNode }
constructor TAssignmentNode.Create(const AIdentifier: IIdentifierNode; const AValue: IExpressionNode);
constructor TAssignmentNode.Create(const AIdentifier: IIdentifierNode; const AValue: IAstNode);
begin
inherited Create;
FIdentifier := AIdentifier;
@@ -711,14 +701,14 @@ begin
Result := FIdentifier;
end;
function TAssignmentNode.GetValue: IExpressionNode;
function TAssignmentNode.GetValue: IAstNode;
begin
Result := FValue;
end;
{ TIndexerNode }
constructor TIndexerNode.Create(const ABase, AIndex: IExpressionNode);
constructor TIndexerNode.Create(const ABase, AIndex: IAstNode);
begin
inherited Create;
FBase := ABase;
@@ -730,19 +720,19 @@ begin
Result := Visitor.VisitIndexer(Self);
end;
function TIndexerNode.GetBase: IExpressionNode;
function TIndexerNode.GetBase: IAstNode;
begin
Result := FBase;
end;
function TIndexerNode.GetIndex: IExpressionNode;
function TIndexerNode.GetIndex: IAstNode;
begin
Result := FIndex;
end;
{ TMemberAccessNode }
constructor TMemberAccessNode.Create(const ABase: IExpressionNode; const AMember: IIdentifierNode);
constructor TMemberAccessNode.Create(const ABase: IAstNode; const AMember: IIdentifierNode);
begin
inherited Create;
FBase := ABase;
@@ -754,7 +744,7 @@ begin
Result := Visitor.VisitMemberAccess(Self);
end;
function TMemberAccessNode.GetBase: IExpressionNode;
function TMemberAccessNode.GetBase: IAstNode;
begin
Result := FBase;
end;
@@ -784,7 +774,7 @@ end;
{ TAddSeriesItemNode }
constructor TAddSeriesItemNode.Create(const ASeries: IIdentifierNode; const AValue, ALookback: IExpressionNode);
constructor TAddSeriesItemNode.Create(const ASeries: IIdentifierNode; const AValue, ALookback: IAstNode);
begin
inherited Create;
FSeries := ASeries;
@@ -797,7 +787,7 @@ begin
Result := Visitor.VisitAddSeriesItem(Self);
end;
function TAddSeriesItemNode.GetLookback: IExpressionNode;
function TAddSeriesItemNode.GetLookback: IAstNode;
begin
Result := FLookback;
end;
@@ -807,7 +797,7 @@ begin
Result := FSeries;
end;
function TAddSeriesItemNode.GetValue: IExpressionNode;
function TAddSeriesItemNode.GetValue: IAstNode;
begin
Result := FValue;
end;
@@ -968,7 +958,7 @@ end;
{ TAst }
class function TAst.Bind(const RootNode: IExpressionNode; const ParentScope: IExecutionScope): IExecutionScope;
class function TAst.Bind(const RootNode: IAstNode; const ParentScope: IExecutionScope): IExecutionScope;
var
binder: TBinder;
visitor: IAstVisitor;
@@ -990,26 +980,22 @@ begin
end;
end;
class function TAst.AddSeriesItem(
const ASeries: IIdentifierNode;
const AValue: IExpressionNode;
const ALookback: IExpressionNode
): IAddSeriesItemNode;
class function TAst.AddSeriesItem(const ASeries: IIdentifierNode; const AValue: IAstNode; const ALookback: IAstNode): IAddSeriesItemNode;
begin
Result := TAddSeriesItemNode.Create(ASeries, AValue, ALookback);
end;
class function TAst.Assign(const AIdentifier: IIdentifierNode; const AValue: IExpressionNode): IAssignmentNode;
class function TAst.Assign(const AIdentifier: IIdentifierNode; const AValue: IAstNode): IAssignmentNode;
begin
Result := TAssignmentNode.Create(AIdentifier, AValue);
end;
class function TAst.AssignResult(const AValue: IExpressionNode): IAssignmentNode;
class function TAst.AssignResult(const AValue: IAstNode): IAssignmentNode;
begin
Result := Assign(TAst.Identifier('Result'), AValue);
end;
class function TAst.Block(const AExpressions: array of IExpressionNode): IBlockExpressionNode;
class function TAst.Block(const AExpressions: array of IAstNode): IBlockExpressionNode;
begin
Result := TBlockExpressionNode.Create(AExpressions);
end;
@@ -1024,7 +1010,7 @@ begin
Result := TCreateSeriesNode.Create(ADefinition);
end;
class function TAst.BinaryExpr(ALeft: IExpressionNode; AOperator: TBinaryOperator; ARight: IExpressionNode): IBinaryExpressionNode;
class function TAst.BinaryExpr(ALeft: IAstNode; AOperator: TBinaryOperator; ARight: IAstNode): IBinaryExpressionNode;
begin
Result := TBinaryExpressionNode.Create(ALeft, AOperator, ARight);
end;
@@ -1040,7 +1026,7 @@ begin
Result := TScopeDescriptor.Create(nil);
end;
class function TAst.FunctionCall(const ACallee: IExpressionNode; const AArguments: array of IExpressionNode): IFunctionCallNode;
class function TAst.FunctionCall(const ACallee: IAstNode; const AArguments: array of IAstNode): IFunctionCallNode;
begin
Result := TFunctionCallNode.Create(ACallee, AArguments);
end;
@@ -1050,17 +1036,17 @@ begin
Result := TIdentifierNode.Create(AName);
end;
class function TAst.IfExpr(const ACondition: IExpressionNode; const AThenBranch, AElseBranch: IExpressionNode): IIfExpressionNode;
class function TAst.IfExpr(const ACondition: IAstNode; const AThenBranch, AElseBranch: IAstNode): IIfExpressionNode;
begin
Result := TIfExpressionNode.Create(ACondition, AThenBranch, AElseBranch);
end;
class function TAst.Indexer(const ABase, AIndex: IExpressionNode): IIndexerNode;
class function TAst.Indexer(const ABase, AIndex: IAstNode): IIndexerNode;
begin
Result := TIndexerNode.Create(ABase, AIndex);
end;
class function TAst.MemberAccess(const ABase: IExpressionNode; const AMember: IIdentifierNode): IMemberAccessNode;
class function TAst.MemberAccess(const ABase: IAstNode; const AMember: IIdentifierNode): IMemberAccessNode;
begin
Result := TMemberAccessNode.Create(ABase, AMember);
end;
@@ -1070,22 +1056,22 @@ begin
Result := TSeriesLengthNode.Create(ASeries);
end;
class function TAst.TernaryExpr(const ACondition: IExpressionNode; const AThenBranch, AElseBranch: IExpressionNode): ITernaryExpressionNode;
class function TAst.TernaryExpr(const ACondition: IAstNode; const AThenBranch, AElseBranch: IAstNode): ITernaryExpressionNode;
begin
Result := TTernaryExpressionNode.Create(ACondition, AThenBranch, AElseBranch);
end;
class function TAst.LambdaExpr(const AParameters: TArray<IIdentifierNode>; const ABody: IExpressionNode): ILambdaExpressionNode;
class function TAst.LambdaExpr(const AParameters: TArray<IIdentifierNode>; const ABody: IAstNode): ILambdaExpressionNode;
begin
Result := TLambdaExpressionNode.Create(AParameters, ABody);
end;
class function TAst.UnaryExpr(const AOperator: TUnaryOperator; const ARight: IExpressionNode): IUnaryExpressionNode;
class function TAst.UnaryExpr(const AOperator: TUnaryOperator; const ARight: IAstNode): IUnaryExpressionNode;
begin
Result := TUnaryExpressionNode.Create(AOperator, ARight);
end;
class function TAst.VarDecl(const AIdentifier: IIdentifierNode; AInitializer: IExpressionNode): IVariableDeclarationNode;
class function TAst.VarDecl(const AIdentifier: IIdentifierNode; AInitializer: IAstNode): IVariableDeclarationNode;
begin
Result := TVariableDeclarationNode.Create(AIdentifier, AInitializer);
end;
@@ -1114,7 +1100,7 @@ end;
function TAstTraverser.VisitBlockExpression(const Node: IBlockExpressionNode): TAstValue;
var
expr: IExpressionNode;
expr: IAstNode;
begin
for expr in Node.Expressions do
expr.Accept(Self);
@@ -1130,7 +1116,7 @@ end;
function TAstTraverser.VisitFunctionCall(const Node: IFunctionCallNode): TAstValue;
var
arg: IExpressionNode;
arg: IAstNode;
begin
Node.Callee.Accept(Self);
for arg in Node.Arguments do
+2 -2
View File
@@ -6,6 +6,7 @@ uses
System.SysUtils,
System.Rtti,
System.TypInfo,
System.JSON,
Myc.Data.Scalar;
type
@@ -24,8 +25,7 @@ implementation
uses
Myc.Data.Decimal,
System.Generics.Collections,
System.JSON;
System.Generics.Collections;
{ TRttiAstHelper }