66 lines
2.0 KiB
C#
66 lines
2.0 KiB
C#
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;
|
|
}
|
|
} |