From 3ae1ed8f482847d5928cfe6487a300cdc88c2287 Mon Sep 17 00:00:00 2001 From: Michael Schimmel Date: Wed, 19 Nov 2025 15:47:09 +0100 Subject: [PATCH] Static specialization WIP Fix in ScopeDescriptor --- Src/AST/Myc.Ast.Nodes.pas | 2 +- Src/AST/Myc.Ast.Scope.pas | 2 +- Src/AST/Myc.Ast.Visitor.pas | 4 ++-- Src/AST/Myc.Ast.pas | 25 ++++++++++++------------- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Src/AST/Myc.Ast.Nodes.pas b/Src/AST/Myc.Ast.Nodes.pas index 6fcbf1f..b19c713 100644 --- a/Src/AST/Myc.Ast.Nodes.pas +++ b/Src/AST/Myc.Ast.Nodes.pas @@ -289,7 +289,7 @@ type property Value: IAstNode read GetValue; end; - IMacroDefinitionNode = interface(IAstNode) + IMacroDefinitionNode = interface(IAstTypedNode) {$region 'private'} function GetName: IIdentifierNode; function GetParameters: TArray; diff --git a/Src/AST/Myc.Ast.Scope.pas b/Src/AST/Myc.Ast.Scope.pas index 95414ac..c81baf0 100644 --- a/Src/AST/Myc.Ast.Scope.pas +++ b/Src/AST/Myc.Ast.Scope.pas @@ -273,7 +273,7 @@ begin if Assigned(ADescriptor) then FDescriptor := ADescriptor else if Assigned(AParent) then - FDescriptor := AParent.CreateDescriptor // Inherit descriptor if nil + FDescriptor := TScopeDescriptor.Create(AParent.CreateDescriptor) else FDescriptor := TScope.CreateDescriptor(nil); // Create new root descriptor diff --git a/Src/AST/Myc.Ast.Visitor.pas b/Src/AST/Myc.Ast.Visitor.pas index f5f1704..0bba394 100644 --- a/Src/AST/Myc.Ast.Visitor.pas +++ b/Src/AST/Myc.Ast.Visitor.pas @@ -509,10 +509,10 @@ end; function TAstTransformer.VisitMacroExpansionNode(const Node: IMacroExpansionNode): IAstNode; var - newBody: IAstTypedNode; + newBody: IAstNode; begin // Visit the body and check if it changed - newBody := Accept(Node.ExpandedBody).AsTypedNode; + newBody := Accept(Node.ExpandedBody); if newBody = Node.ExpandedBody then exit(Node); diff --git a/Src/AST/Myc.Ast.pas b/Src/AST/Myc.Ast.pas index 2b1d74c..55a5fa8 100644 --- a/Src/AST/Myc.Ast.pas +++ b/Src/AST/Myc.Ast.pas @@ -298,7 +298,7 @@ type property Value: IKeyword read FValue; end; - TMacroDefinitionNode = class(TAstNode, IMacroDefinitionNode) + TMacroDefinitionNode = class(TAstTypedNode, IMacroDefinitionNode) private FName: IIdentifierNode; FParameters: TArray; @@ -355,16 +355,16 @@ type TMacroExpansionNode = class(TAstTypedNode, IMacroExpansionNode) private FCallNode: IFunctionCallNode; - FExpandedBody: IAstTypedNode; + FExpandedBody: IAstNode; function GetCallNode: IFunctionCallNode; function GetExpandedBody: IAstNode; function GetKind: TAstNodeKind; override; public - constructor Create(const ACallNode: IFunctionCallNode; const AExpandedBody: IAstTypedNode); + constructor Create(const ACallNode: IFunctionCallNode; const AExpandedBody: IAstNode); function Accept(const Visitor: IAstVisitor): TDataValue; override; function AsMacroExpansion: IMacroExpansionNode; override; property CallNode: IFunctionCallNode read GetCallNode; - property ExpandedBody: IAstTypedNode read FExpandedBody; + property ExpandedBody: IAstNode read FExpandedBody; end; TRecurNode = class(TAstTypedNode, IRecurNode) @@ -788,11 +788,7 @@ end; class function TAst.MacroExpansionNode(const AOriginalCallNode: IFunctionCallNode; const AExpandedBody: IAstNode): IMacroExpansionNode; begin - // A MacroExpansionNode MUST have a typed body. - if not AExpandedBody.IsTyped then - raise ETypeException.Create('MacroExpansionNode cannot be created with an untyped body.'); - - Result := TMacroExpansionNode.Create(AOriginalCallNode, AExpandedBody.AsTypedNode); + Result := TMacroExpansionNode.Create(AOriginalCallNode, AExpandedBody); end; class function TAst.MemberAccess( @@ -1316,7 +1312,7 @@ end; constructor TMacroDefinitionNode.Create(const AName: IIdentifierNode; const AParameters: TArray; const ABody: IAstNode); begin - inherited Create; + inherited Create(TTypes.Void); FName := AName; FParameters := AParameters; FBody := ABody; @@ -1490,10 +1486,13 @@ end; { TMacroExpansionNode } -constructor TMacroExpansionNode.Create(const ACallNode: IFunctionCallNode; const AExpandedBody: IAstTypedNode); +constructor TMacroExpansionNode.Create(const ACallNode: IFunctionCallNode; const AExpandedBody: IAstNode); begin - // The StaticType of the wrapper *is* the StaticType of the body - inherited Create(AExpandedBody.StaticType); + if AExpandedBody.IsTyped then + inherited Create(AExpandedBody.AsTypedNode.StaticType) + else + inherited Create(TTypes.Unknown); + FExpandedBody := AExpandedBody; FCallNode := ACallNode; end;