Refactoring for immutable nodes

This commit is contained in:
Michael Schimmel
2025-11-05 13:21:17 +01:00
parent 9bd2d6f7ab
commit c0a689d2bc
13 changed files with 217 additions and 190 deletions
+9 -18
View File
@@ -87,28 +87,19 @@ begin
end;
function TTypeChecker.VisitConstant(const Node: IConstantNode): IAstNode;
var
constType: IStaticType;
begin
// This is a leaf node.
// Assign the type based on the literal value
case Node.Value.Kind of
TDataValueKind.vkScalar: constType := TTypes.FromScalarKind(Node.Value.AsScalar.Kind);
TDataValueKind.vkText: constType := TTypes.Text;
TDataValueKind.vkVoid: constType := TTypes.Void;
else
constType := TTypes.Unknown;
end;
// Create a new node with the correct type
Result := TConstantNode.Create(Node.Value, constType);
// If the constructor couldn't set the type, nobody can
Assert(Node.StaticType.Kind <> stUnknown);
Result := Node;
end;
function TTypeChecker.VisitKeyword(const Node: IKeywordNode): IAstNode;
begin
// This is a leaf node.
// The TKeywordNode constructor *forces* the type to be TTypes.Keyword.
Result := TKeywordNode.Create(Node.Value);
Assert(Node.StaticType.Kind = stKeyword);
Result := Node;
end;
function TTypeChecker.VisitIdentifier(const Node: IIdentifierNode): IAstNode;
@@ -123,7 +114,7 @@ begin
adr := Node.AsBoundIdentifierNode.Address;
// Create a new node, copying the address and assigning the inferred type
Result := TBoundIdentifierNode.Create(Node.Name, adr, symbol.StaticType);
Result := TAst.BoundIdentifier(Node.Name, adr, symbol.StaticType);
end;
function TTypeChecker.VisitRecurNode(const Node: IRecurNode): IAstNode;
@@ -169,7 +160,7 @@ begin
FCurrentDescriptor.UpdateType(adr.SlotIndex, initType);
// 5. Create the new (typed) identifier node
newIdent := TBoundIdentifierNode.Create(boundIdent.Name, adr, initType);
newIdent := TAst.BoundIdentifier(boundIdent.Name, adr, initType);
// 6. Create the new VariableDeclaration node
Result := TVariableDeclarationNode.Create(newIdent.AsIdentifier, newInitializer, initType);
@@ -205,7 +196,7 @@ begin
FCurrentDescriptor.UpdateType(adr.SlotIndex, sourceType);
// Re-create the identifier node *with the new type*
newIdent := TBoundIdentifierNode.Create(newIdent.AsIdentifier.Name, adr, sourceType);
newIdent := TAst.BoundIdentifier(newIdent.AsIdentifier.Name, adr, sourceType);
targetType := sourceType;
end;
@@ -239,7 +230,7 @@ begin
// (even if they are just TTypes.Unknown for now, for type inference placeholders)
var paramIdent := boundNode.Parameters[i];
var paramAdr := paramIdent.AsBoundIdentifierNode.Address;
var newParam := TBoundIdentifierNode.Create(paramIdent.Name, paramAdr, TTypes.Unknown);
var newParam := TAst.BoundIdentifier(paramIdent.Name, paramAdr);
newParams[i] := newParam;
paramTypes[i] := TTypes.Unknown;