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
File diff suppressed because one or more lines are too long
+45 -31
View File
@@ -29,47 +29,40 @@ object Form1: TForm1
Text = 'Test 2'
OnClick = Test2ButtonClick
end
object PrettyPrintButton: TButton
Position.X = 24.000000000000000000
Position.Y = 479.000000000000000000
TabOrder = 3
Text = 'Print'
OnClick = PrettyPrintButtonClick
end
object RecursionButton: TButton
Position.X = 24.000000000000000000
Position.Y = 92.000000000000000000
Size.Width = 80.000000000000000000
Size.Height = 22.000000000000000000
Size.PlatformDefault = False
TabOrder = 4
TabOrder = 3
Text = 'Recursion'
OnClick = RecursionButtonClick
end
object ShowScopeBox: TCheckBox
Position.X = 24.000000000000000000
Position.Y = 452.000000000000000000
TabOrder = 5
TabOrder = 4
Text = 'Scope'
end
object FibonacciButton: TButton
Position.X = 24.000000000000000000
Position.Y = 122.000000000000000000
TabOrder = 7
TabOrder = 6
Text = 'Fibonacci'
OnClick = FibonacciButtonClick
end
object CrerateTriggerExampleButton: TButton
Position.X = 24.000000000000000000
Position.Y = 168.000000000000000000
TabOrder = 8
TabOrder = 7
Text = 'TriggerTest'
OnClick = CreateTriggerExampleButtonClick
end
object DoTriggerButton: TButton
Position.X = 24.000000000000000000
Position.Y = 198.000000000000000000
TabOrder = 9
TabOrder = 8
Text = 'Trigger!'
OnClick = DoTriggerButtonClick
object DoTrigger2Button: TButton
@@ -85,7 +78,7 @@ object Form1: TForm1
Size.Width = 80.000000000000000000
Size.Height = 22.000000000000000000
Size.PlatformDefault = False
TabOrder = 10
TabOrder = 9
Text = 'Clear'
OnClick = ClearButtonClick
end
@@ -95,84 +88,84 @@ object Form1: TForm1
Size.Width = 80.000000000000000000
Size.Height = 22.000000000000000000
Size.PlatformDefault = False
TabOrder = 12
TabOrder = 11
Text = 'Series'
OnClick = SeriesTestButtonClick
end
object OHLCButton: TButton
Position.X = 24.000000000000000000
Position.Y = 288.000000000000000000
TabOrder = 13
TabOrder = 12
Text = 'OHLC'
OnClick = OHLCButtonClick
end
object DebugBox: TCheckBox
Position.X = 24.000000000000000000
Position.Y = 439.000000000000000000
TabOrder = 14
Position.Y = 436.000000000000000000
TabOrder = 13
Text = 'Debug'
OnChange = DebugBoxChange
end
object FromJSONButton: TButton
Position.X = 24.000000000000000000
Position.Y = 569.000000000000000000
TabOrder = 15
TabOrder = 14
Text = 'From JSON'
OnClick = FromJSONButtonClick
end
object ToJSONButton: TButton
Position.X = 24.000000000000000000
Position.Y = 599.000000000000000000
TabOrder = 16
TabOrder = 15
Text = 'To JSON'
OnClick = ToJSONButtonClick
end
object ExternalFuncButton: TButton
Position.X = 24.000000000000000000
Position.Y = 318.000000000000000000
TabOrder = 17
TabOrder = 16
Text = 'External Func'
OnClick = ExternalFuncButtonClick
end
object InnerLambdaButton: TButton
Position.X = 24.000000000000000000
Position.Y = 348.000000000000000000
TabOrder = 18
TabOrder = 17
Text = 'Inner Lambda'
OnClick = InnerLambdaButtonClick
end
object DumpButton: TButton
Position.X = 24.000000000000000000
Position.Y = 509.000000000000000000
TabOrder = 19
TabOrder = 18
Text = 'Dump'
OnClick = DumpButtonClick
end
object FailingUpvalueButton: TButton
Position.X = 24.000000000000000000
Position.Y = 378.000000000000000000
TabOrder = 21
TabOrder = 20
Text = 'Upvalue'
OnClick = FailingUpvalueButtonClick
end
object TailCallButten: TButton
Position.X = 24.000000000000000000
Position.Y = 406.000000000000000000
TabOrder = 22
TabOrder = 21
Text = 'Tail calls'
OnClick = TailCallButtenClick
end
object SaveUserLibButton: TButton
Position.X = 24.000000000000000000
Position.Y = 629.000000000000000000
TabOrder = 23
TabOrder = 22
Text = 'Save Lib'
OnClick = SaveUserLibButtonClick
end
object LoadUserLibButton: TButton
Position.X = 24.000000000000000000
Position.Y = 659.000000000000000000
TabOrder = 24
TabOrder = 23
Text = 'LoadLib'
OnClick = LoadUserLibButtonClick
end
@@ -187,7 +180,7 @@ object Form1: TForm1
Size.Width = 113.000000000000000000
Size.Height = 184.000000000000000000
Size.PlatformDefault = False
TabOrder = 25
TabOrder = 24
OnChange = RTLListViewChange
end
end
@@ -197,6 +190,22 @@ object Form1: TForm1
Size.Height = 609.000000000000000000
Size.PlatformDefault = False
TabOrder = 3
object CompilerStageBox: TComboBox
Anchors = [akRight, akBottom]
Items.Strings = (
'Unbound'
'Expanded'
'Bound'
'Lowered')
ItemIndex = 0
Position.X = 672.000000000000000000
Position.Y = 576.000000000000000000
Size.Width = 104.000000000000000000
Size.Height = 25.000000000000000000
Size.PlatformDefault = False
TabOrder = 0
OnChange = CompilerStageBoxChange
end
end
object Memo1: TMemo
Touch.InteractiveGestures = [Pan, LongTap, DoubleTap]
@@ -236,10 +245,15 @@ object Form1: TForm1
Touch.InteractiveGestures = [Pan, LongTap, DoubleTap]
DataDetectorTypes = []
Lines.Strings = (
'(do'
'(defmacro test [body] `(fn [] ~body ))'
'(defmacro test2 [body] `(fn [] ~body ))'
'(test2 "hi")'
'(do '
' (defmacro test [body] `(fn [] ~body ))'
' (defmacro test2 [body] `(fn [] ~body ))'
' (test2 "hi")'
''
' (def debug-mode :true)'
' '
' (stopwatch '
' (if debug-mode (print "Debug an")))'
')')
StyledSettings = [Size, Style, FontColor]
TextSettings.Font.Family = 'Consolas'
+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