aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--CMakeLists.txt23
-rw-r--r--Makefile2
-rw-r--r--README.md2
-rw-r--r--base_test.c2
-rw-r--r--generate.fish2
-rw-r--r--include/json.h (renamed from json.h)10
-rw-r--r--include/log.h (renamed from log.h)0
-rw-r--r--include/memory.h (renamed from memory.h)5
-rw-r--r--include/str.h (renamed from string.h)8
-rw-r--r--include/testing.h (renamed from testing.h)0
-rw-r--r--include/types.h (renamed from types.h)0
-rw-r--r--include/vec.h (renamed from vec.h)8
-rw-r--r--main.c6
-rw-r--r--src/json.c (renamed from json.c)8
-rw-r--r--src/log.c (renamed from log.c)7
-rw-r--r--src/memory.c (renamed from memory.c)4
-rw-r--r--src/str.c (renamed from string.c)5
-rw-r--r--src/vec.c (renamed from vec.c)6
-rw-r--r--tests/test_json.c (renamed from test_json.c)4
-rw-r--r--tests/test_string.c (renamed from test_string.c)4
-rw-r--r--tests/test_vec.c (renamed from test_vec.c)4
22 files changed, 43 insertions, 68 deletions
diff --git a/.gitignore b/.gitignore
index c7a86e2..2ec9e4f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@ Debug/
Release/
test.c
+out.json
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c022fc3..a5fd18e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,26 +2,13 @@ cmake_minimum_required(VERSION 3.30.5)
project(kagamic VERSION 0.1 LANGUAGES C)
set(CMAKE_C_STANDARD 17)
-add_executable(main
- main.c
- string.h string.c
- memory.h memory.c
- vec.h vec.c
- json.h json.c
- log.h log.c
-)
+file(GLOB SOURCES "src/*.c")
+file(GLOB TEST_SOURCES "test/*.c")
+
+add_executable(main main.c ${SOURCES})
target_link_libraries(main m)
target_compile_options(main PRIVATE -Wall -pedantic -Wextra)
-
-add_executable(test
- test.c
- testing.h
- string.h string.c
- memory.h memory.c
- vec.h vec.c
- json.h json.c
- log.h log.c
-)
+add_executable(test test.c ${SOURCES} ${TEST_SOURCES})
target_link_libraries(test m)
target_compile_options(test PRIVATE -Wall -pedantic -Wextra)
diff --git a/Makefile b/Makefile
index 234128c..2239241 100644
--- a/Makefile
+++ b/Makefile
@@ -16,4 +16,4 @@ test:
./Debug/test;
format:
- clang-format -i *.c *.h
+ clang-format -i src/*.c include/*.h tests/*.c *.c
diff --git a/README.md b/README.md
index d9cdf05..18c997c 100644
--- a/README.md
+++ b/README.md
@@ -7,8 +7,6 @@
- Benchmark 100MB file
- json.loads: 526 ms ± 77 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
- self: 348 ms (mean of 7 runs, 1 loop each). 2079 MB/s
-- More modern include pragma (once)
-- File structure
- Static functions for private?
- `valgrind --leak-check=full ./your_program`
- assert with error msg?
diff --git a/base_test.c b/base_test.c
index 6d095f8..a06aab4 100644
--- a/base_test.c
+++ b/base_test.c
@@ -1,4 +1,4 @@
-#include "types.h"
+#include "include/types.h"
#define test(func) \
switch (func()) { \
diff --git a/generate.fish b/generate.fish
index b0e5308..96f8542 100644
--- a/generate.fish
+++ b/generate.fish
@@ -1,5 +1,5 @@
echo "" > test.c;
-set fs (ls test_*.c);
+set fs (ls tests/test_*.c);
for f in $fs;
echo "#include \"$f\"" >> test.c;
end;
diff --git a/json.h b/include/json.h
index 60e853c..749a3e0 100644
--- a/json.h
+++ b/include/json.h
@@ -1,8 +1,7 @@
-#ifndef JSON_H
-#define JSON_H
-#include "memory.h"
-#include "string.h"
-#include "vec.h"
+#pragma once
+#include "../include/memory.h"
+#include "../include/str.h"
+#include "../include/vec.h"
typedef struct {
Vec keys; // Vec<Str>
@@ -33,4 +32,3 @@ 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);
-#endif
diff --git a/log.h b/include/log.h
index 1458f03..1458f03 100644
--- a/log.h
+++ b/include/log.h
diff --git a/memory.h b/include/memory.h
index 1a33452..d202e97 100644
--- a/memory.h
+++ b/include/memory.h
@@ -1,8 +1,8 @@
+#pragma once
#include <stddef.h>
-#ifndef MEMORY_H
#include "types.h"
-#define MEMORY_H
+
typedef struct {
size cap;
byte *beg;
@@ -14,4 +14,3 @@ Arena Arena_new(size cap);
void *alloc(Arena *a, size count, size tsize, size align);
size Arena_get_used(Arena a);
void Arena_reset(Arena *a);
-#endif
diff --git a/string.h b/include/str.h
index a4c1e1a..300d5ea 100644
--- a/string.h
+++ b/include/str.h
@@ -1,12 +1,11 @@
+#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
-#include "memory.h"
-#include "types.h"
+#include "../include/memory.h"
+#include "../include/types.h"
-#ifndef STRING_H
-#define STRING_H
#define S(s) \
(Str) { s, lengthof(s) }
typedef struct {
@@ -17,4 +16,3 @@ 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);
-#endif
diff --git a/testing.h b/include/testing.h
index 9e0c022..9e0c022 100644
--- a/testing.h
+++ b/include/testing.h
diff --git a/types.h b/include/types.h
index bacedf8..bacedf8 100644
--- a/types.h
+++ b/include/types.h
diff --git a/vec.h b/include/vec.h
index 1d1999b..bf6729b 100644
--- a/vec.h
+++ b/include/vec.h
@@ -1,11 +1,10 @@
+#pragma once
#include <stddef.h>
#include <stdint.h>
-#include "memory.h"
-#include "types.h"
+#include "../include/memory.h"
+#include "../include/types.h"
-#ifndef VEC_H
-#define VEC_H
typedef struct {
void *data;
size len;
@@ -18,4 +17,3 @@ 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);
-#endif
diff --git a/main.c b/main.c
index 19e8c20..01df170 100644
--- a/main.c
+++ b/main.c
@@ -3,9 +3,9 @@
#include <sys/stat.h>
#include <time.h>
-#include "json.h"
-#include "memory.h"
-#include "types.h"
+#include "include/json.h"
+#include "include/memory.h"
+#include "include/types.h"
// Load entire file into memory
byte *load_file(const char *filename, size *file_size) {
diff --git a/json.c b/src/json.c
index b02c413..6f00da8 100644
--- a/json.c
+++ b/src/json.c
@@ -1,5 +1,3 @@
-#include "json.h"
-
#include <assert.h>
#include <ctype.h>
#include <stddef.h>
@@ -7,9 +5,9 @@
#include <stdio.h>
#include <string.h>
-#include "json.h"
-#include "string.h"
-#include "types.h"
+#include "../include/json.h"
+#include "../include/str.h"
+#include "../include/types.h"
JSONObject *JSONObject_new(Arena *a) {
JSONObject *object = new(a, JSONObject, 1);
diff --git a/log.c b/src/log.c
index 9cff1a0..c1d1717 100644
--- a/log.c
+++ b/src/log.c
@@ -1,13 +1,12 @@
-#include "log.h"
-
#include <stdio.h>
-#include "types.h"
+#include "../include/log.h"
+#include "../include/types.h"
void info(Logger l, const byte *name, const byte *str, ...) {
if (l.level >= INFO) {
printf("[%s] ", name);
- printf(str);
+ printf("%s", str);
printf("\n");
}
}
diff --git a/memory.c b/src/memory.c
index f60f044..63acb39 100644
--- a/memory.c
+++ b/src/memory.c
@@ -3,8 +3,8 @@
#include <stdlib.h>
#include <string.h>
-#include "memory.h"
-#include "types.h"
+#include "../include/memory.h"
+#include "../include/types.h"
Arena Arena_new(size cap) {
Arena a = {0};
diff --git a/string.c b/src/str.c
index 6246a17..eccacd8 100644
--- a/string.c
+++ b/src/str.c
@@ -1,10 +1,9 @@
-#include "string.h"
-
#include <stdint.h>
#include <stdio.h>
#include <string.h>
-#include "memory.h"
+#include "../include/memory.h"
+#include "../include/str.h"
// Returns 1 if strings are equal, 0 otherwise
i32 Str_equals(Str a, Str b) {
diff --git a/vec.c b/src/vec.c
index 3dfbf31..a5c2fb4 100644
--- a/vec.c
+++ b/src/vec.c
@@ -1,8 +1,8 @@
#include <string.h>
-#include "assert.h"
-#include "types.h"
-#include "vec.h"
+#include "../include/assert.h"
+#include "../include/types.h"
+#include "../include/vec.h"
Vec _Vec_new(Arena *a, size tsize, size cap, size align) {
assert(cap > 0);
diff --git a/test_json.c b/tests/test_json.c
index a37996c..4ef3d5d 100644
--- a/test_json.c
+++ b/tests/test_json.c
@@ -1,5 +1,5 @@
-#include "json.h"
-#include "testing.h"
+#include "../include/json.h"
+#include "../include/testing.h"
i32 test_json_integer() {
Arena perm = Arena_new(128e3);
diff --git a/test_string.c b/tests/test_string.c
index 0d6dd71..1baab6a 100644
--- a/test_string.c
+++ b/tests/test_string.c
@@ -1,5 +1,5 @@
-#include "string.h"
-#include "testing.h"
+#include "../include/string.h"
+#include "../include/testing.h"
i32 test_string_literal_equality() {
t_assert(Str_equals(S("Bonjour"), S("Bonjour")));
diff --git a/test_vec.c b/tests/test_vec.c
index 180ba41..68cb0ce 100644
--- a/test_vec.c
+++ b/tests/test_vec.c
@@ -1,5 +1,5 @@
-#include "testing.h"
-#include "vec.h"
+#include "../include/testing.h"
+#include "../include/vec.h"
i32 test_int_vec() {
Arena perm = Arena_new(128e3);