This commit is contained in:
Michael Schimmel
2025-09-12 08:51:52 +02:00
parent 9b359eb75b
commit 695e854cc3
5 changed files with 783 additions and 32 deletions
+37 -30
View File
@@ -27,7 +27,6 @@ uses
Myc.Ast,
Myc.Ast.Evaluator,
Myc.Ast.Printer,
Myc.Ast.Persistence,
FMX.Layouts,
FMX.Objects,
Myc.Ast.Scope; // Added for TExecutionScope
@@ -99,7 +98,8 @@ uses
// Myc.Ast.Scope is now in the interface uses
Myc.Data.Scalar.JSON,
Myc.Data.Decimal,
System.Diagnostics; // For TStopwatch
System.Diagnostics, // For TStopwatch
Myc.Ast.Json; // For TAstJson serialization
{$R *.fmx}
@@ -646,55 +646,62 @@ begin
end;
procedure TForm1.FromJSONButtonClick(Sender: TObject);
var
jsonString: string;
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;
// Implemented TODO: Reads an AST JSON string from the memo and deserializes it into FLastAst.
Memo1.Lines.BeginUpdate;
try
Memo1.Lines.Insert(0, '--- Deserializing AST from JSON ---');
FLastAst := TAstProjectPersistence.JsonStringToAstNode(Memo1.Lines.Text);
if Assigned(FLastAst) then
jsonString := Memo1.Lines.Text;
Memo1.Lines.Clear;
if jsonString.IsEmpty 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. ---');
Memo1.Lines.Add('Memo is empty. Please paste an AST JSON string.');
exit;
end;
except
on E: Exception do
begin
Memo1.Lines.Add('--- ERROR during deserialization ---');
Memo1.Lines.Add(E.ClassName + ': ' + E.Message);
try
FLastAst := TAstJson.Deserialize(jsonString);
Memo1.Lines.Add('AST deserialized successfully from JSON.');
Memo1.Lines.Add('You can now visualize it (Middle Mouse Click) or pretty-print it.');
except
on E: Exception do
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;
end;
finally
Memo1.Lines.EndUpdate;
end;
end;
procedure TForm1.ToJSONButtonClick(Sender: TObject);
var
jsonString: string;
begin
// Serializes the current FLastAst to a JSON string and displays it in the memo.
// Implemented TODO: 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.');
Memo1.Lines.Add('No AST available to serialize. Please generate one first.');
exit;
end;
try
Memo1.Lines.Text := TAstProjectPersistence.AstNodeToJsonString(FLastAst);
jsonString := TAstJson.Serialize(FLastAst);
Memo1.Lines.Text := jsonString;
except
on E: Exception do
begin
Memo1.Lines.Clear;
Memo1.Lines.Add('--- ERROR during serialization ---');
Memo1.Lines.Add(E.ClassName + ': ' + E.Message);
Memo1.Lines.Add('Error serializing AST to JSON:');
Memo1.Lines.Add(E.Message);
end;
end;
end;