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

@@ -12,14 +12,13 @@ public static class CType
NubBoolType => "bool" + (variableName != null ? $" {variableName}" : ""),
NubIntType intType => CreateIntType(intType, variableName),
NubFloatType floatType => CreateFloatType(floatType, variableName),
NubCStringType => "char*" + (variableName != null ? $" {variableName}" : ""),
NubPointerType ptr => CreatePointerType(ptr, variableName),
NubSliceType => "slice" + (variableName != null ? $" {variableName}" : ""),
NubStringType => "string" + (variableName != null ? $" {variableName}" : ""),
NubSliceType => "struct nub_slice" + (variableName != null ? $" {variableName}" : ""),
NubStringType => "struct nub_string" + (variableName != null ? $" {variableName}" : ""),
NubConstArrayType arr => CreateConstArrayType(arr, variableName, constArraysAsPointers),
NubArrayType arr => CreateArrayType(arr, variableName),
NubFuncType fn => CreateFuncType(fn, variableName),
NubStructType st => $"{st.Module}_{st.Name}" + (variableName != null ? $" {variableName}" : ""),
NubStructType st => $"struct {st.Module}_{st.Name}" + (variableName != null ? $" {variableName}" : ""),
_ => throw new NotSupportedException($"C type generation not supported for: {type}")
};
}
@@ -28,10 +27,10 @@ public static class CType
{
var cType = intType.Width switch
{
8 => intType.Signed ? "int8_t" : "uint8_t",
16 => intType.Signed ? "int16_t" : "uint16_t",
32 => intType.Signed ? "int32_t" : "uint32_t",
64 => intType.Signed ? "int64_t" : "uint64_t",
8 => intType.Signed ? "char" : "unsigned char",
16 => intType.Signed ? "short" : "unsigned short",
32 => intType.Signed ? "int" : "unsigned int",
64 => intType.Signed ? "long long" : "unsigned long long",
_ => throw new NotSupportedException($"Unsupported integer width: {intType.Width}")
};
return cType + (varName != null ? $" {varName}" : "");

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)