...
This commit is contained in:
@@ -4,42 +4,34 @@ namespace NubLang.Modules;
|
||||
|
||||
public class Module
|
||||
{
|
||||
private readonly List<ModuleStruct> _structs = [];
|
||||
private readonly List<StructTemplateSyntax> _structTemplates = [];
|
||||
private readonly List<ModuleFunction> _functions = [];
|
||||
private readonly List<DefinitionSyntax> _definitions = [];
|
||||
|
||||
public void RegisterStruct(bool exported, string name, List<ModuleStructField> fields, List<ModuleStructFunction> functions)
|
||||
public void Register(DefinitionSyntax definition)
|
||||
{
|
||||
_structs.Add(new ModuleStruct(exported, name, fields, functions));
|
||||
_definitions.Add(definition);
|
||||
}
|
||||
|
||||
public void RegisterStructTemplate(StructTemplateSyntax structTemplate)
|
||||
public List<StructSyntax> Structs(bool includePrivate)
|
||||
{
|
||||
_structTemplates.Add(structTemplate);
|
||||
return _definitions
|
||||
.OfType<StructSyntax>()
|
||||
.Where(x => x.Exported || includePrivate)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void RegisterFunction(bool exported, string name, string? externSymbol, List<ModuleFunctionParameter> parameters, TypeSyntax returnType)
|
||||
public List<StructTemplateSyntax> StructTemplates(bool includePrivate)
|
||||
{
|
||||
_functions.Add(new ModuleFunction(exported, name, externSymbol, parameters, returnType));
|
||||
return _definitions
|
||||
.OfType<StructTemplateSyntax>()
|
||||
.Where(x => x.Exported || includePrivate)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ModuleStruct> StructTypes(bool includePrivate)
|
||||
public List<FuncSyntax> Functions(bool includePrivate)
|
||||
{
|
||||
return _structs.Where(x => x.Exported || includePrivate).ToList();
|
||||
return _definitions
|
||||
.OfType<FuncSyntax>()
|
||||
.Where(x => x.Exported || includePrivate)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ModuleFunction> Functions(bool includePrivate)
|
||||
{
|
||||
return _functions.Where(x => x.Exported || includePrivate).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public record ModuleStructField(string Name, TypeSyntax Type, bool HasDefaultValue);
|
||||
|
||||
public record ModuleStructFunction(string Name, string? Hook, List<ModuleFunctionParameter> Parameters, TypeSyntax ReturnType);
|
||||
|
||||
public record ModuleStruct(bool Exported, string Name, List<ModuleStructField> Fields, List<ModuleStructFunction> Functions);
|
||||
|
||||
public record ModuleFunctionParameter(string Name, TypeSyntax Type);
|
||||
|
||||
public record ModuleFunction(bool Exported, string Name, string? ExternSymbol, List<ModuleFunctionParameter> Parameters, TypeSyntax ReturnType);
|
||||
}
|
||||
@@ -10,8 +10,7 @@ public class ModuleRepository
|
||||
{
|
||||
foreach (var syntaxTree in syntaxTrees)
|
||||
{
|
||||
var moduleName = syntaxTree.Metadata.ModuleName;
|
||||
var module = GetOrCreate(moduleName);
|
||||
var module = GetOrCreate(syntaxTree.Metadata.ModuleName);
|
||||
ProcessSyntaxTree(module, syntaxTree);
|
||||
}
|
||||
}
|
||||
@@ -20,47 +19,7 @@ public class ModuleRepository
|
||||
{
|
||||
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:
|
||||
{
|
||||
module.RegisterStructTemplate(structDef);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(definition));
|
||||
}
|
||||
}
|
||||
module.Register(definition);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user