diff options
| author | nasr <nsrddyn@gmail.com> | 2026-03-14 21:40:32 +0000 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-03-14 21:40:32 +0000 |
| commit | 53cd9c4c3408b5f2e54e891baf471c0d774ea2cd (patch) | |
| tree | 7fcdfdd25b7e163b40fe3d2961a605b3a2720ba4 /source | |
| parent | e51d57e2da70ea2688a72602aeb5f7c4040e72b7 (diff) | |
feature(base): ascii colors, error framework ( ai help ). testing idea meta program. refactor
Diffstat (limited to 'source')
| -rwxr-xr-x | source/base/base.h | 8 | ||||
| -rwxr-xr-x | source/base/base_arena.c | 2 | ||||
| -rw-r--r-- | source/base/base_error.h | 47 | ||||
| -rwxr-xr-x | source/base/base_include.h | 9 | ||||
| -rw-r--r-- | source/base/base_io.h | 25 | ||||
| -rw-r--r-- | source/base/base_os.h | 15 | ||||
| -rw-r--r-- | source/base/base_string.h | 12 | ||||
| -rw-r--r-- | source/base/base_test.h | 38 |
8 files changed, 105 insertions, 51 deletions
diff --git a/source/base/base.h b/source/base/base.h index cf2a15f..e1af950 100755 --- a/source/base/base.h +++ b/source/base/base.h | |||
| @@ -70,4 +70,12 @@ typedef intptr_t smm; | |||
| 70 | #define TRUE (1 == 1) | 70 | #define TRUE (1 == 1) |
| 71 | #define FALSE (1 != 1) | 71 | #define FALSE (1 != 1) |
| 72 | 72 | ||
| 73 | #define RED "\x1b[31m" | ||
| 74 | #define GREEN "\x1b[32m" | ||
| 75 | #define RESET "\x1b[0m" | ||
| 76 | #define BLUE "\x1b[34m" | ||
| 77 | #define YELLOW "\x1b[33m" | ||
| 78 | |||
| 79 | #define LEN(s) (sizeof(s) - 1) | ||
| 80 | |||
| 73 | #endif | 81 | #endif |
diff --git a/source/base/base_arena.c b/source/base/base_arena.c index 5855e5e..7e9c44b 100755 --- a/source/base/base_arena.c +++ b/source/base/base_arena.c | |||
| @@ -110,7 +110,7 @@ arena_resize_align(mem_arena *arena, void *old_memory, u64 new_size, u64 old_siz | |||
| 110 | } | 110 | } |
| 111 | else | 111 | else |
| 112 | { | 112 | { |
| 113 | check(0); | 113 | verify(0); |
| 114 | } | 114 | } |
| 115 | return NULL; | 115 | return NULL; |
| 116 | } | 116 | } |
diff --git a/source/base/base_error.h b/source/base/base_error.h index e69de29..b2bb4c0 100644 --- a/source/base/base_error.h +++ b/source/base/base_error.h | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | /* base library internal logging system */ | ||
| 2 | #ifndef BASE_ERROR_H | ||
| 3 | #define BASE_ERROR_H | ||
| 4 | |||
| 5 | #define error_at(msg) \ | ||
| 6 | do \ | ||
| 7 | { \ | ||
| 8 | os_write(STDERR_FD, RED "[ERROR] ", LEN(RED "[ERROR] ")); \ | ||
| 9 | write_string(STDERR_FD, __FILE__); \ | ||
| 10 | write_string(STDERR_FD, ":"); \ | ||
| 11 | write_int(__LINE__); \ | ||
| 12 | write_string(STDERR_FD, " in "); \ | ||
| 13 | write_string(STDERR_FD, __func__); \ | ||
| 14 | write_string(STDERR_FD, ": "); \ | ||
| 15 | write_string(STDERR_FD, (msg)); \ | ||
| 16 | os_write(STDERR_FD, RESET "\n", LEN(RESET "\n")); \ | ||
| 17 | } while (0) | ||
| 18 | |||
| 19 | #define fatal(msg) \ | ||
| 20 | do \ | ||
| 21 | { \ | ||
| 22 | error_at(msg); \ | ||
| 23 | _exit(1); \ | ||
| 24 | } while (0) | ||
| 25 | |||
| 26 | #define assert_msg(expr, msg) \ | ||
| 27 | do \ | ||
| 28 | { \ | ||
| 29 | if (!(expr)) \ | ||
| 30 | { \ | ||
| 31 | fatal(msg); \ | ||
| 32 | } \ | ||
| 33 | } while (0) | ||
| 34 | |||
| 35 | #define warn(msg) \ | ||
| 36 | do \ | ||
| 37 | { \ | ||
| 38 | os_write(STDERR_FD, YELLOW "[WARN] ", LEN(YELLOW "[WARN] ")); \ | ||
| 39 | write_string(STDERR_FD, __FILE__); \ | ||
| 40 | write_string(STDERR_FD, ":"); \ | ||
| 41 | write_int(__LINE__); \ | ||
| 42 | write_string(STDERR_FD, ": "); \ | ||
| 43 | write_string(STDERR_FD, (msg)); \ | ||
| 44 | os_write(STDERR_FD, RESET "\n", LEN(RESET "\n")); \ | ||
| 45 | } while (0) | ||
| 46 | |||
| 47 | #endif /* BASE_ERROR_H */ | ||
diff --git a/source/base/base_include.h b/source/base/base_include.h index 07856d0..c8eec41 100755 --- a/source/base/base_include.h +++ b/source/base/base_include.h | |||
| @@ -15,10 +15,11 @@ | |||
| 15 | #include "base_mem.h" | 15 | #include "base_mem.h" |
| 16 | #include "base_arena.h" | 16 | #include "base_arena.h" |
| 17 | #include "base_stack.h" | 17 | #include "base_stack.h" |
| 18 | #include "base_test.h" | ||
| 19 | #include "base_string.h" | 18 | #include "base_string.h" |
| 19 | |||
| 20 | #include "base_io.h" | 20 | #include "base_io.h" |
| 21 | #include "base_os.h" | 21 | #include "base_error.h" |
| 22 | #include "base_test.h" | ||
| 22 | 23 | ||
| 23 | #ifdef BASE_UNITY | 24 | #ifdef BASE_UNITY |
| 24 | 25 | ||
| @@ -26,4 +27,8 @@ | |||
| 26 | #include "base_stack.c" | 27 | #include "base_stack.c" |
| 27 | 28 | ||
| 28 | #endif | 29 | #endif |
| 30 | |||
| 31 | #include "base_os.h" | ||
| 32 | |||
| 33 | |||
| 29 | #endif | 34 | #endif |
diff --git a/source/base/base_io.h b/source/base/base_io.h index a90b41e..ac55737 100644 --- a/source/base/base_io.h +++ b/source/base/base_io.h | |||
| @@ -32,4 +32,29 @@ print(const char *str) | |||
| 32 | 32 | ||
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | internal void | ||
| 36 | write_int(s32 num) | ||
| 37 | { | ||
| 38 | |||
| 39 | if (num < 0) | ||
| 40 | { | ||
| 41 | write(STDERR_FILENO, "-", 1); | ||
| 42 | num = -num; | ||
| 43 | } | ||
| 44 | if (num >= 10) | ||
| 45 | write_int(num / 10); | ||
| 46 | char digit = '0' + (num % 10); | ||
| 47 | |||
| 48 | write(STDERR_FILENO, &digit, 1); | ||
| 49 | } | ||
| 50 | |||
| 51 | internal void | ||
| 52 | write_string(s32 fd, const char *str) | ||
| 53 | { | ||
| 54 | s32 len = 0; | ||
| 55 | while (str[len]) len++; | ||
| 56 | os_write(fd, str, len); | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 35 | #endif /* BASE_IO_H */ | 60 | #endif /* BASE_IO_H */ |
diff --git a/source/base/base_os.h b/source/base/base_os.h index abe8628..9397fbb 100644 --- a/source/base/base_os.h +++ b/source/base/base_os.h | |||
| @@ -2,21 +2,28 @@ | |||
| 2 | #define BASE_OS_H | 2 | #define BASE_OS_H |
| 3 | 3 | ||
| 4 | internal string8 | 4 | internal string8 |
| 5 | load_file(const char *path) | 5 | load_file(mem_arena *arena, const char *path) |
| 6 | { | 6 | { |
| 7 | string8 result = {0}; | 7 | string8 result = {0}; |
| 8 | struct stat sbuf = {0}; | 8 | struct stat sbuf = {0}; |
| 9 | 9 | ||
| 10 | s32 file = open(path, O_RDONLY); | 10 | s32 file = open(path, O_RDONLY); |
| 11 | if(file == -1) return result; | 11 | if(file == -1) |
| 12 | { | ||
| 13 | warn("fialed to open file. path could be invalid"); | ||
| 14 | return (string8){0}; | ||
| 15 | } | ||
| 12 | 16 | ||
| 13 | if(fstat(file, &sbuf) == -1) | 17 | if(fstat(file, &sbuf) == -1) |
| 14 | { | 18 | { |
| 15 | print("error: fstat failed"); | 19 | warn("error: fstat failed"); |
| 16 | close(file); | 20 | close(file); |
| 17 | return result; | 21 | return (string8){0}; |
| 18 | } | 22 | } |
| 19 | 23 | ||
| 24 | |||
| 25 | result = PushString(arena, sbuf.st_size); | ||
| 26 | |||
| 20 | result.size = (u64)sbuf.st_size; | 27 | result.size = (u64)sbuf.st_size; |
| 21 | if(result.size != 0) | 28 | if(result.size != 0) |
| 22 | { | 29 | { |
diff --git a/source/base/base_string.h b/source/base/base_string.h index ef2cf16..29ccf1e 100644 --- a/source/base/base_string.h +++ b/source/base/base_string.h | |||
| @@ -1,17 +1,7 @@ | |||
| 1 | #ifndef BASE_STRING_H | 1 | #ifndef BASE_STRING_H |
| 2 | #define BASE_STRING_H | 2 | #define BASE_STRING_H |
| 3 | 3 | ||
| 4 | #define StringLit(string) \ | 4 | #define PushString(arena, count) (string8){ .data = (PushArrayZero(arena, u8, (count))), .size = (count) } |
| 5 | (string8){ .data = (u8 *)(string), .size = (sizeof(string) - 1) } | ||
| 6 | |||
| 7 | #define PushString(arena, size) \ | ||
| 8 | ({ \ | ||
| 9 | string8 *result = PushStruct((arena), string8); \ | ||
| 10 | result->data = PushArray((arena), u8, (size)); \ | ||
| 11 | result->size = (u64)(size); \ | ||
| 12 | result; \ | ||
| 13 | }) | ||
| 14 | |||
| 15 | #define StringCast(data, size) (string8){(u8 *)(data), (u64)(size) } | 5 | #define StringCast(data, size) (string8){(u8 *)(data), (u64)(size) } |
| 16 | #define StringPCast(data, size) (string8 *){(u8 *)(data), (u64)(size) } | 6 | #define StringPCast(data, size) (string8 *){(u8 *)(data), (u64)(size) } |
| 17 | 7 | ||
diff --git a/source/base/base_test.h b/source/base/base_test.h index 7619cfb..fd47abc 100644 --- a/source/base/base_test.h +++ b/source/base/base_test.h | |||
| @@ -1,29 +1,10 @@ | |||
| 1 | // TODO(nasr): metaprogram that takes an expected output and generates a test for that specified | ||
| 2 | // function | ||
| 3 | /* base library testing framework */ | ||
| 1 | #ifndef BASE_TEST_H | 4 | #ifndef BASE_TEST_H |
| 2 | #define BASE_TEST_H | 5 | #define BASE_TEST_H |
| 3 | 6 | ||
| 4 | #define RED "\x1b[31m" | 7 | // helper macro |
| 5 | #define GREEN "\x1b[32m" | ||
| 6 | #define RESET "\x1b[0m" | ||
| 7 | #define BLUE "\x1b[34m" | ||
| 8 | |||
| 9 | #define LEN(s) (sizeof(s) - 1) | ||
| 10 | |||
| 11 | internal void | ||
| 12 | write_int(s32 num) | ||
| 13 | { | ||
| 14 | |||
| 15 | if (num < 0) | ||
| 16 | { | ||
| 17 | write(STDERR_FILENO, "-", 1); | ||
| 18 | num = -num; | ||
| 19 | } | ||
| 20 | if (num >= 10) | ||
| 21 | write_int(num / 10); | ||
| 22 | char digit = '0' + (num % 10); | ||
| 23 | |||
| 24 | write(STDERR_FILENO, &digit, 1); | ||
| 25 | } | ||
| 26 | |||
| 27 | #define show \ | 8 | #define show \ |
| 28 | do \ | 9 | do \ |
| 29 | { \ | 10 | { \ |
| @@ -45,7 +26,7 @@ write_int(s32 num) | |||
| 45 | } \ | 26 | } \ |
| 46 | } | 27 | } |
| 47 | 28 | ||
| 48 | #define check(expr) \ | 29 | #define verify(expr) \ |
| 49 | { \ | 30 | { \ |
| 50 | if ((expr) != 0) \ | 31 | if ((expr) != 0) \ |
| 51 | { \ | 32 | { \ |
| @@ -62,13 +43,4 @@ write_int(s32 num) | |||
| 62 | } \ | 43 | } \ |
| 63 | } | 44 | } |
| 64 | 45 | ||
| 65 | #define checkpoint_output "<<CHECKPOINT>>\n" | ||
| 66 | #define checkpoint_end_output "^^^^^^^^^^^^^^\n\n\n" | ||
| 67 | #define checkpoint \ | ||
| 68 | { \ | ||
| 69 | write(STDERR_FILENO, BLUE checkpoint_output, LEN(BLUE checkpoint_output)); \ | ||
| 70 | show; \ | ||
| 71 | write(STDERR_FILENO, BLUE checkpoint_end_output, LEN(BLUE checkpoint_end_output)); \ | ||
| 72 | } | ||
| 73 | |||
| 74 | #endif /* BASE_TEST_H */ | 46 | #endif /* BASE_TEST_H */ |
