AST Refactoring

This commit is contained in:
Michael Schimmel
2025-09-12 22:45:29 +02:00
parent a36ca2c7e3
commit be6665ad6e
11 changed files with 428 additions and 633 deletions
+173 -97
View File
@@ -23,14 +23,15 @@ uses
FMX.Controls.Presentation,
Myc.Ast.Visualizer,
Myc.Data.Scalar,
Myc.Data.Value, // Added
Myc.Data.Value,
Myc.Ast.Nodes,
Myc.Ast,
Myc.Ast.Evaluator,
Myc.Ast.Printer,
FMX.Layouts,
FMX.Objects,
Myc.Ast.Scope;
Myc.Ast.Scope,
Myc.Ast.Debugger; // Added for TDebugSession
type
// A test record
@@ -63,11 +64,13 @@ type
DebugBox: TCheckBox;
FromJSONButton: TButton;
ToJSONButton: TButton;
ExternalFuncButton: TButton;
procedure ClearButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure CreateTriggerExampleButtonClick(Sender: TObject);
procedure DoTrigger2ButtonClick(Sender: TObject);
procedure DoTriggerButtonClick(Sender: TObject);
procedure ExternalFuncButtonClick(Sender: TObject);
procedure FibonacciButtonClick(Sender: TObject);
procedure OHLCButtonClick(Sender: TObject);
procedure PrettyPrintButtonClick(Sender: TObject);
@@ -82,9 +85,8 @@ type
FLastAst: IAstNode;
FGScope: IExecutionScope;
FWorkspace: TAuraWorkspace;
function CreateVisitor(const AScope: IExecutionScope): IAstVisitor;
procedure WorkspaceMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
// New helper function to encapsulate the Bind -> Evaluate pattern
// Helper function to encapsulate the Bind -> Evaluate pattern
function ExecuteAst(const ANode: IAstNode; const AParentScope: IExecutionScope): TDataValue;
public
{ Public declarations }
@@ -113,26 +115,32 @@ begin
RegisterNativeFunctions(FGScope);
end;
function TForm1.CreateVisitor(const AScope: IExecutionScope): IAstVisitor;
begin
// Conditionally create a debug visitor if the checkbox is checked.
if DebugBox.IsChecked then
begin
Memo1.Lines.Add('--- Creating DEBUG visitor ---');
Result := TDebugEvaluatorVisitor.Create(AScope, Memo1.Lines, ShowScopeBox.IsChecked);
end
else
Result := TEvaluatorVisitor.Create(AScope);
end;
function TForm1.ExecuteAst(const ANode: IAstNode; const AParentScope: IExecutionScope): TDataValue;
var
scriptScope: IExecutionScope;
begin
// This helper function handles simple, one-off script executions.
// It binds the AST and then decides whether to run a debug session or a standard evaluation.
scriptScope := TAst.Bind(ANode, AParentScope);
var visitor := CreateVisitor(scriptScope);
Result := ANode.Accept(visitor);
if DebugBox.IsChecked then
begin
// Debug path: Create, run, and destroy a debug session.
var session := TDebugSession.Create(ShowScopeBox.IsChecked);
try
Result := session.Execute(ANode, scriptScope);
Memo1.Lines.Add('--- DEBUG LOG ---');
Memo1.Lines.AddStrings(session.Log);
finally
session.Free;
end;
end
else
begin
// Production path: Create and use the standard evaluator.
var visitor := TEvaluatorVisitor.Create(scriptScope);
Result := ANode.Accept(visitor);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
@@ -186,8 +194,8 @@ begin
);
FLastAst := root;
// Execute with nil as parent scope
result := ExecuteAst(root, nil);
// Execute with FGScope as parent. The helper function handles debug/prod switching.
result := ExecuteAst(root, FGScope);
sw.Stop;
Memo1.Lines.Add(Format('Result: fib(30) %s (calculated in %d ms)', [result.ToString, sw.ElapsedMilliseconds]));
@@ -247,8 +255,8 @@ begin
);
FLastAst := root;
// Execute with nil as parent scope
result := ExecuteAst(root, nil);
// Execute with FGScope as parent.
result := ExecuteAst(root, FGScope);
sw.Stop;
Memo1.Lines.Add(Format('Result: %s (calculated in %d ms)', [result.ToString, sw.ElapsedMilliseconds]));
@@ -256,18 +264,17 @@ end;
procedure TForm1.SeriesTestButtonClick(Sender: TObject);
var
scope: IExecutionScope;
ast, callAst: IAstNode;
resultValue: TDataValue;
series: TScalarRecordSeries;
recordDef: TScalarRecordDefinition;
i: Integer;
scope: IExecutionScope;
begin
Memo1.Lines.Clear;
Memo1.Lines.Add('--- Series Test ---');
// 1. Arrange: Create a new scope and populate it with Delphi data
scope := TExecutionScope.Create(FGScope); // Inherits native functions
scope := TExecutionScope.Create(FGScope);
recordDef := TRttiAstHelper.JsonToRecordDefinition(TRttiAstHelper.RecordDefinitionToJson<TOHLCV>);
series := TScalarRecordSeries.Create(recordDef);
for i := 0 to 4 do
@@ -288,7 +295,6 @@ begin
end;
scope.Define('ohlcvSeries', TDataValue.FromRecordSeries(series));
// 2. Act: Define and execute the script AST
ast :=
TAst.LambdaExpr(
[],
@@ -307,7 +313,6 @@ begin
callAst := TAst.FunctionCall(ast, []);
resultValue := ExecuteAst(callAst, scope);
// 3. Assert (logging only)
Memo1.Lines.Add(Format('Result of script: %s', [resultValue.ToString]));
end;
@@ -338,7 +343,6 @@ begin
FLastAst := main;
callAst := TAst.FunctionCall(main, []);
// Execute with FGScope as parent
result := ExecuteAst(callAst, FGScope);
sw.Stop;
@@ -376,7 +380,6 @@ begin
);
FLastAst := root;
// Execute with FGScope as parent
result := ExecuteAst(root, FGScope);
sw.Stop;
@@ -403,14 +406,14 @@ const
lookback = 50;
smaSlowLength = 20;
smaFastLength = 10;
var
visitor: IAstVisitor;
scope: IExecutionScope;
session: TDebugSession;
begin
// 1. Setup
Memo1.Lines.Clear;
Memo1.Lines.Add(Format('--- Simulating O(1) SMA Crossover Strategy for %d ticks ---', [numRecs]));
var sw := TStopwatch.StartNew;
// 2. Create the setup AST with the strategy logic
var setupAst :=
TAst.Block(
[
@@ -511,77 +514,106 @@ begin
);
FLastAst := setupAst;
var scope := TAst.Bind(setupAst, FGScope);
setupAst.Accept(CreateVisitor(scope));
// 1. Bind and execute the setup script.
scope := TAst.Bind(setupAst, FGScope);
// Declare the series variable in the scope BEFORE binding the call AST
// This is a temporary visitor just for the setup execution.
var setupVisitor: IAstVisitor;
if DebugBox.IsChecked then
begin
session := TDebugSession.Create(ShowScopeBox.IsChecked);
try
// Execute setup script within a temporary session.
session.Execute(setupAst, scope);
Memo1.Lines.Add('--- DEBUG LOG (Setup) ---');
Memo1.Lines.AddStrings(session.Log);
session.Log.Clear; // Clear log for the next phase
finally
session.Free;
end;
end
else
begin
setupVisitor := TEvaluatorVisitor.Create(scope);
setupAst.Accept(setupVisitor);
end;
// 2. Prepare for the simulation loop by modifying the now-populated scope.
scope.Define('current_series', TDataValue.Void);
// Create the call AST that will be executed in the loop
var currentSeriesIdent := TAst.Identifier('current_series');
var callAst := TAst.FunctionCall(TAst.Identifier('maCrossStrategy'), [currentSeriesIdent]);
// Bind the call AST once, before the loop.
// 3. Re-bind the scope with the new AST. This creates the FINAL scope for the loop.
scope := TAst.Bind(callAst, scope);
var visitor := CreateVisitor(scope);
// 4. Simulation Loop
Memo1.Lines.Add('Starting simulation...');
Application.ProcessMessages;
var lastClose := 1000.0;
Randomize;
var recDef := TRttiAstHelper.JsonToRecordDefinition(TRttiAstHelper.RecordDefinitionToJson<TOHLCV>);
var series := TDataValue.FromRecordSeries(TScalarRecordSeries.Create(recDef));
var seriesAddress := currentSeriesIdent.Address;
var nw := Now;
var ohlcvRec: TOHLCV;
for var i := 1 to numRecs do
begin
ohlcvRec.Timestamp := nw + (i / (24 * 60));
ohlcvRec.Open := lastClose + (Random * 0.05);
ohlcvRec.Close := lastClose + (Random - 0.49) * 2;
ohlcvRec.High := Max(ohlcvRec.Open, ohlcvRec.Close) + Random;
ohlcvRec.Low := Min(ohlcvRec.Open, ohlcvRec.Close) - Random;
ohlcvRec.Volume := RandomRange(1000, 50000);
lastClose := ohlcvRec.Close;
var recordValue :=
TScalarRecord.Create(
recDef,
[
TScalarValue.FromDateTime(ohlcvRec.Timestamp),
TScalarValue.FromDouble(ohlcvRec.Open),
TScalarValue.FromDouble(ohlcvRec.High),
TScalarValue.FromDouble(ohlcvRec.Low),
TScalarValue.FromDouble(ohlcvRec.Close),
TScalarValue.FromInt64(ohlcvRec.Volume)
]
);
series.AsRecordSeries.Value.Add(recordValue, lookback);
if series.AsRecordSeries.Value.TotalCount >= smaSlowLength then
// 4. NOW create the visitor for the simulation loop, using the final scope.
session := nil;
try
if DebugBox.IsChecked then
begin
// Update the 'current_series' value in the scope using the FAST index-based assignment
scope[seriesAddress].Value := series;
// Create a new session that will live for the duration of the loop.
session := TDebugSession.Create(ShowScopeBox.IsChecked);
visitor := TDebugEvaluatorVisitor.Create(scope, session.Log, ShowScopeBox.IsChecked, 1);
end
else
begin
visitor := TEvaluatorVisitor.Create(scope);
end;
// Execute the PRE-BOUND call AST
var resultValue := callAst.Accept(visitor);
// 5. Simulation Loop
Memo1.Lines.Add('Starting simulation...');
Application.ProcessMessages;
if i mod 50 = 0 then
var lastClose := 1000.0;
Randomize;
var recDef := TRttiAstHelper.JsonToRecordDefinition(TRttiAstHelper.RecordDefinitionToJson<TOHLCV>);
var series := TDataValue.FromRecordSeries(TScalarRecordSeries.Create(recDef));
var nw := Now;
var ohlcvRec: TOHLCV;
for var i := 1 to numRecs do
begin
// ... (simulation logic is unchanged) ...
ohlcvRec.Timestamp := nw + (i / (24 * 60));
ohlcvRec.Open := lastClose + (Random * 0.05);
ohlcvRec.Close := lastClose + (Random - 0.49) * 2;
ohlcvRec.High := Max(ohlcvRec.Open, ohlcvRec.Close) + Random;
ohlcvRec.Low := Min(ohlcvRec.Open, ohlcvRec.Close) - Random;
ohlcvRec.Volume := RandomRange(1000, 50000);
lastClose := ohlcvRec.Close;
var recordValue :=
TScalarRecord.Create(
recDef,
[
TScalarValue.FromDateTime(ohlcvRec.Timestamp),
TScalarValue.FromDouble(ohlcvRec.Open),
TScalarValue.FromDouble(ohlcvRec.High),
TScalarValue.FromDouble(ohlcvRec.Low),
TScalarValue.FromDouble(ohlcvRec.Close),
TScalarValue.FromInt64(ohlcvRec.Volume)
]
);
series.AsRecordSeries.Value.Add(recordValue, lookback);
if series.AsRecordSeries.Value.TotalCount >= smaSlowLength then
begin
Memo1.Lines.Add(Format('Tick %d/%d: Close = %.2f, Signal = %s', [i, numRecs, ohlcvRec.Close, resultValue.ToString]));
Application.ProcessMessages;
scope[seriesAddress].Value := series;
var resultValue := callAst.Accept(visitor);
if i mod 50 = 0 then
begin
Memo1.Lines.Add(Format('Tick %d/%d: Close = %.2f, Signal = %s', [i, numRecs, ohlcvRec.Close, resultValue.ToString]));
Application.ProcessMessages;
end;
end;
end;
end;
sw.Stop;
Memo1.Lines.Add('--- Simulation Finished ---');
Memo1.Lines.Add(Format('Total time: %d ms', [sw.ElapsedMilliseconds]));
sw.Stop;
Memo1.Lines.Add('--- Simulation Finished ---');
Memo1.Lines.Add(Format('Total time: %d ms', [sw.ElapsedMilliseconds]));
finally
if Assigned(session) then
session.Free;
end;
end;
var
@@ -590,12 +622,10 @@ var
procedure TForm1.CreateTriggerExampleButtonClick(Sender: TObject);
var
blk: IAstNode;
visitor: IAstVisitor;
begin
// This is a setup script, so its goal is to create and populate FGScope.
Memo1.Lines.Clear;
Memo1.Lines.Add('--- Creating Trigger Blueprint ---');
// Define the AST for the setup script.
blk :=
TAst.Block(
[
@@ -612,9 +642,25 @@ begin
TriggerScope := TAst.Bind(blk, FGScope);
blk.Accept(CreateVisitor(TriggerScope));
// This case is simple enough to just inline the logic from ExecuteAst
if DebugBox.IsChecked then
begin
var session := TDebugSession.Create(ShowScopeBox.IsChecked);
try
session.Execute(blk, TriggerScope);
Memo1.Lines.Add('--- DEBUG LOG ---');
Memo1.Lines.AddStrings(session.Log);
finally
session.Free;
end;
end
else
begin
visitor := TEvaluatorVisitor.Create(TriggerScope);
blk.Accept(visitor);
end;
FLastAst := blk; // Store for visualization
FLastAst := blk;
Memo1.Lines.Add('Variable "X" and function "tickHandler" defined in persistent scope.');
Memo1.Lines.Add('Click "Do Trigger" to execute.');
@@ -644,11 +690,43 @@ begin
Memo1.Lines.Add(Format('Tick(2)! New value of X: %s', [X.ToString]));
end;
procedure TForm1.ExternalFuncButtonClick(Sender: TObject);
var
scope: IExecutionScope;
callAst: IAstNode;
resultValue: TDataValue;
begin
Memo1.Lines.Clear;
Memo1.Lines.Add('--- Calling external Delphi function from AST ---');
scope := TExecutionScope.Create(FGScope);
scope.Define(
'delphiAdd',
function(const ArgNodes: TArray<TDataValue>): TDataValue
var
val1, val2: Int64;
begin
if Length(ArgNodes) <> 2 then
raise Exception.Create('delphiAdd requires exactly 2 arguments.');
val1 := ArgNodes[0].AsScalar.Value.AsInt64;
val2 := ArgNodes[1].AsScalar.Value.AsInt64;
Result := TScalar.FromInt64(val1 + val2);
end
);
callAst :=
TAst.FunctionCall(TAst.Identifier('delphiAdd'), [TAst.Constant(TScalar.FromInt64(100)), TAst.Constant(TScalar.FromInt64(23))]);
FLastAst := callAst;
resultValue := ExecuteAst(callAst, scope);
Memo1.Lines.Add(Format('Result from delphiAdd(100, 23): %s', [resultValue.ToString]));
end;
procedure TForm1.FromJSONButtonClick(Sender: TObject);
var
jsonString: string;
begin
// Implemented TODO: Reads an AST JSON string from the memo and deserializes it into FLastAst.
Memo1.Lines.BeginUpdate;
try
jsonString := Memo1.Lines.Text;
@@ -670,7 +748,6 @@ begin
FLastAst := nil;
Memo1.Lines.Add('Error deserializing AST from JSON:');
Memo1.Lines.Add(E.Message);
// Restore original text for correction
Memo1.Lines.Add('--- Original JSON ---');
Memo1.Lines.Text := Memo1.Lines.Text + sLineBreak + jsonString;
end;
@@ -684,7 +761,6 @@ procedure TForm1.ToJSONButtonClick(Sender: TObject);
var
jsonString: string;
begin
// Implemented TODO: Serializes the current FLastAst to a JSON string and displays it in the memo.
Memo1.Lines.Clear;
if not Assigned(FLastAst) then