AST refactoring

This commit is contained in:
Michael Schimmel
2025-08-28 16:56:26 +02:00
parent 9a5f2c1b1d
commit 5451f4fed9
3 changed files with 26 additions and 5 deletions
+1
View File
@@ -145,6 +145,7 @@ begin
Memo1.Lines.Add('');
Memo1.Lines.Add('');
Memo1.Lines.Add('--- Recursive fib with AST---');
Application.ProcessMessages; // Update UI before blocking
sw.Start;
root :=
+20 -1
View File
@@ -38,6 +38,7 @@ type
procedure Indent;
procedure Unindent;
procedure AppendLine(const S: string);
procedure AppendMultiline(const S: string);
procedure ShowScope;
protected
function CreateVisitorForScope(AScope: TExecutionScope): IAstVisitor; override;
@@ -57,7 +58,8 @@ type
implementation
uses
Myc.Data.Decimal;
Myc.Data.Decimal,
Myc.Ast.Printer;
type
// Concrete implementation of the IEvaluatorClosure interface, private to this unit.
@@ -349,6 +351,18 @@ begin
FLog.Add(pad + S);
end;
procedure TDebugEvaluatorVisitor.AppendMultiline(const S: string);
begin
var Str := TStringList.Create;
try
Str.Text := s;
for var i := 0 to Str.Count - 1 do
AppendLine(Str[i]);
finally
Str.Free;
end;
end;
procedure TDebugEvaluatorVisitor.ShowScope;
var
scopeDump: TArray<string>;
@@ -419,6 +433,11 @@ begin
AppendLine('LambdaExpr{');
Indent;
try
var pp: IAstVisitor := TPrettyPrintVisitor.Create(0);
pp.VisitLambdaExpression(Node);
AppendMultiline((pp as TPrettyPrintVisitor).GetResult);
Result := inherited VisitLambdaExpression(Node);
finally
Unindent;
+5 -4
View File
@@ -17,7 +17,7 @@ type
procedure Unindent;
procedure AppendLine(const S: string);
public
constructor Create;
constructor Create(AIndentLevel: Integer = 0);
destructor Destroy; override;
function GetResult: string;
// IAstVisitor
@@ -40,11 +40,12 @@ uses
{ TPrettyPrintVisitor }
constructor TPrettyPrintVisitor.Create;
constructor TPrettyPrintVisitor.Create(AIndentLevel: Integer);
begin
inherited Create;
FBuilder := TStringBuilder.Create;
FIndentLevel := 0;
FIndentLevel := AIndentLevel;
end;
destructor TPrettyPrintVisitor.Destroy;
@@ -88,7 +89,7 @@ end;
function TPrettyPrintVisitor.VisitBinaryExpression(const Node: IBinaryExpressionNode): TAstValue;
begin
AppendLine(Format('BinaryExpr (%s)', [Node.Operator.ToString]));
AppendLine(Format('BinaryExpr "%s"', [Node.Operator.ToString]));
Indent;
Node.Left.Accept(Self);
Node.Right.Accept(Self);
@@ -98,7 +99,7 @@ end;
function TPrettyPrintVisitor.VisitUnaryExpression(const Node: IUnaryExpressionNode): TAstValue;
begin
AppendLine(Format('UnaryExpr (%s)', [Node.Operator.ToString]));
AppendLine(Format('UnaryExpr "%s"', [Node.Operator.ToString]));
Indent;
Node.Right.Accept(Self);
Unindent;