...
This commit is contained in:
48
compiler/NubLang/Modules/Module.cs
Normal file
48
compiler/NubLang/Modules/Module.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using NubLang.Parsing.Syntax;
|
||||
|
||||
namespace NubLang.Modules;
|
||||
|
||||
public class Module
|
||||
{
|
||||
private readonly List<ModuleStructType> _structs = [];
|
||||
private readonly List<ModuleInterfaceType> _interfaces = [];
|
||||
private readonly List<ModuleFuncType> _functions = [];
|
||||
|
||||
public void RegisterStruct(bool exported, string name, IReadOnlyList<ModuleStructTypeField> fields)
|
||||
{
|
||||
_structs.Add(new ModuleStructType(exported, name, fields));
|
||||
}
|
||||
|
||||
public void RegisterInterface(bool exported, string name)
|
||||
{
|
||||
_interfaces.Add(new ModuleInterfaceType(exported, name));
|
||||
}
|
||||
|
||||
public void RegisterFunction(bool exported, string name, string? externSymbol, FuncTypeSyntax type)
|
||||
{
|
||||
_functions.Add(new ModuleFuncType(exported, name, externSymbol, type));
|
||||
}
|
||||
|
||||
public IReadOnlyList<ModuleStructType> StructTypes(bool includePrivate)
|
||||
{
|
||||
return _structs.Where(x => x.Exported || includePrivate).ToList();
|
||||
}
|
||||
|
||||
public IReadOnlyList<ModuleInterfaceType> InterfaceTypes(bool includePrivate)
|
||||
{
|
||||
return _interfaces.Where(x => x.Exported || includePrivate).ToList();
|
||||
}
|
||||
|
||||
public IReadOnlyList<ModuleFuncType> Functions(bool includePrivate)
|
||||
{
|
||||
return _functions.Where(x => x.Exported || includePrivate).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public record ModuleStructTypeField(string Name, TypeSyntax Type, bool HasDefaultValue);
|
||||
|
||||
public record ModuleStructType(bool Exported, string Name, IReadOnlyList<ModuleStructTypeField> Fields);
|
||||
|
||||
public record ModuleInterfaceType(bool Exported, string Name);
|
||||
|
||||
public record ModuleFuncType(bool Exported, string Name, string? ExternSymbol, FuncTypeSyntax FuncType);
|
||||
66
compiler/NubLang/Modules/ModuleRepository.cs
Normal file
66
compiler/NubLang/Modules/ModuleRepository.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using NubLang.Parsing.Syntax;
|
||||
|
||||
namespace NubLang.Modules;
|
||||
|
||||
public class ModuleRepository
|
||||
{
|
||||
private readonly Dictionary<string, Module> _modules = new();
|
||||
|
||||
public ModuleRepository(IReadOnlyList<SyntaxTree> syntaxTrees)
|
||||
{
|
||||
foreach (var syntaxTree in syntaxTrees)
|
||||
{
|
||||
var moduleName = syntaxTree.Metadata.ModuleName;
|
||||
var module = GetOrCreate(moduleName);
|
||||
ProcessSyntaxTree(module, syntaxTree);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ProcessSyntaxTree(Module module, SyntaxTree syntaxTree)
|
||||
{
|
||||
foreach (var definition in syntaxTree.Definitions)
|
||||
{
|
||||
switch (definition)
|
||||
{
|
||||
case FuncSyntax funcDef:
|
||||
{
|
||||
var parameters = funcDef.Signature.Parameters.Select(x => x.Type).ToList();
|
||||
var type = new FuncTypeSyntax([], parameters, funcDef.Signature.ReturnType);
|
||||
module.RegisterFunction(funcDef.Exported, funcDef.Name, funcDef.ExternSymbol, type);
|
||||
break;
|
||||
}
|
||||
case StructSyntax structDef:
|
||||
{
|
||||
var fields = structDef.Fields.Select(x => new ModuleStructTypeField(x.Name, x.Type, x.Value.HasValue)).ToList();
|
||||
module.RegisterStruct(structDef.Exported, structDef.Name, fields);
|
||||
break;
|
||||
}
|
||||
case InterfaceSyntax interfaceDef:
|
||||
{
|
||||
module.RegisterInterface(interfaceDef.Exported, interfaceDef.Name);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(definition));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<string, Module> Modules()
|
||||
{
|
||||
return _modules;
|
||||
}
|
||||
|
||||
private Module GetOrCreate(string name)
|
||||
{
|
||||
if (!_modules.TryGetValue(name, out var module))
|
||||
{
|
||||
module = new Module();
|
||||
_modules[name] = module;
|
||||
}
|
||||
|
||||
return module;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user