AST refactoring

This commit is contained in:
Michael Schimmel
2025-08-29 09:44:17 +02:00
parent 5593761551
commit fa9328a183
9 changed files with 524 additions and 514 deletions
+3 -1
View File
@@ -5,7 +5,9 @@ uses
FMX.Forms,
MainForm in 'MainForm.pas' {Form1},
Myc.Ast.Evaluator in '..\Src\AST\Myc.Ast.Evaluator.pas',
Myc.Ast.Printer in '..\Src\AST\Myc.Ast.Printer.pas';
Myc.Ast.Printer in '..\Src\AST\Myc.Ast.Printer.pas',
Myc.Ast.Nodes in '..\Src\AST\Myc.Ast.Nodes.pas',
Myc.Ast.Scope in '..\Src\AST\Myc.Ast.Scope.pas';
{$R *.res}
+2
View File
@@ -137,6 +137,8 @@
</DCCReference>
<DCCReference Include="..\Src\AST\Myc.Ast.Evaluator.pas"/>
<DCCReference Include="..\Src\AST\Myc.Ast.Printer.pas"/>
<DCCReference Include="..\Src\AST\Myc.Ast.Nodes.pas"/>
<DCCReference Include="..\Src\AST\Myc.Ast.Scope.pas"/>
<BuildConfiguration Include="Base">
<Key>Base</Key>
</BuildConfiguration>
-1
View File
@@ -8,7 +8,6 @@ object Form1: TForm1
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
OnCreate = FormCreate
OnDestroy = FormDestroy
DesignerMasterStyle = 0
object Panel1: TPanel
Align = Left
+58 -77
View File
@@ -19,6 +19,7 @@ uses
FMX.Memo,
FMX.Controls.Presentation,
Myc.Data.POD,
Myc.Ast.Nodes,
Myc.Ast,
Myc.Ast.Evaluator,
Myc.Ast.Printer; // Added for TExecutionScope
@@ -37,7 +38,6 @@ type
CrerateTriggerExampleButton: TButton;
DoTriggerButton: TButton;
DoTrigger2Button: TButton;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure CrerateTriggerExampleButtonClick(Sender: TObject);
procedure DebugButtonClick(Sender: TObject);
@@ -51,7 +51,7 @@ type
private
// Stores the last AST generated by Test1 or Test2 button clicks.
FLastAst: IAstNode;
FGScope: TExecutionScope;
FGScope: IExecutionScope;
public
{ Public declarations }
end;
@@ -62,23 +62,19 @@ var
implementation
uses
Myc.Ast.Scope,
System.Diagnostics; // For TStopwatch
{$R *.fmx}
procedure TForm1.FormDestroy(Sender: TObject);
begin
FGScope.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FGScope := TExecutionScope.Create(nil);
FGScope := T_ExecutionScope.Create(nil);
end;
procedure TForm1.DebugButtonClick(Sender: TObject);
var
scope: TExecutionScope;
scope: IExecutionScope;
visitor: IAstVisitor;
result: TAstValue;
sw: TStopwatch;
@@ -90,26 +86,22 @@ begin
exit;
end;
scope := TExecutionScope.Create(FGScope);
try
Memo1.Lines.Clear;
Memo1.Lines.Add('--- Debug Evaluator Trace ---');
scope := T_ExecutionScope.Create(FGScope);
visitor := TDebugEvaluatorVisitor.Create(scope, Memo1.Lines, ShowScopeBox.IsChecked, 0);
Memo1.Lines.Clear;
Memo1.Lines.Add('--- Debug Evaluator Trace ---');
sw := TStopwatch.StartNew;
result := FLastAst.Accept(visitor);
sw.Stop;
visitor := TDebugEvaluatorVisitor.Create(scope, Memo1.Lines, ShowScopeBox.IsChecked, 0);
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.)');
sw := TStopwatch.StartNew;
result := FLastAst.Accept(visitor);
sw.Stop;
finally
scope.Free;
end;
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.)');
end;
procedure TForm1.FibonacciButtonClick(Sender: TObject);
@@ -124,7 +116,7 @@ procedure TForm1.FibonacciButtonClick(Sender: TObject);
end;
var
scope: TExecutionScope;
scope: IExecutionScope;
visitor: IAstVisitor;
root: IExpressionNode;
result: TAstValue;
@@ -198,18 +190,15 @@ begin
);
FLastAst := root;
scope := TExecutionScope.Create(nil);
try
visitor := TEvaluatorVisitor.Create(scope);
result := root.Accept(visitor);
sw.Stop;
scope := T_ExecutionScope.Create(nil);
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;
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.)');
end;
procedure TForm1.PrettyPrintButtonClick(Sender: TObject);
@@ -242,7 +231,7 @@ end;
procedure TForm1.RecursionButtonClick(Sender: TObject);
var
scope: TExecutionScope;
scope: IExecutionScope;
visitor: IAstVisitor;
root: IExpressionNode;
result: TAstValue;
@@ -281,24 +270,21 @@ begin
);
FLastAst := root;
scope := TExecutionScope.Create(nil);
try
visitor := TEvaluatorVisitor.Create(scope);
result := root.Accept(visitor);
sw.Stop;
scope := T_ExecutionScope.Create(nil);
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;
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.)');
end;
procedure TForm1.Test1ButtonClick(Sender: TObject);
var
root: IAstNode;
scope: TExecutionScope;
scope: IExecutionScope;
visitor: IAstVisitor;
result: TAstValue;
sw: TStopwatch;
@@ -320,26 +306,23 @@ begin
);
FLastAst := root;
scope := TExecutionScope.Create(nil);
try
visitor := TEvaluatorVisitor.Create(scope);
result := root.Accept(visitor);
sw.Stop;
scope := T_ExecutionScope.Create(FGScope);
if not result.IsUndefined then
Memo1.Lines.Add(Format('Result: %s (calculated in %d ms)', [result.ToString, sw.ElapsedMilliseconds]))
else
Memo1.Lines.Add(Format('<undefined result> (calculated in %d ms)', [sw.ElapsedMilliseconds]));
Memo1.Lines.Add('');
Memo1.Lines.Add('(AST structure stored. Click "Pretty Print" to view.)');
finally
scope.Free;
end;
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('<undefined result> (calculated in %d ms)', [sw.ElapsedMilliseconds]));
Memo1.Lines.Add('');
Memo1.Lines.Add('(AST structure stored. Click "Pretty Print" to view.)');
end;
procedure TForm1.Test2ButtonClick(Sender: TObject);
var
scope: TExecutionScope;
scope: IExecutionScope;
visitor: IAstVisitor;
root: IExpressionNode;
result: TAstValue;
@@ -373,19 +356,17 @@ begin
);
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;
scope := T_ExecutionScope.Create(FGScope);
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.)');
end;
procedure TForm1.CrerateTriggerExampleButtonClick(Sender: TObject);