Add some simple string functions. And fix bug relating to typedefs.

This commit is contained in:
Christoffer Lerno
2021-12-19 21:04:32 +01:00
parent 802398dd44
commit 4e559e886c
6 changed files with 67 additions and 8 deletions

59
lib/std/str.c3 Normal file
View File

@@ -0,0 +1,59 @@
module std::str;
import std::mem;
define ZString = char*;
define String = char[];
fn ZString copy_zstring(String s)
{
usize len = s.len;
char* str = mem::alloc(len + 1);
mem::copy(str, s.ptr, len);
str[len] = 0;
return str;
}
fn ZString tcopy_zstring(String s)
{
usize len = s.len;
char* str = mem::talloc(len + 1)!!;
mem::copy(str, s.ptr, len);
str[len] = 0;
return str;
}
fn String copy(String s)
{
usize len = s.len;
ZString str_copy = copy_zstring(s) @inline;
return str_copy[..len];
}
fn String tcopy(String s)
{
usize len = s.len;
ZString str_copy = tcopy_zstring(s) @inline;
return str_copy[..len];
}
fn String tconcat(String s1, String s2)
{
usize full_len = s1.len + s2.len;
char* str = mem::talloc(full_len + 1)!!;
usize s1_len = s1.len;
mem::copy(str, s1.ptr, s1_len);
mem::copy(str + s1_len, s2.ptr, s2.len);
str[full_len] = 0;
return str[..full_len];
}
fn String concat(String s1, String s2)
{
usize full_len = s1.len + s2.len;
char* str = mem::alloc(full_len + 1);
usize s1_len = s1.len;
mem::copy(str, s1.ptr, s1_len);
mem::copy(str + s1_len, s2.ptr, s2.len);
str[full_len] = 0;
return str[..full_len];
}

View File

@@ -170,7 +170,7 @@ static bool add_error_token_at(Lexer *lexer, const char *loc, uint32_t len, cons
}
// Add a new regular token.
static bool add_token(Lexer *lexer, TokenType type, const char *string)
static inline bool add_token(Lexer *lexer, TokenType type, const char *string)
{
add_generic_token(lexer, type);
lexer->latest_token_data->string = string;

View File

@@ -138,12 +138,12 @@ static bool sema_resolve_type_identifier(Context *context, TypeInfo *type_info)
case DECL_UNION:
case DECL_ERRTYPE:
case DECL_ENUM:
case DECL_TYPEDEF:
case DECL_DISTINCT:
type_info->type = decl->type;
type_info->resolve_status = RESOLVE_DONE;
DEBUG_LOG("Resolved %s.", TOKSTR(type_info->unresolved.name_loc));
return true;
case DECL_TYPEDEF:
case DECL_DISTINCT:
case DECL_DEFINE:
if (!sema_analyse_decl(context, decl)) return type_info_poison(type_info);
type_info->type = decl->type;

View File

@@ -221,7 +221,7 @@ static inline SymEntry *entry_find(const char *key, uint32_t key_len, uint32_t h
{
return entry;
}
index = (index + 1) % (symtab.capacity - 1);
index = (index + 1) & (symtab.capacity - 1);
}
}
@@ -309,7 +309,7 @@ void *stable_set(STable *table, const char *key, void *value)
assert(value && "Cannot insert NULL");
if (table->count + 1 > table->capacity * TABLE_MAX_LOAD)
{
ASSERT(table->capacity < MAX_HASH_SIZE, "Table size too large, exceeded %d", MAX_HASH_SIZE);
ASSERT(table->capacity < MAX_HASH_SIZE, "Table size too large, exceeded max hash size");
uint32_t new_capacity = table->capacity ? (table->capacity << 1u) : 16u;
SEntry *new_data = MALLOC(new_capacity * sizeof(SEntry));

View File

@@ -932,10 +932,10 @@ static void type_append_name_to_scratch(Type *type)
scratch_buffer_append_char('!');
break;
case TYPE_SUBARRAY:
type_append_name_to_scratch(type->pointer);
type_append_name_to_scratch(type->array.base);
scratch_buffer_append("[]");
case TYPE_FLEXIBLE_ARRAY:
type_append_name_to_scratch(type->pointer);
type_append_name_to_scratch(type->array.base);
scratch_buffer_append("[*]");
case TYPE_VOID:
case TYPE_BOOL:

View File

@@ -11,7 +11,7 @@ typedef unsigned type##Id; \
static inline type *name##_alloc(void) { return (type *)vmem_alloc(&name##_arena, sizeof(type)); } \
static inline void name##_arena_free(void) { vmem_free(&name##_arena); } \
static inline type *name##_calloc(void) { \
type *ptr = name##_alloc(); \
type *ptr = name##_alloc(); \
memset(ptr, 0, sizeof(type)); \
return ptr; } \
static inline type *name##ptr(type ## Id id) { return ((type *)name##_arena.ptr) + id; } \