aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-07-22 08:59:24 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-07-22 08:59:24 +0200
commit13fdee74dc28f0b068503843c6e14071a7c339f2 (patch)
tree323f35e351394c89faeaf61cccad716ba4e4fb1a
parente02bfc1854effe0bb55ea6d4320e2b1902eaeae3 (diff)
feat: More integer types
-rw-r--r--base_test.c6
-rw-r--r--generate.fish4
-rw-r--r--json.c20
-rw-r--r--json.h8
-rw-r--r--memory.c7
-rw-r--r--memory.h4
-rw-r--r--string.c10
-rw-r--r--string.h6
-rw-r--r--test_json.c16
-rw-r--r--test_string.c4
-rw-r--r--types.h3
-rw-r--r--vec.c12
-rw-r--r--vec.h8
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";
diff --git a/json.c b/json.c
index 529dc3a..e782d02 100644
--- a/json.c
+++ b/json.c
@@ -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;
}
diff --git a/json.h b/json.h
index 824c5c6..032c23c 100644
--- a/json.h
+++ b/json.h
@@ -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
diff --git a/memory.c b/memory.c
index 7fa3e1d..83757b6 100644
--- a/memory.c
+++ b/memory.c
@@ -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); }
diff --git a/memory.h b/memory.h
index b12f4f9..db48773 100644
--- a/memory.h
+++ b/memory.h
@@ -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
diff --git a/string.c b/string.c
index d788f58..2c4e749 100644
--- a/string.c
+++ b/string.c
@@ -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);
}
diff --git a/string.h b/string.h
index 2785e99..aed7945 100644
--- a/string.h
+++ b/string.h
@@ -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");
diff --git a/types.h b/types.h
index 1c84c74..724e5a1 100644
--- a/types.h
+++ b/types.h
@@ -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)))
diff --git a/vec.c b/vec.c
index f555885..e91147e 100644
--- a/vec.c
+++ b/vec.c
@@ -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; }
diff --git a/vec.h b/vec.h
index c94b29e..8a9a0e8 100644
--- a/vec.h
+++ b/vec.h
@@ -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