This commit is contained in:
nub31
2026-02-09 19:34:47 +01:00
parent 9fb9c50a0b
commit 96670b1201
7 changed files with 335 additions and 262 deletions

View File

@@ -2,15 +2,15 @@
namespace Compiler;
public sealed class Generator(TypedAst ast)
public sealed class Generator(List<TypedNodeDefinitionFunc> functions)
{
public static string Emit(TypedAst ast)
public static string Emit(List<TypedNodeDefinitionFunc> functions)
{
return new Generator(ast).Emit();
return new Generator(functions).Emit();
}
private IndentedTextWriter writer = new();
private Dictionary<NubTypeStruct, string> structTypeNames = new();
private readonly IndentedTextWriter writer = new();
private readonly Dictionary<NubTypeStruct, string> structTypeNames = new();
private string Emit()
{
@@ -28,31 +28,31 @@ public sealed class Generator(TypedAst ast)
""");
for (var i = 0; i < ast.StructTypes.Count; i++)
{
var structType = ast.StructTypes[i];
structTypeNames[structType] = $"s{i}";
}
foreach (var structType in ast.StructTypes)
{
var name = structTypeNames[structType];
writer.WriteLine($"struct {name}");
writer.WriteLine("{");
using (writer.Indent())
{
foreach (var field in structType.Fields)
{
writer.WriteLine($"{CType(field.Type, field.Name)};");
}
}
writer.WriteLine("};");
}
// for (var i = 0; i < ast.StructTypes.Count; i++)
// {
// var structType = ast.StructTypes[i];
// structTypeNames[structType] = $"s{i}";
// }
//
// foreach (var structType in ast.StructTypes)
// {
// var name = structTypeNames[structType];
// writer.WriteLine($"struct {name}");
// writer.WriteLine("{");
// using (writer.Indent())
// {
// foreach (var field in structType.Fields)
// {
// writer.WriteLine($"{CType(field.Type, field.Name)};");
// }
// }
//
// writer.WriteLine("};");
// }
writer.WriteLine();
foreach (var node in ast.Functions)
foreach (var node in functions)
{
var parameters = node.Parameters.Select(x => CType(x.Type, x.Name.Ident));
writer.WriteLine($"{CType(node.ReturnType, node.Name.Ident)}({string.Join(", ", parameters)});");
@@ -60,7 +60,7 @@ public sealed class Generator(TypedAst ast)
writer.WriteLine();
foreach (var node in ast.Functions)
foreach (var node in functions)
{
var parameters = node.Parameters.Select(x => CType(x.Type, x.Name.Ident));
writer.WriteLine($"{CType(node.ReturnType, node.Name.Ident)}({string.Join(", ", parameters)})");