aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--generate.fish2
-rw-r--r--json.c30
-rw-r--r--json.h4
-rw-r--r--log.c4
-rw-r--r--log.h5
-rw-r--r--main.c2
-rw-r--r--memory.c13
-rw-r--r--memory.h11
-rw-r--r--string.c6
-rw-r--r--string.h7
-rw-r--r--test_json.c12
-rw-r--r--test_string.c4
-rw-r--r--types.h6
-rw-r--r--vec.c7
-rw-r--r--vec.h9
15 files changed, 69 insertions, 53 deletions
diff --git a/generate.fish b/generate.fish
index 1994cc9..ac24d3e 100644
--- a/generate.fish
+++ b/generate.fish
@@ -9,7 +9,7 @@ echo -e "" >> test.c;
echo "int main() {" >> test.c;
for f in $fs;
echo "Parsing $f";
- set funcs (rg 'char test_' $f);
+ set funcs (rg 'int 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 f170e3f..94dab1d 100644
--- a/json.c
+++ b/json.c
@@ -10,17 +10,17 @@
typedef struct {
JSON *value;
- uintptr_t pos;
+ uptr pos;
} parse_any_result;
-parse_any_result parse_any(Arena *a, char *src, uintptr_t pos);
+parse_any_result parse_any(Arena *a, byte *src, uptr pos);
typedef struct {
int *value;
- uintptr_t pos;
+ uptr pos;
} parse_integer_result;
-parse_integer_result parse_integer(Arena *a, char *src, uintptr_t pos) {
- char ints[16];
+parse_integer_result parse_integer(Arena *a, byte *src, uptr pos) {
+ byte ints[16];
int i = 0;
while (isdigit(src[pos + i])) {
int val = (src[pos + i] - '0');
@@ -34,7 +34,7 @@ parse_integer_result parse_integer(Arena *a, char *src, uintptr_t pos) {
return (parse_integer_result){result, pos + i};
}
-uintptr_t absorb_whitespaces(char *src, uintptr_t pos) {
+uptr absorb_whitespaces(byte *src, uptr pos) {
int i = 0;
while (isspace(src[pos + i])) {
i++;
@@ -44,10 +44,10 @@ uintptr_t absorb_whitespaces(char *src, uintptr_t pos) {
typedef struct {
Vec *items; // Vec<JSON>
- uintptr_t pos;
+ uptr pos;
} parse_array_result;
-parse_array_result parse_array(Arena *a, char *src, uintptr_t pos) {
+parse_array_result parse_array(Arena *a, byte *src, uptr pos) {
assert(src[pos] == '[');
pos++;
Vec items = Vec_new(a, JSON, 2);
@@ -70,10 +70,10 @@ parse_array_result parse_array(Arena *a, char *src, uintptr_t pos) {
typedef struct {
Str *string;
- uintptr_t pos;
+ uptr pos;
} parse_string_result;
-parse_string_result parse_string(Arena *a, char *src, uintptr_t pos) {
+parse_string_result parse_string(Arena *a, byte *src, uptr pos) {
assert(src[pos] == '"');
int i = 1;
@@ -88,10 +88,10 @@ parse_string_result parse_string(Arena *a, char *src, uintptr_t pos) {
typedef struct {
JSONObject *object;
- uintptr_t pos;
+ uptr pos;
} parse_object_result;
-parse_object_result parse_object(Arena *a, char *src, uintptr_t pos) {
+parse_object_result parse_object(Arena *a, byte *src, uptr pos) {
assert(src[pos] == '{');
JSONObject *object = new(a, JSONObject, 1);
object->len = 0;
@@ -120,7 +120,7 @@ parse_object_result parse_object(Arena *a, char *src, uintptr_t pos) {
return (parse_object_result){object, pos+1};
}
-parse_any_result parse_any(Arena *a, char *src, uintptr_t pos) {
+parse_any_result parse_any(Arena *a, byte *src, uptr pos) {
JSON *value = new (a, JSON, 1);
if (isdigit(src[pos])) {
parse_integer_result result = parse_integer(a, src, pos);
@@ -143,7 +143,7 @@ parse_any_result parse_any(Arena *a, char *src, uintptr_t pos) {
return (parse_any_result){value, pos};
}
-JSON JSON_parse(Arena *a, char *src) {
+JSON JSON_parse(Arena *a, byte *src) {
parse_any_result result = parse_any(a, src, 0);
return *result.value;
}
@@ -156,7 +156,7 @@ JSON JSON_Int(Arena *a, int val) {
}
JSON* JSONObject_get(JSONObject *obj, Str key) {
- for (ptrdiff_t i = 0; i < obj->len; i++) {
+ for (size i = 0; i < obj->len; i++) {
if (Str_equals(*Vec_get(&obj->keys, Str, i), key)) {
return Vec_get(&obj->values, JSON, i);
}
diff --git a/json.h b/json.h
index 5f2f4c0..fc2dcfe 100644
--- a/json.h
+++ b/json.h
@@ -7,7 +7,7 @@
typedef struct {
Vec keys; // Vec<Str>
Vec values; // Vec<JSON>
- ptrdiff_t len;
+ size len;
} JSONObject;
typedef struct {
int *integer;
@@ -16,6 +16,6 @@ typedef struct {
JSONObject *object;
} JSON;
JSON* JSONObject_get(JSONObject *obj, Str key);
-JSON JSON_parse(Arena *a, char *src);
+JSON JSON_parse(Arena *a, byte *src);
JSON JSON_Int(Arena *a, int val);
#endif
diff --git a/log.c b/log.c
index 76bee11..d95299d 100644
--- a/log.c
+++ b/log.c
@@ -2,7 +2,9 @@
#include <stdio.h>
-void info(Logger l, const char *name, const char *str, ...) {
+#include "types.h"
+
+void info(Logger l, const byte *name, const byte *str, ...) {
if (l.level >= INFO) {
printf("[%s] ", name);
printf(str);
diff --git a/log.h b/log.h
index 51ce333..a2744f1 100644
--- a/log.h
+++ b/log.h
@@ -1,7 +1,10 @@
+
+#include "types.h"
+
typedef enum { INFO } LogLevel;
typedef struct {
LogLevel level;
} Logger;
-void info(Logger l, const char *name, const char *str, ...);
+void info(Logger l, const byte *name, const byte *str, ...);
diff --git a/main.c b/main.c
index e5c5a25..3d23212 100644
--- a/main.c
+++ b/main.c
@@ -10,7 +10,7 @@ Inspirations:
#include "string.h"
#include "vec.h"
-#define sizeof(x) (ptrdiff_t)sizeof(x)
+#define sizeof(x) (size)sizeof(x)
#define countof(a) (sizeof(a) / sizeof(*(a)))
#define lengthof(s) (countof(s) - 1)
diff --git a/memory.c b/memory.c
index f2248b6..f11af57 100644
--- a/memory.c
+++ b/memory.c
@@ -4,8 +4,9 @@
#include <string.h>
#include "memory.h"
+#include "types.h"
-Arena Arena_new(ptrdiff_t cap) {
+Arena Arena_new(size cap) {
Arena a = {0};
a.cap = cap;
a.beg = malloc(cap);
@@ -15,12 +16,12 @@ Arena Arena_new(ptrdiff_t cap) {
// Magic to allow optimisation __attribute((malloc, alloc_size(2, 4),
// alloc_align(3)))
-void *alloc(Arena *a, ptrdiff_t count, int size, int align) {
- int padding = -(uintptr_t)a->beg & (align - 1);
- assert(count < (a->end - a->beg - padding) / size);
+void *alloc(Arena *a, size count, size tsize, int align) {
+ int padding = -(uptr)a->beg & (align - 1);
+ assert(count < (a->end - a->beg - padding) / tsize);
void *cursor = a->beg + padding;
- a->beg += padding + count * size;
- return memset(cursor, 0, count * size);
+ a->beg += padding + count * tsize;
+ return memset(cursor, 0, count * tsize);
}
int Arena_get_used(Arena a) { return a.cap - (a.end - a.beg); }
diff --git a/memory.h b/memory.h
index e1c5fdd..846a40d 100644
--- a/memory.h
+++ b/memory.h
@@ -1,15 +1,16 @@
#include <stddef.h>
#ifndef MEMORY_H
+#include "types.h"
#define MEMORY_H
typedef struct {
- ptrdiff_t cap;
- char *beg;
- char *end;
+ size cap;
+ byte *beg;
+ byte *end;
} Arena;
-Arena Arena_new(ptrdiff_t cap);
+Arena Arena_new(size cap);
#define new(a, T, n) (T *)alloc(a, n, sizeof(T), _Alignof(T))
-void *alloc(Arena *a, ptrdiff_t count, int size, int align);
+void *alloc(Arena *a, size count, size size, int align);
int Arena_get_used(Arena a);
#endif
diff --git a/string.c b/string.c
index 0fc8341..3d5841a 100644
--- a/string.c
+++ b/string.c
@@ -13,17 +13,17 @@ int Str_equals(Str a, Str b) {
uint64_t Str_hash64(Str s) {
uint64_t h = 0x100;
- for (ptrdiff_t i = 0; i < s.len; i++) {
+ for (size i = 0; i < s.len; i++) {
h ^= s.data[i] & 255;
h *= 1111111111111111111;
}
return h;
}
-Str* Str_copy(Arena *a, char *data, uintptr_t len) {
+Str* Str_copy(Arena *a, byte *data, size len) {
Str *str = new(a, Str, 1);
str->len = len;
- str->data = new (a, char, len);
+ str->data = new (a, byte, len);
if (str->len)
memcpy(str->data, data, len);
return str;
diff --git a/string.h b/string.h
index 10b3869..3c05bc2 100644
--- a/string.h
+++ b/string.h
@@ -2,16 +2,17 @@
#include <stdint.h>
#include "memory.h"
+#include "types.h"
#ifndef STRING_H
#define STRING_H
#define S(s) (Str){s, sizeof(s) - 1}
typedef struct {
- char *data;
- ptrdiff_t len;
+ byte *data;
+ size len;
} Str;
int Str_equals(Str a, Str b);
uint64_t Str_hash64(Str s);
-Str* Str_copy(Arena *a, char *data, uintptr_t len);
+Str* Str_copy(Arena *a, byte *data, size len);
void Str_print(Str s);
#endif
diff --git a/test_json.c b/test_json.c
index d2a4595..ee7f0f3 100644
--- a/test_json.c
+++ b/test_json.c
@@ -2,7 +2,7 @@
#include "testing.h"
#include <stdio.h>
-char test_json_integer() {
+int test_json_integer() {
Arena perm = Arena_new(128e3);
JSON json_int = JSON_parse(&perm, "123");
@@ -12,7 +12,7 @@ char test_json_integer() {
return 0;
}
-char test_json_int_array() {
+int test_json_int_array() {
Arena perm = Arena_new(128e3);
JSON json_int_array = JSON_parse(&perm, "[1,2,3]");
@@ -35,7 +35,7 @@ char test_json_int_array() {
return 0;
}
-char test_json_nested_array() {
+int test_json_nested_array() {
Arena perm = Arena_new(128e3);
JSON doc = JSON_parse(&perm, "[[1,\"abc\"]]");
@@ -53,7 +53,7 @@ char test_json_nested_array() {
return 0;
}
-char test_json_string() {
+int test_json_string() {
Arena perm = Arena_new(128e3);
JSON js = JSON_parse(&perm, "\"abc\"");
@@ -65,7 +65,7 @@ char test_json_string() {
return 0;
}
-char test_json_empty_object() {
+int test_json_empty_object() {
Arena perm = Arena_new(128e3);
JSON doc = JSON_parse(&perm, "{}");
@@ -76,7 +76,7 @@ char test_json_empty_object() {
return 0;
}
-char test_json_object() {
+int test_json_object() {
Arena perm = Arena_new(128e3);
JSON doc = JSON_parse(&perm, "{\"key\": \"value\"}");
diff --git a/test_string.c b/test_string.c
index f62f4ab..59da739 100644
--- a/test_string.c
+++ b/test_string.c
@@ -1,12 +1,12 @@
#include "string.h"
#include "testing.h"
-char test_string_literal_equality() {
+int test_string_literal_equality() {
t_assert(Str_equals(S("Bonjour"), S("Bonjour")));
return 0;
}
-char test_string_copy() {
+int test_string_copy() {
Arena perm = Arena_new(128e3);
Str src = S("Hello");
diff --git a/types.h b/types.h
new file mode 100644
index 0000000..584da21
--- /dev/null
+++ b/types.h
@@ -0,0 +1,6 @@
+#include <stddef.h>
+#include <stdint.h>
+
+typedef ptrdiff_t size;
+typedef char byte;
+typedef uintptr_t uptr;
diff --git a/vec.c b/vec.c
index af5f338..c9fe4b5 100644
--- a/vec.c
+++ b/vec.c
@@ -2,11 +2,12 @@
#include "assert.h"
#include "vec.h"
+#include "types.h"
-Vec _Vec_new(Arena *a, int size, ptrdiff_t cap, int align) {
+Vec _Vec_new(Arena *a, size tsize, size cap, int align) {
assert(cap > 0);
- Vec v = {.size = size, .cap = cap, .len = 0, .align = align,.data = NULL};
- v.data = alloc(a, cap, size, align);
+ Vec v = {.size = tsize, .cap = cap, .len = 0, .align = align,.data = NULL};
+ v.data = alloc(a, cap, tsize, align);
return v;
}
diff --git a/vec.h b/vec.h
index f7e5295..d43fafb 100644
--- a/vec.h
+++ b/vec.h
@@ -2,18 +2,19 @@
#include <stdint.h>
#include "memory.h"
+#include "types.h"
#ifndef VEC_H
#define VEC_H
typedef struct {
void *data;
- ptrdiff_t len;
- ptrdiff_t cap;
- uintptr_t size;
+ size len;
+ size cap;
+ size size;
int align;
} Vec;
#define Vec_new(a, T, cap) _Vec_new(a, sizeof(T), cap, _Alignof(T))
-Vec _Vec_new(Arena *a, int size, ptrdiff_t cap, int align);
+Vec _Vec_new(Arena *a, size tsize, size cap, int 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);