From 4e559e886c3a7074b247bfe3d8c3364e3aabfa12 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 19 Dec 2021 21:04:32 +0100 Subject: [PATCH] Add some simple string functions. And fix bug relating to typedefs. --- lib/std/str.c3 | 59 +++++++++++++++++++++++++++++++++++++++ src/compiler/lexer.c | 2 +- src/compiler/sema_types.c | 4 +-- src/compiler/symtab.c | 4 +-- src/compiler/types.c | 4 +-- src/utils/malloc.h | 2 +- 6 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 lib/std/str.c3 diff --git a/lib/std/str.c3 b/lib/std/str.c3 new file mode 100644 index 000000000..6e22999bf --- /dev/null +++ b/lib/std/str.c3 @@ -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]; +} \ No newline at end of file diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c index bc97784cc..c09e673ec 100644 --- a/src/compiler/lexer.c +++ b/src/compiler/lexer.c @@ -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; diff --git a/src/compiler/sema_types.c b/src/compiler/sema_types.c index ed42fb45e..2148c39a9 100644 --- a/src/compiler/sema_types.c +++ b/src/compiler/sema_types.c @@ -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; diff --git a/src/compiler/symtab.c b/src/compiler/symtab.c index ebf1b0984..eea656da0 100644 --- a/src/compiler/symtab.c +++ b/src/compiler/symtab.c @@ -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)); diff --git a/src/compiler/types.c b/src/compiler/types.c index f6db854b5..b10f0dfda 100644 --- a/src/compiler/types.c +++ b/src/compiler/types.c @@ -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: diff --git a/src/utils/malloc.h b/src/utils/malloc.h index c50d30d16..abec91049 100644 --- a/src/utils/malloc.h +++ b/src/utils/malloc.h @@ -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; } \