Adding Pipes

This commit is contained in:
Michael Schimmel
2025-12-18 11:21:18 +01:00
parent 0b015fe4e7
commit 8bde31a478
10 changed files with 865 additions and 31 deletions
+189 -3
View File
@@ -141,7 +141,11 @@ type
akAddSeriesItem,
akSeriesLength,
akRecur,
akNop
akNop,
// Pipes
akPipe,
akPipeInput,
akPipeInputList
);
TAstNodeKindHelper = record helper for TAstNodeKind
@@ -452,10 +456,38 @@ type
property Series: IIdentifierNode read GetSeries;
end;
// Represents a single input definition within a pipe: "btc [:Close]"
// - StreamSource: The identifier of the stream (e.g., "btc")
// - Selector: Optional keyword path to select a field (e.g., :Close)
IPipeInputNode = interface(IAstTypedNode)
{$region 'private'}
function GetStreamSource: IIdentifierNode;
function GetSelector: IKeywordNode; // Can be nil if the whole record is used
{$endregion}
property StreamSource: IIdentifierNode read GetStreamSource;
property Selector: IKeywordNode read GetSelector;
end;
// A list of pipe inputs
IPipeInputList = interface(INodeList<IPipeInputNode>)
end;
// The main pipe definition: (pipe [inputs...] (fn ...))
IPipeNode = interface(IAstTypedNode)
{$region 'private'}
function GetInputs: IPipeInputList;
function GetTransformation: ILambdaExpressionNode;
{$endregion}
property Inputs: IPipeInputList read GetInputs;
property Transformation: ILambdaExpressionNode read GetTransformation;
end;
IParameterList = interface(INodeList<IIdentifierNode>)
end;
IArgumentList = interface(INodeList<IAstNode>)
end;
IExpressionList = interface(INodeList<IAstNode>)
end;
@@ -491,6 +523,12 @@ type
function VisitSeriesLength(const Node: ISeriesLengthNode): TDataValue;
function VisitRecurNode(const Node: IRecurNode): TDataValue;
function VisitNop(const Node: INopNode): TDataValue;
// Pipes
// Not implemented yet. that's ok, it just needs to compile
// function VisitPipeInput(const Node: IPipeInputNode): TDataValue;
// function VisitPipeInputList(const Node: IPipeInputList): TDataValue;
// function VisitPipe(const Node: IPipeNode): TDataValue;
end;
IEvaluatorVisitor = interface(IAstVisitor)
@@ -502,9 +540,10 @@ type
TAstNode = class(TInterfacedObject, IAstNode)
private
FIdentity: IAstIdentity;
function GetIdentity: IAstIdentity;
protected
function GetKind: TAstNodeKind; virtual; abstract;
function GetIsTyped: Boolean; virtual;
function GetIdentity: IAstIdentity;
public
constructor Create(const AIdentity: IAstIdentity);
function Accept(const Visitor: IAstVisitor): TDataValue; virtual; abstract;
@@ -551,6 +590,7 @@ type
private
FStaticType: IStaticType;
function GetStaticType: IStaticType;
protected
function GetIsTyped: Boolean; override;
public
constructor Create(const AStaticType: IStaticType; const AIdentity: IAstIdentity);
@@ -617,6 +657,7 @@ type
FValue: IAstNode;
function GetKey: IKeywordNode;
function GetValue: IAstNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const AKey: IKeywordNode; const AValue: IAstNode; const AIdentity: IAstIdentity);
@@ -638,6 +679,7 @@ type
private
FConstIdentity: IConstantIdentity; // Typed reference for fast access
function GetValue: TDataValue;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const AIdentity: IConstantIdentity; const AStaticType: IStaticType);
@@ -651,6 +693,7 @@ type
FAddress: TResolvedAddress;
function GetAddress: TResolvedAddress;
function GetName: string;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const AIdentity: INamedIdentity; const AAddress: TResolvedAddress; const AStaticType: IStaticType);
@@ -662,6 +705,7 @@ type
private
FKeywordIdentity: IKeywordIdentity;
function GetValue: IKeyword;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const AIdentity: IKeywordIdentity);
@@ -673,6 +717,7 @@ type
private
FDefIdentity: IDefinitionIdentity;
function GetDefinition: String;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const AIdentity: IDefinitionIdentity; const AStaticType: IStaticType);
@@ -688,6 +733,7 @@ type
function GetCondition: IAstNode;
function GetThenBranch: IAstNode;
function GetElseBranch: IAstNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(
@@ -705,6 +751,7 @@ type
FElseBranch: IAstNode;
function GetPairs: TArray<TCondPair>;
function GetElseBranch: IAstNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(
@@ -732,6 +779,7 @@ type
function GetUpvalues: TArray<TResolvedAddress>;
function GetHasNestedLambdas: Boolean;
function GetIsPure: Boolean;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(
@@ -760,6 +808,7 @@ type
function GetIsTailCall: Boolean;
function GetStaticTarget: TDataValue.TFunc;
function GetIsTargetPure: Boolean;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(
@@ -783,6 +832,7 @@ type
function GetName: IIdentifierNode;
function GetParameters: IParameterList;
function GetBody: IAstNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(
@@ -801,6 +851,7 @@ type
FExpandedBody: IAstNode;
function GetCallNode: IFunctionCallNode;
function GetExpandedBody: IAstNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const ACallNode: IFunctionCallNode; const AExpandedBody: IAstNode; const AIdentity: IAstIdentity);
@@ -812,6 +863,7 @@ type
private
FExpression: IAstNode;
function GetExpression: IAstNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const AExpression: IAstNode; const AIdentity: IAstIdentity);
@@ -823,6 +875,7 @@ type
private
FExpression: IAstNode;
function GetExpression: IAstNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const AExpression: IAstNode; const AIdentity: IAstIdentity);
@@ -834,6 +887,7 @@ type
private
FExpression: IQuasiquoteNode;
function GetExpression: IQuasiquoteNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const AExpression: IQuasiquoteNode; const AIdentity: IAstIdentity);
@@ -845,6 +899,7 @@ type
private
FArguments: IArgumentList;
function GetArguments: IArgumentList;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const AArguments: IArgumentList; const AStaticType: IStaticType; const AIdentity: IAstIdentity);
@@ -856,6 +911,7 @@ type
private
FExpressions: IExpressionList;
function GetExpressions: IExpressionList;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const AExpressions: IExpressionList; const AStaticType: IStaticType; const AIdentity: IAstIdentity);
@@ -870,6 +926,7 @@ type
function GetTarget: IAstNode;
function GetInitializer: IAstNode;
function GetIsBoxed: Boolean;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(
@@ -888,6 +945,7 @@ type
FTarget, FValue: IAstNode;
function GetTarget: IAstNode;
function GetValue: IAstNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const ATarget, AValue: IAstNode; const AStaticType: IStaticType; const AIdentity: IAstIdentity);
@@ -900,6 +958,7 @@ type
FBase, FIndex: IAstNode;
function GetBase: IAstNode;
function GetIndex: IAstNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const ABase, AIndex: IAstNode; const AStaticType: IStaticType; const AIdentity: IAstIdentity);
@@ -913,6 +972,7 @@ type
FMember: IKeywordNode;
function GetBase: IAstNode;
function GetMember: IKeywordNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(
@@ -933,6 +993,7 @@ type
function GetFields: IRecordFieldList;
function GetGenericDefinition: IGenericRecordDefinition;
function GetScalarDefinition: IScalarRecordDefinition;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(
@@ -953,6 +1014,7 @@ type
function GetSeries: IIdentifierNode;
function GetValue: IAstNode;
function GetLookback: IAstNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(
@@ -969,6 +1031,7 @@ type
private
FSeries: IIdentifierNode;
function GetSeries: IIdentifierNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const ASeries: IIdentifierNode; const AStaticType: IStaticType; const AIdentity: IAstIdentity);
@@ -977,7 +1040,7 @@ type
end;
TNopNode = class(TAstTypedNode, INopNode)
private
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(const AStaticType: IStaticType; const AIdentity: IAstIdentity);
@@ -985,6 +1048,49 @@ type
function AsNop: INopNode; override;
end;
TPipeInputNode = class(TAstTypedNode, IPipeInputNode)
private
FStreamSource: IIdentifierNode;
FSelector: IKeywordNode;
function GetStreamSource: IIdentifierNode;
function GetSelector: IKeywordNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(
const AStreamSource: IIdentifierNode;
const ASelector: IKeywordNode;
const AIdentity: IAstIdentity;
const AStaticType: IStaticType
);
function Accept(const Visitor: IAstVisitor): TDataValue; override;
end;
TPipeInputList = class(TAstNodeList<IPipeInputNode>, IPipeInputList)
protected
function GetKind: TAstNodeKind; override;
public
function Accept(const Visitor: IAstVisitor): TDataValue; override;
end;
TPipeNode = class(TAstTypedNode, IPipeNode)
private
FInputs: IPipeInputList;
FTransformation: ILambdaExpressionNode;
function GetInputs: IPipeInputList;
function GetTransformation: ILambdaExpressionNode;
protected
function GetKind: TAstNodeKind; override;
public
constructor Create(
const AInputs: IPipeInputList;
const ATransformation: ILambdaExpressionNode;
const AIdentity: IAstIdentity;
const AStaticType: IStaticType
);
function Accept(const Visitor: IAstVisitor): TDataValue; override;
end;
implementation
uses
@@ -2260,4 +2366,84 @@ begin
Inc(FIndex);
end;
{ TPipeInputNode }
constructor TPipeInputNode.Create(
const AStreamSource: IIdentifierNode;
const ASelector: IKeywordNode;
const AIdentity: IAstIdentity;
const AStaticType: IStaticType
);
begin
inherited Create(AStaticType, AIdentity);
FStreamSource := AStreamSource;
FSelector := ASelector;
end;
function TPipeInputNode.Accept(const Visitor: IAstVisitor): TDataValue;
begin
// Result := Visitor.VisitPipeInput(Self);
end;
function TPipeInputNode.GetKind: TAstNodeKind;
begin
Result := akPipeInput;
end;
function TPipeInputNode.GetSelector: IKeywordNode;
begin
Result := FSelector;
end;
function TPipeInputNode.GetStreamSource: IIdentifierNode;
begin
Result := FStreamSource;
end;
{ TPipeInputList }
function TPipeInputList.Accept(const Visitor: IAstVisitor): TDataValue;
begin
// Result := Visitor.VisitPipeInputList(Self);
end;
function TPipeInputList.GetKind: TAstNodeKind;
begin
Result := akPipeInputList;
end;
{ TPipeNode }
constructor TPipeNode.Create(
const AInputs: IPipeInputList;
const ATransformation: ILambdaExpressionNode;
const AIdentity: IAstIdentity;
const AStaticType: IStaticType
);
begin
inherited Create(AStaticType, AIdentity);
FInputs := AInputs;
FTransformation := ATransformation;
end;
function TPipeNode.Accept(const Visitor: IAstVisitor): TDataValue;
begin
// Result := Visitor.VisitPipe(Self);
end;
function TPipeNode.GetInputs: IPipeInputList;
begin
Result := FInputs;
end;
function TPipeNode.GetKind: TAstNodeKind;
begin
Result := akPipe;
end;
function TPipeNode.GetTransformation: ILambdaExpressionNode;
begin
Result := FTransformation;
end;
end.