82 lines
2.6 KiB
C#
82 lines
2.6 KiB
C#
using NubLang.Parsing.Syntax;
|
|
|
|
namespace NubLang.Modules;
|
|
|
|
public class ModuleRepository
|
|
{
|
|
private readonly Dictionary<string, Module> _modules = new();
|
|
|
|
public ModuleRepository(List<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 => 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 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;
|
|
}
|
|
case StructTemplateSyntax structDef:
|
|
{
|
|
// todo(nub31): Include templates in modules
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(definition));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public Dictionary<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;
|
|
}
|
|
} |