Ast series indexer
This commit is contained in:
+138
-13
@@ -5,6 +5,7 @@ interface
|
||||
uses
|
||||
System.SysUtils,
|
||||
System.Types,
|
||||
System.TypInfo,
|
||||
System.UITypes,
|
||||
System.Classes,
|
||||
System.Variants,
|
||||
@@ -46,6 +47,7 @@ type
|
||||
Panel2: TPanel;
|
||||
ClearButton: TButton;
|
||||
FlowOnlyBox: TCheckBox;
|
||||
SeriesTestButton: TButton;
|
||||
procedure ClearButtonClick(Sender: TObject);
|
||||
procedure FormCreate(Sender: TObject);
|
||||
procedure CrerateTriggerExampleButtonClick(Sender: TObject);
|
||||
@@ -55,6 +57,7 @@ type
|
||||
procedure FibonacciButtonClick(Sender: TObject);
|
||||
procedure PrettyPrintButtonClick(Sender: TObject);
|
||||
procedure RecursionButtonClick(Sender: TObject);
|
||||
procedure SeriesTestButtonClick(Sender: TObject);
|
||||
procedure Test1ButtonClick(Sender: TObject);
|
||||
procedure Test2ButtonClick(Sender: TObject);
|
||||
private
|
||||
@@ -74,6 +77,8 @@ implementation
|
||||
|
||||
uses
|
||||
Myc.Ast.Scope,
|
||||
Myc.Ast.RttiUtils,
|
||||
Myc.Data.Decimal,
|
||||
System.Diagnostics; // For TStopwatch
|
||||
|
||||
{$R *.fmx}
|
||||
@@ -185,6 +190,7 @@ begin
|
||||
Application.ProcessMessages; // Update UI before blocking
|
||||
|
||||
sw.Start;
|
||||
{
|
||||
root :=
|
||||
TAst.Block(
|
||||
[
|
||||
@@ -215,7 +221,7 @@ begin
|
||||
TAst.FunctionCall(TAst.Identifier('fib'), [TAst.Constant(TScalar.FromInt64(30))])
|
||||
]
|
||||
);
|
||||
|
||||
}
|
||||
root :=
|
||||
TAst.Block(
|
||||
[
|
||||
@@ -344,18 +350,15 @@ begin
|
||||
[TAst.Identifier('n')],
|
||||
TAst.Block(
|
||||
[
|
||||
TAst.Assign(
|
||||
TAst.Identifier('Result'),
|
||||
TAst.TernaryExpr(
|
||||
TAst.BinaryExpr(TAst.Identifier('n'), boLess, TAst.Constant(TScalar.FromInt64(2))),
|
||||
TAst.Constant(TScalar.FromInt64(1)),
|
||||
TAst.BinaryExpr(
|
||||
TAst.Identifier('n'),
|
||||
boMultiply,
|
||||
TAst.FunctionCall(
|
||||
TAst.Identifier('factorial'),
|
||||
[TAst.BinaryExpr(TAst.Identifier('n'), boSubtract, TAst.Constant(TScalar.FromInt64(1)))]
|
||||
)
|
||||
TAst.TernaryExpr(
|
||||
TAst.BinaryExpr(TAst.Identifier('n'), boLess, TAst.Constant(TScalar.FromInt64(2))),
|
||||
TAst.Constant(TScalar.FromInt64(1)),
|
||||
TAst.BinaryExpr(
|
||||
TAst.Identifier('n'),
|
||||
boMultiply,
|
||||
TAst.FunctionCall(
|
||||
TAst.Identifier('factorial'),
|
||||
[TAst.BinaryExpr(TAst.Identifier('n'), boSubtract, TAst.Constant(TScalar.FromInt64(1)))]
|
||||
)
|
||||
)
|
||||
)
|
||||
@@ -379,6 +382,128 @@ begin
|
||||
Memo1.Lines.Add('(AST structure stored. Click "Pretty Print" or "Debug" to view.)');
|
||||
end;
|
||||
|
||||
procedure TForm1.SeriesTestButtonClick(Sender: TObject);
|
||||
type
|
||||
// A test record to generate a definition from.
|
||||
TOHLCV = record
|
||||
Timestamp: TDateTime;
|
||||
Open: Double;
|
||||
High: Double;
|
||||
Low: Double;
|
||||
Close: Double;
|
||||
Volume: Int64;
|
||||
end;
|
||||
var
|
||||
jsonDef: string;
|
||||
recordDef: TScalarRecordDefinition;
|
||||
series: TScalarRecordSeries;
|
||||
recordValue: TScalarRecord;
|
||||
visitor: IAstVisitor;
|
||||
ast: IAstNode;
|
||||
resultValue: TAstValue;
|
||||
resultRecord: TScalarRecord;
|
||||
i: Integer;
|
||||
begin
|
||||
Memo1.Lines.Clear;
|
||||
Memo1.Lines.Add('--- Series Test ---');
|
||||
|
||||
// --- 1. Arrange (in Delphi) ---
|
||||
Memo1.Lines.Add('1. Arranging test data in Delphi...');
|
||||
|
||||
// Clear and prepare the global scope for the script.
|
||||
FGScope.Clear;
|
||||
RegisterNativeFunctions(FGScope);
|
||||
|
||||
// Generate JSON definition from our test record type.
|
||||
jsonDef := TRttiAstHelper.RecordDefinitionToJson(TypeInfo(TOHLCV));
|
||||
FGScope.SetValue('ohlcvDef', TAstValue.FromText(jsonDef));
|
||||
Memo1.Lines.Add(' - Generated and injected JSON definition for TOHLCV.');
|
||||
|
||||
// Create a TScalarRecordSeries and populate it with some data.
|
||||
recordDef := TRttiAstHelper.JsonToRecordDefinition(jsonDef);
|
||||
series := TScalarRecordSeries.Create(recordDef);
|
||||
for i := 0 to 4 do
|
||||
begin
|
||||
recordValue :=
|
||||
TScalarRecord.Create(
|
||||
recordDef,
|
||||
[
|
||||
TScalarValue.FromDateTime(Now + i),
|
||||
TScalarValue.FromDouble(100.0 + i),
|
||||
TScalarValue.FromDouble(105.0 + i),
|
||||
TScalarValue.FromDouble(98.0 + i),
|
||||
TScalarValue.FromDouble(102.0 + i),
|
||||
TScalarValue.FromInt64(10000 * (i + 1))
|
||||
]
|
||||
);
|
||||
series.Add(recordValue);
|
||||
end;
|
||||
|
||||
// Inject the pre-populated series into the script's scope.
|
||||
FGScope.SetValue('ohlcvSeries', TAstValue.FromRecordSeries(series));
|
||||
Memo1.Lines.Add(Format(' - Created and injected a series with %d records.', [series.TotalCount]));
|
||||
Memo1.Lines.Add('');
|
||||
|
||||
// --- 2. Act (in Script) ---
|
||||
Memo1.Lines.Add('2. Executing script...');
|
||||
|
||||
// Create an AST that:
|
||||
// a) Creates a new, empty series to test the native function.
|
||||
// b) Accesses the 3rd element (index 2) of the pre-populated series.
|
||||
// c) Returns the accessed record as the final result.
|
||||
ast :=
|
||||
TAst.Block(
|
||||
[
|
||||
TAst.VarDecl(
|
||||
TAst.Identifier('newSeries'),
|
||||
TAst.FunctionCall(TAst.Identifier('CreateRecordSeries'), [TAst.Identifier('ohlcvDef')])
|
||||
),
|
||||
TAst.Indexer(TAst.Identifier('ohlcvSeries'), TAst.Constant(TScalar.FromInt64(2)))
|
||||
]
|
||||
);
|
||||
FLastAst := ast;
|
||||
|
||||
visitor := TEvaluatorVisitor.Create(FGScope);
|
||||
resultValue := ast.Accept(visitor);
|
||||
Memo1.Lines.Add(' - Script finished.');
|
||||
Memo1.Lines.Add(Format(' - Script returned value of type: %s', [GetEnumName(TypeInfo(TAstValueKind), Ord(resultValue.Kind))]));
|
||||
Memo1.Lines.Add('');
|
||||
|
||||
// --- 3. Assert (in Delphi) ---
|
||||
Memo1.Lines.Add('3. Asserting results in Delphi...');
|
||||
|
||||
if (resultValue.Kind <> avkRecord) then
|
||||
begin
|
||||
Memo1.Lines.Add('TEST FAILED: Script did not return a record.');
|
||||
exit;
|
||||
end;
|
||||
|
||||
resultRecord := resultValue.AsRecord;
|
||||
Memo1.Lines.Add(' - Result is a record, as expected.');
|
||||
|
||||
// Verify the 'Close' price of the 3rd record (index 2)
|
||||
var closeValue := resultRecord.Items['Close'].Value.AsDouble;
|
||||
var expectedClose := 102.0 + 2; // from the data generation loop for i=2
|
||||
if (abs(closeValue - expectedClose) > 0.001) then
|
||||
begin
|
||||
Memo1.Lines.Add(Format('TEST FAILED: Expected Close price %f, but got %f.', [expectedClose, closeValue]));
|
||||
exit;
|
||||
end;
|
||||
Memo1.Lines.Add(Format(' - Verified Close price: %f.', [closeValue]));
|
||||
|
||||
// Verify the 'Volume' of the 3rd record (index 2)
|
||||
var volumeValue := resultRecord.Items['Volume'].Value.AsInt64;
|
||||
var expectedVolume := 10000 * (2 + 1); // for i=2
|
||||
if (volumeValue <> expectedVolume) then
|
||||
begin
|
||||
Memo1.Lines.Add(Format('TEST FAILED: Expected Volume %d, but got %d.', [expectedVolume, volumeValue]));
|
||||
exit;
|
||||
end;
|
||||
Memo1.Lines.Add(Format(' - Verified Volume: %d.', [volumeValue]));
|
||||
Memo1.Lines.Add('');
|
||||
Memo1.Lines.Add('--- TEST PASSED ---');
|
||||
end;
|
||||
|
||||
procedure TForm1.Test1ButtonClick(Sender: TObject);
|
||||
var
|
||||
scope: IExecutionScope;
|
||||
|
||||
Reference in New Issue
Block a user