Binder refactoring, Monster refactoring

This commit is contained in:
Michael Schimmel
2025-11-02 19:38:52 +01:00
parent 8f29212cba
commit ea39a57b77
22 changed files with 3061 additions and 2638 deletions
+23 -27
View File
@@ -12,8 +12,7 @@ uses
Myc.Ast.Visitor,
Myc.Ast.Scope,
Myc.Ast.Types,
Myc.Ast,
Myc.Ast.Binding.Nodes;
Myc.Ast;
type
IAstLowerer = interface(IAstVisitor)
@@ -30,7 +29,7 @@ type
function SetType(const Node: IAstNode; const AType: IStaticType): IAstNode;
protected
// Override to find nodes to lower
function VisitFunctionCall(const Node: IFunctionCallNode): TDataValue; override;
function VisitFunctionCall(const Node: IFunctionCallNode): IAstNode; override;
public
constructor Create;
destructor Destroy; override;
@@ -50,6 +49,7 @@ uses
constructor TAstLowerer.Create;
var
op: TScalar.TBinaryOp;
uOp: TScalar.TUnaryOp; // Added for unary
begin
inherited Create;
@@ -59,7 +59,8 @@ begin
FBinaryOperators.Add(op.ToString, op);
FUnaryOperators := TDictionary<string, TScalar.TUnaryOp>.Create;
FUnaryOperators.Add('not', TScalar.TUnaryOp.Not);
for uOp := Low(TScalar.TUnaryOp) to High(TScalar.TUnaryOp) do // Changed
FUnaryOperators.Add(uOp.ToString, uOp);
// Note: '-' is handled as a special case in VisitFunctionCall
end;
@@ -78,11 +79,9 @@ end;
function TAstLowerer.Execute(const RootNode: IAstNode): IAstNode;
begin
var transformedValue := Accept(RootNode);
if transformedValue.IsVoid then
Result := TAst.Block([])
else
Result := transformedValue.AsIntf<IAstNode>;
Result := Accept(RootNode); // Use IAstNode-returning Accept
if not Assigned(Result) then
Result := TAst.Block([]);
if Assigned(Result) then
(Result as TAstNode).StaticType := (Result as TAstNode).StaticType;
@@ -90,25 +89,18 @@ end;
function TAstLowerer.SetType(const Node: IAstNode; const AType: IStaticType): IAstNode;
begin
(Node as TAstNode).StaticType := AType;
if Assigned(Node) then // Add nil check for safety
(Node as TAstNode).StaticType := AType;
Result := Node;
end;
function TAstLowerer.VisitFunctionCall(const Node: IFunctionCallNode): TDataValue;
function TAstLowerer.VisitFunctionCall(const Node: IFunctionCallNode): IAstNode;
var
calleeIdentifier: TIdentifierNode;
binaryOp: TScalar.TBinaryOp;
unaryOp: TScalar.TUnaryOp;
left, right: IAstNode;
begin
// --- Transformation: Keyword-as-Function ---
// (This logic is correctly handled in TAstBinder)
if (Node.Callee is TKeywordNode) then
begin
// This node should have been transformed by the TAstBinder.
exit(inherited VisitFunctionCall(Node));
end;
// --- Optimization: Operator Folding ---
if (Node.Callee is TIdentifierNode) then
begin
@@ -118,10 +110,12 @@ begin
begin
if FBinaryOperators.TryGetValue(calleeIdentifier.Name, binaryOp) then
begin
left := Accept(Node.Arguments[0]).AsIntf<IAstNode>;
right := Accept(Node.Arguments[1]).AsIntf<IAstNode>;
// Note: We MUST visit children *before* creating the new node
left := Accept(Node.Arguments[0]);
right := Accept(Node.Arguments[1]);
var binExpr := TAst.BinaryExpr(left, binaryOp, right);
Result := TDataValue.FromIntf<IAstNode>(SetType(binExpr, (Node as TAstNode).StaticType));
// Copy metadata (StaticType) from old node to new node
Result := SetType(binExpr, (Node as TAstNode).StaticType);
exit;
end;
end;
@@ -130,23 +124,25 @@ begin
begin
if FUnaryOperators.TryGetValue(calleeIdentifier.Name, unaryOp) then
begin
right := Accept(Node.Arguments[0]).AsIntf<IAstNode>;
right := Accept(Node.Arguments[0]);
var unExpr := TAst.UnaryExpr(unaryOp, right);
Result := TDataValue.FromIntf<IAstNode>(SetType(unExpr, (Node as TAstNode).StaticType));
// Copy metadata
Result := SetType(unExpr, (Node as TAstNode).StaticType);
exit;
end;
if (calleeIdentifier.Name = '-') then
begin
right := Accept(Node.Arguments[0]).AsIntf<IAstNode>;
right := Accept(Node.Arguments[0]);
var unExpr := TAst.UnaryExpr(TScalar.TUnaryOp.Negate, right);
Result := TDataValue.FromIntf<IAstNode>(SetType(unExpr, (Node as TAstNode).StaticType));
// Copy metadata
Result := SetType(unExpr, (Node as TAstNode).StaticType);
exit;
end;
end;
end;
// If no rewrite matched, continue traversal
// If no rewrite matched, continue traversal using the base implementation
Result := inherited VisitFunctionCall(Node);
end;