Compare commits

...

6 Commits

Author SHA1 Message Date
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
21 changed files with 1089 additions and 262 deletions

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 DiagnosticSeverity Severity { get; } = severity; private Diagnostic(DiagnosticSeverity severity, string message, string? help, FileInfo? file)
public string Message { get; } = message; {
public string? Help { get; } = help; Severity = severity;
public FileInfo? File { get; } = 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,24 +87,10 @@ 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 string File { get; } = file;
public int Line { get; } = line;
public int Column { get; } = column;
public int Length { get; } = length;
}
public enum DiagnosticSeverity
{
Info,
Warning,
Error,
}
public sealed class CompileException(Diagnostic diagnostic) : Exception
{ {
public Diagnostic Diagnostic { get; } = diagnostic; public Diagnostic Diagnostic { get; } = diagnostic;
} }
@@ -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),
}; };

View File

@@ -2,18 +2,31 @@
namespace Compiler; namespace Compiler;
public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGraph moduleGraph) public class Generator
{ {
public static string Emit(List<TypedNodeDefinitionFunc> functions, ModuleGraph moduleGraph) public static string Emit(List<TypedNodeDefinitionFunc> functions, ModuleGraph moduleGraph, bool compileLib)
{ {
return new Generator(functions, moduleGraph).Emit(); return new Generator(functions, moduleGraph, compileLib).Emit();
} }
private Generator(List<TypedNodeDefinitionFunc> functions, ModuleGraph moduleGraph, bool compileLib)
{
this.functions = functions;
this.moduleGraph = moduleGraph;
this.compileLib = compileLib;
}
private readonly List<TypedNodeDefinitionFunc> functions;
private readonly ModuleGraph moduleGraph;
private readonly bool compileLib;
private readonly IndentedTextWriter writer = new(); private readonly IndentedTextWriter writer = new();
private readonly Dictionary<NubTypeStruct, string> structTypeNames = new(); private readonly Dictionary<NubTypeStruct, string> structTypeNames = new();
private string Emit() private string Emit()
{ {
foreach (var (i, structType) in moduleGraph.GetModules().SelectMany(x => x.GetCustomTypes().OfType<NubTypeStruct>().Index()))
structTypeNames[structType] = $"s{i}";
writer.WriteLine(""" writer.WriteLine("""
#include <float.h> #include <float.h>
#include <stdarg.h> #include <stdarg.h>
@@ -21,15 +34,14 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
struct string { struct nub_core_string
{
const char *data; const char *data;
int length; int length;
}; };
"""); """);
foreach (var (i, structType) in moduleGraph.GetModules().SelectMany(x => x.GetCustomTypes().OfType<NubTypeStruct>().Index())) writer.WriteLine();
structTypeNames[structType] = $"s{i}";
foreach (var typeName in structTypeNames) foreach (var typeName in structTypeNames)
writer.WriteLine($"struct {typeName.Value};"); writer.WriteLine($"struct {typeName.Value};");
@@ -38,37 +50,68 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
foreach (var typeName in structTypeNames) foreach (var typeName in structTypeNames)
{ {
writer.WriteLine($"struct {typeName.Value}"); writer.Write("struct ");
if (typeName.Key.Packed)
{
writer.Write("__attribute__((__packed__)) ");
}
writer.WriteLine(typeName.Value);
writer.WriteLine("{"); writer.WriteLine("{");
using (writer.Indent()) using (writer.Indent())
{ {
foreach (var field in typeName.Key.Fields) foreach (var field in typeName.Key.Fields)
writer.WriteLine($"{CType(field.Type, field.Name)};"); writer.WriteLine($"{CType(field.Type, field.Name)};");
} }
writer.WriteLine("};"); writer.WriteLine("};");
} }
writer.WriteLine(); writer.WriteLine();
foreach (var node in functions) foreach (var module in moduleGraph.GetModules())
{ {
var parameters = node.Parameters.Select(x => CType(x.Type, x.Name.Ident)); foreach (var (name, type) in module.GetIdentifierTypes())
writer.WriteLine($"{CType(node.ReturnType, node.Name.Ident)}({string.Join(", ", parameters)});"); {
if (type is NubTypeFunc fn)
{
if (!functions.Any(x => x.GetMangledName() == SymbolNameGen.Exported(module.Name, name, type)))
writer.Write("extern ");
writer.WriteLine($"{CType(fn.ReturnType, SymbolNameGen.Exported(module.Name, name, type))}({string.Join(", ", fn.Parameters.Select(p => CType(p)))});");
}
else
{
writer.WriteLine($"{CType(type, SymbolNameGen.Exported(module.Name, name, type))};");
}
}
} }
writer.WriteLine(); writer.WriteLine();
foreach (var node in functions) if (!compileLib)
{ {
var parameters = node.Parameters.Select(x => CType(x.Type, x.Name.Ident)); var main = functions.First(x => x.Module == "main" && x.Name.Ident == "main");
writer.WriteLine($"{CType(node.ReturnType, node.Name.Ident)}({string.Join(", ", parameters)})");
writer.WriteLine($$"""
int main(int argc, char *argv[])
{
return {{main.GetMangledName()}}();
}
""");
}
writer.WriteLine();
foreach (var function in functions)
{
var parameters = function.Parameters.Select(x => CType(x.Type, x.Name.Ident));
writer.WriteLine($"{CType(function.ReturnType, function.GetMangledName())}({string.Join(", ", parameters)})");
writer.WriteLine("{"); writer.WriteLine("{");
using (writer.Indent()) using (writer.Indent())
{ {
EmitStatement(node.Body); EmitStatement(function.Body);
} }
writer.WriteLine("}"); writer.WriteLine("}");
writer.WriteLine(); writer.WriteLine();
} }
@@ -114,7 +157,6 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
foreach (var statement in node.Statements) foreach (var statement in node.Statements)
EmitStatement(statement); EmitStatement(statement);
} }
writer.WriteLine("}"); writer.WriteLine("}");
} }
@@ -153,7 +195,6 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
{ {
EmitStatement(statement.ThenBlock); EmitStatement(statement.ThenBlock);
} }
writer.WriteLine("}"); writer.WriteLine("}");
if (statement.ElseBlock != null) if (statement.ElseBlock != null)
@@ -169,7 +210,6 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
{ {
EmitStatement(statement.ElseBlock); EmitStatement(statement.ElseBlock);
} }
writer.WriteLine("}"); writer.WriteLine("}");
} }
} }
@@ -183,7 +223,6 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
{ {
EmitStatement(statement.Block); EmitStatement(statement.Block);
} }
writer.WriteLine("}"); writer.WriteLine("}");
} }
@@ -195,11 +234,11 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
TypedNodeExpressionUnary expression => EmitExpressionUnary(expression), TypedNodeExpressionUnary expression => EmitExpressionUnary(expression),
TypedNodeExpressionBoolLiteral expression => expression.Value.Value ? "true" : "false", TypedNodeExpressionBoolLiteral expression => expression.Value.Value ? "true" : "false",
TypedNodeExpressionIntLiteral expression => expression.Value.Value.ToString(), TypedNodeExpressionIntLiteral expression => expression.Value.Value.ToString(),
TypedNodeExpressionStringLiteral expression => $"(struct string){{ \"{expression.Value.Value}\", {expression.Value.Value.Length} }}", TypedNodeExpressionStringLiteral expression => $"(struct nub_core_string){{ \"{expression.Value.Value}\", {expression.Value.Value.Length} }}",
TypedNodeExpressionStructLiteral expression => EmitExpressionStructLiteral(expression), TypedNodeExpressionStructLiteral expression => EmitExpressionStructLiteral(expression),
TypedNodeExpressionMemberAccess expression => EmitExpressionMemberAccess(expression), TypedNodeExpressionMemberAccess expression => EmitExpressionMemberAccess(expression),
TypedNodeExpressionLocalIdent expression => expression.Value.Ident, TypedNodeExpressionLocalIdent expression => expression.Value.Ident,
TypedNodeExpressionModuleIdent expression => expression.Value.Ident, TypedNodeExpressionModuleIdent expression => SymbolNameGen.Exported(expression.Module.Ident, expression.Value.Ident, expression.Type),
TypedNodeExpressionFuncCall expression => EmitExpressionFuncCall(expression), TypedNodeExpressionFuncCall expression => EmitExpressionFuncCall(expression),
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null) _ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
}; };
@@ -281,7 +320,7 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
NubTypeSInt type => $"int{type.Width}_t" + (varName != null ? $" {varName}" : ""), NubTypeSInt type => $"int{type.Width}_t" + (varName != null ? $" {varName}" : ""),
NubTypeUInt type => $"uint{type.Width}_t" + (varName != null ? $" {varName}" : ""), NubTypeUInt type => $"uint{type.Width}_t" + (varName != null ? $" {varName}" : ""),
NubTypePointer type => CType(type.To) + (varName != null ? $" *{varName}" : "*"), NubTypePointer type => CType(type.To) + (varName != null ? $" *{varName}" : "*"),
NubTypeString => "struct string" + (varName != null ? $" {varName}" : ""), NubTypeString => "struct nub_core_string" + (varName != null ? $" {varName}" : ""),
NubTypeFunc type => $"{CType(type.ReturnType)} (*{varName})({string.Join(", ", type.Parameters.Select(p => CType(p)))})", NubTypeFunc type => $"{CType(type.ReturnType)} (*{varName})({string.Join(", ", type.Parameters.Select(p => CType(p)))})",
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null) _ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
}; };

View File

@@ -3,9 +3,16 @@ 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()
{ {
@@ -18,102 +25,196 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
return module != null; return module != null;
} }
public sealed class Module(string name) public Manifest CreateManifest()
{
return new Manifest(1, GetModules().Select(x => x.CreateManifestModule()).ToList());
}
public class Module(string name)
{ {
public string Name { get; } = name; public string Name { get; } = name;
private readonly Dictionary<string, NubType> customTypes = new(); private readonly Dictionary<string, CustomTypeInfo> customTypes = new();
private readonly Dictionary<string, NubType> identifierTypes = new(); private readonly Dictionary<string, IdentifierInfo> identifierTypes = new();
public List<NubType> GetCustomTypes() public IReadOnlyList<NubType> GetCustomTypes()
{ {
return customTypes.Values.ToList(); return customTypes.Values.Select(x => x.Type).ToList();
} }
public bool TryResolveCustomType(string name, [NotNullWhen(true)] out NubType? customType) public IReadOnlyDictionary<string, NubType> GetIdentifierTypes()
{ {
customType = customTypes.GetValueOrDefault(name); return identifierTypes.ToDictionary(x => x.Key, x => x.Value.Type);
return customType != null;
} }
public bool TryResolveIdentifierType(string name, [NotNullWhen(true)] out NubType? identifier) public bool TryResolveCustomType(string name, bool searchPrivate, [NotNullWhen(true)] out NubType? customType)
{ {
identifier = identifierTypes.GetValueOrDefault(name); var info = customTypes.GetValueOrDefault(name);
return identifier != null; if (info == null)
{
customType = null;
return false;
} }
public void AddCustomType(string name, NubType type) if (info.Exported || searchPrivate)
{ {
customTypes.Add(name, type); customType = info.Type;
return true;
} }
public void AddIdentifier(string name, NubType identifier) customType = null;
return false;
}
public bool TryResolveIdentifierType(string name, bool searchPrivate, [NotNullWhen(true)] out NubType? identifierType)
{ {
identifierTypes.Add(name, identifier); var info = identifierTypes.GetValueOrDefault(name);
if (info == null)
{
identifierType = null;
return false;
}
if (info.Exported || searchPrivate)
{
identifierType = info.Type;
return true;
}
identifierType = null;
return false;
}
public void AddCustomType(string name, CustomTypeInfo info)
{
customTypes.Add(name, info);
}
public void AddIdentifier(string name, IdentifierInfo info)
{
identifierTypes.Add(name, info);
}
public ManifestModule CreateManifestModule()
{
var manifestCustomTypes = customTypes.ToDictionary(x => x.Key, x => x.Value.CreateManifestCustomTypeInfo());
var manifestIdentifiers = identifierTypes.ToDictionary(x => x.Key, x => x.Value.CreateManifestIdentifierInfo());
return new ManifestModule(Name, manifestCustomTypes, manifestIdentifiers);
}
public enum Source
{
Ast,
Lib,
}
public class CustomTypeInfo(NubType type, bool exported, Source source)
{
public NubType Type { get; } = type;
public bool Exported { get; } = exported;
public Source Source { get; } = source;
public ManifestCustomTypeInfo CreateManifestCustomTypeInfo()
{
return new ManifestCustomTypeInfo(TypeEncoder.Encode(Type), Exported);
}
}
public class IdentifierInfo(NubType type, bool exported, Source source)
{
public NubType Type { get; } = type;
public bool Exported { get; } = exported;
public Source Source { get; } = source;
public ManifestIdentifierInfo CreateManifestIdentifierInfo()
{
return new ManifestIdentifierInfo(TypeEncoder.Encode(Type), Exported);
}
} }
} }
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 // First pass: Register libraries
foreach (var manifest in manifests)
{
foreach (var manifestModule in manifest.Modules)
{
if (!modules.TryGetValue(manifestModule.Name, out var module))
{
module = new Module(manifestModule.Name);
modules.Add(manifestModule.Name, module);
}
foreach (var customType in manifestModule.CustomTypes)
{
var decoded = TypeDecoder.Decode(customType.Value.EncodedType);
module.AddCustomType(customType.Key, new Module.CustomTypeInfo(decoded, customType.Value.Exported, Module.Source.Lib));
}
foreach (var identifier in manifestModule.Identifiers)
{
var decoded = TypeDecoder.Decode(identifier.Value.EncodedType);
module.AddIdentifier(identifier.Key, new Module.IdentifierInfo(decoded, identifier.Value.Exported, Module.Source.Lib));
}
}
}
var astModuleCache = new Dictionary<Ast, Module>();
// First pass: Register modules from ast
foreach (var ast in asts) foreach (var ast in asts)
{ {
var moduleDefinitions = ast.Definitions.OfType<NodeDefinitionModule>().ToList(); if (!modules.ContainsKey(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; var module = new Module(ast.ModuleName.Ident);
modules.Add(ast.ModuleName.Ident, module);
if (!modules.ContainsKey(currentModule))
{
var module = new Module(currentModule);
modules.Add(currentModule, module);
astModuleCache[ast] = module; astModuleCache[ast] = module;
} }
} }
}
// Second pass: Register struct types without fields // Second pass: Register struct types without fields
foreach (var ast in asts) foreach (var ast in asts)
{ {
var module = astModuleCache.GetValueOrDefault(ast); var module = astModuleCache[ast];
if (module == null) continue;
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>()) foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
module.AddCustomType(structDef.Name.Ident, new NubTypeStruct(module.Name, structDef.Name.Ident)); {
var type = new NubTypeStruct(module.Name, structDef.Name.Ident, structDef.Packed);
var info = new Module.CustomTypeInfo(type, structDef.Exported, Module.Source.Ast);
module.AddCustomType(structDef.Name.Ident, info);
}
} }
// Third pass: Resolve struct fields // Third pass: Resolve struct fields
foreach (var ast in asts) foreach (var ast in asts)
{ {
var module = astModuleCache.GetValueOrDefault(ast); var module = astModuleCache[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)) if (!module.TryResolveCustomType(structDef.Name.Ident, true, out var customType))
throw new UnreachableException($"{nameof(customType)} should always be registered"); 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(); var fields = structDef.Fields.Select(f => new NubTypeStruct.Field(f.Name.Ident, ResolveType(f.Type, module.Name))).ToList();
((NubTypeStruct)customType).ResolveFields(fields); ((NubTypeStruct)customType).ResolveFields(fields);
} }
} }
@@ -121,28 +222,38 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
// Fourth pass: Register identifiers // Fourth pass: Register identifiers
foreach (var ast in asts) foreach (var ast in asts)
{ {
var module = astModuleCache.GetValueOrDefault(ast); var module = astModuleCache[ast];
if (module == null) continue;
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 = 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(funcType, funcDef.Exported, Module.Source.Ast);
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(type, globalVariable.Exported, Module.Source.Ast);
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), NodeTypeCustom type => ResolveCustomType(type, currentModule),
NodeTypeFunc type => NubTypeFunc.Get(type.Parameters.Select(ResolveType).ToList(), ResolveType(type.ReturnType)), NodeTypeFunc type => NubTypeFunc.Get(type.Parameters.Select(x => ResolveType(x, currentModule)).ToList(), ResolveType(type.ReturnType, currentModule)),
NodeTypePointer type => NubTypePointer.Get(ResolveType(type.To)), 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,
@@ -151,13 +262,15 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
}; };
} }
NubType ResolveCustomType(NodeTypeCustom type) NubType ResolveCustomType(NodeTypeCustom type, string currentModule)
{ {
var module = modules.GetValueOrDefault(type.Module.Ident); var module = modules.GetValueOrDefault(type.Module.Ident);
if (module == null) if (module == null)
throw new CompileException(Diagnostic.Error($"Unknown module: {type.Module.Ident}").Build()); throw new CompileException(Diagnostic.Error($"Unknown module: {type.Module.Ident}").Build());
if (!module.TryResolveCustomType(type.Name.Ident, out var customType)) var includePrivate = currentModule == type.Module.Ident;
if (!module.TryResolveCustomType(type.Name.Ident, includePrivate, out var customType))
throw new CompileException(Diagnostic.Error($"Unknown custom type: {type.Module.Ident}::{type.Name.Ident}").Build()); throw new CompileException(Diagnostic.Error($"Unknown custom type: {type.Module.Ident}::{type.Name.Ident}").Build());
return customType; return customType;
@@ -165,3 +278,8 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
} }
} }
} }
public record Manifest(int Version, IReadOnlyList<ManifestModule> Modules);
public record ManifestModule(string Name, Dictionary<string, ManifestCustomTypeInfo> CustomTypes, Dictionary<string, ManifestIdentifierInfo> Identifiers);
public record ManifestCustomTypeInfo(string EncodedType, bool Exported);
public record ManifestIdentifierInfo(string EncodedType, bool Exported);

View File

@@ -1,3 +1,5 @@
using System.Text;
namespace Compiler; namespace Compiler;
public abstract class NubType public abstract class NubType
@@ -5,7 +7,7 @@ public abstract class NubType
public abstract override string ToString(); public abstract override string ToString();
} }
public sealed class NubTypeVoid : NubType public class NubTypeVoid : NubType
{ {
public static readonly NubTypeVoid Instance = new(); public static readonly NubTypeVoid Instance = new();
@@ -16,7 +18,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 +40,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 +62,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 +73,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,19 +84,21 @@ public sealed class NubTypeString : NubType
public override string ToString() => "string"; public override string ToString() => "string";
} }
public sealed class NubTypeStruct : NubType public class NubTypeStruct : NubType
{ {
public string Name { get; } public string Name { get; }
public string Module { get; } public string Module { get; }
public bool Packed { get; }
private IReadOnlyList<Field>? _resolvedFields; private IReadOnlyList<Field>? _resolvedFields;
public IReadOnlyList<Field> Fields => _resolvedFields ?? throw new InvalidOperationException(); public IReadOnlyList<Field> Fields => _resolvedFields ?? throw new InvalidOperationException();
public NubTypeStruct(string module, string name) public NubTypeStruct(string module, string name, bool packed)
{ {
Module = module; Module = module;
Name = name; Name = name;
Packed = packed;
} }
public void ResolveFields(IReadOnlyList<Field> fields) public void ResolveFields(IReadOnlyList<Field> fields)
@@ -107,14 +111,14 @@ public sealed class NubTypeStruct : NubType
public override string ToString() => _resolvedFields == null ? $"struct {Module}::{Name} <unresolved>" : $"struct {Module}::{Name} {{ {string.Join(' ', Fields.Select(f => $"{f.Name}: {f.Type}"))} }}"; public override string ToString() => _resolvedFields == null ? $"struct {Module}::{Name} <unresolved>" : $"struct {Module}::{Name} {{ {string.Join(' ', Fields.Select(f => $"{f.Name}: {f.Type}"))} }}";
public sealed class Field(string name, NubType 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;
} }
} }
public sealed class NubTypePointer : NubType public class NubTypePointer : NubType
{ {
private static readonly Dictionary<NubType, NubTypePointer> Cache = new(); private static readonly Dictionary<NubType, NubTypePointer> Cache = new();
@@ -136,7 +140,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();
@@ -153,6 +157,8 @@ public sealed class NubTypeFunc : NubType
public IReadOnlyList<NubType> Parameters { get; } public IReadOnlyList<NubType> Parameters { get; }
public NubType ReturnType { get; } public NubType ReturnType { get; }
public string MangledName(string name, string module) => SymbolNameGen.Exported(name, module, this);
private NubTypeFunc(List<NubType> parameters, NubType returnType) private NubTypeFunc(List<NubType> parameters, NubType returnType)
{ {
Parameters = parameters; Parameters = parameters;
@@ -163,3 +169,281 @@ public sealed class NubTypeFunc : NubType
private readonly record struct Signature(IReadOnlyList<NubType> Parameters, NubType ReturnType); private readonly record struct Signature(IReadOnlyList<NubType> Parameters, NubType ReturnType);
} }
static class TypeEncoder
{
public static string Encode(NubType type)
{
var sb = new StringBuilder();
Encode(type, sb);
return sb.ToString();
}
private static void Encode(NubType type, StringBuilder sb)
{
switch (type)
{
case NubTypeVoid:
sb.Append('V');
break;
case NubTypeBool:
sb.Append('B');
break;
case NubTypeUInt u:
sb.Append("U(").Append(u.Width).Append(')');
break;
case NubTypeSInt s:
sb.Append("I(").Append(s.Width).Append(')');
break;
case NubTypeString:
sb.Append('S');
break;
case NubTypePointer p:
sb.Append("P(");
Encode(p.To, sb);
sb.Append(')');
break;
case NubTypeStruct st:
sb.Append("T(");
sb.Append(st.Module).Append("::").Append(st.Name);
sb.Append(',').Append(st.Packed ? '1' : '0');
sb.Append('{');
for (int i = 0; i < st.Fields.Count; i++)
{
var field = st.Fields[i];
sb.Append(field.Name).Append(':');
Encode(field.Type, sb);
if (i < st.Fields.Count - 1)
sb.Append(',');
}
sb.Append("})");
break;
case NubTypeFunc fn:
sb.Append("F(");
foreach (var parameter in fn.Parameters)
{
sb.Append(Encode(parameter));
sb.Append(',');
}
sb.Append(Encode(fn.ReturnType));
sb.Append(')');
break;
default:
throw new NotSupportedException(type.GetType().Name);
}
}
}
class TypeDecoder
{
public static NubType Decode(string encoded)
{
return new TypeDecoder(encoded).Decode();
}
private TypeDecoder(string encoded)
{
this.encoded = encoded;
}
private readonly string encoded;
private int pos;
private NubType Decode()
{
return Parse();
}
private NubType Parse()
{
if (pos >= encoded.Length)
throw new InvalidOperationException("Unexpected end of string");
char c = encoded[pos++];
return c switch
{
'V' => NubTypeVoid.Instance,
'B' => NubTypeBool.Instance,
'S' => NubTypeString.Instance,
'U' => ParseUInt(),
'I' => ParseSInt(),
'P' => ParsePointer(),
'T' => ParseStruct(),
'F' => ParseFunc(),
_ => throw new NotSupportedException($"Unknown type code '{c}' at position {pos - 1}")
};
}
private NubTypeUInt ParseUInt()
{
ExpectChar('(');
int width = ReadNumber();
ExpectChar(')');
return NubTypeUInt.Get(width);
}
private NubTypeSInt ParseSInt()
{
ExpectChar('(');
int width = ReadNumber();
ExpectChar(')');
return NubTypeSInt.Get(width);
}
private NubTypePointer ParsePointer()
{
ExpectChar('(');
var to = Parse();
ExpectChar(')');
return NubTypePointer.Get(to);
}
private NubTypeStruct ParseStruct()
{
ExpectChar('(');
int start = pos;
while (pos < encoded.Length && encoded[pos] != ',' && encoded[pos] != '{') pos++;
var fullName = encoded[start..pos];
var parts = fullName.Split("::");
if (parts.Length != 2)
throw new InvalidOperationException($"Invalid struct name: {fullName}");
string module = parts[0], name = parts[1];
bool packed = false;
if (encoded[pos] == ',')
{
pos += 1;
packed = encoded[pos += 1] == '1';
}
var st = new NubTypeStruct(module, name, packed);
ExpectChar('{');
var fields = new List<NubTypeStruct.Field>();
while (encoded[pos] != '}')
{
int nameStart = pos;
while (encoded[pos] != ':') pos += 1;
string fieldName = encoded[nameStart..pos];
pos += 1;
var fieldType = Parse();
fields.Add(new NubTypeStruct.Field(fieldName, fieldType));
if (encoded[pos] == ',') pos += 1;
}
ExpectChar('}');
ExpectChar(')');
st.ResolveFields(fields);
return st;
}
private NubTypeFunc ParseFunc()
{
ExpectChar('(');
var parameters = new List<NubType>();
while (true)
{
if (encoded[pos] == ')')
{
pos++;
break;
}
var param = Parse();
parameters.Add(param);
if (encoded[pos] == ',')
{
pos += 1;
}
else if (encoded[pos] == ')')
{
pos += 1;
break;
}
else
{
throw new InvalidOperationException($"Unexpected char '{encoded[pos]}' in function type at {pos}");
}
}
if (parameters.Count == 0)
throw new InvalidOperationException("Function must have a return type");
var returnType = parameters[^1];
var paramTypes = parameters.Take(parameters.Count - 1).ToList();
return NubTypeFunc.Get(paramTypes, returnType);
}
private void ExpectChar(char expected)
{
if (pos >= encoded.Length || encoded[pos] != expected)
throw new InvalidOperationException($"Expected '{expected}' at position {pos}");
pos += 1;
}
private int ReadNumber()
{
int start = pos;
while (pos < encoded.Length && char.IsDigit(encoded[pos])) pos += 1;
if (start == pos) throw new InvalidOperationException($"Expected number at position {start}");
return int.Parse(encoded[start..pos]);
}
}
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;
}
}
static class SymbolNameGen
{
public static string Exported(string module, string function, NubType type)
{
var canonical = TypeEncoder.Encode(type);
var hash = Hashing.Fnv1a64(canonical);
return $"nub_{Sanitize(module)}_{Sanitize(function)}_{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();
}
}

View File

@@ -2,22 +2,35 @@
namespace Compiler; namespace Compiler;
public sealed class Parser(string fileName, List<Token> tokens) public class Parser
{ {
public static Ast Parse(string fileName, List<Token> tokens, out List<Diagnostic> diagnostics) public static Ast? Parse(string fileName, List<Token> tokens, out List<Diagnostic> diagnostics)
{ {
return new Parser(fileName, tokens).Parse(out diagnostics); return new Parser(fileName, tokens).Parse(out diagnostics);
} }
private Parser(string fileName, List<Token> tokens)
{
this.fileName = fileName;
this.tokens = tokens;
}
private readonly string fileName;
private readonly List<Token> tokens;
private int index; private int index;
private Ast Parse(out List<Diagnostic> diagnostics) private Ast? Parse(out List<Diagnostic> diagnostics)
{ {
var definitions = new List<NodeDefinition>(); var definitions = new List<NodeDefinition>();
diagnostics = []; diagnostics = [];
TokenIdent? moduleName = null;
try try
{ {
ExpectKeyword(Keyword.Module);
moduleName = ExpectIdent();
while (Peek() != null) while (Peek() != null)
{ {
definitions.Add(ParseDefinition()); definitions.Add(ParseDefinition());
@@ -28,15 +41,48 @@ public sealed class Parser(string fileName, List<Token> tokens)
diagnostics.Add(e.Diagnostic); diagnostics.Add(e.Diagnostic);
} }
return new Ast(definitions, fileName); if (moduleName == null || diagnostics.Any(x => x.Severity == Diagnostic.DiagnosticSeverity.Error))
return null;
return new Ast(fileName, moduleName, definitions);
} }
private NodeDefinition ParseDefinition() private NodeDefinition ParseDefinition()
{ {
var startIndex = index; var startIndex = index;
Dictionary<Keyword, TokenKeyword> modifiers = [];
while (true)
{
if (Peek() is TokenKeyword keyword)
{
switch (keyword.Keyword)
{
case Keyword.Export:
Next();
modifiers[Keyword.Export] = keyword;
break;
case Keyword.Packed:
Next();
modifiers[Keyword.Packed] = keyword;
break;
default:
goto modifier_done;
}
}
}
modifier_done:
if (TryExpectKeyword(Keyword.Func)) if (TryExpectKeyword(Keyword.Func))
{ {
var exported = modifiers.Remove(Keyword.Export);
foreach (var modifier in modifiers)
// todo(nub31): Add to diagnostics instead of throwing
throw new CompileException(Diagnostic.Error("Invalid modifier for function").At(fileName, modifier.Value).Build());
var name = ExpectIdent(); var name = ExpectIdent();
var parameters = new List<NodeDefinitionFunc.Param>(); var parameters = new List<NodeDefinitionFunc.Param>();
@@ -55,11 +101,18 @@ public sealed class Parser(string fileName, List<Token> tokens)
var body = ParseStatement(); var body = ParseStatement();
return new NodeDefinitionFunc(TokensFrom(startIndex), name, parameters, body, returnType); return new NodeDefinitionFunc(TokensFrom(startIndex), exported, name, parameters, body, returnType);
} }
if (TryExpectKeyword(Keyword.Struct)) if (TryExpectKeyword(Keyword.Struct))
{ {
var exported = modifiers.Remove(Keyword.Export);
var packed = modifiers.Remove(Keyword.Packed);
foreach (var modifier in modifiers)
// todo(nub31): Add to diagnostics instead of throwing
throw new CompileException(Diagnostic.Error("Invalid modifier for struct").At(fileName, modifier.Value).Build());
var name = ExpectIdent(); var name = ExpectIdent();
var fields = new List<NodeDefinitionStruct.Field>(); var fields = new List<NodeDefinitionStruct.Field>();
@@ -73,13 +126,22 @@ public sealed class Parser(string fileName, List<Token> tokens)
fields.Add(new NodeDefinitionStruct.Field(TokensFrom(fieldStartIndex), fieldName, fieldType)); fields.Add(new NodeDefinitionStruct.Field(TokensFrom(fieldStartIndex), fieldName, fieldType));
} }
return new NodeDefinitionStruct(TokensFrom(startIndex), name, fields); return new NodeDefinitionStruct(TokensFrom(startIndex), exported, packed, name, fields);
} }
if (TryExpectKeyword(Keyword.Module)) if (TryExpectKeyword(Keyword.Let))
{ {
var exported = modifiers.Remove(Keyword.Export);
foreach (var modifier in modifiers)
// todo(nub31): Add to diagnostics instead of throwing
throw new CompileException(Diagnostic.Error("Invalid modifier for global variable").At(fileName, modifier.Value).Build());
var name = ExpectIdent(); var name = ExpectIdent();
return new NodeDefinitionModule(TokensFrom(startIndex), name); ExpectSymbol(Symbol.Colon);
var type = ParseType();
return new NodeDefinitionGlobalVariable(TokensFrom(startIndex), exported, name, type);
} }
throw new CompileException(Diagnostic.Error("Not a valid definition").At(fileName, Peek()).Build()); throw new CompileException(Diagnostic.Error("Not a valid definition").At(fileName, Peek()).Build());
@@ -355,6 +417,17 @@ public sealed class Parser(string fileName, List<Token> tokens)
return tokens.GetRange(startIndex, index - startIndex); return tokens.GetRange(startIndex, index - startIndex);
} }
private void ExpectKeyword(Keyword keyword)
{
if (Peek() is TokenKeyword token && token.Keyword == keyword)
{
Next();
return;
}
throw new CompileException(Diagnostic.Error($"Expected '{keyword.AsString()}'").At(fileName, Peek()).Build());
}
private bool TryExpectKeyword(Keyword keyword) private bool TryExpectKeyword(Keyword keyword)
{ {
if (Peek() is TokenKeyword token && token.Keyword == keyword) if (Peek() is TokenKeyword token && token.Keyword == keyword)
@@ -529,9 +602,10 @@ public sealed class Parser(string fileName, List<Token> tokens)
} }
} }
public sealed class Ast(List<NodeDefinition> definitions, string fileName) public class Ast(string fileName, TokenIdent moduleName, List<NodeDefinition> definitions)
{ {
public string FileName { get; } = fileName; public string FileName { get; } = fileName;
public TokenIdent ModuleName { get; } = moduleName;
public List<NodeDefinition> Definitions { get; } = definitions; public List<NodeDefinition> Definitions { get; } = definitions;
} }
@@ -542,75 +616,80 @@ public abstract class Node(List<Token> tokens)
public abstract class NodeDefinition(List<Token> tokens) : Node(tokens); public abstract class NodeDefinition(List<Token> tokens) : Node(tokens);
public sealed class NodeDefinitionModule(List<Token> tokens, TokenIdent name) : NodeDefinition(tokens) public class NodeDefinitionFunc(List<Token> tokens, bool exported, TokenIdent name, List<NodeDefinitionFunc.Param> parameters, NodeStatement body, NodeType returnType) : NodeDefinition(tokens)
{
public TokenIdent Name { get; } = name;
}
public sealed class NodeDefinitionFunc(List<Token> tokens, TokenIdent name, List<NodeDefinitionFunc.Param> parameters, NodeStatement body, NodeType returnType) : NodeDefinition(tokens)
{ {
public bool Exported { get; } = exported;
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
public List<Param> Parameters { get; } = parameters; public List<Param> Parameters { get; } = parameters;
public NodeStatement Body { get; } = body; public NodeStatement Body { get; } = body;
public NodeType ReturnType { get; } = returnType; public NodeType ReturnType { get; } = returnType;
public sealed class Param(List<Token> tokens, TokenIdent name, NodeType type) : Node(tokens) public class Param(List<Token> tokens, TokenIdent name, NodeType type) : Node(tokens)
{ {
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
public NodeType Type { get; } = type; public NodeType Type { get; } = type;
} }
} }
public sealed class NodeDefinitionStruct(List<Token> tokens, TokenIdent name, List<NodeDefinitionStruct.Field> fields) : NodeDefinition(tokens) public class NodeDefinitionStruct(List<Token> tokens, bool exported, bool packed, TokenIdent name, List<NodeDefinitionStruct.Field> fields) : NodeDefinition(tokens)
{ {
public bool Exported { get; } = exported;
public bool Packed { get; } = packed;
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
public List<Field> Fields { get; } = fields; public List<Field> Fields { get; } = fields;
public sealed class Field(List<Token> tokens, TokenIdent name, NodeType type) : Node(tokens) public class Field(List<Token> tokens, TokenIdent name, NodeType type) : Node(tokens)
{ {
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
public NodeType Type { get; } = type; public NodeType Type { get; } = type;
} }
} }
public class NodeDefinitionGlobalVariable(List<Token> tokens, bool exported, TokenIdent name, NodeType type) : NodeDefinition(tokens)
{
public bool Exported { get; } = exported;
public TokenIdent Name { get; } = name;
public NodeType Type { get; } = type;
}
public abstract class NodeStatement(List<Token> tokens) : Node(tokens); public abstract class NodeStatement(List<Token> tokens) : Node(tokens);
public sealed class NodeStatementBlock(List<Token> tokens, List<NodeStatement> statements) : NodeStatement(tokens) public class NodeStatementBlock(List<Token> tokens, List<NodeStatement> statements) : NodeStatement(tokens)
{ {
public List<NodeStatement> Statements { get; } = statements; public List<NodeStatement> Statements { get; } = statements;
} }
public sealed class NodeStatementExpression(List<Token> tokens, NodeExpression expression) : NodeStatement(tokens) public class NodeStatementExpression(List<Token> tokens, NodeExpression expression) : NodeStatement(tokens)
{ {
public NodeExpression Expression { get; } = expression; public NodeExpression Expression { get; } = expression;
} }
public sealed class NodeStatementReturn(List<Token> tokens, NodeExpression value) : NodeStatement(tokens) public class NodeStatementReturn(List<Token> tokens, NodeExpression value) : NodeStatement(tokens)
{ {
public NodeExpression Value { get; } = value; public NodeExpression Value { get; } = value;
} }
public sealed class NodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NodeType type, NodeExpression value) : NodeStatement(tokens) public class NodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NodeType type, NodeExpression value) : NodeStatement(tokens)
{ {
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
public NodeType Type { get; } = type; public NodeType Type { get; } = type;
public NodeExpression Value { get; } = value; public NodeExpression Value { get; } = value;
} }
public sealed class NodeStatementAssignment(List<Token> tokens, NodeExpression target, NodeExpression value) : NodeStatement(tokens) public class NodeStatementAssignment(List<Token> tokens, NodeExpression target, NodeExpression value) : NodeStatement(tokens)
{ {
public NodeExpression Target { get; } = target; public NodeExpression Target { get; } = target;
public NodeExpression Value { get; } = value; public NodeExpression Value { get; } = value;
} }
public sealed class NodeStatementIf(List<Token> tokens, NodeExpression condition, NodeStatement thenBlock, NodeStatement? elseBlock) : NodeStatement(tokens) public class NodeStatementIf(List<Token> tokens, NodeExpression condition, NodeStatement thenBlock, NodeStatement? elseBlock) : NodeStatement(tokens)
{ {
public NodeExpression Condition { get; } = condition; public NodeExpression Condition { get; } = condition;
public NodeStatement ThenBlock { get; } = thenBlock; public NodeStatement ThenBlock { get; } = thenBlock;
public NodeStatement? ElseBlock { get; } = elseBlock; public NodeStatement? ElseBlock { get; } = elseBlock;
} }
public sealed class NodeStatementWhile(List<Token> tokens, NodeExpression condition, NodeStatement block) : NodeStatement(tokens) public class NodeStatementWhile(List<Token> tokens, NodeExpression condition, NodeStatement block) : NodeStatement(tokens)
{ {
public NodeExpression Condition { get; } = condition; public NodeExpression Condition { get; } = condition;
public NodeStatement Block { get; } = block; public NodeStatement Block { get; } = block;
@@ -618,58 +697,58 @@ public sealed class NodeStatementWhile(List<Token> tokens, NodeExpression condit
public abstract class NodeExpression(List<Token> tokens) : Node(tokens); public abstract class NodeExpression(List<Token> tokens) : Node(tokens);
public sealed class NodeExpressionIntLiteral(List<Token> tokens, TokenIntLiteral value) : NodeExpression(tokens) public class NodeExpressionIntLiteral(List<Token> tokens, TokenIntLiteral value) : NodeExpression(tokens)
{ {
public TokenIntLiteral Value { get; } = value; public TokenIntLiteral Value { get; } = value;
} }
public sealed class NodeExpressionStringLiteral(List<Token> tokens, TokenStringLiteral value) : NodeExpression(tokens) public class NodeExpressionStringLiteral(List<Token> tokens, TokenStringLiteral value) : NodeExpression(tokens)
{ {
public TokenStringLiteral Value { get; } = value; public TokenStringLiteral Value { get; } = value;
} }
public sealed class NodeExpressionBoolLiteral(List<Token> tokens, TokenBoolLiteral value) : NodeExpression(tokens) public class NodeExpressionBoolLiteral(List<Token> tokens, TokenBoolLiteral value) : NodeExpression(tokens)
{ {
public TokenBoolLiteral Value { get; } = value; public TokenBoolLiteral Value { get; } = value;
} }
public sealed class NodeExpressionStructLiteral(List<Token> tokens, TokenIdent module, TokenIdent name, List<NodeExpressionStructLiteral.Initializer> initializers) : NodeExpression(tokens) public class NodeExpressionStructLiteral(List<Token> tokens, TokenIdent module, TokenIdent name, List<NodeExpressionStructLiteral.Initializer> initializers) : NodeExpression(tokens)
{ {
public TokenIdent Module { get; } = module; public TokenIdent Module { get; } = module;
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
public List<Initializer> Initializers { get; } = initializers; public List<Initializer> Initializers { get; } = initializers;
public sealed class Initializer(List<Token> tokens, TokenIdent name, NodeExpression value) : Node(tokens) public class Initializer(List<Token> tokens, TokenIdent name, NodeExpression value) : Node(tokens)
{ {
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
public NodeExpression Value { get; } = value; public NodeExpression Value { get; } = value;
} }
} }
public sealed class NodeExpressionMemberAccess(List<Token> tokens, NodeExpression target, TokenIdent name) : NodeExpression(tokens) public class NodeExpressionMemberAccess(List<Token> tokens, NodeExpression target, TokenIdent name) : NodeExpression(tokens)
{ {
public NodeExpression Target { get; } = target; public NodeExpression Target { get; } = target;
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
} }
public sealed class NodeExpressionFuncCall(List<Token> tokens, NodeExpression target, List<NodeExpression> parameters) : NodeExpression(tokens) public class NodeExpressionFuncCall(List<Token> tokens, NodeExpression target, List<NodeExpression> parameters) : NodeExpression(tokens)
{ {
public NodeExpression Target { get; } = target; public NodeExpression Target { get; } = target;
public List<NodeExpression> Parameters { get; } = parameters; public List<NodeExpression> Parameters { get; } = parameters;
} }
public sealed class NodeExpressionLocalIdent(List<Token> tokens, TokenIdent value) : NodeExpression(tokens) public class NodeExpressionLocalIdent(List<Token> tokens, TokenIdent value) : NodeExpression(tokens)
{ {
public TokenIdent Value { get; } = value; public TokenIdent Value { get; } = value;
} }
public sealed class NodeExpressionModuleIdent(List<Token> tokens, TokenIdent module, TokenIdent value) : NodeExpression(tokens) public class NodeExpressionModuleIdent(List<Token> tokens, TokenIdent module, TokenIdent value) : NodeExpression(tokens)
{ {
public TokenIdent Module { get; } = module; public TokenIdent Module { get; } = module;
public TokenIdent Value { get; } = value; public TokenIdent Value { get; } = value;
} }
public sealed class NodeExpressionBinary(List<Token> tokens, NodeExpression left, NodeExpressionBinary.Op operation, NodeExpression right) : NodeExpression(tokens) public class NodeExpressionBinary(List<Token> tokens, NodeExpression left, NodeExpressionBinary.Op operation, NodeExpression right) : NodeExpression(tokens)
{ {
public NodeExpression Left { get; } = left; public NodeExpression Left { get; } = left;
public Op Operation { get; } = operation; public Op Operation { get; } = operation;
@@ -702,7 +781,7 @@ public sealed class NodeExpressionBinary(List<Token> tokens, NodeExpression left
} }
} }
public sealed class NodeExpressionUnary(List<Token> tokens, NodeExpression target, NodeExpressionUnary.Op op) : NodeExpression(tokens) public class NodeExpressionUnary(List<Token> tokens, NodeExpression target, NodeExpressionUnary.Op op) : NodeExpression(tokens)
{ {
public NodeExpression Target { get; } = target; public NodeExpression Target { get; } = target;
public Op Operation { get; } = op; public Op Operation { get; } = op;
@@ -716,34 +795,34 @@ public sealed class NodeExpressionUnary(List<Token> tokens, NodeExpression targe
public abstract class NodeType(List<Token> tokens) : Node(tokens); public abstract class NodeType(List<Token> tokens) : Node(tokens);
public sealed class NodeTypeVoid(List<Token> tokens) : NodeType(tokens); public class NodeTypeVoid(List<Token> tokens) : NodeType(tokens);
public sealed class NodeTypeUInt(List<Token> tokens, int width) : NodeType(tokens) public class NodeTypeUInt(List<Token> tokens, int width) : NodeType(tokens)
{ {
public int Width { get; } = width; public int Width { get; } = width;
} }
public sealed class NodeTypeSInt(List<Token> tokens, int width) : NodeType(tokens) public class NodeTypeSInt(List<Token> tokens, int width) : NodeType(tokens)
{ {
public int Width { get; } = width; public int Width { get; } = width;
} }
public sealed class NodeTypeBool(List<Token> tokens) : NodeType(tokens); public class NodeTypeBool(List<Token> tokens) : NodeType(tokens);
public sealed class NodeTypeString(List<Token> tokens) : NodeType(tokens); public class NodeTypeString(List<Token> tokens) : NodeType(tokens);
public sealed class NodeTypeCustom(List<Token> tokens, TokenIdent module, TokenIdent name) : NodeType(tokens) public class NodeTypeCustom(List<Token> tokens, TokenIdent module, TokenIdent name) : NodeType(tokens)
{ {
public TokenIdent Module { get; } = module; public TokenIdent Module { get; } = module;
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
} }
public sealed class NodeTypePointer(List<Token> tokens, NodeType to) : NodeType(tokens) public class NodeTypePointer(List<Token> tokens, NodeType to) : NodeType(tokens)
{ {
public NodeType To { get; } = to; public NodeType To { get; } = to;
} }
public sealed class NodeTypeFunc(List<Token> tokens, List<NodeType> parameters, NodeType returnType) : NodeType(tokens) public class NodeTypeFunc(List<Token> tokens, List<NodeType> parameters, NodeType returnType) : NodeType(tokens)
{ {
public List<NodeType> Parameters { get; } = parameters; public List<NodeType> Parameters { get; } = parameters;
public NodeType ReturnType { get; } = returnType; public NodeType ReturnType { get; } = returnType;

View File

@@ -1,10 +1,77 @@
using System.Diagnostics; using System.Diagnostics;
using System.IO.Compression;
using System.Text.Json;
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 = ReadNublib(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 +80,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 +88,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 +100,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 +109,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 +121,99 @@ foreach (var ast in asts)
} }
} }
var output = Generator.Emit(functions, moduleGraph); var output = Generator.Emit(functions, moduleGraph, compileLib);
Directory.Delete(".build", recursive: true); if (Directory.Exists(".build"))
Directory.CreateDirectory(".build"); {
CleanDirectory(".build");
}
else
{
Directory.CreateDirectory(".build");
}
File.WriteAllText(".build/out.c", output); if (compileLib)
{
Process.Start("gcc", ["-Og", "-g", "-o", ".build/out", ".build/out.c"]); File.WriteAllText(".build/out.c", output);
Process.Start("gcc", ["-Og", "-fvisibility=hidden", "-fno-builtin", "-c", "-o", ".build/out.o", ".build/out.c", .. archivePaths]).WaitForExit();
Process.Start("ar", ["rcs", ".build/out.a", ".build/out.o"]).WaitForExit();
WriteNublib(".build/out.nublib", ".build/out.a", moduleGraph.CreateManifest());
}
else
{
File.WriteAllText(".build/out.c", output);
Process.Start("gcc", ["-Og", "-fvisibility=hidden", "-fno-builtin", "-o", ".build/out", ".build/out.c", .. 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();
}
}
static void WriteNublib(string outputPath, string archivePath, Manifest manifest)
{
using var fs = new FileStream(outputPath, FileMode.Create);
using var zip = new ZipArchive(fs, ZipArchiveMode.Create);
var serialized = JsonSerializer.Serialize(manifest, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
});
File.WriteAllText(".build/manifest.json", serialized);
var manifestEntry = zip.CreateEntry("manifest.json");
using (var writer = new StreamWriter(manifestEntry.Open()))
{
writer.Write(JsonSerializer.Serialize(manifest));
}
var archiveEntry = zip.CreateEntry("lib.a");
using var entryStream = archiveEntry.Open();
using var fileStream = File.OpenRead(archivePath);
fileStream.CopyTo(entryStream);
}
static NublibLoadResult ReadNublib(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) ?? 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);

View File

@@ -3,18 +3,26 @@ 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 = [];
@@ -53,6 +61,9 @@ public sealed class Tokenizer(string fileName, string contents)
} }
} }
if (diagnostics.Any(x => x.Severity == Diagnostic.DiagnosticSeverity.Error))
return null;
return tokens; return tokens;
} }
@@ -376,12 +387,14 @@ 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),
"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),
"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),
"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)
@@ -454,22 +467,22 @@ public abstract class Token(int line, int column, int length)
public int Length { get; } = 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 string Ident { get; } = 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 { get; } = 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 string Value { get; } = 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 bool Value { get; } = value; public bool Value { get; } = value;
} }
@@ -511,7 +524,7 @@ 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 Symbol Symbol { get; } = symbol; public Symbol Symbol { get; } = symbol;
} }
@@ -520,15 +533,17 @@ public enum Keyword
{ {
Func, Func,
Struct, Struct,
Packed,
Let, Let,
If, If,
Else, Else,
While, While,
Return, Return,
Module, Module,
Export,
} }
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 Keyword Keyword { get; } = keyword; public Keyword Keyword { get; } = keyword;
} }
@@ -582,12 +597,14 @@ public static class TokenExtensions
{ {
Keyword.Func => "func", Keyword.Func => "func",
Keyword.Struct => "struct", Keyword.Struct => "struct",
Keyword.Packed => "packed",
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.Return => "return", Keyword.Return => "return",
Keyword.Module => "module", Keyword.Module => "module",
Keyword.Export => "export",
_ => throw new ArgumentOutOfRangeException(nameof(symbol), symbol, null) _ => throw new ArgumentOutOfRangeException(nameof(symbol), symbol, null)
}; };
} }

View File

@@ -1,12 +1,24 @@
namespace Compiler; namespace Compiler;
public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, ModuleGraph moduleGraph) public class TypeChecker
{ {
public static TypedNodeDefinitionFunc? CheckFunction(string fileName, NodeDefinitionFunc function, ModuleGraph moduleGraph, out List<Diagnostic> diagnostics) public static TypedNodeDefinitionFunc? CheckFunction(string fileName, string moduleName, NodeDefinitionFunc function, ModuleGraph moduleGraph, out List<Diagnostic> diagnostics)
{ {
return new TypeChecker(fileName, function, moduleGraph).CheckFunction(out diagnostics); return new TypeChecker(fileName, moduleName, function, moduleGraph).CheckFunction(out diagnostics);
} }
private TypeChecker(string fileName, string moduleName, NodeDefinitionFunc function, ModuleGraph moduleGraph)
{
this.fileName = fileName;
this.moduleName = moduleName;
this.function = function;
this.moduleGraph = moduleGraph;
}
private readonly string fileName;
private readonly string moduleName;
private readonly NodeDefinitionFunc function;
private readonly ModuleGraph moduleGraph;
private readonly Scope scope = new(null); private readonly Scope scope = new(null);
private TypedNodeDefinitionFunc? CheckFunction(out List<Diagnostic> diagnostics) private TypedNodeDefinitionFunc? CheckFunction(out List<Diagnostic> diagnostics)
@@ -20,6 +32,8 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
foreach (var parameter in function.Parameters) foreach (var parameter in function.Parameters)
{ {
scope.DeclareIdentifier(parameter.Name.Ident, ResolveType(parameter.Type));
try try
{ {
parameters.Add(CheckDefinitionFuncParameter(parameter)); parameters.Add(CheckDefinitionFuncParameter(parameter));
@@ -52,7 +66,7 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
if (body == null || returnType == null || invalidParameter) if (body == null || returnType == null || invalidParameter)
return null; return null;
return new TypedNodeDefinitionFunc(function.Tokens, function.Name, parameters, body, returnType); return new TypedNodeDefinitionFunc(function.Tokens, moduleName, function.Name, parameters, body, returnType);
} }
private TypedNodeDefinitionFunc.Param CheckDefinitionFuncParameter(NodeDefinitionFunc.Param node) private TypedNodeDefinitionFunc.Param CheckDefinitionFuncParameter(NodeDefinitionFunc.Param node)
@@ -291,7 +305,9 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
if (!moduleGraph.TryResolveModule(expression.Module.Ident, out var module)) if (!moduleGraph.TryResolveModule(expression.Module.Ident, out var module))
throw new CompileException(Diagnostic.Error($"Module '{expression.Module.Ident}' not found").At(fileName, expression.Module).Build()); throw new CompileException(Diagnostic.Error($"Module '{expression.Module.Ident}' not found").At(fileName, expression.Module).Build());
if (!module.TryResolveIdentifierType(expression.Value.Ident, out var identifierType)) var includePrivate = expression.Module.Ident == moduleName;
if (!module.TryResolveIdentifierType(expression.Value.Ident, includePrivate, out var identifierType))
throw new CompileException(Diagnostic.Error($"Identifier '{expression.Module.Ident}::{expression.Value.Ident}' not found").At(fileName, expression.Value).Build()); throw new CompileException(Diagnostic.Error($"Identifier '{expression.Module.Ident}::{expression.Value.Ident}' not found").At(fileName, expression.Value).Build());
return new TypedNodeExpressionModuleIdent(expression.Tokens, identifierType, expression.Module, expression.Value); return new TypedNodeExpressionModuleIdent(expression.Tokens, identifierType, expression.Module, expression.Value);
@@ -336,7 +352,9 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
if (!moduleGraph.TryResolveModule(expression.Module.Ident, out var module)) if (!moduleGraph.TryResolveModule(expression.Module.Ident, out var module))
throw new CompileException(Diagnostic.Error($"Module '{expression.Module.Ident}' not found").At(fileName, expression.Module).Build()); throw new CompileException(Diagnostic.Error($"Module '{expression.Module.Ident}' not found").At(fileName, expression.Module).Build());
if (!module.TryResolveCustomType(expression.Name.Ident, out var customType)) var includePrivate = expression.Module.Ident == moduleName;
if (!module.TryResolveCustomType(expression.Name.Ident, includePrivate, out var customType))
throw new CompileException(Diagnostic.Error($"Struct '{expression.Module.Ident}::{expression.Name.Ident}' not found").At(fileName, expression.Name).Build()); throw new CompileException(Diagnostic.Error($"Struct '{expression.Module.Ident}::{expression.Name.Ident}' not found").At(fileName, expression.Name).Build());
if (customType is not NubTypeStruct structType) if (customType is not NubTypeStruct structType)
@@ -380,7 +398,9 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
if (!moduleGraph.TryResolveModule(type.Module.Ident, out var module)) if (!moduleGraph.TryResolveModule(type.Module.Ident, out var module))
throw new CompileException(Diagnostic.Error($"Module '{type.Module.Ident}' not found").At(fileName, type.Module).Build()); throw new CompileException(Diagnostic.Error($"Module '{type.Module.Ident}' not found").At(fileName, type.Module).Build());
if (!module.TryResolveCustomType(type.Name.Ident, out var customType)) var includePrivate = type.Module.Ident == moduleName;
if (!module.TryResolveCustomType(type.Name.Ident, includePrivate, out var customType))
throw new CompileException(Diagnostic.Error($"Custom type '{type.Module.Ident}::{type.Name.Ident}' not found").At(fileName, type.Name).Build()); throw new CompileException(Diagnostic.Error($"Custom type '{type.Module.Ident}::{type.Name.Ident}' not found").At(fileName, type.Name).Build());
return customType; return customType;
@@ -409,16 +429,29 @@ public abstract class TypedNode(List<Token> tokens)
public List<Token> Tokens { get; } = tokens; public List<Token> Tokens { get; } = tokens;
} }
public abstract class TypedNodeDefinition(List<Token> tokens) : TypedNode(tokens); public abstract class TypedNodeDefinition(List<Token> tokens, string module) : TypedNode(tokens)
{
public string Module { get; } = module;
}
public sealed class TypedNodeDefinitionFunc(List<Token> tokens, TokenIdent name, List<TypedNodeDefinitionFunc.Param> parameters, TypedNodeStatement body, NubType returnType) : TypedNodeDefinition(tokens) public class TypedNodeDefinitionFunc(List<Token> tokens, string module, TokenIdent name, List<TypedNodeDefinitionFunc.Param> parameters, TypedNodeStatement body, NubType returnType) : TypedNodeDefinition(tokens, module)
{ {
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
public List<Param> Parameters { get; } = parameters; public List<Param> Parameters { get; } = parameters;
public TypedNodeStatement Body { get; } = body; public TypedNodeStatement Body { get; } = body;
public NubType ReturnType { get; } = returnType; public NubType ReturnType { get; } = returnType;
public sealed class Param(List<Token> tokens, TokenIdent name, NubType type) : TypedNode(tokens) public NubTypeFunc GetNubType()
{
return NubTypeFunc.Get(Parameters.Select(x => x.Type).ToList(), ReturnType);
}
public string GetMangledName()
{
return SymbolNameGen.Exported(Module, Name.Ident, GetNubType());
}
public class Param(List<Token> tokens, TokenIdent name, NubType type) : TypedNode(tokens)
{ {
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
public NubType Type { get; } = type; public NubType Type { get; } = type;
@@ -427,43 +460,43 @@ public sealed class TypedNodeDefinitionFunc(List<Token> tokens, TokenIdent name,
public abstract class TypedNodeStatement(List<Token> tokens) : TypedNode(tokens); public abstract class TypedNodeStatement(List<Token> tokens) : TypedNode(tokens);
public sealed class TypedNodeStatementBlock(List<Token> tokens, List<TypedNodeStatement> statements) : TypedNodeStatement(tokens) public class TypedNodeStatementBlock(List<Token> tokens, List<TypedNodeStatement> statements) : TypedNodeStatement(tokens)
{ {
public List<TypedNodeStatement> Statements { get; } = statements; public List<TypedNodeStatement> Statements { get; } = statements;
} }
public sealed class TypedNodeStatementFuncCall(List<Token> tokens, TypedNodeExpression target, List<TypedNodeExpression> parameters) : TypedNodeStatement(tokens) public class TypedNodeStatementFuncCall(List<Token> tokens, TypedNodeExpression target, List<TypedNodeExpression> parameters) : TypedNodeStatement(tokens)
{ {
public TypedNodeExpression Target { get; } = target; public TypedNodeExpression Target { get; } = target;
public List<TypedNodeExpression> Parameters { get; } = parameters; public List<TypedNodeExpression> Parameters { get; } = parameters;
} }
public sealed class TypedNodeStatementReturn(List<Token> tokens, TypedNodeExpression value) : TypedNodeStatement(tokens) public class TypedNodeStatementReturn(List<Token> tokens, TypedNodeExpression value) : TypedNodeStatement(tokens)
{ {
public TypedNodeExpression Value { get; } = value; public TypedNodeExpression Value { get; } = value;
} }
public sealed class TypedNodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NubType type, TypedNodeExpression value) : TypedNodeStatement(tokens) public class TypedNodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NubType type, TypedNodeExpression value) : TypedNodeStatement(tokens)
{ {
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
public NubType Type { get; } = type; public NubType Type { get; } = type;
public TypedNodeExpression Value { get; } = value; public TypedNodeExpression Value { get; } = value;
} }
public sealed class TypedNodeStatementAssignment(List<Token> tokens, TypedNodeExpression target, TypedNodeExpression value) : TypedNodeStatement(tokens) public class TypedNodeStatementAssignment(List<Token> tokens, TypedNodeExpression target, TypedNodeExpression value) : TypedNodeStatement(tokens)
{ {
public TypedNodeExpression Target { get; } = target; public TypedNodeExpression Target { get; } = target;
public TypedNodeExpression Value { get; } = value; public TypedNodeExpression Value { get; } = value;
} }
public sealed class TypedNodeStatementIf(List<Token> tokens, TypedNodeExpression condition, TypedNodeStatement thenBlock, TypedNodeStatement? elseBlock) : TypedNodeStatement(tokens) public class TypedNodeStatementIf(List<Token> tokens, TypedNodeExpression condition, TypedNodeStatement thenBlock, TypedNodeStatement? elseBlock) : TypedNodeStatement(tokens)
{ {
public TypedNodeExpression Condition { get; } = condition; public TypedNodeExpression Condition { get; } = condition;
public TypedNodeStatement ThenBlock { get; } = thenBlock; public TypedNodeStatement ThenBlock { get; } = thenBlock;
public TypedNodeStatement? ElseBlock { get; } = elseBlock; public TypedNodeStatement? ElseBlock { get; } = elseBlock;
} }
public sealed class TypedNodeStatementWhile(List<Token> tokens, TypedNodeExpression condition, TypedNodeStatement block) : TypedNodeStatement(tokens) public class TypedNodeStatementWhile(List<Token> tokens, TypedNodeExpression condition, TypedNodeStatement block) : TypedNodeStatement(tokens)
{ {
public TypedNodeExpression Condition { get; } = condition; public TypedNodeExpression Condition { get; } = condition;
public TypedNodeStatement Block { get; } = block; public TypedNodeStatement Block { get; } = block;
@@ -474,56 +507,56 @@ public abstract class TypedNodeExpression(List<Token> tokens, NubType type) : Ty
public NubType Type { get; } = type; public NubType Type { get; } = type;
} }
public sealed class TypedNodeExpressionIntLiteral(List<Token> tokens, NubType type, TokenIntLiteral value) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionIntLiteral(List<Token> tokens, NubType type, TokenIntLiteral value) : TypedNodeExpression(tokens, type)
{ {
public TokenIntLiteral Value { get; } = value; public TokenIntLiteral Value { get; } = value;
} }
public sealed class TypedNodeExpressionStringLiteral(List<Token> tokens, NubType type, TokenStringLiteral value) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionStringLiteral(List<Token> tokens, NubType type, TokenStringLiteral value) : TypedNodeExpression(tokens, type)
{ {
public TokenStringLiteral Value { get; } = value; public TokenStringLiteral Value { get; } = value;
} }
public sealed class TypedNodeExpressionBoolLiteral(List<Token> tokens, NubType type, TokenBoolLiteral value) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionBoolLiteral(List<Token> tokens, NubType type, TokenBoolLiteral value) : TypedNodeExpression(tokens, type)
{ {
public TokenBoolLiteral Value { get; } = value; public TokenBoolLiteral Value { get; } = value;
} }
public sealed class TypedNodeExpressionStructLiteral(List<Token> tokens, NubType type, List<TypedNodeExpressionStructLiteral.Initializer> initializers) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionStructLiteral(List<Token> tokens, NubType type, List<TypedNodeExpressionStructLiteral.Initializer> initializers) : TypedNodeExpression(tokens, type)
{ {
public List<Initializer> Initializers { get; } = initializers; public List<Initializer> Initializers { get; } = initializers;
public sealed class Initializer(List<Token> tokens, TokenIdent name, TypedNodeExpression value) : Node(tokens) public class Initializer(List<Token> tokens, TokenIdent name, TypedNodeExpression value) : Node(tokens)
{ {
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
public TypedNodeExpression Value { get; } = value; public TypedNodeExpression Value { get; } = value;
} }
} }
public sealed class TypedNodeExpressionMemberAccess(List<Token> tokens, NubType type, TypedNodeExpression target, TokenIdent name) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionMemberAccess(List<Token> tokens, NubType type, TypedNodeExpression target, TokenIdent name) : TypedNodeExpression(tokens, type)
{ {
public TypedNodeExpression Target { get; } = target; public TypedNodeExpression Target { get; } = target;
public TokenIdent Name { get; } = name; public TokenIdent Name { get; } = name;
} }
public sealed class TypedNodeExpressionFuncCall(List<Token> tokens, NubType type, TypedNodeExpression target, List<TypedNodeExpression> parameters) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionFuncCall(List<Token> tokens, NubType type, TypedNodeExpression target, List<TypedNodeExpression> parameters) : TypedNodeExpression(tokens, type)
{ {
public TypedNodeExpression Target { get; } = target; public TypedNodeExpression Target { get; } = target;
public List<TypedNodeExpression> Parameters { get; } = parameters; public List<TypedNodeExpression> Parameters { get; } = parameters;
} }
public sealed class TypedNodeExpressionLocalIdent(List<Token> tokens, NubType type, TokenIdent value) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionLocalIdent(List<Token> tokens, NubType type, TokenIdent value) : TypedNodeExpression(tokens, type)
{ {
public TokenIdent Value { get; } = value; public TokenIdent Value { get; } = value;
} }
public sealed class TypedNodeExpressionModuleIdent(List<Token> tokens, NubType type, TokenIdent module, TokenIdent value) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionModuleIdent(List<Token> tokens, NubType type, TokenIdent module, TokenIdent value) : TypedNodeExpression(tokens, type)
{ {
public TokenIdent Module { get; } = module; public TokenIdent Module { get; } = module;
public TokenIdent Value { get; } = value; public TokenIdent Value { get; } = value;
} }
public sealed class TypedNodeExpressionBinary(List<Token> tokens, NubType type, TypedNodeExpression left, TypedNodeExpressionBinary.Op operation, TypedNodeExpression right) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionBinary(List<Token> tokens, NubType type, TypedNodeExpression left, TypedNodeExpressionBinary.Op operation, TypedNodeExpression right) : TypedNodeExpression(tokens, type)
{ {
public TypedNodeExpression Left { get; } = left; public TypedNodeExpression Left { get; } = left;
public Op Operation { get; } = operation; public Op Operation { get; } = operation;
@@ -556,7 +589,7 @@ public sealed class TypedNodeExpressionBinary(List<Token> tokens, NubType type,
} }
} }
public sealed class TypedNodeExpressionUnary(List<Token> tokens, NubType type, TypedNodeExpression target, TypedNodeExpressionUnary.Op op) : TypedNodeExpression(tokens, type) public class TypedNodeExpressionUnary(List<Token> tokens, NubType type, TypedNodeExpression target, TypedNodeExpressionUnary.Op op) : TypedNodeExpression(tokens, type)
{ {
public TypedNodeExpression Target { get; } = target; public TypedNodeExpression Target { get; } = target;
public Op Operation { get; } = op; public Op Operation { get; } = op;

View File

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

11
examples/build.sh Executable file
View File

@@ -0,0 +1,11 @@
pushd math
dotnet run --project ../../compiler math.nub --type=lib
popd
pushd program
dotnet run --project ../../compiler main.nub ../math/.build/out.nublib
popd

View File

@@ -0,0 +1,15 @@
{
"version": 1,
"modules": [
{
"name": "math",
"customTypes": {},
"identifiers": {
"add": {
"encodedType": "F(I(32),I(32),I(32))",
"exported": true
}
}
}
]
}

BIN
examples/math/.build/out.a Normal file

Binary file not shown.

View File

@@ -0,0 +1,24 @@
#include <float.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
struct nub_core_string
{
const char *data;
int length;
};
int32_t nub_math_add_500748c2c6d70959(int32_t, int32_t);
int32_t nub_math_add_500748c2c6d70959(int32_t a, int32_t b)
{
{
return (a + b);
}
}

Binary file not shown.

BIN
examples/math/.build/out.o Normal file

Binary file not shown.

6
examples/math/math.nub Normal file
View File

@@ -0,0 +1,6 @@
module math
export func add(a: i32 b: i32): i32
{
return a + b
}

BIN
examples/program/.build/out Executable file

Binary file not shown.

View File

@@ -0,0 +1,29 @@
#include <float.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
struct nub_core_string
{
const char *data;
int length;
};
extern int32_t nub_math_add_500748c2c6d70959(int32_t, int32_t);
int32_t nub_main_main_55882df37f903935();
int main(int argc, char *argv[])
{
return nub_main_main_55882df37f903935();
}
int32_t nub_main_main_55882df37f903935()
{
{
return nub_math_add_500748c2c6d70959(1, 2);
}
}

View File

@@ -0,0 +1,6 @@
module main
func main(): i32
{
return math::add(1 2)
}

View File

@@ -1,5 +1,7 @@
module main module main
let global: i32
func main(): i32 { func main(): i32 {
let x: i32 = 23 let x: i32 = 23
x = 24 x = 24
@@ -23,5 +25,8 @@ func main(): i32 {
x = test::do_something(me.name) x = test::do_something(me.name)
test::do_something(me.name) test::do_something(me.name)
return x
main::global = 123
return main::global
} }

10
examples/test/test2.nub Normal file
View File

@@ -0,0 +1,10 @@
module test
export packed struct person {
age: i32
name: string
}
export func do_something(name: string): i32 {
return 3
}