ref counted strings

This commit is contained in:
nub31
2026-02-28 01:07:02 +01:00
parent 84627dde45
commit d58e006be4
3 changed files with 34 additions and 12 deletions

View File

@@ -5,21 +5,21 @@ namespace Compiler;
public class Generator
{
public static string Emit(List<TypedNodeDefinitionFunc> functions, ModuleGraph moduleGraph, bool compileLib)
public static string Emit(List<TypedNodeDefinitionFunc> functions, ModuleGraph moduleGraph, string? entryPoint)
{
return new Generator(functions, moduleGraph, compileLib).Emit();
return new Generator(functions, moduleGraph, entryPoint).Emit();
}
private Generator(List<TypedNodeDefinitionFunc> functions, ModuleGraph moduleGraph, bool compileLib)
private Generator(List<TypedNodeDefinitionFunc> functions, ModuleGraph moduleGraph, string? entryPoint)
{
this.functions = functions;
this.moduleGraph = moduleGraph;
this.compileLib = compileLib;
this.entryPoint = entryPoint;
}
private readonly List<TypedNodeDefinitionFunc> functions;
private readonly ModuleGraph moduleGraph;
private readonly bool compileLib;
private readonly string? entryPoint;
private IndentedTextWriter writer = new();
private HashSet<NubType> referencedTypes = new();
private readonly HashSet<NubType> 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("}");