Compare commits

...

74 Commits

Author SHA1 Message Date
nub31
aa5a494d39 Add generic unt/uint 2026-03-16 21:13:49 +01:00
nub31
bc65c3f4fd ... 2026-03-16 18:10:03 +01:00
nub31
bdeb2c4d73 Update build 2026-03-16 17:05:24 +01:00
nub31
48760dcdda ... 2026-03-16 16:53:48 +01:00
nub31
63d0eb660b CType -> TypeName 2026-03-16 16:04:43 +01:00
nub31
918d25f8c8 perhaps 2026-03-15 01:38:59 +01:00
nub31
6e631ba1ba STR -> RC for report 2026-03-12 16:07:40 +01:00
nub31
832184053f better rc 2026-03-12 15:42:44 +01:00
nub31
2ab20652f8 ... 2026-03-10 17:53:42 +01:00
nub31
6507624e90 .. 2026-03-10 17:09:07 +01:00
nub31
83255980d7 foreach 2026-03-10 16:37:59 +01:00
nub31
1a1dc1389d ... 2026-03-09 17:35:34 +01:00
nub31
4210ad878b tmp... 2026-03-09 16:38:09 +01:00
nub31
32042d0769 Add comma parsing to func call 2026-03-09 15:44:30 +01:00
nub31
dd44e3edc4 Prefix core functions 2026-03-05 23:10:38 +01:00
nub31
24d41d3dcb Remove todo 2026-03-05 23:00:44 +01:00
nub31
c9bf212aa2 Add string debugging 2026-03-05 22:58:20 +01:00
nub31
0e099d0baf ... 2026-03-05 22:02:35 +01:00
nub31
0be4e35628 Add check for duplicate local ident 2026-03-05 21:59:24 +01:00
nub31
7dd635fa91 ... 2026-03-05 21:45:04 +01:00
nub31
693b119781 ... 2026-03-01 18:46:36 +01:00
nub31
2b7eb56895 ... 2026-03-01 18:14:54 +01:00
nub31
5f4a4172bf ... 2026-03-01 15:06:19 +01:00
nub31
9da370e387 ... 2026-03-01 00:15:11 +01:00
nub31
faf506f409 static string literals 2026-03-01 00:12:16 +01:00
nub31
16d27c76ab ... 2026-02-28 23:19:57 +01:00
nub31
270be1f740 ... 2026-02-28 02:42:25 +01:00
nub31
da2fa81c39 ... 2026-02-28 02:27:44 +01:00
nub31
677c7dea9e Whitespace... 2026-02-28 01:15:30 +01:00
nub31
6ba05d00d7 Prevent freeing string literals 2026-02-28 01:15:03 +01:00
nub31
d58e006be4 ref counted strings 2026-02-28 01:07:02 +01:00
nub31
84627dde45 ... 2026-02-28 00:57:48 +01:00
nub31
e7aad861d3 ... 2026-02-27 23:00:48 +01:00
nub31
ecb628c4c2 ... 2026-02-27 22:33:21 +01:00
nub31
e5227f7a99 ... 2026-02-27 22:32:29 +01:00
nub31
73ecbf8e9b extern 2026-02-27 22:20:32 +01:00
nub31
e512650440 ... 2026-02-27 21:29:28 +01:00
nub31
272ea33616 ... 2026-02-27 18:26:10 +01:00
nub31
b7dc77cb1c ... 2026-02-26 20:57:25 +01:00
nub31
3323d760e8 infer enum types 2026-02-26 20:44:29 +01:00
nub31
aa5bf0b568 ... 2026-02-26 20:00:31 +01:00
nub31
660166221c ... 2026-02-26 19:04:40 +01:00
nub31
49734544e6 ... 2026-02-25 21:51:32 +01:00
nub31
cb4aeb9c01 Add anonymous structs 2026-02-25 21:21:28 +01:00
nub31
d771396bd4 Remove unused BasicError 2026-02-25 20:04:21 +01:00
nub31
7b1e202424 ... 2026-02-25 19:39:31 +01:00
nub31
282dd0502a fix bug 2026-02-24 22:05:30 +01:00
nub31
3ebaa67b6d ... 2026-02-24 21:51:36 +01:00
nub31
35867ffa28 ... 2026-02-24 20:34:27 +01:00
nub31
76efc84984 ... 2026-02-23 18:35:08 +01:00
nub31
583c01201f ... 2026-02-23 18:07:23 +01:00
nub31
1511d5d2b8 match parsing 2026-02-15 04:33:21 +01:00
nub31
caa3b378b3 union definitions 2026-02-15 04:23:36 +01:00
nub31
cbe27c0ae8 ... 2026-02-15 03:46:16 +01:00
nub31
4d0f7c715f Only add exported and not external functions and types to manifest 2026-02-15 03:14:09 +01:00
nub31
c342b2e102 Save mangled name in module graph 2026-02-15 02:55:11 +01:00
nub31
ee485e4119 ... 2026-02-13 00:27:59 +01:00
nub31
21a095f691 .... 2026-02-11 21:47:51 +01:00
nub31
0a5b39b217 pack/unpack class 2026-02-10 23:37:37 +01:00
nub31
ab2f5750dc ... 2026-02-10 23:31:44 +01:00
nub31
b45a0fa8cc ... 2026-02-10 23:28:32 +01:00
nub31
7daccba9f3 proper scopes 2026-02-10 23:26:22 +01:00
nub31
924bdae89b ignore .build 2026-02-10 23:05:19 +01:00
nub31
5364b0420a remove .build 2026-02-10 23:05:11 +01:00
nub31
c65fbeba13 ... 2026-02-10 23:03:27 +01:00
nub31
9d8d8f255a libs working 2026-02-10 22:43:03 +01:00
nub31
576abe1240 ... 2026-02-10 22:26:18 +01:00
nub31
d0c31ad17f global variables 2026-02-10 20:51:28 +01:00
nub31
7872a4b6b8 module exports and name mangling 2026-02-10 20:33:27 +01:00
nub31
6ae10d5f90 ... 2026-02-10 19:50:55 +01:00
nub31
d3e2dcede8 expression function calls 2026-02-09 22:30:25 +01:00
nub31
88fe03c048 ... 2026-02-09 22:03:08 +01:00
nub31
9ccdd5f835 tokenizer improvements 2026-02-09 22:03:00 +01:00
nub31
b7cfdd2519 Fix formatting 2026-02-09 21:39:09 +01:00
17 changed files with 3899 additions and 1012 deletions

3
TODO.txt Normal file
View File

@@ -0,0 +1,3 @@
string formatting
Dynamic arrays
C-style arrays

View File

@@ -1,29 +1,51 @@
namespace Compiler; namespace Compiler;
public sealed class Diagnostic(DiagnosticSeverity severity, string message, string? help, FileInfo? file) public class Diagnostic
{ {
public static DiagnosticBuilder Info(string message) => new DiagnosticBuilder(DiagnosticSeverity.Info, message); public static Builder Info(string message) => new Builder(DiagnosticSeverity.Info, message);
public static DiagnosticBuilder Warning(string message) => new DiagnosticBuilder(DiagnosticSeverity.Warning, message); public static Builder Warning(string message) => new Builder(DiagnosticSeverity.Warning, message);
public static DiagnosticBuilder Error(string message) => new DiagnosticBuilder(DiagnosticSeverity.Error, message); public static Builder Error(string message) => new Builder(DiagnosticSeverity.Error, message);
public readonly DiagnosticSeverity Severity = severity; private Diagnostic(DiagnosticSeverity severity, string message, string? help, FileInfo? file)
public readonly string Message = message; {
public readonly string? Help = help; Severity = severity;
public readonly FileInfo? File = file; Message = message;
} Help = help;
File = file;
}
public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string message) public class FileInfo(string file, int line, int column, int length)
{ {
public string File { get; } = file;
public int Line { get; } = line;
public int Column { get; } = column;
public int Length { get; } = length;
}
public DiagnosticSeverity Severity { get; }
public string Message { get; }
public string? Help { get; }
public FileInfo? File { get; }
public enum DiagnosticSeverity
{
Info,
Warning,
Error,
}
public class Builder(DiagnosticSeverity severity, string message)
{
private FileInfo? file; private FileInfo? file;
private string? help; private string? help;
public DiagnosticBuilder At(string fileName, int line, int column, int length) public Builder At(string fileName, int line, int column, int length)
{ {
file = new FileInfo(fileName, line, column, length); file = new FileInfo(fileName, line, column, length);
return this; return this;
} }
public DiagnosticBuilder At(string fileName, Token? token) public Builder At(string fileName, Token? token)
{ {
if (token != null) if (token != null)
{ {
@@ -33,7 +55,7 @@ public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string messag
return this; return this;
} }
public DiagnosticBuilder At(string fileName, Node? node) public Builder At(string fileName, Node? node)
{ {
if (node != null && node.Tokens.Count != 0) if (node != null && node.Tokens.Count != 0)
{ {
@@ -44,7 +66,7 @@ public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string messag
return this; return this;
} }
public DiagnosticBuilder At(string fileName, TypedNode? node) public Builder At(string fileName, TypedNode? node)
{ {
if (node != null && node.Tokens.Count != 0) if (node != null && node.Tokens.Count != 0)
{ {
@@ -55,7 +77,7 @@ public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string messag
return this; return this;
} }
public DiagnosticBuilder WithHelp(string helpMessage) public Builder WithHelp(string helpMessage)
{ {
help = helpMessage; help = helpMessage;
return this; return this;
@@ -65,26 +87,12 @@ public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string messag
{ {
return new Diagnostic(severity, message, help, file); return new Diagnostic(severity, message, help, file);
} }
}
} }
public sealed class FileInfo(string file, int line, int column, int length) public class CompileException(Diagnostic diagnostic) : Exception
{ {
public readonly string File = file; public Diagnostic Diagnostic { get; } = diagnostic;
public readonly int Line = line;
public readonly int Column = column;
public readonly int Length = length;
}
public enum DiagnosticSeverity
{
Info,
Warning,
Error,
}
public sealed class CompileException(Diagnostic diagnostic) : Exception
{
public readonly Diagnostic Diagnostic = diagnostic;
} }
public static class DiagnosticFormatter public static class DiagnosticFormatter
@@ -93,9 +101,9 @@ public static class DiagnosticFormatter
{ {
var (label, color) = diagnostic.Severity switch var (label, color) = diagnostic.Severity switch
{ {
DiagnosticSeverity.Info => ("info", Ansi.Cyan), Diagnostic.DiagnosticSeverity.Info => ("info", Ansi.Cyan),
DiagnosticSeverity.Warning => ("warning", Ansi.Yellow), Diagnostic.DiagnosticSeverity.Warning => ("warning", Ansi.Yellow),
DiagnosticSeverity.Error => ("error", Ansi.Red), Diagnostic.DiagnosticSeverity.Error => ("error", Ansi.Red),
_ => ("unknown", Ansi.Reset), _ => ("unknown", Ansi.Reset),
}; };

File diff suppressed because it is too large Load Diff

View File

@@ -3,165 +3,444 @@ using System.Diagnostics.CodeAnalysis;
namespace Compiler; namespace Compiler;
public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules) public class ModuleGraph
{ {
public static Builder Create() => new(); public static Builder CreateBuilder() => new();
private ModuleGraph(Dictionary<string, Module> modules)
{
this.modules = modules;
}
private readonly Dictionary<string, Module> modules;
public List<Module> GetModules() public List<Module> GetModules()
{ {
return modules.Values.ToList(); return modules.Values.ToList();
} }
public bool TryResolveIdentifier(string moduleName, string identifierName, bool searchPrivate, [NotNullWhen(true)] out Module.IdentifierInfo? info)
{
if (!TryResolveModule(moduleName, out var module))
{
info = null;
return false;
}
if (!module.TryResolveIdentifier(identifierName, searchPrivate, out info))
return false;
return true;
}
public bool TryResolveType(string moduleName, string typeName, bool searchPrivate, [NotNullWhen(true)] out Module.TypeInfo? info)
{
if (!TryResolveModule(moduleName, out var module))
{
info = null;
return false;
}
if (!module.TryResolveType(typeName, searchPrivate, out info))
return false;
return true;
}
public bool TryResolveModule(string moduleName, [NotNullWhen(true)] out Module? module) public bool TryResolveModule(string moduleName, [NotNullWhen(true)] out Module? module)
{ {
module = modules.GetValueOrDefault(moduleName); module = modules.GetValueOrDefault(moduleName);
return module != null; return module != null;
} }
public sealed class Module(string name)
{
public string Name { get; } = name;
private readonly Dictionary<string, NubType> customTypes = new();
private readonly Dictionary<string, NubType> identifierTypes = new();
public List<NubType> GetCustomTypes()
{
return customTypes.Values.ToList();
}
public bool TryResolveCustomType(string name, [NotNullWhen(true)] out NubType? customType)
{
customType = customTypes.GetValueOrDefault(name);
return customType != null;
}
public bool TryResolveIdentifierType(string name, [NotNullWhen(true)] out NubType? identifier)
{
identifier = identifierTypes.GetValueOrDefault(name);
return identifier != null;
}
public void AddCustomType(string name, NubType type)
{
customTypes.Add(name, type);
}
public void AddIdentifier(string name, NubType identifier)
{
identifierTypes.Add(name, identifier);
}
}
public class Builder public class Builder
{ {
private readonly List<Ast> asts = []; private readonly List<Ast> asts = [];
private readonly List<Manifest> manifests = [];
public void AddAst(Ast ast) public void AddAst(Ast ast)
{ {
asts.Add(ast); asts.Add(ast);
} }
public ModuleGraph Build(out List<Diagnostic> diagnostics) public void AddManifest(Manifest manifest)
{
manifests.Add(manifest);
}
public ModuleGraph? Build(out List<Diagnostic> diagnostics)
{ {
diagnostics = []; diagnostics = [];
var astModuleCache = new Dictionary<Ast, Module>();
var modules = new Dictionary<string, Module>(); var modules = new Dictionary<string, Module>();
// First pass: Register modules foreach (var manifest in manifests)
{
foreach (var (moduleName, manifestModule) in manifest.Modules)
{
var module = GetOrCreateModule(moduleName);
foreach (var (name, type) in manifestModule.Types)
{
switch (type)
{
case Manifest.Module.TypeInfoStruct s:
{
var info = new Module.TypeInfoStruct(Module.DefinitionSource.Imported, s.Exported, s.Packed);
var fields = s.Fields.Select(x => new Module.TypeInfoStruct.Field(x.Name, x.Type)).ToList();
info.SetFields(fields);
module.AddType(name, info);
break;
}
case Manifest.Module.TypeInfoEnum e:
{
var info = new Module.TypeInfoEnum(Module.DefinitionSource.Imported, e.Exported);
var variants = e.Variants.Select(v => new Module.TypeInfoEnum.Variant(v.Name, v.Type)).ToList();
info.SetVariants(variants);
module.AddType(name, info);
break;
}
default:
throw new ArgumentOutOfRangeException(nameof(type));
}
}
foreach (var (name, identifier) in manifestModule.Identifiers)
{
module.AddIdentifier(name, new Module.IdentifierInfo(Module.DefinitionSource.Imported, identifier.Exported, identifier.Extern, identifier.Type, identifier.MangledName));
}
}
}
foreach (var ast in asts) foreach (var ast in asts)
{ {
var moduleDefinitions = ast.Definitions.OfType<NodeDefinitionModule>().ToList(); var module = GetOrCreateModule(ast.ModuleName.Ident);
if (moduleDefinitions.Count == 0)
diagnostics.Add(Diagnostic.Error("Missing module declaration").At(ast.FileName, 1, 1, 1).Build());
foreach (var extraModuleDefinition in moduleDefinitions.Skip(1))
diagnostics.Add(Diagnostic.Warning("Duplicate module declaration will be ignored").At(ast.FileName, extraModuleDefinition).Build());
if (moduleDefinitions.Count >= 1)
{
var currentModule = moduleDefinitions[0].Name.Ident;
if (!modules.ContainsKey(currentModule))
{
var module = new Module(currentModule);
modules.Add(currentModule, module);
astModuleCache[ast] = module;
}
}
}
// Second pass: Register struct types without fields
foreach (var ast in asts)
{
var module = astModuleCache.GetValueOrDefault(ast);
if (module == null) continue;
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
module.AddCustomType(structDef.Name.Ident, new NubTypeStruct(module.Name, structDef.Name.Ident));
}
// Third pass: Resolve struct fields
foreach (var ast in asts)
{
var module = astModuleCache.GetValueOrDefault(ast);
if (module == null) continue;
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>()) foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
{ {
if (!module.TryResolveCustomType(structDef.Name.Ident, out var customType)) module.AddType(structDef.Name.Ident, new Module.TypeInfoStruct(Module.DefinitionSource.Internal, structDef.Exported, structDef.Packed));
throw new UnreachableException($"{nameof(customType)} should always be registered"); }
var fields = structDef.Fields.Select(f => new NubTypeStruct.Field(f.Name.Ident, ResolveType(f.Type))).ToList(); foreach (var enumDef in ast.Definitions.OfType<NodeDefinitionEnum>())
((NubTypeStruct)customType).ResolveFields(fields); {
module.AddType(enumDef.Name.Ident, new Module.TypeInfoEnum(Module.DefinitionSource.Internal, enumDef.Exported));
} }
} }
// Fourth pass: Register identifiers
foreach (var ast in asts) foreach (var ast in asts)
{ {
var module = astModuleCache.GetValueOrDefault(ast); var module = GetOrCreateModule(ast.ModuleName.Ident);
if (module == null) continue;
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
{
if (!module.TryResolveType(structDef.Name.Ident, true, out var typeInfo))
throw new UnreachableException($"{nameof(typeInfo)} should always be registered");
if (typeInfo is Module.TypeInfoStruct structType)
{
var fields = structDef.Fields.Select(f => new Module.TypeInfoStruct.Field(f.Name.Ident, ResolveType(f.Type, module.Name))).ToList();
structType.SetFields(fields);
}
}
foreach (var enumDef in ast.Definitions.OfType<NodeDefinitionEnum>())
{
if (!module.TryResolveType(enumDef.Name.Ident, true, out var typeInfo))
throw new UnreachableException($"{nameof(typeInfo)} should always be registered");
if (typeInfo is Module.TypeInfoEnum enumType)
{
var variants = enumDef.Variants.Select(v => new Module.TypeInfoEnum.Variant(v.Name.Ident, v.Type == null ? null : ResolveType(v.Type, module.Name))).ToList();
enumType.SetVariants(variants);
}
}
}
foreach (var ast in asts)
{
var module = GetOrCreateModule(ast.ModuleName.Ident);
foreach (var funcDef in ast.Definitions.OfType<NodeDefinitionFunc>()) foreach (var funcDef in ast.Definitions.OfType<NodeDefinitionFunc>())
{ {
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type)).ToList(); var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type, module.Name)).ToList();
var returnType = ResolveType(funcDef.ReturnType); var returnType = funcDef.ReturnType == null ? NubTypeVoid.Instance : ResolveType(funcDef.ReturnType, module.Name);
var funcType = NubTypeFunc.Get(parameters, returnType); var funcType = NubTypeFunc.Get(parameters, returnType);
module.AddIdentifier(funcDef.Name.Ident, funcType); var info = new Module.IdentifierInfo(Module.DefinitionSource.Internal, funcDef.Exported, false, funcType, NameMangler.Mangle(module.Name, funcDef.Name.Ident, funcType));
module.AddIdentifier(funcDef.Name.Ident, info);
}
foreach (var funcDef in ast.Definitions.OfType<NodeDefinitionExternFunc>())
{
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type, module.Name)).ToList();
var returnType = funcDef.ReturnType == null ? NubTypeVoid.Instance : ResolveType(funcDef.ReturnType, module.Name);
var funcType = NubTypeFunc.Get(parameters, returnType);
var info = new Module.IdentifierInfo(Module.DefinitionSource.Internal, funcDef.Exported, true, funcType, funcDef.Name.Ident);
module.AddIdentifier(funcDef.Name.Ident, info);
}
foreach (var globalVariable in ast.Definitions.OfType<NodeDefinitionGlobalVariable>())
{
var type = ResolveType(globalVariable.Type, module.Name);
var info = new Module.IdentifierInfo(Module.DefinitionSource.Internal, globalVariable.Exported, false, type, NameMangler.Mangle(module.Name, globalVariable.Name.Ident, type));
module.AddIdentifier(globalVariable.Name.Ident, info);
}
foreach (var globalVariable in ast.Definitions.OfType<NodeDefinitionExternGlobalVariable>())
{
var type = ResolveType(globalVariable.Type, module.Name);
var info = new Module.IdentifierInfo(Module.DefinitionSource.Internal, globalVariable.Exported, true, type, NameMangler.Mangle(module.Name, globalVariable.Name.Ident, type));
module.AddIdentifier(globalVariable.Name.Ident, info);
} }
} }
if (diagnostics.Any(x => x.Severity == Diagnostic.DiagnosticSeverity.Error))
return null;
return new ModuleGraph(modules); return new ModuleGraph(modules);
NubType ResolveType(NodeType node) NubType ResolveType(NodeType node, string currentModule)
{ {
return node switch return node switch
{ {
NodeTypeBool => NubTypeBool.Instance, NodeTypeBool => NubTypeBool.Instance,
NodeTypeCustom type => ResolveCustomType(type), NodeTypeNamed type => ResolveNamedType(type, currentModule),
NodeTypeFunc type => NubTypeFunc.Get(type.Parameters.Select(ResolveType).ToList(), ResolveType(type.ReturnType)), NodeTypeAnonymousStruct type => NubTypeAnonymousStruct.Get(type.Fields.Select(x => new NubTypeAnonymousStruct.Field(x.Name.Ident, ResolveType(x.Type, currentModule))).ToList()),
NodeTypePointer type => NubTypePointer.Get(ResolveType(type.To)), NodeTypeFunc type => NubTypeFunc.Get(type.Parameters.Select(x => ResolveType(x, currentModule)).ToList(), ResolveType(type.ReturnType, currentModule)),
NodeTypePointer type => NubTypePointer.Get(ResolveType(type.To, currentModule)),
NodeTypeSInt type => NubTypeSInt.Get(type.Width), NodeTypeSInt type => NubTypeSInt.Get(type.Width),
NodeTypeUInt type => NubTypeUInt.Get(type.Width), NodeTypeUInt type => NubTypeUInt.Get(type.Width),
NodeTypeString => NubTypeString.Instance, NodeTypeString => NubTypeString.Instance,
NodeTypeChar => NubTypeChar.Instance,
NodeTypeVoid => NubTypeVoid.Instance, NodeTypeVoid => NubTypeVoid.Instance,
NodeTypeArray type => NubTypeArray.Get(ResolveType(type.ElementType, currentModule)),
_ => throw new ArgumentOutOfRangeException(nameof(node)) _ => throw new ArgumentOutOfRangeException(nameof(node))
}; };
} }
NubType ResolveCustomType(NodeTypeCustom type) NubType ResolveNamedType(NodeTypeNamed type, string currentModule)
{ {
var module = modules.GetValueOrDefault(type.Module.Ident); return type.Sections.Count switch
if (module == null) {
throw new CompileException(Diagnostic.Error($"Unknown module: {type.Module.Ident}").Build()); 3 => ResolveThreePartType(type.Sections[0], type.Sections[1], type.Sections[2], currentModule),
2 => ResolveTwoPartType(type.Sections[0], type.Sections[1], currentModule),
1 => ResolveOnePartType(type.Sections[0], currentModule),
_ => throw BasicError("Invalid type name")
};
}
if (!module.TryResolveCustomType(type.Name.Ident, out var customType)) NubType ResolveThreePartType(TokenIdent first, TokenIdent second, TokenIdent third, string currentModule)
throw new CompileException(Diagnostic.Error($"Unknown custom type: {type.Module.Ident}::{type.Name.Ident}").Build()); {
var module = ResolveModule(first);
if (!module.TryResolveType(second.Ident, currentModule == module.Name, out var typeInfo))
throw BasicError($"Named type '{module.Name}::{second.Ident}' not found");
return customType; if (typeInfo is not Module.TypeInfoEnum enumInfo)
throw BasicError($"'{module.Name}::{second.Ident}' is not an enum");
var variant = enumInfo.Variants.FirstOrDefault(v => v.Name == third.Ident);
if (variant == null)
throw BasicError($"Enum '{module.Name}::{second.Ident}' does not have a variant named '{third.Ident}'");
return NubTypeEnumVariant.Get(NubTypeEnum.Get(module.Name, second.Ident), third.Ident);
}
NubType ResolveTwoPartType(TokenIdent first, TokenIdent second, string currentModule)
{
if (TryResolveEnumVariant(currentModule, first.Ident, second.Ident, out var variant))
return variant;
var module = ResolveModule(first);
if (!module.TryResolveType(second.Ident, currentModule == module.Name, out var typeInfo))
throw BasicError($"Named type '{module.Name}::{second.Ident}' not found");
return typeInfo switch
{
Module.TypeInfoStruct => NubTypeStruct.Get(module.Name, second.Ident),
Module.TypeInfoEnum => NubTypeEnum.Get(module.Name, second.Ident),
_ => throw new ArgumentOutOfRangeException(nameof(typeInfo))
};
}
NubType ResolveOnePartType(TokenIdent name, string currentModule)
{
if (!modules.TryGetValue(currentModule, out var module))
throw BasicError($"Module '{currentModule}' not found");
if (!module.TryResolveType(name.Ident, true, out var typeInfo))
throw BasicError($"Named type '{module.Name}::{name.Ident}' not found");
return typeInfo switch
{
Module.TypeInfoStruct => NubTypeStruct.Get(module.Name, name.Ident),
Module.TypeInfoEnum => NubTypeEnum.Get(module.Name, name.Ident),
_ => throw new ArgumentOutOfRangeException(nameof(typeInfo))
};
}
Module ResolveModule(TokenIdent name)
{
if (!modules.TryGetValue(name.Ident, out var module))
throw BasicError($"Module '{name.Ident}' not found");
return module;
}
bool TryResolveEnumVariant(string moduleName, string enumName, string variantName, [NotNullWhen(true)] out NubType? result)
{
result = null;
if (!modules.TryGetValue(moduleName, out var module))
return false;
if (!module.TryResolveType(enumName, true, out var typeInfo))
return false;
if (typeInfo is not Module.TypeInfoEnum enumInfo)
return false;
var variant = enumInfo.Variants.FirstOrDefault(v => v.Name == variantName);
if (variant == null)
return false;
result = NubTypeEnumVariant.Get(
NubTypeEnum.Get(moduleName, enumName),
variantName);
return true;
}
Exception BasicError(string message)
{
return new CompileException(Diagnostic.Error(message).Build());
}
Module GetOrCreateModule(string name)
{
if (!modules.TryGetValue(name, out var module))
{
module = new Module(name);
modules.Add(name, module);
}
return module;
} }
} }
} }
} }
public class Module(string name)
{
public string Name { get; } = name;
private Dictionary<string, TypeInfo> types = new();
private Dictionary<string, IdentifierInfo> identifiers = new();
public IReadOnlyDictionary<string, TypeInfo> GetTypes() => types;
public IReadOnlyDictionary<string, IdentifierInfo> GetIdentifiers() => identifiers;
public bool TryResolveType(string name, bool searchPrivate, [NotNullWhen(true)] out TypeInfo? customType)
{
var info = types.GetValueOrDefault(name);
if (info == null)
{
customType = null;
return false;
}
if (searchPrivate || info.Source == DefinitionSource.Internal)
{
customType = info;
return true;
}
customType = null;
return false;
}
public bool TryResolveIdentifier(string name, bool searchPrivate, [NotNullWhen(true)] out IdentifierInfo? identifierType)
{
var info = identifiers.GetValueOrDefault(name);
if (info == null)
{
identifierType = null;
return false;
}
if (searchPrivate || info.Source == DefinitionSource.Internal)
{
identifierType = info;
return true;
}
identifierType = null;
return false;
}
public void AddType(string name, TypeInfo info)
{
types.Add(name, info);
}
public void AddIdentifier(string name, IdentifierInfo info)
{
identifiers.Add(name, info);
}
public enum DefinitionSource
{
Internal,
Imported,
}
public class IdentifierInfo(DefinitionSource source, bool exported, bool @extern, NubType type, string mangledName)
{
public DefinitionSource Source { get; } = source;
public bool Exported { get; } = exported;
public bool Extern { get; } = @extern;
public NubType Type { get; } = type;
public string MangledName { get; } = mangledName;
}
public abstract class TypeInfo(DefinitionSource source, bool exported)
{
public DefinitionSource Source { get; } = source;
public bool Exported { get; } = exported;
}
public class TypeInfoStruct(DefinitionSource source, bool exported, bool packed) : TypeInfo(source, exported)
{
private IReadOnlyList<Field>? fields;
public bool Packed { get; } = packed;
public IReadOnlyList<Field> Fields => fields ?? throw new InvalidOperationException("Fields has not been set yet");
public void SetFields(IReadOnlyList<Field> fields)
{
this.fields = fields;
}
public class Field(string name, NubType type)
{
public string Name { get; } = name;
public NubType Type { get; } = type;
}
}
public class TypeInfoEnum(DefinitionSource source, bool exported) : TypeInfo(source, exported)
{
private IReadOnlyList<Variant>? variants;
public IReadOnlyList<Variant> Variants => variants ?? throw new InvalidOperationException("Fields has not been set yet");
public void SetVariants(IReadOnlyList<Variant> variants)
{
this.variants = variants;
}
public class Variant(string name, NubType? type)
{
public string Name { get; } = name;
public NubType? Type { get; } = type;
}
}
}

110
compiler/NubLib.cs Normal file
View File

@@ -0,0 +1,110 @@
using System.IO.Compression;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Compiler;
public class NubLib
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
};
public static void Pack(string outputPath, string archivePath, Manifest manifest)
{
using var fs = new FileStream(outputPath, FileMode.Create);
using var zip = new ZipArchive(fs, ZipArchiveMode.Create);
var manifestEntry = zip.CreateEntry("manifest.json");
using (var writer = new StreamWriter(manifestEntry.Open()))
{
var serialized = JsonSerializer.Serialize(manifest, JsonOptions);
writer.Write(serialized);
}
var archiveEntry = zip.CreateEntry("lib.a");
using var entryStream = archiveEntry.Open();
using var fileStream = File.OpenRead(archivePath);
fileStream.CopyTo(entryStream);
}
public static NubLibLoadResult Unpack(string nublibPath)
{
using var fs = new FileStream(nublibPath, FileMode.Open, FileAccess.Read);
using var zip = new ZipArchive(fs, ZipArchiveMode.Read);
var manifestEntry = zip.GetEntry("manifest.json") ?? throw new FileNotFoundException("Manifest not found in nublib", "manifest.json");
Manifest manifest;
using (var reader = new StreamReader(manifestEntry.Open()))
{
var json = reader.ReadToEnd();
manifest = JsonSerializer.Deserialize<Manifest>(json, JsonOptions) ?? throw new InvalidDataException("Failed to deserialize manifest.json");
}
var archiveEntry = zip.Entries.FirstOrDefault(e => e.Name.EndsWith(".a")) ?? throw new FileNotFoundException("Archive not found in nublib", "*.a");
string tempArchivePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".a");
using (var entryStream = archiveEntry.Open())
using (var tempFile = File.Create(tempArchivePath))
{
entryStream.CopyTo(tempFile);
}
return new NubLibLoadResult(manifest, tempArchivePath);
}
public record NubLibLoadResult(Manifest Manifest, string ArchivePath);
}
public record Manifest(Dictionary<string, Manifest.Module> Modules)
{
public static Manifest Create(ModuleGraph moduleGraph)
{
var modules = new Dictionary<string, Module>();
foreach (var module in moduleGraph.GetModules())
{
var types = module.GetTypes().ToDictionary(x => x.Key, x => ConvertType(x.Value));
var identifiers = module.GetIdentifiers().ToDictionary(x => x.Key, x => new Module.IdentifierInfo(x.Value.Type, x.Value.Exported, x.Value.Extern, x.Value.MangledName));
modules[module.Name] = new Module(types, identifiers);
}
return new Manifest(modules);
static Module.TypeInfo ConvertType(Compiler.Module.TypeInfo typeInfo)
{
return typeInfo switch
{
Compiler.Module.TypeInfoStruct s => new Module.TypeInfoStruct(s.Exported, s.Packed, s.Fields.Select(x => new Module.TypeInfoStruct.Field(x.Name, x.Type)).ToList()),
Compiler.Module.TypeInfoEnum e => new Module.TypeInfoEnum(e.Exported, e.Variants.Select(v => new Module.TypeInfoEnum.Variant(v.Name, v.Type)).ToList()),
_ => throw new ArgumentOutOfRangeException(nameof(typeInfo))
};
}
}
public record Module(Dictionary<string, Module.TypeInfo> Types, Dictionary<string, Module.IdentifierInfo> Identifiers)
{
public record IdentifierInfo(NubType Type, bool Exported, bool Extern, string MangledName);
[JsonDerivedType(typeof(TypeInfoStruct), "struct")]
[JsonDerivedType(typeof(TypeInfoEnum), "enum")]
public abstract record TypeInfo(bool Exported);
public record TypeInfoStruct(bool Exported, bool Packed, IReadOnlyList<TypeInfoStruct.Field> Fields) : TypeInfo(Exported)
{
public record Field(string Name, NubType Type);
}
public record TypeInfoEnum(bool Exported, IReadOnlyList<TypeInfoEnum.Variant> Variants) : TypeInfo(Exported)
{
public record Variant(string Name, NubType? Type);
}
}
}

View File

@@ -1,11 +1,31 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Compiler; namespace Compiler;
[JsonConverter(typeof(NubTypeJsonConverter))]
public abstract class NubType public abstract class NubType
{ {
public abstract override string ToString(); public abstract override string ToString();
[Obsolete("Use IsAssignableTo instead of ==", error: true)]
public static bool operator ==(NubType? a, NubType? b) => throw new InvalidOperationException("Use IsAssignableTo");
[Obsolete("Use IsAssignableTo instead of ==", error: true)]
public static bool operator !=(NubType? a, NubType? b) => throw new InvalidOperationException("Use IsAssignableTo");
public bool IsAssignableTo(NubType target)
{
return (this, target) switch
{
(NubTypeEnumVariant variant, NubTypeEnum targetEnum) => ReferenceEquals(variant.EnumType, targetEnum),
_ => ReferenceEquals(this, target),
};
}
} }
public sealed class NubTypeVoid : NubType public class NubTypeVoid : NubType
{ {
public static readonly NubTypeVoid Instance = new(); public static readonly NubTypeVoid Instance = new();
@@ -16,7 +36,7 @@ public sealed class NubTypeVoid : NubType
public override string ToString() => "void"; public override string ToString() => "void";
} }
public sealed class NubTypeUInt : NubType public class NubTypeUInt : NubType
{ {
private static readonly Dictionary<int, NubTypeUInt> Cache = new(); private static readonly Dictionary<int, NubTypeUInt> Cache = new();
@@ -38,7 +58,7 @@ public sealed class NubTypeUInt : NubType
public override string ToString() => $"u{Width}"; public override string ToString() => $"u{Width}";
} }
public sealed class NubTypeSInt : NubType public class NubTypeSInt : NubType
{ {
private static readonly Dictionary<int, NubTypeSInt> Cache = new(); private static readonly Dictionary<int, NubTypeSInt> Cache = new();
@@ -60,7 +80,7 @@ public sealed class NubTypeSInt : NubType
public override string ToString() => $"i{Width}"; public override string ToString() => $"i{Width}";
} }
public sealed class NubTypeBool : NubType public class NubTypeBool : NubType
{ {
public static readonly NubTypeBool Instance = new(); public static readonly NubTypeBool Instance = new();
@@ -71,7 +91,7 @@ public sealed class NubTypeBool : NubType
public override string ToString() => "bool"; public override string ToString() => "bool";
} }
public sealed class NubTypeString : NubType public class NubTypeString : NubType
{ {
public static readonly NubTypeString Instance = new(); public static readonly NubTypeString Instance = new();
@@ -82,39 +102,122 @@ public sealed class NubTypeString : NubType
public override string ToString() => "string"; public override string ToString() => "string";
} }
public sealed class NubTypeStruct : NubType public class NubTypeChar : NubType
{ {
public string Name { get; } public static readonly NubTypeChar Instance = new();
public string Module { get; }
private IReadOnlyList<Field>? _resolvedFields; private NubTypeChar()
{
}
public IReadOnlyList<Field> Fields => _resolvedFields ?? throw new InvalidOperationException(); public override string ToString() => "char";
}
public NubTypeStruct(string module, string name) public class NubTypeStruct : NubType
{
private static readonly Dictionary<(string Module, string Name), NubTypeStruct> Cache = new();
public static NubTypeStruct Get(string module, string name)
{
if (!Cache.TryGetValue((module, name), out var structType))
Cache[(module, name)] = structType = new NubTypeStruct(module, name);
return structType;
}
private NubTypeStruct(string module, string name)
{ {
Module = module; Module = module;
Name = name; Name = name;
} }
public void ResolveFields(IReadOnlyList<Field> fields) public string Module { get; }
{ public string Name { get; }
if (_resolvedFields != null)
throw new InvalidOperationException($"{Name} already resolved");
_resolvedFields = fields; public override string ToString() => $"{Module}::{Name}";
}
public class NubTypeAnonymousStruct : NubType
{
private static readonly Dictionary<Signature, NubTypeAnonymousStruct> Cache = new();
public static NubTypeAnonymousStruct Get(List<Field> fields)
{
var sig = new Signature(fields);
if (!Cache.TryGetValue(sig, out var func))
Cache[sig] = func = new NubTypeAnonymousStruct(fields);
return func;
} }
public override string ToString() => _resolvedFields == null ? $"struct {Module}::{Name} <unresolved>" : $"struct {Module}::{Name} {{ {string.Join(' ', Fields.Select(f => $"{f.Name}: {f.Type}"))} }}"; private NubTypeAnonymousStruct(IReadOnlyList<Field> fields)
{
Fields = fields;
}
public sealed class Field(string name, NubType type) public IReadOnlyList<Field> Fields { get; }
public override string ToString() => $"{{ {string.Join(", ", Fields.Select(x => $"{x.Name}: {x.Type}"))} }}";
public class Field(string name, NubType type)
{ {
public string Name { get; } = name; public string Name { get; } = name;
public NubType Type { get; } = type; public NubType Type { get; } = type;
} }
private record Signature(IReadOnlyList<Field> Fields);
} }
public sealed class NubTypePointer : NubType public class NubTypeEnum : NubType
{
private static readonly Dictionary<(string Module, string Name), NubTypeEnum> Cache = new();
public static NubTypeEnum Get(string module, string name)
{
if (!Cache.TryGetValue((module, name), out var enumType))
Cache[(module, name)] = enumType = new NubTypeEnum(module, name);
return enumType;
}
private NubTypeEnum(string module, string name)
{
Module = module;
Name = name;
}
public string Module { get; }
public string Name { get; }
public override string ToString() => $"{Module}::{Name}";
}
public class NubTypeEnumVariant : NubType
{
private static readonly Dictionary<(NubTypeEnum EnumType, string Variant), NubTypeEnumVariant> Cache = new();
public static NubTypeEnumVariant Get(NubTypeEnum enumType, string variant)
{
if (!Cache.TryGetValue((enumType, variant), out var variantType))
Cache[(enumType, variant)] = variantType = new NubTypeEnumVariant(enumType, variant);
return variantType;
}
private NubTypeEnumVariant(NubTypeEnum enumType, string variant)
{
EnumType = enumType;
Variant = variant;
}
public NubTypeEnum EnumType { get; }
public string Variant { get; }
public override string ToString() => $"{EnumType}.{Variant}";
}
public class NubTypePointer : NubType
{ {
private static readonly Dictionary<NubType, NubTypePointer> Cache = new(); private static readonly Dictionary<NubType, NubTypePointer> Cache = new();
@@ -136,7 +239,7 @@ public sealed class NubTypePointer : NubType
public override string ToString() => $"^{To}"; public override string ToString() => $"^{To}";
} }
public sealed class NubTypeFunc : NubType public class NubTypeFunc : NubType
{ {
private static readonly Dictionary<Signature, NubTypeFunc> Cache = new(); private static readonly Dictionary<Signature, NubTypeFunc> Cache = new();
@@ -161,5 +264,445 @@ public sealed class NubTypeFunc : NubType
public override string ToString() => $"func({string.Join(' ', Parameters)}): {ReturnType}"; public override string ToString() => $"func({string.Join(' ', Parameters)}): {ReturnType}";
private readonly record struct Signature(IReadOnlyList<NubType> Parameters, NubType ReturnType); private record Signature(IReadOnlyList<NubType> Parameters, NubType ReturnType);
}
public class NubTypeArray : NubType
{
private static readonly Dictionary<NubType, NubTypeArray> Cache = new();
public static NubTypeArray Get(NubType to)
{
if (!Cache.TryGetValue(to, out var ptr))
Cache[to] = ptr = new NubTypeArray(to);
return ptr;
}
public NubType ElementType { get; }
private NubTypeArray(NubType elementType)
{
ElementType = elementType;
}
public override string ToString() => $"[]{ElementType}";
}
public class TypeEncoder
{
public static string Encode(NubType type)
{
return new TypeEncoder().EncodeRoot(type);
}
private TypeEncoder()
{
}
private string EncodeRoot(NubType type)
{
var sb = new StringBuilder();
EncodeType(sb, type);
return sb.ToString();
}
private void EncodeType(StringBuilder sb, NubType type)
{
switch (type)
{
case NubTypeVoid:
sb.Append('V');
break;
case NubTypeBool:
sb.Append('B');
break;
case NubTypeUInt u:
sb.Append($"U(");
sb.Append(u.Width);
sb.Append(')');
break;
case NubTypeSInt s:
sb.Append($"I(");
sb.Append(s.Width);
sb.Append(')');
break;
case NubTypeString:
sb.Append('S');
break;
case NubTypeChar:
sb.Append('C');
break;
case NubTypePointer p:
sb.Append("P(");
EncodeType(sb, p.To);
sb.Append(')');
break;
case NubTypeStruct st:
sb.Append("TN(");
sb.Append(st.Module);
sb.Append(':');
sb.Append(st.Name);
sb.Append(')');
break;
case NubTypeEnum e:
sb.Append("EN(");
sb.Append(e.Module);
sb.Append(':');
sb.Append(e.Name);
sb.Append(')');
break;
case NubTypeEnumVariant ev:
sb.Append("EV(");
sb.Append(ev.EnumType.Module);
sb.Append(':');
sb.Append(ev.EnumType.Name);
sb.Append(':');
sb.Append(ev.Variant);
sb.Append(')');
break;
case NubTypeFunc fn:
sb.Append("F(");
for (int i = 0; i < fn.Parameters.Count; i++)
{
EncodeType(sb, fn.Parameters[i]);
}
EncodeType(sb, fn.ReturnType);
sb.Append(')');
break;
case NubTypeAnonymousStruct s:
sb.Append("TA(");
foreach (var field in s.Fields)
{
sb.Append(field.Name);
sb.Append(':');
EncodeType(sb, field.Type);
}
sb.Append(')');
break;
case NubTypeArray a:
sb.Append("A(");
EncodeType(sb, a.ElementType);
sb.Append(')');
break;
default:
throw new NotSupportedException(type.GetType().Name);
}
}
private class Definition(int index)
{
public int Index { get; } = index;
public string? Encoded { get; set; }
}
}
public class TypeDecoder
{
public static NubType Decode(string encoded)
{
return new TypeDecoder(encoded).DecodeType();
}
private TypeDecoder(string encoded)
{
this.encoded = encoded;
}
private readonly string encoded;
private int index = 0;
private NubType DecodeType()
{
var start = Consume();
return start switch
{
'V' => NubTypeVoid.Instance,
'B' => NubTypeBool.Instance,
'U' => DecodeUInt(),
'I' => DecodeSInt(),
'S' => NubTypeString.Instance,
'C' => NubTypeChar.Instance,
'P' => DecodePointer(),
'F' => DecodeFunc(),
'T' => DecodeStruct(),
'E' => DecodeEnum(),
'A' => DecodeArray(),
_ => throw new Exception($"'{start}' is not a valid start to a type")
};
}
private NubTypeUInt DecodeUInt()
{
Expect('(');
var width = ExpectInt();
Expect(')');
return NubTypeUInt.Get(width);
}
private NubTypeSInt DecodeSInt()
{
Expect('(');
var width = ExpectInt();
Expect(')');
return NubTypeSInt.Get(width);
}
private NubTypePointer DecodePointer()
{
Expect('(');
var to = DecodeType();
Expect(')');
return NubTypePointer.Get(to);
}
private NubTypeFunc DecodeFunc()
{
var types = new List<NubType>();
Expect('(');
while (!TryExpect(')'))
{
types.Add(DecodeType());
}
return NubTypeFunc.Get(types.Take(types.Count - 1).ToList(), types.Last());
}
private NubType DecodeStruct()
{
if (TryExpect('A'))
{
var sb = new StringBuilder();
var fields = new List<NubTypeAnonymousStruct.Field>();
Expect('(');
while (!TryExpect(')'))
{
while (!TryExpect(':'))
{
sb.Append(Consume());
}
var name = sb.ToString();
sb.Clear();
var type = DecodeType();
fields.Add(new NubTypeAnonymousStruct.Field(name, type));
}
return NubTypeAnonymousStruct.Get(fields);
}
if (TryExpect('N'))
{
var sb = new StringBuilder();
Expect('(');
while (!TryExpect(':'))
sb.Append(Consume());
var module = sb.ToString();
sb.Clear();
while (!TryExpect(')'))
sb.Append(Consume());
var name = sb.ToString();
return NubTypeStruct.Get(module, name);
}
throw new Exception("Expected 'A' or 'N'");
}
private NubType DecodeEnum()
{
var sb = new StringBuilder();
if (TryExpect('V'))
{
Expect('(');
while (!TryExpect(':'))
sb.Append(Consume());
var module = sb.ToString();
sb.Clear();
while (!TryExpect(':'))
sb.Append(Consume());
var name = sb.ToString();
while (!TryExpect(')'))
sb.Append(Consume());
var variant = sb.ToString();
return NubTypeEnumVariant.Get(NubTypeEnum.Get(module, name), variant);
}
else if (TryExpect('N'))
{
Expect('(');
while (!TryExpect(':'))
sb.Append(Consume());
var module = sb.ToString();
sb.Clear();
while (!TryExpect(')'))
sb.Append(Consume());
var name = sb.ToString();
return NubTypeEnum.Get(module, name);
}
throw new Exception($"Expected 'V' or 'N'");
}
private NubTypeArray DecodeArray()
{
Expect('(');
var elementType = DecodeType();
Expect(')');
return NubTypeArray.Get(elementType);
}
private bool TryPeek(out char c)
{
if (index >= encoded.Length)
{
c = '\0';
return false;
}
c = encoded[index];
return true;
}
private bool TryConsume(out char c)
{
if (index >= encoded.Length)
{
c = '\0';
return false;
}
c = encoded[index];
index += 1;
return true;
}
private char Consume()
{
if (!TryConsume(out var c))
throw new Exception("Unexpected end of string");
return c;
}
private bool TryExpect(char c)
{
if (index >= encoded.Length)
return false;
if (encoded[index] != c)
return false;
Consume();
return true;
}
private void Expect(char c)
{
if (!TryExpect(c))
throw new Exception($"Expected '{c}'");
}
private int ExpectInt()
{
var buf = string.Empty;
while (TryPeek(out var c))
{
if (!char.IsDigit(c))
break;
buf += Consume();
}
return int.Parse(buf);
}
}
public class NubTypeJsonConverter : JsonConverter<NubType>
{
public override NubType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return TypeDecoder.Decode(reader.GetString()!);
}
public override void Write(Utf8JsonWriter writer, NubType value, JsonSerializerOptions options)
{
writer.WriteStringValue(TypeEncoder.Encode(value));
}
}
public static class Hashing
{
public static ulong Fnv1a64(string text)
{
const ulong offset = 14695981039346656037UL;
const ulong prime = 1099511628211UL;
ulong hash = offset;
foreach (var c in Encoding.UTF8.GetBytes(text))
{
hash ^= c;
hash *= prime;
}
return hash;
}
}
public static class NameMangler
{
public static string Mangle(string module, string name, NubType type)
{
var canonical = TypeEncoder.Encode(type);
var hash = Hashing.Fnv1a64(canonical);
return $"nub_{Sanitize(module)}_{Sanitize(name)}_{hash:x16}";
}
private static string Sanitize(string s)
{
var sb = new StringBuilder(s.Length);
foreach (var c in s)
{
if (char.IsLetterOrDigit(c))
sb.Append(c);
else
sb.Append('_');
}
return sb.ToString();
}
} }

File diff suppressed because it is too large Load Diff

View File

@@ -1,10 +1,75 @@
using System.Diagnostics; using System.Diagnostics;
using Compiler; using Compiler;
var moduleGraphBuilder = ModuleGraph.Create(); var nubFiles = new List<string>();
var asts = new List<Ast>(); var libFiles = new List<string>();
var compileLib = false;
foreach (var fileName in args) for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
if (arg.StartsWith("--type="))
{
var value = arg.Split("--type=")[1];
switch (value)
{
case "lib":
compileLib = true;
break;
case "exe":
compileLib = false;
break;
default:
DiagnosticFormatter.Print(Diagnostic.Error("Type must be 'exe' or 'lib'").Build(), Console.Error);
return 1;
}
}
else if (arg.EndsWith(".nub"))
{
nubFiles.Add(arg);
}
else if (arg.EndsWith(".nublib"))
{
libFiles.Add(arg);
}
else if (arg == "--help")
{
Console.WriteLine("""
Usage: nubc [options] <files>
Options:
--type=exe Compile the input files into an executable (default)
--type=lib Compile the input files into a library
--help Show this help message
Files:
*.nub Nub source files to compile
*.nublib Precompiled Nub libraries to link
Example:
nubc --type=exe main.nub utils.nub math.nublib
""");
}
else
{
DiagnosticFormatter.Print(Diagnostic.Error($"Unrecognized option '{arg}'").Build(), Console.Error);
return 1;
}
}
var moduleGraphBuilder = ModuleGraph.CreateBuilder();
var asts = new List<Ast>();
var archivePaths = new List<string>();
foreach (var libPath in libFiles)
{
var lib = NubLib.Unpack(libPath);
archivePaths.Add(lib.ArchivePath);
moduleGraphBuilder.AddManifest(lib.Manifest);
}
foreach (var fileName in nubFiles)
{ {
var file = File.ReadAllText(fileName); var file = File.ReadAllText(fileName);
@@ -13,7 +78,7 @@ foreach (var fileName in args)
foreach (var diagnostic in tokenizerDiagnostics) foreach (var diagnostic in tokenizerDiagnostics)
DiagnosticFormatter.Print(diagnostic, Console.Error); DiagnosticFormatter.Print(diagnostic, Console.Error);
if (tokenizerDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error)) if (tokens == null)
return 1; return 1;
var ast = Parser.Parse(fileName, tokens, out var parserDiagnostics); var ast = Parser.Parse(fileName, tokens, out var parserDiagnostics);
@@ -21,7 +86,7 @@ foreach (var fileName in args)
foreach (var diagnostic in parserDiagnostics) foreach (var diagnostic in parserDiagnostics)
DiagnosticFormatter.Print(diagnostic, Console.Error); DiagnosticFormatter.Print(diagnostic, Console.Error);
if (parserDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error)) if (ast == null)
return 1; return 1;
moduleGraphBuilder.AddAst(ast); moduleGraphBuilder.AddAst(ast);
@@ -33,7 +98,7 @@ var moduleGraph = moduleGraphBuilder.Build(out var moduleGraphDiagnostics);
foreach (var diagnostic in moduleGraphDiagnostics) foreach (var diagnostic in moduleGraphDiagnostics)
DiagnosticFormatter.Print(diagnostic, Console.Error); DiagnosticFormatter.Print(diagnostic, Console.Error);
if (moduleGraphDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error)) if (moduleGraph == null)
return 1; return 1;
var functions = new List<TypedNodeDefinitionFunc>(); var functions = new List<TypedNodeDefinitionFunc>();
@@ -42,7 +107,7 @@ foreach (var ast in asts)
{ {
foreach (var func in ast.Definitions.OfType<NodeDefinitionFunc>()) foreach (var func in ast.Definitions.OfType<NodeDefinitionFunc>())
{ {
var typedFunction = TypeChecker.CheckFunction(ast.FileName, func, moduleGraph, out var typeCheckerDiagnostics); var typedFunction = TypeChecker.CheckFunction(ast.FileName, ast.ModuleName.Ident, func, moduleGraph, out var typeCheckerDiagnostics);
foreach (var diagnostic in typeCheckerDiagnostics) foreach (var diagnostic in typeCheckerDiagnostics)
DiagnosticFormatter.Print(diagnostic, Console.Error); DiagnosticFormatter.Print(diagnostic, Console.Error);
@@ -54,13 +119,61 @@ foreach (var ast in asts)
} }
} }
var output = Generator.Emit(functions, moduleGraph); if (Directory.Exists(".build"))
{
CleanDirectory(".build");
}
else
{
Directory.CreateDirectory(".build");
}
Directory.Delete(".build", recursive: true); string? entryPoint = null;
Directory.CreateDirectory(".build");
File.WriteAllText(".build/out.c", output); if (!compileLib)
{
if (!moduleGraph.TryResolveIdentifier("main", "main", true, out var info) || info.Type is not NubTypeFunc entryPointType)
{
DiagnosticFormatter.Print(Diagnostic.Error("func main::main(): i32 is not defined. If you wanted to compile as a library, specify --type=lib").Build(), Console.Error);
return 1;
}
Process.Start("gcc", ["-Og", "-g", "-o", ".build/out", ".build/out.c"]); if (!entryPointType.ReturnType.IsAssignableTo(NubTypeSInt.Get(32)))
{
DiagnosticFormatter.Print(Diagnostic.Error($"Entrypoint must return an i32 (currently '{entryPointType.ReturnType}')").Build(), Console.Error);
return 1;
}
entryPoint = info.MangledName;
}
var outFile = Generator.Emit(functions, moduleGraph, entryPoint);
if (compileLib)
{
Process.Start("gcc", ["-Og", "-g", "-Wall", "-Werror", "-c", "-o", ".build/out.o", outFile, .. archivePaths]).WaitForExit();
Process.Start("ar", ["rcs", ".build/out.a", ".build/out.o"]).WaitForExit();
NubLib.Pack(".build/out.nublib", ".build/out.a", Manifest.Create(moduleGraph));
}
else
{
Process.Start("gcc", ["-Og", "-g", "-Wall", "-Werror", "-o", ".build/out", outFile, .. archivePaths]).WaitForExit();
}
return 0; return 0;
static void CleanDirectory(string dirName)
{
var dir = new DirectoryInfo(dirName);
foreach (var file in dir.GetFiles())
{
file.Delete();
}
foreach (var subdir in dir.GetDirectories())
{
CleanDirectory(subdir.FullName);
subdir.Delete();
}
}

View File

@@ -3,24 +3,33 @@ using System.Text;
namespace Compiler; namespace Compiler;
public sealed class Tokenizer(string fileName, string contents) public class Tokenizer
{ {
public static List<Token> Tokenize(string fileName, string contents, out List<Diagnostic> diagnostics) public static List<Token>? Tokenize(string fileName, string contents, out List<Diagnostic> diagnostics)
{ {
return new Tokenizer(fileName, contents).Tokenize(out diagnostics); return new Tokenizer(fileName, contents).Tokenize(out diagnostics);
} }
private Tokenizer(string fileName, string contents)
{
this.fileName = fileName;
this.contents = contents;
}
private readonly string fileName;
private readonly string contents;
private int index; private int index;
private int line = 1; private int line = 1;
private int column = 1; private int column = 1;
private List<Token> Tokenize(out List<Diagnostic> diagnostics) private List<Token>? Tokenize(out List<Diagnostic> diagnostics)
{ {
var tokens = new List<Token>(); var tokens = new List<Token>();
diagnostics = []; diagnostics = [];
try
{
while (true) while (true)
{
try
{ {
if (!TryPeek(out var c)) if (!TryPeek(out var c))
break; break;
@@ -31,13 +40,29 @@ public sealed class Tokenizer(string fileName, string contents)
continue; continue;
} }
tokens.Add(ParseToken()); if (c == '/' && Peek(1) == '/')
{
Consume();
Consume();
while (TryPeek(out c) && c != '\n')
Consume();
Consume();
continue;
} }
tokens.Add(ParseToken());
} }
catch (CompileException e) catch (CompileException e)
{ {
diagnostics.Add(e.Diagnostic); diagnostics.Add(e.Diagnostic);
// Skip current token if parsing failed, this prevents an infinite loop when ParseToken fails before consuming any tokens
TryConsume(out _);
} }
}
if (diagnostics.Any(x => x.Severity == Diagnostic.DiagnosticSeverity.Error))
return null;
return tokens; return tokens;
} }
@@ -57,6 +82,7 @@ public sealed class Tokenizer(string fileName, string contents)
Consume(); Consume();
var parsed = BigInteger.Zero; var parsed = BigInteger.Zero;
var seenDigit = false;
while (TryPeek(out c)) while (TryPeek(out c))
{ {
@@ -69,6 +95,7 @@ public sealed class Tokenizer(string fileName, string contents)
if (!char.IsAsciiHexDigit(c)) if (!char.IsAsciiHexDigit(c))
break; break;
seenDigit = true;
parsed <<= 4; parsed <<= 4;
Consume(); Consume();
@@ -81,6 +108,9 @@ public sealed class Tokenizer(string fileName, string contents)
}; };
} }
if (!seenDigit)
throw new CompileException(Diagnostic.Error("Expected hexadecimal digits after 0x").At(fileName, line, startColumn, column - startColumn).Build());
return new TokenIntLiteral(line, startColumn, column - startColumn, parsed); return new TokenIntLiteral(line, startColumn, column - startColumn, parsed);
} }
case '0' when Peek(1) is 'b': case '0' when Peek(1) is 'b':
@@ -89,6 +119,7 @@ public sealed class Tokenizer(string fileName, string contents)
Consume(); Consume();
var parsed = BigInteger.Zero; var parsed = BigInteger.Zero;
var seenDigit = false;
while (TryPeek(out c)) while (TryPeek(out c))
{ {
@@ -101,11 +132,16 @@ public sealed class Tokenizer(string fileName, string contents)
if (c is not '0' and not '1') if (c is not '0' and not '1')
break; break;
seenDigit = true;
parsed <<= 1; parsed <<= 1;
if (Consume() == '1') if (Consume() == '1')
parsed += BigInteger.One; parsed += BigInteger.One;
} }
if (!seenDigit)
throw new CompileException(Diagnostic.Error("Expected binary digits after 0b").At(fileName, line, startColumn, column - startColumn).Build());
return new TokenIntLiteral(line, startColumn, column - startColumn, parsed); return new TokenIntLiteral(line, startColumn, column - startColumn, parsed);
} }
default: default:
@@ -137,16 +173,26 @@ public sealed class Tokenizer(string fileName, string contents)
case '"': case '"':
{ {
Consume(); Consume();
var buf = new StringBuilder(); var buf = new StringBuilder();
while (TryPeek(out c) && c != '"') while (true)
{
if (!TryPeek(out c))
throw new CompileException(Diagnostic.Error("Unterminated string literal").At(fileName, line, column, 0).Build());
if (c == '"')
break;
if (c == '\n')
throw new CompileException(Diagnostic.Error("Unterminated string literal").At(fileName, line, column, 1).Build());
buf.Append(Consume()); buf.Append(Consume());
}
Consume(); Consume();
return new TokenStringLiteral(line, startColumn, column - startColumn, buf.ToString()); return new TokenStringLiteral(line, startColumn, column - startColumn, buf.ToString());
} }
case '{': case '{':
{ {
Consume(); Consume();
@@ -157,6 +203,16 @@ public sealed class Tokenizer(string fileName, string contents)
Consume(); Consume();
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.CloseCurly); return new TokenSymbol(line, startColumn, column - startColumn, Symbol.CloseCurly);
} }
case '[':
{
Consume();
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.OpenSquare);
}
case ']':
{
Consume();
return new TokenSymbol(line, startColumn, column - startColumn, Symbol.CloseSquare);
}
case '(': case '(':
{ {
Consume(); Consume();
@@ -341,29 +397,40 @@ public sealed class Tokenizer(string fileName, string contents)
{ {
"func" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Func), "func" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Func),
"struct" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Struct), "struct" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Struct),
"packed" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Packed),
"enum" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Enum),
"new" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.New),
"match" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Match),
"let" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Let), "let" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Let),
"if" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.If), "if" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.If),
"else" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Else), "else" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Else),
"while" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.While), "while" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.While),
"for" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.For),
"in" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.In),
"return" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Return), "return" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Return),
"module" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Module), "module" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Module),
"export" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Export),
"extern" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Extern),
"true" => new TokenBoolLiteral(line, startColumn, column - startColumn, true), "true" => new TokenBoolLiteral(line, startColumn, column - startColumn, true),
"false" => new TokenBoolLiteral(line, startColumn, column - startColumn, false), "false" => new TokenBoolLiteral(line, startColumn, column - startColumn, false),
_ => new TokenIdent(line, startColumn, column - startColumn, value) _ => new TokenIdent(line, startColumn, column - startColumn, value)
}; };
} }
throw new Exception($"Unexpected character '{c}'"); throw new CompileException(Diagnostic.Error($"Unexpected character '{c}'").At(fileName, line, column, 1).Build());
} }
} }
} }
private char Consume() private bool TryConsume(out char c)
{ {
if (index >= contents.Length) if (index >= contents.Length)
throw new CompileException(Diagnostic.Error("Unexpected end of file").At(fileName, line, column, 0).Build()); {
c = '\0';
return false;
}
var c = contents[index]; c = contents[index];
if (c == '\n') if (c == '\n')
{ {
@@ -377,6 +444,14 @@ public sealed class Tokenizer(string fileName, string contents)
index += 1; index += 1;
return true;
}
private char Consume()
{
if (!TryConsume(out var c))
throw new CompileException(Diagnostic.Error("Unexpected end of file").At(fileName, line, column, 0).Build());
return c; return c;
} }
@@ -403,29 +478,29 @@ public sealed class Tokenizer(string fileName, string contents)
public abstract class Token(int line, int column, int length) public abstract class Token(int line, int column, int length)
{ {
public int Line = line; public int Line { get; } = line;
public int Column = column; public int Column { get; } = column;
public int Length = length; public int Length { get; } = length;
} }
public sealed class TokenIdent(int line, int column, int length, string ident) : Token(line, column, length) public class TokenIdent(int line, int column, int length, string ident) : Token(line, column, length)
{ {
public readonly string Ident = ident; public string Ident { get; } = ident;
} }
public sealed class TokenIntLiteral(int line, int column, int length, BigInteger value) : Token(line, column, length) public class TokenIntLiteral(int line, int column, int length, BigInteger value) : Token(line, column, length)
{ {
public BigInteger Value = value; public BigInteger Value { get; } = value;
} }
public sealed class TokenStringLiteral(int line, int column, int length, string value) : Token(line, column, length) public class TokenStringLiteral(int line, int column, int length, string value) : Token(line, column, length)
{ {
public readonly string Value = value; public string Value { get; } = value;
} }
public sealed class TokenBoolLiteral(int line, int column, int length, bool value) : Token(line, column, length) public class TokenBoolLiteral(int line, int column, int length, bool value) : Token(line, column, length)
{ {
public readonly bool Value = value; public bool Value { get; } = value;
} }
public enum Symbol public enum Symbol
@@ -434,6 +509,8 @@ public enum Symbol
CloseCurly, CloseCurly,
OpenParen, OpenParen,
CloseParen, CloseParen,
OpenSquare,
CloseSquare,
Comma, Comma,
Period, Period,
Colon, Colon,
@@ -465,26 +542,34 @@ public enum Symbol
PipePipe, PipePipe,
} }
public sealed class TokenSymbol(int line, int column, int length, Symbol symbol) : Token(line, column, length) public class TokenSymbol(int line, int column, int length, Symbol symbol) : Token(line, column, length)
{ {
public readonly Symbol Symbol = symbol; public Symbol Symbol { get; } = symbol;
} }
public enum Keyword public enum Keyword
{ {
Func, Func,
Struct, Struct,
Packed,
Enum,
New,
Match,
Let, Let,
If, If,
Else, Else,
While, While,
For,
In,
Return, Return,
Module, Module,
Export,
Extern,
} }
public sealed class TokenKeyword(int line, int column, int length, Keyword keyword) : Token(line, column, length) public class TokenKeyword(int line, int column, int length, Keyword keyword) : Token(line, column, length)
{ {
public readonly Keyword Keyword = keyword; public Keyword Keyword { get; } = keyword;
} }
public static class TokenExtensions public static class TokenExtensions
@@ -497,15 +582,17 @@ public static class TokenExtensions
Symbol.CloseCurly => "}", Symbol.CloseCurly => "}",
Symbol.OpenParen => "(", Symbol.OpenParen => "(",
Symbol.CloseParen => ")", Symbol.CloseParen => ")",
Symbol.OpenSquare => "[",
Symbol.CloseSquare => "]",
Symbol.Comma => ",", Symbol.Comma => ",",
Symbol.Period => ",", Symbol.Period => ".",
Symbol.Colon => ":", Symbol.Colon => ":",
Symbol.ColonColon => "::", Symbol.ColonColon => "::",
Symbol.Caret => "^", Symbol.Caret => "^",
Symbol.Bang => "!", Symbol.Bang => "!",
Symbol.Equal => "=", Symbol.Equal => "=",
Symbol.EqualEqual => "==", Symbol.EqualEqual => "==",
Symbol.BangEqual => "!+", Symbol.BangEqual => "!=",
Symbol.LessThan => "<", Symbol.LessThan => "<",
Symbol.LessThanLessThan => "<<", Symbol.LessThanLessThan => "<<",
Symbol.LessThanEqual => "<=", Symbol.LessThanEqual => "<=",
@@ -536,12 +623,20 @@ public static class TokenExtensions
{ {
Keyword.Func => "func", Keyword.Func => "func",
Keyword.Struct => "struct", Keyword.Struct => "struct",
Keyword.Packed => "packed",
Keyword.Enum => "enum",
Keyword.New => "new",
Keyword.Match => "enum",
Keyword.Let => "let", Keyword.Let => "let",
Keyword.If => "if", Keyword.If => "if",
Keyword.Else => "else", Keyword.Else => "else",
Keyword.While => "while", Keyword.While => "while",
Keyword.For => "for",
Keyword.In => "in",
Keyword.Return => "return", Keyword.Return => "return",
Keyword.Module => "module", Keyword.Module => "module",
Keyword.Export => "export",
Keyword.Extern => "extern",
_ => throw new ArgumentOutOfRangeException(nameof(symbol), symbol, null) _ => throw new ArgumentOutOfRangeException(nameof(symbol), symbol, null)
}; };
} }

File diff suppressed because it is too large Load Diff

View File

@@ -1,27 +0,0 @@
module main
func main(): i32 {
let x: i32 = 23
x = 24
if !true {
x = 49
return x
} else {
x = 3
}
let i: i32 = 0
x = 1 + 2 * 34
while i < 10 {
i = i + 1
x = i
}
let me: test::person = struct test::person { age = 21 name = "Oliver" }
test::do_something(me.name)
return x
}

View File

@@ -1,9 +0,0 @@
module test
struct person {
age: i32
name: string
}
func do_something(name: string): void {
}

1
examples/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.build

21
examples/build Executable file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
set -e
SCRIPT_DIR=$(dirname "$0")
pushd $SCRIPT_DIR
pushd ../compiler
dotnet build -c Release
popd
pushd core
time ../../compiler/bin/Release/net9.0/Compiler sys.nub print.nub --type=lib
popd
pushd program
time ../../compiler/bin/Release/net9.0/Compiler main.nub ../core/.build/out.nublib
popd
./program/.build/out
popd

10
examples/core/print.nub Normal file
View File

@@ -0,0 +1,10 @@
module core
export func print(text: string) {
sys::write(0, text.ptr, text.length)
}
export func println(text: string) {
print(text)
print("\n")
}

5
examples/core/sys.nub Normal file
View File

@@ -0,0 +1,5 @@
module sys
export extern func read(fd: u32, buf: ^char, count: u64): i64
export extern func write(fd: u32, buf: ^char, count: u64): i64
export extern func open(fileName: ^char, flags: i32, mode: u16): i64

32
examples/program/main.nub Normal file
View File

@@ -0,0 +1,32 @@
module main
struct Human {
name: string
age: i32
}
enum Message {
Quit
Say: string
}
func main(): i32 {
let x = new string("test".ptr) + " " + "uwu"
core::println(x)
let messages: []Message = [new Message::Say("first"), new Message::Quit]
for message in messages {
match message {
Say msg {
core::println(msg)
}
Quit {
core::println("quit")
}
}
}
return 0
}