remove cstring

This commit is contained in:
nub31
2025-10-24 15:27:14 +02:00
parent a5f73a84cc
commit 766ca9a6b9
7 changed files with 211 additions and 143 deletions

View File

@@ -37,26 +37,23 @@ public class Generator
public string Emit()
{
_writer.WriteLine("""
#include <stdint.h>
#include <stddef.h>
typedef struct
struct nub_string
{
size_t length;
unsigned long long length;
char *data;
} string;
};
typedef struct
struct nub_slice
{
size_t length;
unsigned long long length;
void *data;
} slice;
};
""");
foreach (var structType in _compilationUnit.ImportedStructTypes)
{
_writer.WriteLine("typedef struct");
_writer.WriteLine($"struct {StructName(structType.Module, structType.Name)}");
_writer.WriteLine("{");
using (_writer.Indent())
{
@@ -66,7 +63,7 @@ public class Generator
}
}
_writer.WriteLine($"}} {StructName(structType.Module, structType.Name)};");
_writer.WriteLine("};");
_writer.WriteLine();
}
@@ -198,7 +195,7 @@ public class Generator
var target = EmitExpression(forSliceNode.Target);
var indexName = forSliceNode.IndexName ?? NewTmp();
_writer.WriteLine($"for (size_t {indexName} = 0; {indexName} < {target}.length; ++{indexName})");
_writer.WriteLine($"for (unsigned long long {indexName} = 0; {indexName} < {target}.length; ++{indexName})");
_writer.WriteLine("{");
using (_writer.Indent())
{
@@ -215,7 +212,7 @@ public class Generator
var target = EmitExpression(forConstArrayNode.Target);
var indexName = forConstArrayNode.IndexName ?? NewTmp();
_writer.WriteLine($"for (size_t {indexName} = 0; {indexName} < {targetType.Size}; ++{indexName})");
_writer.WriteLine($"for (unsigned long long {indexName} = 0; {indexName} < {targetType.Size}; ++{indexName})");
_writer.WriteLine("{");
using (_writer.Indent())
{
@@ -470,15 +467,9 @@ public class Generator
{
var value = EmitExpression(castNode.Value);
if (castNode is { Type: NubSliceType, Value.Type: NubConstArrayType arrayType })
if (castNode is { Type: NubSliceType sliceType, Value.Type: NubConstArrayType arrayType })
{
return $"(slice){{.length = {arrayType.Size}, .data = (void*){value}}}";
}
// todo(nub31): Stop depending on libc
if (castNode is { Type: NubCStringType, Value.Type: NubStringType })
{
return $"(string){{.length = strlen({value}), .data = {value}}}";
return $"({CType.Create(sliceType)}){{.length = {arrayType.Size}, .data = (void*){value}}}";
}
return $"({CType.Create(castNode.Type)}){value}";
@@ -509,7 +500,7 @@ public class Generator
private string EmitStringLiteral(StringLiteralNode stringLiteralNode)
{
var length = Encoding.UTF8.GetByteCount(stringLiteralNode.Value);
return $"(string){{.length = {length}, .data = \"{stringLiteralNode.Value}\"}}";
return $"(nub_string){{.length = {length}, .data = \"{stringLiteralNode.Value}\"}}";
}
private string EmitStructFieldAccess(StructFieldAccessNode structFieldAccessNode)