Static specialization WIP

This commit is contained in:
Michael Schimmel
2025-11-19 14:38:40 +01:00
parent c129c1a3ae
commit 138e7ac454
26 changed files with 2349 additions and 1110 deletions
-57
View File
@@ -27,8 +27,6 @@ type
FBinaryOperators: TDictionary<string, TScalar.TBinaryOp>;
FUnaryOperators: TDictionary<string, TScalar.TUnaryOp>;
protected
// Override to find nodes to lower
function VisitFunctionCall(const Node: IFunctionCallNode): IAstNode; override;
public
constructor Create;
destructor Destroy; override;
@@ -46,9 +44,6 @@ uses
{ TAstLowerer }
constructor TAstLowerer.Create;
var
op: TScalar.TBinaryOp;
uOp: TScalar.TUnaryOp;
begin
inherited Create;
@@ -91,56 +86,4 @@ begin
Result := TAst.Block([]);
end;
function TAstLowerer.VisitFunctionCall(const Node: IFunctionCallNode): IAstNode;
var
calleeIdentifier: IIdentifierNode;
binaryOp: TScalar.TBinaryOp;
unaryOp: TScalar.TUnaryOp;
left, right: IAstNode;
nodeType: IStaticType;
begin
// --- Optimization: Operator Folding ---
if Node.Callee.Kind = akIdentifier then
begin
calleeIdentifier := Node.Callee.AsIdentifier;
nodeType := Node.StaticType; // Get type from (un-lowered) node
if (Length(Node.Arguments) = 2) then
begin
if FBinaryOperators.TryGetValue(calleeIdentifier.Name, binaryOp) then
begin
// Note: We MUST visit children *before* creating the new node
left := Accept(Node.Arguments[0]);
right := Accept(Node.Arguments[1]);
// Call constructor directly, passing the inferred type
Result := TAst.BinaryExpr(left, binaryOp, right, nodeType);
exit;
end;
end;
if (Length(Node.Arguments) = 1) then
begin
if FUnaryOperators.TryGetValue(calleeIdentifier.Name, unaryOp) then
begin
right := Accept(Node.Arguments[0]);
// Call constructor directly, passing the inferred type
Result := TAst.UnaryExpr(unaryOp, right, nodeType);
exit;
end;
if (calleeIdentifier.Name = '-') then
begin
right := Accept(Node.Arguments[0]);
// Call constructor directly, passing the inferred type
Result := TAst.UnaryExpr(TScalar.TUnaryOp.Negate, right, nodeType);
exit;
end;
end;
end;
// If no rewrite matched, continue traversal using the base implementation
Result := inherited VisitFunctionCall(Node);
end;
end.