Sample SMA strategy

This commit is contained in:
Michael Schimmel
2025-09-03 19:21:31 +02:00
parent 6b9dcee417
commit 696fb2f9a0
8 changed files with 709 additions and 204 deletions
+228 -4
View File
@@ -33,6 +33,9 @@ type
function VisitAssignment(const Node: IAssignmentNode): TAstValue; virtual;
function VisitIndexer(const Node: IIndexerNode): TAstValue; virtual;
function VisitMemberAccess(const Node: IMemberAccessNode): TAstValue; virtual;
function VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue; virtual;
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue; virtual;
function VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue;
end;
// TDebugEvaluatorVisitor now overrides all visit methods for full tracing
@@ -63,6 +66,8 @@ type
function VisitAssignment(const Node: IAssignmentNode): TAstValue; override;
function VisitIndexer(const Node: IIndexerNode): TAstValue; override;
function VisitMemberAccess(const Node: IMemberAccessNode): TAstValue; override;
function VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue; override;
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue; override;
end;
// Registers all native core functions in the given scope.
@@ -71,7 +76,9 @@ procedure RegisterNativeFunctions(const AScope: IExecutionScope);
implementation
uses
System.TypInfo,
Myc.Data.Decimal,
Myc.Data.Series,
Myc.Ast.Scope,
Myc.Ast.Printer,
Myc.Data.Scalar.JSON; // Added for JsonToRecordDefinition
@@ -105,6 +112,61 @@ type
property Method: TNativeFunction read FMethod;
end;
// --- Helper Functions ---
function ScalarKindToString(AKind: TScalarKind): string;
begin
case AKind of
skInteger: Result := 'integer';
skInt64: Result := 'int64';
skUInt64: Result := 'uint64';
skSingle: Result := 'single';
skDouble: Result := 'double';
skDateTime: Result := 'datetime';
skTimestamp: Result := 'timestamp';
skBoolean: Result := 'boolean';
skChar: Result := 'char';
skPChar: Result := 'pchar';
skString: Result := 'string';
skBytes: Result := 'bytes';
skDecimal: Result := 'decimal';
else
Result := 'unknown';
end;
end;
function StringToScalarKind(const AName: string): TScalarKind;
begin
if SameText(AName, 'integer') then
Result := skInteger
else if SameText(AName, 'int64') then
Result := skInt64
else if SameText(AName, 'uint64') then
Result := skUInt64
else if SameText(AName, 'single') then
Result := skSingle
else if SameText(AName, 'double') then
Result := skDouble
else if SameText(AName, 'datetime') then
Result := skDateTime
else if SameText(AName, 'timestamp') then
Result := skTimestamp
else if SameText(AName, 'boolean') then
Result := skBoolean
else if SameText(AName, 'char') then
Result := skChar
else if SameText(AName, 'pchar') then
Result := skPChar
else if SameText(AName, 'string') then
Result := skString
else if SameText(AName, 'bytes') then
Result := skBytes
else if SameText(AName, 'decimal') then
Result := skDecimal
else
raise EArgumentException.CreateFmt('Unknown scalar type name: "%s"', [AName]);
end;
// --- Native Functions Implementation ---
function NativeCreateRecordSeries(const Args: TArray<TAstValue>): TAstValue;
@@ -216,6 +278,66 @@ begin
end;
end;
function TEvaluatorVisitor.VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue;
var
itemValue, lookbackValue: TAstValue;
lookback: Int64;
varName: string;
seriesVar: TAstValue;
begin
// The target series is now guaranteed to be an identifier by the AST node definition.
varName := Node.Series.Name;
if not FScope.FindValue(varName, seriesVar) then
raise EArgumentException.CreateFmt('Identifier not found: "%s"', [varName]);
itemValue := Node.Value.Accept(Self);
lookback := -1; // Default: no lookback limit
if Assigned(Node.Lookback) then
begin
lookbackValue := Node.Lookback.Accept(Self);
if (lookbackValue.Kind <> avkScalar) or not (lookbackValue.AsScalar.Kind in [skInteger, skInt64]) then
raise EArgumentException.Create('Lookback parameter must be an integer.');
if lookbackValue.AsScalar.Kind = skInteger then
lookback := lookbackValue.AsScalar.Value.AsInteger
else
lookback := lookbackValue.AsScalar.Value.AsInt64;
end;
// Dispatch based on series type
case seriesVar.Kind of
avkSeries:
begin
if (itemValue.Kind <> avkScalar) then
raise EArgumentException.Create('Can only add scalar values to a TScalarSeries.');
with seriesVar.AsSeries.Value do
begin
if (itemValue.AsScalar.Kind <> Kind) then
raise EArgumentException.CreateFmt(
'Type mismatch: Cannot add %s to a series of %s.',
[ScalarKindToString(itemValue.AsScalar.Kind), ScalarKindToString(Kind)]);
Items.Add(itemValue.AsScalar.Value, lookback);
end;
end;
avkRecordSeries:
begin
if (itemValue.Kind <> avkRecord) then
raise EArgumentException.Create('Can only add record values to a TScalarRecordSeries.');
with seriesVar.AsRecordSeries.Value do
begin
Add(itemValue.AsRecord, lookback);
end;
end;
else
raise EArgumentException.Create('"add" operation is only supported for series types.');
end;
Result := TAstValue.Void;
end;
function TEvaluatorVisitor.VisitAssignment(const Node: IAssignmentNode): TAstValue;
var
varName: string;
@@ -237,6 +359,33 @@ begin
Result := TAstValue.FromScalar(Node.Value);
end;
function TEvaluatorVisitor.VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue;
var
def: string;
begin
// CORRECTED: Node.Definition is now a simple string.
def := Node.Definition.Trim;
// Based on the content, create a scalar or a record series.
if def.StartsWith('[') then
begin
// Assumed to be a record definition array, e.g., '[{"Name": "Close", "Kind": "skDouble"}]'
var recordDef := TRttiAstHelper.JsonToRecordDefinition(def);
if Length(recordDef.Fields) = 0 then
raise EArgumentException.Create('Failed to parse record definition from JSON array.');
var recordSeries := TScalarRecordSeries.Create(recordDef);
Result := TAstValue.FromRecordSeries(recordSeries);
end
else
begin
// Assumed to be a single scalar type name, e.g., 'double'
var scalarKind := StringToScalarKind(def);
var scalarSeries := TScalarSeries.Create(scalarKind, Default(TSeries<TScalarValue>));
Result := TAstValue.FromSeries(scalarSeries);
end;
end;
function TEvaluatorVisitor.VisitIdentifier(const Node: IIdentifierNode): TAstValue;
var
val: TAstValue;
@@ -270,9 +419,19 @@ begin
// Base type dispatching
case baseValue.Kind of
avkSeries:
begin
var series := baseValue.AsSeries.Value;
if (index < 0) or (index >= series.Items.TotalCount) then
raise EArgumentException
.CreateFmt('Index %d is out of bounds for series with %d elements.', [index, series.Items.TotalCount]);
var scalarValue := series.Items[Integer(index)];
Result := TAstValue.FromScalar(TScalar.Create(series.Kind, scalarValue));
end;
avkRecordSeries:
begin
var series := baseValue.AsRecordSeries;
var series := baseValue.AsRecordSeries.Value;
if (index < 0) or (index >= series.TotalCount) then
raise EArgumentException.CreateFmt('Index %d is out of bounds for series with %d elements.', [index, series.TotalCount]);
@@ -281,7 +440,7 @@ begin
end;
avkMemberSeries:
begin
var memberSeries := baseValue.AsMemberSeries;
var memberSeries := baseValue.AsMemberSeries.Value;
if (index < 0) or (index >= memberSeries.Count) then
raise EArgumentException
.CreateFmt('Index %d is out of bounds for member series with %d elements.', [index, memberSeries.Count]);
@@ -303,7 +462,19 @@ begin
memberName := Node.Member.Name;
case baseValue.Kind of
avkRecordSeries: Result := TAstValue.FromMemberSeries(baseValue.AsRecordSeries.CreateMemberSeries(memberName));
avkSeries:
begin
var series := baseValue.AsSeries.Value;
if SameText(memberName, 'Count') then
Result := TAstValue.FromScalar(TScalar.FromInt64(series.Items.Count))
else if SameText(memberName, 'TotalCount') then
Result := TAstValue.FromScalar(TScalar.FromInt64(series.Items.TotalCount))
else if SameText(memberName, 'Kind') then
Result := TAstValue.FromText(ScalarKindToString(series.Kind))
else
raise EArgumentException.CreateFmt('Member "%s" not found on TScalarSeries.', [memberName]);
end;
avkRecordSeries: Result := TAstValue.FromMemberSeries(baseValue.AsRecordSeries.Value.CreateMemberSeries(memberName));
avkRecord: Result := TAstValue.FromScalar(baseValue.AsRecord.Items[memberName]);
else
raise EArgumentException.Create('Member access operator `.` is not supported for this value type.');
@@ -499,7 +670,14 @@ begin
if IsTruthy(conditionValue) then
Result := Node.ThenBranch.Accept(Self)
else
Result := Node.ElseBranch.Accept(Self);
begin
// If an else branch exists, evaluate it.
if Assigned(Node.ElseBranch) then
Result := Node.ElseBranch.Accept(Self)
else
// Otherwise, an if-statement without an else branch evaluates to void.
Result := TAstValue.Void;
end;
end;
function TEvaluatorVisitor.VisitTernaryExpression(const Node: ITernaryExpressionNode): TAstValue;
@@ -526,6 +704,28 @@ begin
Result := lastValue;
end;
function TEvaluatorVisitor.VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue;
var
seriesValue: TAstValue;
len: Int64;
begin
// 1. Evaluate the identifier to get the series object from the scope.
seriesValue := Node.Series.Accept(Self);
// 2. Get the length based on the actual series type.
case seriesValue.Kind of
avkSeries: len := seriesValue.AsSeries.Value.Items.Count;
avkRecordSeries: len := seriesValue.AsRecordSeries.Value.Count;
avkMemberSeries: len := seriesValue.AsMemberSeries.Value.Count;
else
// It's an error if we try to get the length of something that isn't a series.
raise EArgumentException.CreateFmt('Cannot get length of type %s.', [GetEnumName(TypeInfo(TAstValueKind), Ord(seriesValue.Kind))]);
end;
// 3. Return the length as a new scalar value.
Result := TAstValue.FromScalar(TScalar.FromInt64(len));
end;
{ TDebugEvaluatorVisitor }
constructor TDebugEvaluatorVisitor.Create(const AScope: IExecutionScope; ALog: TStrings; AShowScope: Boolean; AInitialIndent: Integer = 0);
@@ -596,6 +796,18 @@ begin
end;
end;
function TDebugEvaluatorVisitor.VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue;
begin
AppendLine('AddSeriesItem {');
Indent;
try
Result := inherited VisitAddSeriesItem(Node);
finally
Unindent;
end;
AppendLine('} -> (void)');
end;
function TDebugEvaluatorVisitor.VisitAssignment(const Node: IAssignmentNode): TAstValue;
begin
AppendLine(Format('Assignment %s := {', [Node.Identifier.Name]));
@@ -614,6 +826,18 @@ begin
Result := inherited VisitConstant(Node);
end;
function TDebugEvaluatorVisitor.VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue;
begin
AppendLine('CreateSeries {');
Indent;
try
Result := inherited VisitCreateSeries(Node);
finally
Unindent;
end;
AppendLine(Format('} -> %s', [Result.ToString]));
end;
function TDebugEvaluatorVisitor.VisitIdentifier(const Node: IIdentifierNode): TAstValue;
begin
Result := inherited VisitIdentifier(Node);
+70 -33
View File
@@ -21,7 +21,8 @@ type
end;
// Added avkMemberSeries to represent a TScalarMemberSeries value.
TAstValueKind = (avkUndefined, avkScalar, avkClosure, avkText, avkRecordSeries, avkRecord, avkMemberSeries);
// Added avkSeries to represent a TScalarSeries value.
TAstValueKind = (avkUndefined, avkScalar, avkClosure, avkText, avkSeries, avkRecordSeries, avkRecord, avkMemberSeries);
// --- Forward Declarations to break cycles ---
IExecutionScope = interface;
@@ -40,8 +41,10 @@ type
IVariableDeclarationNode = interface;
IAssignmentNode = interface;
IIndexerNode = interface;
// Added forward declaration for the new member access node.
IMemberAccessNode = interface;
ICreateSeriesNode = interface;
IAddSeriesItemNode = interface;
ISeriesLengthNode = interface;
IEvaluatorClosure = interface;
// --- Concrete Type Definitions ---
@@ -60,17 +63,8 @@ type
TAstValue = record
private
type
IVal<T> = interface
['{7D5AD675-A008-4007-B1A4-CF7A05749510}'] // needed, do not remove
function GetValue: T;
property Value: T read GetValue;
end;
TVal<T> = class(TInterfacedObject, IVal<T>)
private
FValue: T;
function GetValue: T;
public
TVal<T> = class(TInterfacedObject)
Value: T;
constructor Create(const AValue: T);
end;
@@ -86,15 +80,17 @@ type
class function FromScalar(const AValue: TScalar): TAstValue; inline; static;
class function FromClosure(const AValue: IEvaluatorClosure): TAstValue; inline; static;
class function FromText(const AValue: String): TAstValue; inline; static;
class function FromRecordSeries(const AValue: TScalarRecordSeries): TAstValue; inline; static;
class function FromRecord(const AValue: TScalarRecord): TAstValue; inline; static;
class function FromMemberSeries(const AValue: TScalarMemberSeries): TAstValue; inline; static;
class function FromSeries(const [ref] AValue: TScalarSeries): TAstValue; static; inline;
class function FromRecordSeries(const [ref] AValue: TScalarRecordSeries): TAstValue; static; inline;
class function FromRecord(const [ref] AValue: TScalarRecord): TAstValue; static; inline;
class function FromMemberSeries(const [ref] AValue: TScalarMemberSeries): TAstValue; static; inline;
function AsScalar: TScalar; inline;
function AsClosure: IEvaluatorClosure; inline;
function AsText: String; inline;
function AsRecordSeries: TScalarRecordSeries; inline;
function AsRecordSeries: TVal<TScalarRecordSeries>; inline;
function AsRecord: TScalarRecord; inline;
function AsMemberSeries: TScalarMemberSeries; inline;
function AsMemberSeries: TVal<TScalarMemberSeries>; inline;
function AsSeries: TVal<TScalarSeries>; inline;
function ToString: String;
property IsVoid: Boolean read GetIsVoid;
property Kind: TAstValueKind read GetKind;
@@ -126,6 +122,9 @@ type
function VisitAssignment(const Node: IAssignmentNode): TAstValue;
function VisitIndexer(const Node: IIndexerNode): TAstValue;
function VisitMemberAccess(const Node: IMemberAccessNode): TAstValue;
function VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue;
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue;
function VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue;
end;
IAstNode = interface(IInterface)
@@ -256,6 +255,33 @@ type
property Member: IIdentifierNode read GetMember;
end;
// Represents creating a new, empty series (e.g., new series(int)).
ICreateSeriesNode = interface(IExpressionNode)
{$region 'private'}
function GetDefinition: String;
{$endregion}
property Definition: String read GetDefinition;
end;
// Represents adding a value to a series (e.g., my_series.add(value, 100)).
IAddSeriesItemNode = interface(IExpressionNode)
{$region 'private'}
function GetSeries: IIdentifierNode;
function GetValue: IExpressionNode;
function GetLookback: IExpressionNode;
{$endregion}
property Series: IIdentifierNode read GetSeries;
property Value: IExpressionNode read GetValue;
property Lookback: IExpressionNode read GetLookback;
end;
ISeriesLengthNode = interface(IExpressionNode)
{$region 'private'}
function GetSeries: IIdentifierNode;
{$endregion}
property Series: IIdentifierNode read GetSeries;
end;
implementation
uses
@@ -266,12 +292,7 @@ uses
constructor TAstValue.TVal<T>.Create(const AValue: T);
begin
inherited Create;
FValue := AValue;
end;
function TAstValue.TVal<T>.GetValue: T;
begin
Result := FValue;
Value := AValue;
end;
{ TAstValue }
@@ -289,25 +310,25 @@ begin
Result := IEvaluatorClosure(FInterface);
end;
function TAstValue.AsMemberSeries: TScalarMemberSeries;
function TAstValue.AsMemberSeries: TVal<TScalarMemberSeries>;
begin
if (FKind <> avkMemberSeries) then
raise EInvalidCast.Create('Cannot read value as MemberSeries.');
Result := (FInterface as IVal<TScalarMemberSeries>).Value;
Result := TVal<TScalarMemberSeries>(FInterface);
end;
function TAstValue.AsRecord: TScalarRecord;
begin
if (FKind <> avkRecord) then
raise EInvalidCast.Create('Cannot read value as Record.');
Result := (FInterface as IVal<TScalarRecord>).Value;
Result := (FInterface as TVal<TScalarRecord>).Value;
end;
function TAstValue.AsRecordSeries: TScalarRecordSeries;
function TAstValue.AsRecordSeries: TVal<TScalarRecordSeries>;
begin
if (FKind <> avkRecordSeries) then
raise EInvalidCast.Create('Cannot read value as RecordSeries.');
Result := (FInterface as IVal<TScalarRecordSeries>).Value;
Result := TVal<TScalarRecordSeries>(FInterface);
end;
function TAstValue.AsScalar: TScalar;
@@ -317,12 +338,20 @@ begin
Result := FScalar;
end;
// Added support for TScalarSeries
function TAstValue.AsSeries: TVal<TScalarSeries>;
begin
if (FKind <> avkSeries) then
raise EInvalidCast.Create('Cannot read value as Series.');
Result := TVal<TScalarSeries>(FInterface);
end;
function TAstValue.AsText: String;
begin
if (FKind <> avkText) then
raise EInvalidCast.Create('Cannot read value as Text.');
Result := (FInterface as IVal<String>).Value;
Result := (FInterface as TVal<String>).Value;
end;
class function TAstValue.FromClosure(const AValue: IEvaluatorClosure): TAstValue;
@@ -332,21 +361,21 @@ begin
Result.FScalar := Default(TScalar);
end;
class function TAstValue.FromMemberSeries(const AValue: TScalarMemberSeries): TAstValue;
class function TAstValue.FromMemberSeries(const [ref] AValue: TScalarMemberSeries): TAstValue;
begin
Result.FKind := avkMemberSeries;
Result.FInterface := TVal<TScalarMemberSeries>.Create(AValue);
Result.FScalar := Default(TScalar);
end;
class function TAstValue.FromRecord(const AValue: TScalarRecord): TAstValue;
class function TAstValue.FromRecord(const [ref] AValue: TScalarRecord): TAstValue;
begin
Result.FKind := avkRecord;
Result.FInterface := TVal<TScalarRecord>.Create(AValue);
Result.FScalar := Default(TScalar);
end;
class function TAstValue.FromRecordSeries(const AValue: TScalarRecordSeries): TAstValue;
class function TAstValue.FromRecordSeries(const [ref] AValue: TScalarRecordSeries): TAstValue;
begin
Result.FKind := avkRecordSeries;
Result.FInterface := TVal<TScalarRecordSeries>.Create(AValue);
@@ -360,6 +389,13 @@ begin
Result.FInterface := nil;
end;
class function TAstValue.FromSeries(const [ref] AValue: TScalarSeries): TAstValue;
begin
Result.FKind := avkSeries;
Result.FInterface := TVal<TScalarSeries>.Create(AValue);
Result.FScalar := Default(TScalar);
end;
class function TAstValue.FromText(const AValue: String): TAstValue;
begin
Result.FKind := avkText;
@@ -383,6 +419,7 @@ begin
avkScalar: Result := FScalar.ToString;
avkClosure: Result := '<closure>';
avkText: Result := AsText;
avkSeries: Result := '<series>';
avkRecordSeries: Result := '<record_series>';
avkRecord: Result := '<record>';
avkMemberSeries: Result := '<member_series>';
+51 -4
View File
@@ -35,6 +35,9 @@ type
function VisitAssignment(const Node: IAssignmentNode): TAstValue;
function VisitIndexer(const Node: IIndexerNode): TAstValue;
function VisitMemberAccess(const Node: IMemberAccessNode): TAstValue;
function VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue;
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue;
function VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue;
end;
implementation
@@ -123,10 +126,13 @@ begin
Indent;
Node.ThenBranch.Accept(Self);
Unindent;
AppendLine('Else:');
Indent;
Node.ElseBranch.Accept(Self);
Unindent;
if Node.ElseBranch <> nil then
begin
AppendLine('Else:');
Indent;
Node.ElseBranch.Accept(Self);
Unindent;
end;
Unindent;
Result := TAstValue.Void;
end;
@@ -255,4 +261,45 @@ begin
Result := TAstValue.Void;
end;
function TPrettyPrintVisitor.VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue;
begin
AppendLine('CreateSeries');
Indent;
AppendLine('Definition: ' + Node.Definition);
Unindent;
Result := TAstValue.Void;
end;
function TPrettyPrintVisitor.VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue;
begin
AppendLine('AddSeriesItem');
Indent;
AppendLine('Series:');
Indent;
Node.Series.Accept(Self);
Unindent;
AppendLine('Value:');
Indent;
Node.Value.Accept(Self);
Unindent;
if Assigned(Node.Lookback) then
begin
AppendLine('Lookback:');
Indent;
Node.Lookback.Accept(Self);
Unindent;
end;
Unindent;
Result := TAstValue.Void;
end;
function TPrettyPrintVisitor.VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue;
var
seriesStr: string;
begin
// Get the string representation of the series identifier by visiting the node.
seriesStr := Node.Series.Accept(Self).ToString;
Result := TAstValue.FromText(Format('length(%s)', [seriesStr]));
end;
end.
+128
View File
@@ -34,6 +34,14 @@ type
class function AssignResult(const AValue: IExpressionNode): IAssignmentNode; static;
class function Indexer(const ABase: IExpressionNode; const AIndex: IExpressionNode): IIndexerNode; static;
class function MemberAccess(const ABase: IExpressionNode; 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
): IAddSeriesItemNode; static;
// Added the new factory function for SeriesLength
class function SeriesLength(const ASeries: IIdentifierNode): ISeriesLengthNode; static;
end;
implementation
@@ -209,6 +217,41 @@ type
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
{ TCreateSeriesNode }
TCreateSeriesNode = class(TAstNode, ICreateSeriesNode)
private
FDefinition: String;
function GetDefinition: String;
public
constructor Create(const ADefinition: String);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
{ TAddSeriesItemNode }
TAddSeriesItemNode = class(TAstNode, IAddSeriesItemNode)
private
FSeries: IIdentifierNode;
FValue: IExpressionNode;
FLookback: IExpressionNode;
function GetSeries: IIdentifierNode;
function GetValue: IExpressionNode;
function GetLookback: IExpressionNode;
public
constructor Create(const ASeries: IIdentifierNode; const AValue: IExpressionNode; const ALookback: IExpressionNode);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
// Added concrete class for the series length node.
{ TSeriesLengthNode }
TSeriesLengthNode = class(TAstNode, ISeriesLengthNode)
private
FSeries: IIdentifierNode;
function GetSeries: IIdentifierNode;
public
constructor Create(const ASeries: IIdentifierNode);
function Accept(const Visitor: IAstVisitor): TAstValue; override;
end;
{ TConstantNode }
constructor TConstantNode.Create(AValue: TScalar);
@@ -541,8 +584,83 @@ begin
Result := FMember;
end;
{ TCreateSeriesNode }
constructor TCreateSeriesNode.Create(const ADefinition: String);
begin
inherited Create;
FDefinition := ADefinition;
end;
function TCreateSeriesNode.Accept(const Visitor: IAstVisitor): TAstValue;
begin
Result := Visitor.VisitCreateSeries(Self);
end;
function TCreateSeriesNode.GetDefinition: String;
begin
Result := FDefinition;
end;
{ TAddSeriesItemNode }
constructor TAddSeriesItemNode.Create(const ASeries: IIdentifierNode; const AValue, ALookback: IExpressionNode);
begin
inherited Create;
FSeries := ASeries;
FValue := AValue;
FLookback := ALookback;
end;
function TAddSeriesItemNode.Accept(const Visitor: IAstVisitor): TAstValue;
begin
Result := Visitor.VisitAddSeriesItem(Self);
end;
function TAddSeriesItemNode.GetLookback: IExpressionNode;
begin
Result := FLookback;
end;
function TAddSeriesItemNode.GetSeries: IIdentifierNode;
begin
Result := FSeries;
end;
function TAddSeriesItemNode.GetValue: IExpressionNode;
begin
Result := FValue;
end;
{ TSeriesLengthNode }
constructor TSeriesLengthNode.Create(const ASeries: IIdentifierNode);
begin
inherited Create;
FSeries := ASeries;
end;
function TSeriesLengthNode.Accept(const Visitor: IAstVisitor): TAstValue;
begin
Result := Visitor.VisitSeriesLength(Self);
end;
function TSeriesLengthNode.GetSeries: IIdentifierNode;
begin
Result := FSeries;
end;
{ TAst }
class function TAst.AddSeriesItem(
const ASeries: IIdentifierNode;
const AValue: IExpressionNode;
const ALookback: IExpressionNode
): IAddSeriesItemNode;
begin
Result := TAddSeriesItemNode.Create(ASeries, AValue, ALookback);
end;
class function TAst.Assign(const AIdentifier: IIdentifierNode; const AValue: IExpressionNode): IAssignmentNode;
begin
Result := TAssignmentNode.Create(AIdentifier, AValue);
@@ -563,6 +681,11 @@ begin
Result := TConstantNode.Create(AValue);
end;
class function TAst.CreateSeries(const ADefinition: String): ICreateSeriesNode;
begin
Result := TCreateSeriesNode.Create(ADefinition);
end;
class function TAst.BinaryExpr(ALeft: IExpressionNode; AOperator: TBinaryOperator; ARight: IExpressionNode): IBinaryExpressionNode;
begin
Result := TBinaryExpressionNode.Create(ALeft, AOperator, ARight);
@@ -593,6 +716,11 @@ begin
Result := TMemberAccessNode.Create(ABase, AMember);
end;
class function TAst.SeriesLength(const ASeries: IIdentifierNode): ISeriesLengthNode;
begin
Result := TSeriesLengthNode.Create(ASeries);
end;
class function TAst.TernaryExpr(const ACondition: IExpressionNode; const AThenBranch, AElseBranch: IExpressionNode): ITernaryExpressionNode;
begin
Result := TTernaryExpressionNode.Create(ACondition, AThenBranch, AElseBranch);