This commit is contained in:
nub31
2026-02-09 21:16:03 +01:00
parent ea3d374831
commit 00a172b922
3 changed files with 32 additions and 29 deletions

View File

@@ -20,29 +20,29 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
public sealed class Module
{
private readonly Dictionary<string, NubTypeStruct> structTypes = new();
private readonly Dictionary<string, NubType> customTypes = new();
private readonly Dictionary<string, NubType> identifierTypes = new();
public List<NubTypeStruct> GetStructTypes()
public List<NubType> GetCustomTypes()
{
return structTypes.Values.ToList();
return customTypes.Values.ToList();
}
public bool TryResolveStructType(string structName, [NotNullWhen(true)] out NubTypeStruct? structType)
public bool TryResolveCustomType(string name, [NotNullWhen(true)] out NubType? customType)
{
structType = structTypes.GetValueOrDefault(structName);
return structType != null;
customType = customTypes.GetValueOrDefault(name);
return customType != null;
}
public bool TryResolveIdentifierType(string identifierName, [NotNullWhen(true)] out NubType? identifier)
public bool TryResolveIdentifierType(string name, [NotNullWhen(true)] out NubType? identifier)
{
identifier = identifierTypes.GetValueOrDefault(identifierName);
identifier = identifierTypes.GetValueOrDefault(name);
return identifier != null;
}
public void AddStruct(string name, NubTypeStruct structType)
public void AddCustomType(string name, NubType type)
{
structTypes.Add(name, structType);
customTypes.Add(name, type);
}
public void AddIdentifier(string name, NubType identifier)
@@ -98,7 +98,7 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
if (module == null) continue;
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
module.AddStruct(structDef.Name.Ident, new NubTypeStruct());
module.AddCustomType(structDef.Name.Ident, new NubTypeStruct());
}
// Third pass: Resolve struct fields
@@ -109,11 +109,11 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
{
if (!module.TryResolveStructType(structDef.Name.Ident, out var structType))
throw new UnreachableException($"{nameof(structType)} should always be registered");
if (!module.TryResolveCustomType(structDef.Name.Ident, out var customType))
throw new UnreachableException($"{nameof(customType)} should always be registered");
var fields = structDef.Fields.Select(f => new NubTypeStruct.Field(f.Name.Ident, Resolve(f.Type))).ToList();
structType.ResolveFields(fields);
var fields = structDef.Fields.Select(f => new NubTypeStruct.Field(f.Name.Ident, ResolveType(f.Type))).ToList();
((NubTypeStruct)customType).ResolveFields(fields);
}
}
@@ -125,8 +125,8 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
foreach (var funcDef in ast.Definitions.OfType<NodeDefinitionFunc>())
{
var parameters = funcDef.Parameters.Select(x => Resolve(x.Type)).ToList();
var returnType = Resolve(funcDef.ReturnType);
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type)).ToList();
var returnType = ResolveType(funcDef.ReturnType);
var funcType = new NubTypeFunc(parameters, returnType);
module.AddIdentifier(funcDef.Name.Ident, funcType);
}
@@ -134,14 +134,14 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
return new ModuleGraph(modules);
NubType Resolve(NodeType node)
NubType ResolveType(NodeType node)
{
return node switch
{
NodeTypeBool => new NubTypeBool(),
NodeTypeCustom type => ResolveStruct(type),
NodeTypeFunc type => new NubTypeFunc(type.Parameters.Select(Resolve).ToList(), Resolve(type.ReturnType)),
NodeTypePointer type => new NubTypePointer(Resolve(type.To)),
NodeTypeCustom type => ResolveCustomType(type),
NodeTypeFunc type => new NubTypeFunc(type.Parameters.Select(ResolveType).ToList(), ResolveType(type.ReturnType)),
NodeTypePointer type => new NubTypePointer(ResolveType(type.To)),
NodeTypeSInt type => new NubTypeSInt(type.Width),
NodeTypeUInt type => new NubTypeUInt(type.Width),
NodeTypeString => new NubTypeString(),
@@ -150,16 +150,16 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
};
}
NubTypeStruct ResolveStruct(NodeTypeCustom type)
NubType ResolveCustomType(NodeTypeCustom type)
{
var module = modules.GetValueOrDefault(type.Module.Ident);
if (module == null)
throw new CompileException(Diagnostic.Error($"Unknown module: {type.Module.Ident}").Build());
if (!module.TryResolveStructType(type.Name.Ident, out var structType))
if (!module.TryResolveCustomType(type.Name.Ident, out var customType))
throw new CompileException(Diagnostic.Error($"Unknown custom type: {type.Module.Ident}::{type.Name.Ident}").Build());
return structType;
return customType;
}
}
}