This commit is contained in:
nub31
2025-09-20 23:32:19 +02:00
parent 511f0d27ee
commit 700fbd459e
3 changed files with 35 additions and 14 deletions

View File

@@ -1,10 +1,12 @@
using NubLang.Parsing.Syntax; using NubLang.Parsing.Syntax;
using NubLang.TypeChecking.Node;
namespace NubLang.Modules; namespace NubLang.Modules;
public class Module public class Module
{ {
private readonly List<ModuleStruct> _structs = []; private readonly List<ModuleStruct> _structs = [];
private readonly List<ModuleStructTemplate> _structTemplates = [];
private readonly List<ModuleFunction> _functions = []; private readonly List<ModuleFunction> _functions = [];
public void RegisterStruct(bool exported, string name, List<ModuleStructField> fields, List<ModuleStructFunction> functions) public void RegisterStruct(bool exported, string name, List<ModuleStructField> fields, List<ModuleStructFunction> functions)
@@ -12,6 +14,11 @@ public class Module
_structs.Add(new ModuleStruct(exported, name, fields, functions)); _structs.Add(new ModuleStruct(exported, name, fields, functions));
} }
public void RegisterStructTemplate(bool exported, string name, List<string> templateArguments, List<StructFieldNode> fields, List<StructFuncNode> functions)
{
_structTemplates.Add(new ModuleStructTemplate(exported, name, templateArguments, fields, functions));
}
public void RegisterFunction(bool exported, string name, string? externSymbol, List<ModuleFunctionParameter> parameters, TypeSyntax returnType) public void RegisterFunction(bool exported, string name, string? externSymbol, List<ModuleFunctionParameter> parameters, TypeSyntax returnType)
{ {
_functions.Add(new ModuleFunction(exported, name, externSymbol, parameters, 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 ModuleStructField(string Name, TypeSyntax Type, bool HasDefaultValue);
public record ModuleStructFunctionParameter(string Name, TypeSyntax Type); public record ModuleStructFunction(string Name, string? Hook, List<ModuleFunctionParameter> Parameters, TypeSyntax ReturnType);
public record ModuleStructFunction(string Name, string? Hook, List<ModuleStructFunctionParameter> Parameters, TypeSyntax ReturnType);
public record ModuleStruct(bool Exported, string Name, List<ModuleStructField> Fields, List<ModuleStructFunction> Functions); public record ModuleStruct(bool Exported, string Name, List<ModuleStructField> Fields, List<ModuleStructFunction> Functions);
public record ModuleFunctionParameter(string Name, TypeSyntax Type); public record ModuleFunctionParameter(string Name, TypeSyntax Type);
public record ModuleFunction(bool Exported, string Name, string? ExternSymbol, List<ModuleFunctionParameter> Parameters, TypeSyntax ReturnType); public record ModuleFunction(bool Exported, string Name, string? ExternSymbol, List<ModuleFunctionParameter> Parameters, TypeSyntax ReturnType);
public record ModuleStructTemplate(bool Exported, string Name, List<string> TemplateArguments, List<StructFieldNode> Fields, List<StructFuncNode> Functions);

View File

@@ -24,20 +24,29 @@ public class ModuleRepository
{ {
case FuncSyntax funcDef: 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); module.RegisterFunction(funcDef.Exported, funcDef.Name, funcDef.ExternSymbol, parameters, funcDef.Signature.ReturnType);
break; break;
} }
case StructSyntax structDef: 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<ModuleStructFunction>(); var functions = structDef.Functions
foreach (var function in structDef.Functions) .Select(x =>
{ {
var parameters = function.Signature.Parameters.Select(x => new ModuleStructFunctionParameter(x.Name, x.Type)).ToList(); var parameters = x.Signature.Parameters
functions.AddRange(new ModuleStructFunction(function.Name, function.Hook, parameters, function.Signature.ReturnType)); .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); module.RegisterStruct(structDef.Exported, structDef.Name, fields, functions);
break; break;

View File

@@ -837,13 +837,13 @@ public sealed class TypeChecker
BoolTypeSyntax => new NubBoolType(), BoolTypeSyntax => new NubBoolType(),
CStringTypeSyntax => new NubCStringType(), CStringTypeSyntax => new NubCStringType(),
IntTypeSyntax i => new NubIntType(i.Signed, i.Width), IntTypeSyntax i => new NubIntType(i.Signed, i.Width),
CustomTypeSyntax c => ResolveCustomType(c),
FloatTypeSyntax f => new NubFloatType(f.Width), FloatTypeSyntax f => new NubFloatType(f.Width),
FuncTypeSyntax func => new NubFuncType(func.Parameters.Select(ResolveType).ToList(), ResolveType(func.ReturnType)), FuncTypeSyntax func => new NubFuncType(func.Parameters.Select(ResolveType).ToList(), ResolveType(func.ReturnType)),
ArrayTypeSyntax arr => new NubArrayType(ResolveType(arr.BaseType)), ArrayTypeSyntax arr => new NubArrayType(ResolveType(arr.BaseType)),
PointerTypeSyntax ptr => new NubPointerType(ResolveType(ptr.BaseType)), PointerTypeSyntax ptr => new NubPointerType(ResolveType(ptr.BaseType)),
StringTypeSyntax => new NubStringType(), StringTypeSyntax => new NubStringType(),
TemplateTypeSyntax template => throw new NotImplementedException(), CustomTypeSyntax c => ResolveCustomType(c),
TemplateTypeSyntax t => ResolveTemplateType(t),
VoidTypeSyntax => new NubVoidType(), VoidTypeSyntax => new NubVoidType(),
_ => throw new NotSupportedException($"Unknown type syntax: {type}") _ => throw new NotSupportedException($"Unknown type syntax: {type}")
}; };
@@ -907,6 +907,11 @@ public sealed class TypeChecker
_resolvingTypes.Remove(key); _resolvingTypes.Remove(key);
} }
} }
private NubType ResolveTemplateType(TemplateTypeSyntax template)
{
throw new NotImplementedException();
}
} }
public enum VariableKind public enum VariableKind