From d96e6cf41a6643c62edeaa4cf4d329246d56fb23 Mon Sep 17 00:00:00 2001 From: nub31 Date: Sat, 20 Sep 2025 23:32:19 +0200 Subject: [PATCH] ... --- compiler/NubLang/Modules/Module.cs | 15 ++++++++---- compiler/NubLang/Modules/ModuleRepository.cs | 25 +++++++++++++------- compiler/NubLang/TypeChecking/TypeChecker.cs | 9 +++++-- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/compiler/NubLang/Modules/Module.cs b/compiler/NubLang/Modules/Module.cs index c1cbb96..04ebfc9 100644 --- a/compiler/NubLang/Modules/Module.cs +++ b/compiler/NubLang/Modules/Module.cs @@ -1,10 +1,12 @@ using NubLang.Parsing.Syntax; +using NubLang.TypeChecking.Node; namespace NubLang.Modules; public class Module { private readonly List _structs = []; + private readonly List _structTemplates = []; private readonly List _functions = []; public void RegisterStruct(bool exported, string name, List fields, List functions) @@ -12,6 +14,11 @@ public class Module _structs.Add(new ModuleStruct(exported, name, fields, functions)); } + public void RegisterStructTemplate(bool exported, string name, List templateArguments, List fields, List functions) + { + _structTemplates.Add(new ModuleStructTemplate(exported, name, templateArguments, fields, functions)); + } + public void RegisterFunction(bool exported, string name, string? externSymbol, List parameters, TypeSyntax returnType) { _functions.Add(new ModuleFunction(exported, name, externSymbol, parameters, returnType)); @@ -30,12 +37,12 @@ public class Module public record ModuleStructField(string Name, TypeSyntax Type, bool HasDefaultValue); -public record ModuleStructFunctionParameter(string Name, TypeSyntax Type); - -public record ModuleStructFunction(string Name, string? Hook, List Parameters, TypeSyntax ReturnType); +public record ModuleStructFunction(string Name, string? Hook, List Parameters, TypeSyntax ReturnType); public record ModuleStruct(bool Exported, string Name, List Fields, List Functions); public record ModuleFunctionParameter(string Name, TypeSyntax Type); -public record ModuleFunction(bool Exported, string Name, string? ExternSymbol, List Parameters, TypeSyntax ReturnType); \ No newline at end of file +public record ModuleFunction(bool Exported, string Name, string? ExternSymbol, List Parameters, TypeSyntax ReturnType); + +public record ModuleStructTemplate(bool Exported, string Name, List TemplateArguments, List Fields, List Functions); \ No newline at end of file diff --git a/compiler/NubLang/Modules/ModuleRepository.cs b/compiler/NubLang/Modules/ModuleRepository.cs index 30ea56a..a76c8e5 100644 --- a/compiler/NubLang/Modules/ModuleRepository.cs +++ b/compiler/NubLang/Modules/ModuleRepository.cs @@ -24,20 +24,29 @@ public class ModuleRepository { case FuncSyntax funcDef: { - var parameters = funcDef.Signature.Parameters.Select(x => new ModuleFunctionParameter(x.Name, x.Type)).ToList(); + var parameters = funcDef.Signature.Parameters + .Select(x => new ModuleFunctionParameter(x.Name, x.Type)) + .ToList(); + module.RegisterFunction(funcDef.Exported, funcDef.Name, funcDef.ExternSymbol, parameters, funcDef.Signature.ReturnType); break; } case StructSyntax structDef: { - var fields = structDef.Fields.Select(x => new ModuleStructField(x.Name, x.Type, x.Value.HasValue)).ToList(); + var fields = structDef.Fields + .Select(x => new ModuleStructField(x.Name, x.Type, x.Value.HasValue)) + .ToList(); - var functions = new List(); - foreach (var function in structDef.Functions) - { - var parameters = function.Signature.Parameters.Select(x => new ModuleStructFunctionParameter(x.Name, x.Type)).ToList(); - functions.AddRange(new ModuleStructFunction(function.Name, function.Hook, parameters, function.Signature.ReturnType)); - } + var functions = structDef.Functions + .Select(x => + { + var parameters = x.Signature.Parameters + .Select(y => new ModuleFunctionParameter(y.Name, y.Type)) + .ToList(); + + return new ModuleStructFunction(x.Name, x.Hook, parameters, x.Signature.ReturnType); + }) + .ToList(); module.RegisterStruct(structDef.Exported, structDef.Name, fields, functions); break; diff --git a/compiler/NubLang/TypeChecking/TypeChecker.cs b/compiler/NubLang/TypeChecking/TypeChecker.cs index 7930f83..e04ba5e 100644 --- a/compiler/NubLang/TypeChecking/TypeChecker.cs +++ b/compiler/NubLang/TypeChecking/TypeChecker.cs @@ -837,13 +837,13 @@ public sealed class TypeChecker BoolTypeSyntax => new NubBoolType(), CStringTypeSyntax => new NubCStringType(), IntTypeSyntax i => new NubIntType(i.Signed, i.Width), - CustomTypeSyntax c => ResolveCustomType(c), FloatTypeSyntax f => new NubFloatType(f.Width), FuncTypeSyntax func => new NubFuncType(func.Parameters.Select(ResolveType).ToList(), ResolveType(func.ReturnType)), ArrayTypeSyntax arr => new NubArrayType(ResolveType(arr.BaseType)), PointerTypeSyntax ptr => new NubPointerType(ResolveType(ptr.BaseType)), StringTypeSyntax => new NubStringType(), - TemplateTypeSyntax template => throw new NotImplementedException(), + CustomTypeSyntax c => ResolveCustomType(c), + TemplateTypeSyntax t => ResolveTemplateType(t), VoidTypeSyntax => new NubVoidType(), _ => throw new NotSupportedException($"Unknown type syntax: {type}") }; @@ -907,6 +907,11 @@ public sealed class TypeChecker _resolvingTypes.Remove(key); } } + + private NubType ResolveTemplateType(TemplateTypeSyntax template) + { + throw new NotImplementedException(); + } } public enum VariableKind