Macro hygiene

This commit is contained in:
Michael Schimmel
2025-11-09 18:22:09 +01:00
parent d849f65f2d
commit 7aa406f27b
9 changed files with 418 additions and 95 deletions
+30 -26
View File
@@ -39,7 +39,8 @@ uses
FMX.ListView.Appearances,
FMX.ListView.Adapters.Base,
FMX.ListView,
Myc.Ast.Environment; // Added Environment
Myc.Ast.Environment,
FMX.ListBox; // Added Environment
type
// A test record
@@ -57,7 +58,6 @@ type
Memo1: TMemo;
Test1Button: TButton;
Test2Button: TButton;
PrettyPrintButton: TButton;
RecursionButton: TButton;
ShowScopeBox: TCheckBox;
FibonacciButton: TButton;
@@ -82,8 +82,10 @@ type
LoadUserLibButton: TButton;
SaveUserLibButton: TButton;
RTLListView: TListView;
CompilerStageBox: TComboBox;
procedure InnerLambdaButtonClick(Sender: TObject);
procedure ClearButtonClick(Sender: TObject);
procedure CompilerStageBoxChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure CreateTriggerExampleButtonClick(Sender: TObject);
procedure DebugBoxChange(Sender: TObject);
@@ -95,7 +97,6 @@ type
procedure FibonacciButtonClick(Sender: TObject);
procedure FlowOnlyBoxChange(Sender: TObject);
procedure OHLCButtonClick(Sender: TObject);
procedure PrettyPrintButtonClick(Sender: TObject);
procedure RecursionButtonClick(Sender: TObject);
procedure SeriesTestButtonClick(Sender: TObject);
procedure Test1ButtonClick(Sender: TObject);
@@ -205,6 +206,11 @@ begin
FWorkspace.Repaint;
end;
procedure TForm1.CompilerStageBoxChange(Sender: TObject);
begin
ShowVizualization(14, 14);
end;
function TForm1.CreateEnvironment: TAstEnvironment;
begin
if DebugBox.IsChecked then
@@ -361,6 +367,7 @@ begin
.AsMacroDefinition
);
CompilerStageBox.BringToFront;
ClearButtonClick(Self);
end;
@@ -585,12 +592,6 @@ begin
try
var unboundAst := converter.Deserialize(jsonObj);
// Set strategy based on UI
if DebugBox.IsChecked then
FEnvironment.SetDebugMode(Memo1.Lines, ShowScopeBox.IsChecked)
else
FEnvironment.SetStandardMode;
// Run the full pipeline
FCurrExec := FEnvironment.Compile(unboundAst);
@@ -718,21 +719,6 @@ begin
UpdateScript;
end;
procedure TForm1.PrettyPrintButtonClick(Sender: TObject);
begin
Memo1.Lines.Clear;
Memo1.Lines.Add('--- AST Pretty Print ---');
// We print the *compiled* AST
if not Assigned(FCurrExec) then
begin
Memo1.Lines.Add('No AST has been generated yet.');
exit;
end;
Memo1.Lines.Add(TAstScript.Print(FCurrUnboundAst));
end;
procedure TForm1.RecursionButtonClick(Sender: TObject);
var
root: IAstNode;
@@ -1270,9 +1256,10 @@ begin
Memo1.Lines.Clear;
try
FCurrUnboundAst := TAstScript.Parse(ScriptMemo.Lines.Text);
try
// Execute the entire script block when it changes
var result := ExecuteAst(TAstScript.Parse(ScriptMemo.Lines.Text));
var result := ExecuteAst(FCurrUnboundAst);
Memo1.Lines.Add(Format('Script executed. Final result: %s', [result.ToString]));
finally
ShowVizualization(14, 14);
@@ -1295,7 +1282,24 @@ procedure TForm1.ShowVizualization(X, Y: Single);
begin
FWorkspace.DeleteChildren;
FWorkspace.Build(FCurrUnboundAst, TPointF.Create(X, Y));
if FCurrUnboundAst = nil then
exit;
var ast := FCurrUnboundAst;
if CompilerStageBox.ItemIndex > 0 then
ast := FEnvironment.Environment.ExpandMacros(ast);
if CompilerStageBox.ItemIndex > 1 then
begin
var desc: IScopeDescriptor;
ast := FEnvironment.Environment.Bind(ast, desc);
end;
if CompilerStageBox.ItemIndex > 2 then
ast := FEnvironment.Environment.Lower(ast);
FWorkspace.Build(ast, TPointF.Create(X, Y));
//
// if FCurrExec <> nil then
// begin