unit MainForm; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Memo.Types, FMX.StdCtrls, FMX.ScrollBox, FMX.Memo, FMX.Controls.Presentation, Myc.Data.POD, Myc.Ast, Myc.Ast.Evaluator, Myc.Ast.Printer; // Added for TExecutionScope type TForm1 = class(TForm) Panel1: TPanel; Memo1: TMemo; Test1Button: TButton; Test2Button: TButton; PrettyPrintButton: TButton; DebugButton: TButton; RecursionButton: TButton; ShowScopeBox: TCheckBox; FibonacciButton: TButton; procedure DebugButtonClick(Sender: TObject); procedure FibonacciButtonClick(Sender: TObject); procedure PrettyPrintButtonClick(Sender: TObject); procedure RecursionButtonClick(Sender: TObject); procedure Test1ButtonClick(Sender: TObject); procedure Test2ButtonClick(Sender: TObject); private // Stores the last AST generated by Test1 or Test2 button clicks. FLastAst: IAstNode; public { Public declarations } end; var Form1: TForm1; implementation uses System.Diagnostics; // For TStopwatch {$R *.fmx} procedure TForm1.DebugButtonClick(Sender: TObject); var scope: TExecutionScope; visitor: IAstVisitor; result: TAstValue; sw: TStopwatch; begin if not Assigned(FLastAst) then begin Memo1.Lines.Add('No AST has been generated yet.'); Memo1.Lines.Add('Click "Test 1" or "Test 2" first.'); exit; end; scope := TExecutionScope.Create(nil); try Memo1.Lines.Clear; Memo1.Lines.Add('--- Debug Evaluator Trace ---'); visitor := TDebugEvaluatorVisitor.Create(scope, Memo1.Lines, ShowScopeBox.IsChecked, 0); sw := TStopwatch.StartNew; result := FLastAst.Accept(visitor); sw.Stop; Memo1.Lines.Add('-----------------------------'); Memo1.Lines.Add(Format('Final script result: %s', [result.ToString])); Memo1.Lines.Add(Format('Execution time: %d ms', [sw.ElapsedMilliseconds])); Memo1.Lines.Add(''); Memo1.Lines.Add('(AST structure stored. Click "Pretty Print" to view.)'); finally scope.Free; end; end; procedure TForm1.FibonacciButtonClick(Sender: TObject); function NativeFib(n: Integer): Int64; begin // The identical, naive recursive algorithm in native Delphi code. if (n < 2) then Result := n else Result := NativeFib(n - 1) + NativeFib(n - 2); end; var scope: TExecutionScope; visitor: IAstVisitor; root: IExpressionNode; result: TAstValue; sw: TStopwatch; result20, result30, result40: Int64; time20, time30, time40: Int64; begin Memo1.Lines.Clear; Memo1.Lines.Add('--- Native Delphi Fibonacci Performance ---'); Memo1.Lines.Add('Calculating fib(30) and fib(40)...'); Application.ProcessMessages; // Update UI before blocking // --- Calculate fib(30) --- sw := TStopwatch.StartNew; result20 := NativeFib(20); sw.Stop; time20 := sw.ElapsedMilliseconds; // --- Calculate fib(30) --- sw := TStopwatch.StartNew; result30 := NativeFib(30); sw.Stop; time30 := sw.ElapsedMilliseconds; // --- Calculate fib(40) --- sw.Reset; sw.Start; result40 := NativeFib(40); sw.Stop; time40 := sw.ElapsedMilliseconds; sw.Reset; Memo1.Lines.Add(''); Memo1.Lines.Add(Format('fib(20) = %d (calculated in %d ms)', [result20, time20])); Memo1.Lines.Add(Format('fib(30) = %d (calculated in %d ms)', [result30, time30])); Memo1.Lines.Add(Format('fib(40) = %d (calculated in %d ms)', [result40, time40])); Memo1.Lines.Add(''); Memo1.Lines.Add(''); Memo1.Lines.Add('--- Recursive fib with AST---'); sw.Start; root := TAst.Block( [ TAst.VarDecl( TAst.Identifier('fib'), TAst.LambdaExpr( [TAst.Identifier('n')], TAst.IfExpr( TAst.BinaryExpr(TAst.Identifier('n'), boLess, TAst.Constant(TScalar.FromInt64(2))), TAst.Identifier('n'), TAst.BinaryExpr( TAst.FunctionCall( TAst.Identifier('fib'), [TAst.BinaryExpr(TAst.Identifier('n'), boSubtract, TAst.Constant(TScalar.FromInt64(1)))] ), boAdd, TAst.FunctionCall( TAst.Identifier('fib'), [TAst.BinaryExpr(TAst.Identifier('n'), boSubtract, TAst.Constant(TScalar.FromInt64(2)))] ) ) ) ) ), TAst.FunctionCall(TAst.Identifier('fib'), [TAst.Constant(TScalar.FromInt64(30))]) ] ); FLastAst := root; scope := TExecutionScope.Create(nil); try visitor := TEvaluatorVisitor.Create(scope); result := root.Accept(visitor); sw.Stop; Memo1.Lines.Add(Format('Result: fib(30) %s (calculated in %d ms)', [result.ToString, sw.ElapsedMilliseconds])); Memo1.Lines.Add(''); Memo1.Lines.Add('(AST structure stored. Click "Pretty Print" or "Debug" to view.)'); finally scope.Free; end; end; procedure TForm1.PrettyPrintButtonClick(Sender: TObject); var visitor: TPrettyPrintVisitor; sw: TStopwatch; begin Memo1.Lines.Clear; Memo1.Lines.Add('--- AST Pretty Print ---'); if not Assigned(FLastAst) then begin Memo1.Lines.Add('No AST has been generated yet.'); Memo1.Lines.Add('Click "Test 1" or "Test 2" first.'); exit; end; visitor := TPrettyPrintVisitor.Create; try sw := TStopwatch.StartNew; FLastAst.Accept(visitor); sw.Stop; Memo1.Lines.Add(visitor.GetResult); Memo1.Lines.Add(Format('(AST rendered in %d ms)', [sw.ElapsedMilliseconds])); finally // Visitor is an interfaced object and managed automatically. end; end; procedure TForm1.RecursionButtonClick(Sender: TObject); var scope: TExecutionScope; visitor: IAstVisitor; root: IExpressionNode; result: TAstValue; sw: TStopwatch; begin sw := TStopwatch.Create; Memo1.Lines.Clear; Memo1.Lines.Add('--- Recursive factorial(20) ---'); sw.Start; root := TAst.Block( [ TAst.VarDecl( TAst.Identifier('factorial'), TAst.LambdaExpr( [TAst.Identifier('n')], TAst.IfExpr( TAst.BinaryExpr(TAst.Identifier('n'), boLess, TAst.Constant(TScalar.FromInt64(2))), TAst.Constant(TScalar.FromInt64(1)), TAst.BinaryExpr( TAst.Identifier('n'), boMultiply, TAst.FunctionCall( TAst.Identifier('factorial'), [TAst.BinaryExpr(TAst.Identifier('n'), boSubtract, TAst.Constant(TScalar.FromInt64(1)))] ) ) ) ) ), TAst.FunctionCall(TAst.Identifier('factorial'), [TAst.Constant(TScalar.FromInt64(20))]) ] ); FLastAst := root; scope := TExecutionScope.Create(nil); try visitor := TEvaluatorVisitor.Create(scope); result := root.Accept(visitor); sw.Stop; Memo1.Lines.Add(Format('Result: %s (calculated in %d ms)', [result.ToString, sw.ElapsedMilliseconds])); Memo1.Lines.Add(''); Memo1.Lines.Add('(AST structure stored. Click "Pretty Print" or "Debug" to view.)'); finally scope.Free; end; end; procedure TForm1.Test1ButtonClick(Sender: TObject); var root: IAstNode; scope: TExecutionScope; visitor: IAstVisitor; result: TAstValue; sw: TStopwatch; begin sw := TStopwatch.Create; Memo1.Lines.Clear; Memo1.Lines.Add('--- Simple AST Execution ---'); sw.Start; root := TAst.Block( [ TAst.VarDecl(TAst.Identifier('a'), TAst.Constant(TScalar.FromInt64(10))), TAst.VarDecl(TAst.Identifier('b'), TAst.BinaryExpr(TAst.Identifier('a'), boMultiply, TAst.Constant(TScalar.FromInt64(2)))), TAst.BinaryExpr(TAst.Identifier('a'), boAdd, TAst.Identifier('b')) ] ); FLastAst := root; scope := TExecutionScope.Create(nil); try visitor := TEvaluatorVisitor.Create(scope); result := root.Accept(visitor); sw.Stop; if not result.IsUndefined then Memo1.Lines.Add(Format('Result: %s (calculated in %d ms)', [result.ToString, sw.ElapsedMilliseconds])) else Memo1.Lines.Add(Format(' (calculated in %d ms)', [sw.ElapsedMilliseconds])); Memo1.Lines.Add(''); Memo1.Lines.Add('(AST structure stored. Click "Pretty Print" to view.)'); finally scope.Free; end; end; procedure TForm1.Test2ButtonClick(Sender: TObject); var scope: TExecutionScope; visitor: IAstVisitor; root: IExpressionNode; result: TAstValue; sw: TStopwatch; begin sw := TStopwatch.Create; Memo1.Lines.Clear; Memo1.Lines.Add('--- Factory Pattern Demo ---'); sw.Start; root := TAst.Block( [ TAst.VarDecl( TAst.Identifier('createStrategyInstance'), TAst.LambdaExpr( [TAst.Identifier('offset')], TAst.Block( [ TAst.VarDecl(TAst.Identifier('baseValue'), TAst.Constant(TScalar.FromInt64(100))), TAst.BinaryExpr(TAst.Identifier('baseValue'), boAdd, TAst.Identifier('offset')) ] ) ) ), TAst.FunctionCall(TAst.Identifier('createStrategyInstance'), [TAst.Constant(TScalar.FromInt64(20))]), TAst.FunctionCall(TAst.Identifier('createStrategyInstance'), [TAst.Constant(TScalar.FromInt64(55))]) ] ); FLastAst := root; scope := TExecutionScope.Create(nil); try visitor := TEvaluatorVisitor.Create(scope); result := root.Accept(visitor); sw.Stop; Memo1.Lines.Add('The entire script has been executed.'); Memo1.Lines.Add(Format('Result of the final expression: %s (calculated in %d ms)', [result.ToString, sw.ElapsedMilliseconds])); Memo1.Lines.Add(''); Memo1.Lines.Add('(AST structure stored. Click "Pretty Print" to view.)'); finally scope.Free; end; end; end.