diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/json.h | 34 | ||||
| -rw-r--r-- | include/log.h | 10 | ||||
| -rw-r--r-- | include/memory.h | 16 | ||||
| -rw-r--r-- | include/str.h | 18 | ||||
| -rw-r--r-- | include/testing.h | 24 | ||||
| -rw-r--r-- | include/types.h | 14 | ||||
| -rw-r--r-- | include/vec.h | 19 |
7 files changed, 135 insertions, 0 deletions
diff --git a/include/json.h b/include/json.h new file mode 100644 index 0000000..749a3e0 --- /dev/null +++ b/include/json.h @@ -0,0 +1,34 @@ +#pragma once +#include "../include/memory.h" +#include "../include/str.h" +#include "../include/vec.h" + +typedef struct { + Vec keys; // Vec<Str> + Vec values; // Vec<JSON> + size len; +} JSONObject; +typedef enum { + JSON_NULL, + JSON_BOOL, + JSON_INTEGER, + JSON_FLOAT, + JSON_STRING, + JSON_ARRAY, + JSON_OBJECT +} JSONType; +typedef struct { + JSONType type; + union { + bool boolean; + i32 integer; + f32 floating; + Vec array; // Vec<JSON> + Str string; + JSONObject object; + } as; +} JSON; +JSON *JSONObject_get(JSONObject obj, Str key); +i32 JSON_fprint(FILE *w, JSON json); +JSON JSON_parse(Arena *a, byte *src); +JSON JSON_Int(Arena *a, i32 val); diff --git a/include/log.h b/include/log.h new file mode 100644 index 0000000..1458f03 --- /dev/null +++ b/include/log.h @@ -0,0 +1,10 @@ + +#include "types.h" + +typedef enum { INFO } LogLevel; + +typedef struct { + LogLevel level; +} Logger; + +void info(Logger l, const byte *name, const byte *str, ...); diff --git a/include/memory.h b/include/memory.h new file mode 100644 index 0000000..d202e97 --- /dev/null +++ b/include/memory.h @@ -0,0 +1,16 @@ +#pragma once +#include <stddef.h> + +#include "types.h" + +typedef struct { + size cap; + byte *beg; + byte *end; +} Arena; + +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 tsize, size align); +size Arena_get_used(Arena a); +void Arena_reset(Arena *a); diff --git a/include/str.h b/include/str.h new file mode 100644 index 0000000..300d5ea --- /dev/null +++ b/include/str.h @@ -0,0 +1,18 @@ +#pragma once +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> + +#include "../include/memory.h" +#include "../include/types.h" + +#define S(s) \ + (Str) { s, lengthof(s) } +typedef struct { + byte *data; + size len; +} Str; +i32 Str_equals(Str a, Str b); +u64 Str_hash64(Str s); +Str Str_copy(Arena *a, byte *data, size len); +i32 Str_fprint(FILE *w, Str s); diff --git a/include/testing.h b/include/testing.h new file mode 100644 index 0000000..9e0c022 --- /dev/null +++ b/include/testing.h @@ -0,0 +1,24 @@ +#include <stdio.h> + +#define t_assert(fact) \ + if (!(fact)) { \ + fprintf(stderr, "Assertion failed: %s at %s:%d in %s()\n", #fact, \ + __FILE__, __LINE__, __func__); \ + return 1; \ + } + +#define t_assert_equal(a, b) \ + if (!(a == b)) { \ + fprintf(stderr, \ + "Assertion failed: %d (%s) != %d (%s) at %s:%d in %s()\n", \ + (i32)a, #a, (i32)b, #b, __FILE__, __LINE__, __func__); \ + return 1; \ + } + +#define t_assert_fequal(a, b) \ + if ((a - b) > 1e-6 || (b - a) > 1e-6) { \ + fprintf(stderr, \ + "Assertion failed: %f (%s) != %f (%s) at %s:%d in %s()\n", a, \ + #a, b, #b, __FILE__, __LINE__, __func__); \ + return 1; \ + } diff --git a/include/types.h b/include/types.h new file mode 100644 index 0000000..bacedf8 --- /dev/null +++ b/include/types.h @@ -0,0 +1,14 @@ +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +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))) +#define lengthof(s) (countof(s) - 1) diff --git a/include/vec.h b/include/vec.h new file mode 100644 index 0000000..bf6729b --- /dev/null +++ b/include/vec.h @@ -0,0 +1,19 @@ +#pragma once +#include <stddef.h> +#include <stdint.h> + +#include "../include/memory.h" +#include "../include/types.h" + +typedef struct { + void *data; + size len; + size cap; + 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, 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, size pos); |
