Compare commits
74 Commits
d409bb4d92
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa5a494d39 | ||
|
|
bc65c3f4fd | ||
|
|
bdeb2c4d73 | ||
|
|
48760dcdda | ||
|
|
63d0eb660b | ||
|
|
918d25f8c8 | ||
|
|
6e631ba1ba | ||
|
|
832184053f | ||
|
|
2ab20652f8 | ||
|
|
6507624e90 | ||
|
|
83255980d7 | ||
|
|
1a1dc1389d | ||
|
|
4210ad878b | ||
|
|
32042d0769 | ||
|
|
dd44e3edc4 | ||
|
|
24d41d3dcb | ||
|
|
c9bf212aa2 | ||
|
|
0e099d0baf | ||
|
|
0be4e35628 | ||
|
|
7dd635fa91 | ||
|
|
693b119781 | ||
|
|
2b7eb56895 | ||
|
|
5f4a4172bf | ||
|
|
9da370e387 | ||
|
|
faf506f409 | ||
|
|
16d27c76ab | ||
|
|
270be1f740 | ||
|
|
da2fa81c39 | ||
|
|
677c7dea9e | ||
|
|
6ba05d00d7 | ||
|
|
d58e006be4 | ||
|
|
84627dde45 | ||
|
|
e7aad861d3 | ||
|
|
ecb628c4c2 | ||
|
|
e5227f7a99 | ||
|
|
73ecbf8e9b | ||
|
|
e512650440 | ||
|
|
272ea33616 | ||
|
|
b7dc77cb1c | ||
|
|
3323d760e8 | ||
|
|
aa5bf0b568 | ||
|
|
660166221c | ||
|
|
49734544e6 | ||
|
|
cb4aeb9c01 | ||
|
|
d771396bd4 | ||
|
|
7b1e202424 | ||
|
|
282dd0502a | ||
|
|
3ebaa67b6d | ||
|
|
35867ffa28 | ||
|
|
76efc84984 | ||
|
|
583c01201f | ||
|
|
1511d5d2b8 | ||
|
|
caa3b378b3 | ||
|
|
cbe27c0ae8 | ||
|
|
4d0f7c715f | ||
|
|
c342b2e102 | ||
|
|
ee485e4119 | ||
|
|
21a095f691 | ||
|
|
0a5b39b217 | ||
|
|
ab2f5750dc | ||
|
|
b45a0fa8cc | ||
|
|
7daccba9f3 | ||
|
|
924bdae89b | ||
|
|
5364b0420a | ||
|
|
c65fbeba13 | ||
|
|
9d8d8f255a | ||
|
|
576abe1240 | ||
|
|
d0c31ad17f | ||
|
|
7872a4b6b8 | ||
|
|
6ae10d5f90 | ||
|
|
d3e2dcede8 | ||
|
|
88fe03c048 | ||
|
|
9ccdd5f835 | ||
|
|
b7cfdd2519 |
@@ -1,29 +1,51 @@
|
||||
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 DiagnosticBuilder Warning(string message) => new DiagnosticBuilder(DiagnosticSeverity.Warning, message);
|
||||
public static DiagnosticBuilder Error(string message) => new DiagnosticBuilder(DiagnosticSeverity.Error, message);
|
||||
public static Builder Info(string message) => new Builder(DiagnosticSeverity.Info, message);
|
||||
public static Builder Warning(string message) => new Builder(DiagnosticSeverity.Warning, message);
|
||||
public static Builder Error(string message) => new Builder(DiagnosticSeverity.Error, message);
|
||||
|
||||
public readonly DiagnosticSeverity Severity = severity;
|
||||
public readonly string Message = message;
|
||||
public readonly string? Help = help;
|
||||
public readonly FileInfo? File = file;
|
||||
}
|
||||
private Diagnostic(DiagnosticSeverity severity, string message, string? help, FileInfo? file)
|
||||
{
|
||||
Severity = severity;
|
||||
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 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);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DiagnosticBuilder At(string fileName, Token? token)
|
||||
public Builder At(string fileName, Token? token)
|
||||
{
|
||||
if (token != null)
|
||||
{
|
||||
@@ -33,7 +55,7 @@ public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string messag
|
||||
return this;
|
||||
}
|
||||
|
||||
public DiagnosticBuilder At(string fileName, Node? node)
|
||||
public Builder At(string fileName, Node? node)
|
||||
{
|
||||
if (node != null && node.Tokens.Count != 0)
|
||||
{
|
||||
@@ -44,7 +66,7 @@ public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string messag
|
||||
return this;
|
||||
}
|
||||
|
||||
public DiagnosticBuilder At(string fileName, TypedNode? node)
|
||||
public Builder At(string fileName, TypedNode? node)
|
||||
{
|
||||
if (node != null && node.Tokens.Count != 0)
|
||||
{
|
||||
@@ -55,7 +77,7 @@ public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string messag
|
||||
return this;
|
||||
}
|
||||
|
||||
public DiagnosticBuilder WithHelp(string helpMessage)
|
||||
public Builder WithHelp(string helpMessage)
|
||||
{
|
||||
help = helpMessage;
|
||||
return this;
|
||||
@@ -65,26 +87,12 @@ public sealed class DiagnosticBuilder(DiagnosticSeverity severity, string messag
|
||||
{
|
||||
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 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 Diagnostic Diagnostic { get; } = diagnostic;
|
||||
}
|
||||
|
||||
public static class DiagnosticFormatter
|
||||
@@ -93,9 +101,9 @@ public static class DiagnosticFormatter
|
||||
{
|
||||
var (label, color) = diagnostic.Severity switch
|
||||
{
|
||||
DiagnosticSeverity.Info => ("info", Ansi.Cyan),
|
||||
DiagnosticSeverity.Warning => ("warning", Ansi.Yellow),
|
||||
DiagnosticSeverity.Error => ("error", Ansi.Red),
|
||||
Diagnostic.DiagnosticSeverity.Info => ("info", Ansi.Cyan),
|
||||
Diagnostic.DiagnosticSeverity.Warning => ("warning", Ansi.Yellow),
|
||||
Diagnostic.DiagnosticSeverity.Error => ("error", Ansi.Red),
|
||||
_ => ("unknown", Ansi.Reset),
|
||||
};
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -3,165 +3,444 @@ using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
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()
|
||||
{
|
||||
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)
|
||||
{
|
||||
module = modules.GetValueOrDefault(moduleName);
|
||||
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
|
||||
{
|
||||
private readonly List<Ast> asts = [];
|
||||
private readonly List<Manifest> manifests = [];
|
||||
|
||||
public void AddAst(Ast 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 = [];
|
||||
|
||||
var astModuleCache = new Dictionary<Ast, 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)
|
||||
{
|
||||
var moduleDefinitions = ast.Definitions.OfType<NodeDefinitionModule>().ToList();
|
||||
|
||||
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;
|
||||
var module = GetOrCreateModule(ast.ModuleName.Ident);
|
||||
|
||||
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
|
||||
{
|
||||
if (!module.TryResolveCustomType(structDef.Name.Ident, out var customType))
|
||||
throw new UnreachableException($"{nameof(customType)} should always be registered");
|
||||
module.AddType(structDef.Name.Ident, new Module.TypeInfoStruct(Module.DefinitionSource.Internal, structDef.Exported, structDef.Packed));
|
||||
}
|
||||
|
||||
var fields = structDef.Fields.Select(f => new NubTypeStruct.Field(f.Name.Ident, ResolveType(f.Type))).ToList();
|
||||
((NubTypeStruct)customType).ResolveFields(fields);
|
||||
foreach (var enumDef in ast.Definitions.OfType<NodeDefinitionEnum>())
|
||||
{
|
||||
module.AddType(enumDef.Name.Ident, new Module.TypeInfoEnum(Module.DefinitionSource.Internal, enumDef.Exported));
|
||||
}
|
||||
}
|
||||
|
||||
// Fourth pass: Register identifiers
|
||||
foreach (var ast in asts)
|
||||
{
|
||||
var module = astModuleCache.GetValueOrDefault(ast);
|
||||
if (module == null) continue;
|
||||
var module = GetOrCreateModule(ast.ModuleName.Ident);
|
||||
|
||||
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>())
|
||||
{
|
||||
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type)).ToList();
|
||||
var returnType = ResolveType(funcDef.ReturnType);
|
||||
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);
|
||||
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);
|
||||
|
||||
NubType ResolveType(NodeType node)
|
||||
NubType ResolveType(NodeType node, string currentModule)
|
||||
{
|
||||
return node switch
|
||||
{
|
||||
NodeTypeBool => NubTypeBool.Instance,
|
||||
NodeTypeCustom type => ResolveCustomType(type),
|
||||
NodeTypeFunc type => NubTypeFunc.Get(type.Parameters.Select(ResolveType).ToList(), ResolveType(type.ReturnType)),
|
||||
NodeTypePointer type => NubTypePointer.Get(ResolveType(type.To)),
|
||||
NodeTypeNamed type => ResolveNamedType(type, currentModule),
|
||||
NodeTypeAnonymousStruct type => NubTypeAnonymousStruct.Get(type.Fields.Select(x => new NubTypeAnonymousStruct.Field(x.Name.Ident, ResolveType(x.Type, currentModule))).ToList()),
|
||||
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),
|
||||
NodeTypeUInt type => NubTypeUInt.Get(type.Width),
|
||||
NodeTypeString => NubTypeString.Instance,
|
||||
NodeTypeChar => NubTypeChar.Instance,
|
||||
NodeTypeVoid => NubTypeVoid.Instance,
|
||||
NodeTypeArray type => NubTypeArray.Get(ResolveType(type.ElementType, currentModule)),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
|
||||
NubType ResolveCustomType(NodeTypeCustom type)
|
||||
NubType ResolveNamedType(NodeTypeNamed type, string currentModule)
|
||||
{
|
||||
var module = modules.GetValueOrDefault(type.Module.Ident);
|
||||
if (module == null)
|
||||
throw new CompileException(Diagnostic.Error($"Unknown module: {type.Module.Ident}").Build());
|
||||
return type.Sections.Count switch
|
||||
{
|
||||
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))
|
||||
throw new CompileException(Diagnostic.Error($"Unknown custom type: {type.Module.Ident}::{type.Name.Ident}").Build());
|
||||
NubType ResolveThreePartType(TokenIdent first, TokenIdent second, TokenIdent third, string currentModule)
|
||||
{
|
||||
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
110
compiler/NubLib.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,31 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Compiler;
|
||||
|
||||
[JsonConverter(typeof(NubTypeJsonConverter))]
|
||||
public abstract class NubType
|
||||
{
|
||||
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();
|
||||
|
||||
@@ -16,7 +36,7 @@ public sealed class NubTypeVoid : NubType
|
||||
public override string ToString() => "void";
|
||||
}
|
||||
|
||||
public sealed class NubTypeUInt : NubType
|
||||
public class NubTypeUInt : NubType
|
||||
{
|
||||
private static readonly Dictionary<int, NubTypeUInt> Cache = new();
|
||||
|
||||
@@ -38,7 +58,7 @@ public sealed class NubTypeUInt : NubType
|
||||
public override string ToString() => $"u{Width}";
|
||||
}
|
||||
|
||||
public sealed class NubTypeSInt : NubType
|
||||
public class NubTypeSInt : NubType
|
||||
{
|
||||
private static readonly Dictionary<int, NubTypeSInt> Cache = new();
|
||||
|
||||
@@ -60,7 +80,7 @@ public sealed class NubTypeSInt : NubType
|
||||
public override string ToString() => $"i{Width}";
|
||||
}
|
||||
|
||||
public sealed class NubTypeBool : NubType
|
||||
public class NubTypeBool : NubType
|
||||
{
|
||||
public static readonly NubTypeBool Instance = new();
|
||||
|
||||
@@ -71,7 +91,7 @@ public sealed class NubTypeBool : NubType
|
||||
public override string ToString() => "bool";
|
||||
}
|
||||
|
||||
public sealed class NubTypeString : NubType
|
||||
public class NubTypeString : NubType
|
||||
{
|
||||
public static readonly NubTypeString Instance = new();
|
||||
|
||||
@@ -82,39 +102,122 @@ public sealed class NubTypeString : NubType
|
||||
public override string ToString() => "string";
|
||||
}
|
||||
|
||||
public sealed class NubTypeStruct : NubType
|
||||
public class NubTypeChar : NubType
|
||||
{
|
||||
public string Name { get; }
|
||||
public string Module { get; }
|
||||
public static readonly NubTypeChar Instance = new();
|
||||
|
||||
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;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public void ResolveFields(IReadOnlyList<Field> fields)
|
||||
{
|
||||
if (_resolvedFields != null)
|
||||
throw new InvalidOperationException($"{Name} already resolved");
|
||||
public string Module { get; }
|
||||
public string Name { get; }
|
||||
|
||||
_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 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();
|
||||
|
||||
@@ -136,7 +239,7 @@ public sealed class NubTypePointer : NubType
|
||||
public override string ToString() => $"^{To}";
|
||||
}
|
||||
|
||||
public sealed class NubTypeFunc : NubType
|
||||
public class NubTypeFunc : NubType
|
||||
{
|
||||
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}";
|
||||
|
||||
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
@@ -1,10 +1,75 @@
|
||||
using System.Diagnostics;
|
||||
using Compiler;
|
||||
|
||||
var moduleGraphBuilder = ModuleGraph.Create();
|
||||
var asts = new List<Ast>();
|
||||
var nubFiles = new List<string>();
|
||||
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);
|
||||
|
||||
@@ -13,7 +78,7 @@ foreach (var fileName in args)
|
||||
foreach (var diagnostic in tokenizerDiagnostics)
|
||||
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
||||
|
||||
if (tokenizerDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
||||
if (tokens == null)
|
||||
return 1;
|
||||
|
||||
var ast = Parser.Parse(fileName, tokens, out var parserDiagnostics);
|
||||
@@ -21,7 +86,7 @@ foreach (var fileName in args)
|
||||
foreach (var diagnostic in parserDiagnostics)
|
||||
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
||||
|
||||
if (parserDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
||||
if (ast == null)
|
||||
return 1;
|
||||
|
||||
moduleGraphBuilder.AddAst(ast);
|
||||
@@ -33,7 +98,7 @@ var moduleGraph = moduleGraphBuilder.Build(out var moduleGraphDiagnostics);
|
||||
foreach (var diagnostic in moduleGraphDiagnostics)
|
||||
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
||||
|
||||
if (moduleGraphDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
||||
if (moduleGraph == null)
|
||||
return 1;
|
||||
|
||||
var functions = new List<TypedNodeDefinitionFunc>();
|
||||
@@ -42,7 +107,7 @@ foreach (var ast in asts)
|
||||
{
|
||||
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)
|
||||
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);
|
||||
Directory.CreateDirectory(".build");
|
||||
string? entryPoint = null;
|
||||
|
||||
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;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,24 +3,33 @@ using System.Text;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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 line = 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>();
|
||||
diagnostics = [];
|
||||
try
|
||||
{
|
||||
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!TryPeek(out var c))
|
||||
break;
|
||||
@@ -31,13 +40,29 @@ public sealed class Tokenizer(string fileName, string contents)
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
@@ -57,6 +82,7 @@ public sealed class Tokenizer(string fileName, string contents)
|
||||
Consume();
|
||||
|
||||
var parsed = BigInteger.Zero;
|
||||
var seenDigit = false;
|
||||
|
||||
while (TryPeek(out c))
|
||||
{
|
||||
@@ -69,6 +95,7 @@ public sealed class Tokenizer(string fileName, string contents)
|
||||
if (!char.IsAsciiHexDigit(c))
|
||||
break;
|
||||
|
||||
seenDigit = true;
|
||||
parsed <<= 4;
|
||||
|
||||
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);
|
||||
}
|
||||
case '0' when Peek(1) is 'b':
|
||||
@@ -89,6 +119,7 @@ public sealed class Tokenizer(string fileName, string contents)
|
||||
Consume();
|
||||
|
||||
var parsed = BigInteger.Zero;
|
||||
var seenDigit = false;
|
||||
|
||||
while (TryPeek(out c))
|
||||
{
|
||||
@@ -101,11 +132,16 @@ public sealed class Tokenizer(string fileName, string contents)
|
||||
if (c is not '0' and not '1')
|
||||
break;
|
||||
|
||||
seenDigit = true;
|
||||
parsed <<= 1;
|
||||
|
||||
if (Consume() == '1')
|
||||
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);
|
||||
}
|
||||
default:
|
||||
@@ -137,16 +173,26 @@ public sealed class Tokenizer(string fileName, string contents)
|
||||
case '"':
|
||||
{
|
||||
Consume();
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
Consume();
|
||||
|
||||
return new TokenStringLiteral(line, startColumn, column - startColumn, buf.ToString());
|
||||
}
|
||||
|
||||
case '{':
|
||||
{
|
||||
Consume();
|
||||
@@ -157,6 +203,16 @@ public sealed class Tokenizer(string fileName, string contents)
|
||||
Consume();
|
||||
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 '(':
|
||||
{
|
||||
Consume();
|
||||
@@ -341,29 +397,40 @@ public sealed class Tokenizer(string fileName, string contents)
|
||||
{
|
||||
"func" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Func),
|
||||
"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),
|
||||
"if" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.If),
|
||||
"else" => new TokenKeyword(line, startColumn, column - startColumn, Keyword.Else),
|
||||
"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),
|
||||
"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),
|
||||
"false" => new TokenBoolLiteral(line, startColumn, column - startColumn, false),
|
||||
_ => 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)
|
||||
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')
|
||||
{
|
||||
@@ -377,6 +444,14 @@ public sealed class Tokenizer(string fileName, string contents)
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -403,29 +478,29 @@ public sealed class Tokenizer(string fileName, string contents)
|
||||
|
||||
public abstract class Token(int line, int column, int length)
|
||||
{
|
||||
public int Line = line;
|
||||
public int Column = column;
|
||||
public int Length = length;
|
||||
public int Line { get; } = line;
|
||||
public int Column { get; } = column;
|
||||
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
|
||||
@@ -434,6 +509,8 @@ public enum Symbol
|
||||
CloseCurly,
|
||||
OpenParen,
|
||||
CloseParen,
|
||||
OpenSquare,
|
||||
CloseSquare,
|
||||
Comma,
|
||||
Period,
|
||||
Colon,
|
||||
@@ -465,26 +542,34 @@ public enum Symbol
|
||||
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
|
||||
{
|
||||
Func,
|
||||
Struct,
|
||||
Packed,
|
||||
Enum,
|
||||
New,
|
||||
Match,
|
||||
Let,
|
||||
If,
|
||||
Else,
|
||||
While,
|
||||
For,
|
||||
In,
|
||||
Return,
|
||||
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
|
||||
@@ -497,15 +582,17 @@ public static class TokenExtensions
|
||||
Symbol.CloseCurly => "}",
|
||||
Symbol.OpenParen => "(",
|
||||
Symbol.CloseParen => ")",
|
||||
Symbol.OpenSquare => "[",
|
||||
Symbol.CloseSquare => "]",
|
||||
Symbol.Comma => ",",
|
||||
Symbol.Period => ",",
|
||||
Symbol.Period => ".",
|
||||
Symbol.Colon => ":",
|
||||
Symbol.ColonColon => "::",
|
||||
Symbol.Caret => "^",
|
||||
Symbol.Bang => "!",
|
||||
Symbol.Equal => "=",
|
||||
Symbol.EqualEqual => "==",
|
||||
Symbol.BangEqual => "!+",
|
||||
Symbol.BangEqual => "!=",
|
||||
Symbol.LessThan => "<",
|
||||
Symbol.LessThanLessThan => "<<",
|
||||
Symbol.LessThanEqual => "<=",
|
||||
@@ -536,12 +623,20 @@ public static class TokenExtensions
|
||||
{
|
||||
Keyword.Func => "func",
|
||||
Keyword.Struct => "struct",
|
||||
Keyword.Packed => "packed",
|
||||
Keyword.Enum => "enum",
|
||||
Keyword.New => "new",
|
||||
Keyword.Match => "enum",
|
||||
Keyword.Let => "let",
|
||||
Keyword.If => "if",
|
||||
Keyword.Else => "else",
|
||||
Keyword.While => "while",
|
||||
Keyword.For => "for",
|
||||
Keyword.In => "in",
|
||||
Keyword.Return => "return",
|
||||
Keyword.Module => "module",
|
||||
Keyword.Export => "export",
|
||||
Keyword.Extern => "extern",
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(symbol), symbol, null)
|
||||
};
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
module test
|
||||
|
||||
struct person {
|
||||
age: i32
|
||||
name: string
|
||||
}
|
||||
|
||||
func do_something(name: string): void {
|
||||
}
|
||||
1
examples/.gitignore
vendored
Normal file
1
examples/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.build
|
||||
21
examples/build
Executable file
21
examples/build
Executable 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
10
examples/core/print.nub
Normal 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
5
examples/core/sys.nub
Normal 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
32
examples/program/main.nub
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user