From d58e006be4f9c73c01d3874222dbfc9b434488b9 Mon Sep 17 00:00:00 2001 From: nub31 Date: Sat, 28 Feb 2026 01:07:02 +0100 Subject: [PATCH] ref counted strings --- compiler/Generator.cs | 18 +++++++----------- compiler/Program.cs | 27 ++++++++++++++++++++++++++- examples/program/main.nub | 1 + 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/compiler/Generator.cs b/compiler/Generator.cs index f2e4c8d..b3eab4f 100644 --- a/compiler/Generator.cs +++ b/compiler/Generator.cs @@ -5,21 +5,21 @@ namespace Compiler; public class Generator { - public static string Emit(List functions, ModuleGraph moduleGraph, bool compileLib) + public static string Emit(List functions, ModuleGraph moduleGraph, string? entryPoint) { - return new Generator(functions, moduleGraph, compileLib).Emit(); + return new Generator(functions, moduleGraph, entryPoint).Emit(); } - private Generator(List functions, ModuleGraph moduleGraph, bool compileLib) + private Generator(List functions, ModuleGraph moduleGraph, string? entryPoint) { this.functions = functions; this.moduleGraph = moduleGraph; - this.compileLib = compileLib; + this.entryPoint = entryPoint; } private readonly List functions; private readonly ModuleGraph moduleGraph; - private readonly bool compileLib; + private readonly string? entryPoint; private IndentedTextWriter writer = new(); private HashSet referencedTypes = new(); private readonly HashSet emittedTypes = new(); @@ -28,15 +28,12 @@ public class Generator private string Emit() { - if (!compileLib) + if (entryPoint != null) { - if (!moduleGraph.TryResolveIdentifier("main", "main", true, out var info)) - throw new UnreachableException("func main::main() is not defined. This should have been caught earlier"); - writer.WriteLine($$""" int main(int argc, char *argv[]) { - return {{info.MangledName}}(); + return {{entryPoint}}(); } """); @@ -604,7 +601,6 @@ public class Generator writer.WriteLine("{"); using (writer.Indent()) { - writer.WriteLine("puts(\"free\");"); writer.WriteLine($"free({value});"); } writer.WriteLine("}"); diff --git a/compiler/Program.cs b/compiler/Program.cs index 8d3f98b..c0bb509 100644 --- a/compiler/Program.cs +++ b/compiler/Program.cs @@ -128,7 +128,32 @@ else Directory.CreateDirectory(".build"); } -var output = Generator.Emit(functions, moduleGraph, compileLib); +string? entryPoint = null; + +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; + } + + if (!entryPointType.ReturnType.IsAssignableTo(NubTypeSInt.Get(32))) + { + DiagnosticFormatter.Print(Diagnostic.Error($"Entrypoint must return an i32 (currently '{entryPointType.ReturnType}')").Build(), Console.Error); + return 1; + } + + if (entryPointType.Parameters.Any()) + { + DiagnosticFormatter.Print(Diagnostic.Error($"Entrypoint must not take any parameters").Build(), Console.Error); + return 1; + } + + entryPoint = info.MangledName; +} + +var output = Generator.Emit(functions, moduleGraph, entryPoint); File.WriteAllText(".build/out.c", output); if (compileLib) diff --git a/examples/program/main.nub b/examples/program/main.nub index 18879e1..7001305 100644 --- a/examples/program/main.nub +++ b/examples/program/main.nub @@ -1,5 +1,6 @@ module main func main(): i32 { + core::print("Your mom") return 0 } \ No newline at end of file