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
+68 -9
View File
@@ -27,6 +27,7 @@ uses
Myc.Ast,
Myc.Ast.Evaluator,
Myc.Ast.Printer,
Myc.Ast.Persistence,
FMX.Layouts,
FMX.Objects,
Myc.Ast.Scope; // Added for TExecutionScope
@@ -60,6 +61,8 @@ type
SeriesTestButton: TButton;
OHLCButton: TButton;
DebugBox: TCheckBox;
FromJSONButton: TButton;
ToJSONButton: TButton;
procedure ClearButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure CreateTriggerExampleButtonClick(Sender: TObject);
@@ -72,15 +75,17 @@ type
procedure SeriesTestButtonClick(Sender: TObject);
procedure Test1ButtonClick(Sender: TObject);
procedure Test2ButtonClick(Sender: TObject);
procedure FromJSONButtonClick(Sender: TObject);
procedure ToJSONButtonClick(Sender: TObject);
private
// Stores the last AST generated by Test1 or Test2 button clicks.
FLastAst: IExpressionNode;
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
function ExecuteAst(const ANode: IExpressionNode; const AParentScope: IExecutionScope): TAstValue;
function ExecuteAst(const ANode: IAstNode; const AParentScope: IExecutionScope): TAstValue;
public
{ Public declarations }
end;
@@ -120,7 +125,7 @@ begin
Result := TEvaluatorVisitor.Create(AScope);
end;
function TForm1.ExecuteAst(const ANode: IExpressionNode; const AParentScope: IExecutionScope): TAstValue;
function TForm1.ExecuteAst(const ANode: IAstNode; const AParentScope: IExecutionScope): TAstValue;
var
scriptScope: IExecutionScope;
begin
@@ -144,7 +149,7 @@ end;
procedure TForm1.FibonacciButtonClick(Sender: TObject);
var
root: IExpressionNode;
root: IAstNode;
result: TAstValue;
sw: TStopwatch;
begin
@@ -208,7 +213,7 @@ end;
procedure TForm1.RecursionButtonClick(Sender: TObject);
var
root: IExpressionNode;
root: IAstNode;
result: TAstValue;
sw: TStopwatch;
begin
@@ -252,7 +257,7 @@ end;
procedure TForm1.SeriesTestButtonClick(Sender: TObject);
var
scope: IExecutionScope;
ast, callAst: IExpressionNode;
ast, callAst: IAstNode;
resultValue: TAstValue;
series: TScalarRecordSeries;
recordDef: TScalarRecordDefinition;
@@ -308,7 +313,7 @@ end;
procedure TForm1.Test1ButtonClick(Sender: TObject);
var
main, callAst: IExpressionNode;
main, callAst: IAstNode;
result: TAstValue;
sw: TStopwatch;
begin
@@ -342,7 +347,7 @@ end;
procedure TForm1.Test2ButtonClick(Sender: TObject);
var
root: IExpressionNode;
root: IAstNode;
result: TAstValue;
sw: TStopwatch;
begin
@@ -584,7 +589,7 @@ var
procedure TForm1.CreateTriggerExampleButtonClick(Sender: TObject);
var
blk: IExpressionNode;
blk: IAstNode;
begin
// This is a setup script, so its goal is to create and populate FGScope.
Memo1.Lines.Clear;
@@ -640,4 +645,58 @@ begin
Memo1.Lines.Add(Format('Tick(2)! New value of X: %s', [X.ToString]));
end;
procedure TForm1.FromJSONButtonClick(Sender: TObject);
begin
// Reads an AST JSON string from the memo and deserializes it into FLastAst.
if Memo1.Lines.Text.IsEmpty then
begin
Memo1.Lines.Text := 'Memo is empty. Paste an AST JSON structure here first.';
exit;
end;
try
Memo1.Lines.Insert(0, '--- Deserializing AST from JSON ---');
FLastAst := TAstProjectPersistence.JsonStringToAstNode(Memo1.Lines.Text);
if Assigned(FLastAst) then
begin
Memo1.Lines.Add('--- Deserialization successful. ---');
Memo1.Lines.Add('Middle-click on the right panel to visualize the new AST.');
end
else
begin
Memo1.Lines.Add('--- Deserialization failed. Invalid JSON or data structure. ---');
end;
except
on E: Exception do
begin
Memo1.Lines.Add('--- ERROR during deserialization ---');
Memo1.Lines.Add(E.ClassName + ': ' + E.Message);
end;
end;
end;
procedure TForm1.ToJSONButtonClick(Sender: TObject);
begin
// Serializes the current FLastAst to a JSON string and displays it in the memo.
Memo1.Lines.Clear;
Memo1.Lines.Add('--- Serializing AST to JSON ---');
if not Assigned(FLastAst) then
begin
Memo1.Lines.Add('No AST available to serialize. Generate one first.');
exit;
end;
try
Memo1.Lines.Text := TAstProjectPersistence.AstNodeToJsonString(FLastAst);
except
on E: Exception do
begin
Memo1.Lines.Clear;
Memo1.Lines.Add('--- ERROR during serialization ---');
Memo1.Lines.Add(E.ClassName + ': ' + E.Message);
end;
end;
end;
end.