diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-22 08:59:24 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-22 08:59:24 +0200 |
| commit | 13fdee74dc28f0b068503843c6e14071a7c339f2 (patch) | |
| tree | 323f35e351394c89faeaf61cccad716ba4e4fb1a | |
| parent | e02bfc1854effe0bb55ea6d4320e2b1902eaeae3 (diff) | |
feat: More integer types
| -rw-r--r-- | base_test.c | 6 | ||||
| -rw-r--r-- | generate.fish | 4 | ||||
| -rw-r--r-- | json.c | 20 | ||||
| -rw-r--r-- | json.h | 8 | ||||
| -rw-r--r-- | memory.c | 7 | ||||
| -rw-r--r-- | memory.h | 4 | ||||
| -rw-r--r-- | string.c | 10 | ||||
| -rw-r--r-- | string.h | 6 | ||||
| -rw-r--r-- | test_json.c | 16 | ||||
| -rw-r--r-- | test_string.c | 4 | ||||
| -rw-r--r-- | types.h | 3 | ||||
| -rw-r--r-- | vec.c | 12 | ||||
| -rw-r--r-- | vec.h | 8 |
13 files changed, 57 insertions, 51 deletions
diff --git a/base_test.c b/base_test.c index 7a0c77b..6d095f8 100644 --- a/base_test.c +++ b/base_test.c @@ -1,3 +1,5 @@ +#include "types.h" + #define test(func) \ switch (func()) { \ case 0: \ @@ -9,5 +11,5 @@ printf("🚫 %s\n", #func); \ } -int success; -int failures; +i32 success; +i32 failures; diff --git a/generate.fish b/generate.fish index ac24d3e..b0e5308 100644 --- a/generate.fish +++ b/generate.fish @@ -6,10 +6,10 @@ end; echo -e "" >> test.c cat base_test.c >> test.c; echo -e "" >> test.c; -echo "int main() {" >> test.c; +echo "i32 main() {" >> test.c; for f in $fs; echo "Parsing $f"; - set funcs (rg 'int test_' $f); + set funcs (rg 'i32 test_' $f); for func in $funcs; set pfunc (echo $func | awk '{match($0, /test_[^(]+/); print substr($0, RSTART, RLENGTH)}'); echo "Found $pfunc in $f"; @@ -19,7 +19,7 @@ JSONObject *JSONObject_new(Arena *a) { return object; } -int JSON_fprint(FILE *w, JSON json) { +i32 JSON_fprint(FILE *w, JSON json) { if (json.string != 0) return Str_fprint(w, *json.string); if (json.integer != 0) @@ -59,20 +59,20 @@ typedef struct { parse_any_result parse_any(Arena *a, byte *src, uptr pos); typedef struct { - int *value; + i32 *value; uptr pos; } parse_integer_result; parse_integer_result parse_integer(Arena *a, byte *src, uptr start_pos) { byte ints[16]; - int i = 0; + i32 i = 0; while (isdigit(src[start_pos + i])) { - int val = (src[start_pos + i] - '0'); + i32 val = (src[start_pos + i] - '0'); ints[i] = val; i++; } - int *result = new(a, int, 1); - for (int j = 0; j < i; j++) { + i32 *result = new(a, int, 1); + for (i32 j = 0; j < i; j++) { *result += ints[j] * pow(10, (i - j - 1)); } return (parse_integer_result){result, start_pos + i}; @@ -192,11 +192,11 @@ parse_any_result parse_any(Arena *a, byte *src, uptr start_pos) { result.value->object = object_result.object; result.pos = object_result.pos; } else if (memcmp(src + result.pos, "true", 4) == 0) { - result.value->boolean = new(a, int, 1); + result.value->boolean = new(a, i32, 1); *result.value->boolean = 1; result.pos = start_pos + 4; } else if (memcmp(src + result.pos, "false", 5) == 0) { - result.value->boolean = new(a, int, 1); + result.value->boolean = new(a, i32, 1); *result.value->boolean = 0; result.pos = start_pos + 5; } @@ -209,9 +209,9 @@ JSON JSON_parse(Arena *a, byte *src) { return *result.value; } -JSON JSON_Int(Arena *a, int val) { +JSON JSON_Int(Arena *a, i32 val) { JSON json = {0}; - json.integer = new(a, int, 1); + json.integer = new(a, i32, 1); *json.integer = val; return json; } @@ -10,14 +10,14 @@ typedef struct { size len; } JSONObject; typedef struct { - int *boolean; - int *integer; + i32 *boolean; + i32 *integer; Vec *array; // Vec<JSON> Str *string; JSONObject *object; } JSON; JSON *JSONObject_get(JSONObject *obj, Str key); -int JSON_fprint(FILE *w, JSON json); +i32 JSON_fprint(FILE *w, JSON json); JSON JSON_parse(Arena *a, byte *src); -JSON JSON_Int(Arena *a, int val); +JSON JSON_Int(Arena *a, i32 val); #endif @@ -1,5 +1,6 @@ #include <assert.h> #include <stdint.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -16,12 +17,12 @@ Arena Arena_new(size cap) { // Magic to allow optimisation __attribute((malloc, alloc_size(2, 4), // alloc_align(3))) -void *alloc(Arena *a, size count, size tsize, int align) { - int padding = -(uptr)a->beg & (align - 1); +void *alloc(Arena *a, size count, size tsize, size align) { + size padding = -(uptr)a->beg & (align - 1); assert(count < (a->end - a->beg - padding) / tsize); void *cursor = a->beg + padding; a->beg += padding + count * tsize; return memset(cursor, 0, count * tsize); } -int Arena_get_used(Arena a) { return a.cap - (a.end - a.beg); } +size Arena_get_used(Arena a) { return a.cap - (a.end - a.beg); } @@ -11,6 +11,6 @@ typedef struct { Arena Arena_new(size cap); #define new(a, T, n) (T *)alloc(a, n, sizeof(T), _Alignof(T)) -void *alloc(Arena *a, size count, size size, int align); -int Arena_get_used(Arena a); +void *alloc(Arena *a, size count, size tsize, size align); +size Arena_get_used(Arena a); #endif @@ -7,12 +7,12 @@ #include "memory.h" // Returns 1 if strings are equal, 0 otherwise -int Str_equals(Str a, Str b) { +i32 Str_equals(Str a, Str b) { return (a.len == b.len) && (!a.len || !memcmp(a.data, b.data, a.len)); } -uint64_t Str_hash64(Str s) { - uint64_t h = 0x100; +u64 Str_hash64(Str s) { + u64 h = 0x100; for (size i = 0; i < s.len; i++) { h ^= s.data[i] & 255; h *= 1111111111111111111; @@ -29,6 +29,6 @@ Str *Str_copy(Arena *a, byte *data, size len) { return str; } -int Str_fprint(FILE *w, Str s) { - return fprintf(w, "%.*s", (int)s.len, s.data); +i32 Str_fprint(FILE *w, Str s) { + return fprintf(w, "%.*s", (i32)s.len, s.data); } @@ -13,8 +13,8 @@ typedef struct { byte *data; size len; } Str; -int Str_equals(Str a, Str b); -uint64_t Str_hash64(Str s); +i32 Str_equals(Str a, Str b); +u64 Str_hash64(Str s); Str *Str_copy(Arena *a, byte *data, size len); -int Str_fprint(FILE *w, Str s); +i32 Str_fprint(FILE *w, Str s); #endif diff --git a/test_json.c b/test_json.c index 65891fd..240a61e 100644 --- a/test_json.c +++ b/test_json.c @@ -1,7 +1,7 @@ #include "json.h" #include "testing.h" -int test_json_integer() { +i32 test_json_integer() { Arena perm = Arena_new(128e3); JSON json_int = JSON_parse(&perm, "123"); @@ -11,7 +11,7 @@ int test_json_integer() { return 0; } -int test_json_int_array() { +i32 test_json_int_array() { Arena perm = Arena_new(128e3); JSON json_int_array = JSON_parse(&perm, "[1,2,3]"); @@ -34,7 +34,7 @@ int test_json_int_array() { return 0; } -int test_json_empty_array() { +i32 test_json_empty_array() { Arena perm = Arena_new(128e3); JSON json_empty_array = JSON_parse(&perm, "[]"); @@ -45,7 +45,7 @@ int test_json_empty_array() { return 0; } -int test_json_nested_array() { +i32 test_json_nested_array() { Arena perm = Arena_new(128e3); JSON doc = JSON_parse(&perm, "[[1,\"abc\"]]"); @@ -63,7 +63,7 @@ int test_json_nested_array() { return 0; } -int test_json_string() { +i32 test_json_string() { Arena perm = Arena_new(128e3); JSON js = JSON_parse(&perm, "\"abc\""); @@ -75,7 +75,7 @@ int test_json_string() { return 0; } -int test_json_empty_object() { +i32 test_json_empty_object() { Arena perm = Arena_new(128e3); JSON doc = JSON_parse(&perm, "{}"); @@ -86,7 +86,7 @@ int test_json_empty_object() { return 0; } -int test_json_object() { +i32 test_json_object() { Arena perm = Arena_new(128e3); JSON doc = JSON_parse(&perm, "{\"key\": \"value\"}"); @@ -101,7 +101,7 @@ int test_json_object() { return 0; } -int test_json_boolean() { +i32 test_json_boolean() { Arena perm = Arena_new(128e3); JSON doc = JSON_parse(&perm, "{\"a\": true, \"b\": false}"); diff --git a/test_string.c b/test_string.c index ff55e67..7b37e0d 100644 --- a/test_string.c +++ b/test_string.c @@ -1,12 +1,12 @@ #include "string.h" #include "testing.h" -int test_string_literal_equality() { +i32 test_string_literal_equality() { t_assert(Str_equals(S("Bonjour"), S("Bonjour"))); return 0; } -int test_string_copy() { +i32 test_string_copy() { Arena perm = Arena_new(128e3); Str src = S("Hello"); @@ -4,6 +4,9 @@ typedef ptrdiff_t size; typedef char byte; typedef uintptr_t uptr; +typedef int32_t i32; +typedef uint64_t u64; +typedef float f32; #define sizeof(x) (size)sizeof(x) #define countof(a) (sizeof(a) / sizeof(*(a))) @@ -4,10 +4,10 @@ #include "types.h" #include "vec.h" -Vec *_Vec_new(Arena *a, size tsize, size cap, int align) { +Vec *_Vec_new(Arena *a, size tsize, size cap, size align) { assert(cap > 0); Vec *v = new(a, Vec, 1); - v->size = tsize; + v->tsize = tsize; v->cap = cap; v->align = align; v->len = 0; @@ -17,12 +17,12 @@ Vec *_Vec_new(Arena *a, size tsize, size cap, int align) { void Vec_push(Arena *a, Vec *v, void *data) { if (v->len == v->cap) { - void *cpy = alloc(a, v->cap * 2, v->size, v->align); - memcpy(cpy, v->data, v->len * v->size); + void *cpy = alloc(a, v->cap * 2, v->tsize, v->align); + memcpy(cpy, v->data, v->len * v->tsize); v->cap *= 2; } - memcpy(v->data + v->len * v->size, data, v->size); + memcpy(v->data + v->len * v->tsize, data, v->tsize); v->len++; } -void *_Vec_get(Vec *v, int pos) { return v->data + v->size * pos; } +void *_Vec_get(Vec *v, size pos) { return v->data + v->tsize * pos; } @@ -10,12 +10,12 @@ typedef struct { void *data; size len; size cap; - size size; - int align; + size tsize; + size align; } Vec; #define Vec_new(a, T, cap) _Vec_new(a, sizeof(T), cap, _Alignof(T)) -Vec *_Vec_new(Arena *a, size tsize, size cap, int align); +Vec *_Vec_new(Arena *a, size tsize, size cap, size align); void Vec_push(Arena *a, Vec *v, void *data); #define Vec_get(v, T, p) (T *)_Vec_get(v, p) -void *_Vec_get(Vec *v, int pos); +void *_Vec_get(Vec *v, size pos); #endif |
