module exports and name mangling

This commit is contained in:
nub31
2026-02-10 20:33:27 +01:00
parent 6ae10d5f90
commit 7872a4b6b8
7 changed files with 139 additions and 48 deletions

View File

@@ -24,13 +24,15 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
#include <stdbool.h>
#include <stdint.h>
struct string {
struct nub_core_string
{
const char *data;
int length;
};
""");
writer.WriteLine();
foreach (var typeName in structTypeNames)
writer.WriteLine($"struct {typeName.Value};");
@@ -38,7 +40,14 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
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("{");
using (writer.Indent())
{
@@ -74,13 +83,12 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
if (main != null)
{
writer.WriteLine("int main(int argc, char *argv[])");
writer.WriteLine("{");
using (writer.Indent())
{
writer.WriteLine($"return {main.GetMangledName()}();");
}
writer.WriteLine("}");
writer.WriteLine($$"""
int main(int argc, char *argv[])
{
return {{main.GetMangledName()}}();
}
""");
}
writer.WriteLine();
@@ -216,7 +224,7 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
TypedNodeExpressionUnary expression => EmitExpressionUnary(expression),
TypedNodeExpressionBoolLiteral expression => expression.Value.Value ? "true" : "false",
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),
TypedNodeExpressionMemberAccess expression => EmitExpressionMemberAccess(expression),
TypedNodeExpressionLocalIdent expression => expression.Value.Ident,
@@ -302,7 +310,7 @@ public sealed class Generator(List<TypedNodeDefinitionFunc> functions, ModuleGra
NubTypeSInt type => $"int{type.Width}_t" + (varName != null ? $" {varName}" : ""),
NubTypeUInt type => $"uint{type.Width}_t" + (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)))})",
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
};