From 53cd9c4c3408b5f2e54e891baf471c0d774ea2cd Mon Sep 17 00:00:00 2001 From: nasr Date: Sat, 14 Mar 2026 21:40:32 +0000 Subject: feature(base): ascii colors, error framework ( ai help ). testing idea meta program. refactor --- source/base/base.h | 8 ++++++++ source/base/base_arena.c | 2 +- source/base/base_error.h | 47 ++++++++++++++++++++++++++++++++++++++++++++++ source/base/base_include.h | 9 +++++++-- source/base/base_io.h | 25 ++++++++++++++++++++++++ source/base/base_os.h | 15 +++++++++++---- source/base/base_string.h | 12 +----------- 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; #define TRUE (1 == 1) #define FALSE (1 != 1) +#define RED "\x1b[31m" +#define GREEN "\x1b[32m" +#define RESET "\x1b[0m" +#define BLUE "\x1b[34m" +#define YELLOW "\x1b[33m" + +#define LEN(s) (sizeof(s) - 1) + #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 } else { - check(0); + verify(0); } return NULL; } 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 @@ +/* base library internal logging system */ +#ifndef BASE_ERROR_H +#define BASE_ERROR_H + +#define error_at(msg) \ + do \ + { \ + os_write(STDERR_FD, RED "[ERROR] ", LEN(RED "[ERROR] ")); \ + write_string(STDERR_FD, __FILE__); \ + write_string(STDERR_FD, ":"); \ + write_int(__LINE__); \ + write_string(STDERR_FD, " in "); \ + write_string(STDERR_FD, __func__); \ + write_string(STDERR_FD, ": "); \ + write_string(STDERR_FD, (msg)); \ + os_write(STDERR_FD, RESET "\n", LEN(RESET "\n")); \ + } while (0) + +#define fatal(msg) \ + do \ + { \ + error_at(msg); \ + _exit(1); \ + } while (0) + +#define assert_msg(expr, msg) \ + do \ + { \ + if (!(expr)) \ + { \ + fatal(msg); \ + } \ + } while (0) + +#define warn(msg) \ + do \ + { \ + os_write(STDERR_FD, YELLOW "[WARN] ", LEN(YELLOW "[WARN] ")); \ + write_string(STDERR_FD, __FILE__); \ + write_string(STDERR_FD, ":"); \ + write_int(__LINE__); \ + write_string(STDERR_FD, ": "); \ + write_string(STDERR_FD, (msg)); \ + os_write(STDERR_FD, RESET "\n", LEN(RESET "\n")); \ + } while (0) + +#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 @@ #include "base_mem.h" #include "base_arena.h" #include "base_stack.h" -#include "base_test.h" #include "base_string.h" + #include "base_io.h" -#include "base_os.h" +#include "base_error.h" +#include "base_test.h" #ifdef BASE_UNITY @@ -26,4 +27,8 @@ #include "base_stack.c" #endif + +#include "base_os.h" + + #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) } +internal void +write_int(s32 num) +{ + + if (num < 0) + { + write(STDERR_FILENO, "-", 1); + num = -num; + } + if (num >= 10) + write_int(num / 10); + char digit = '0' + (num % 10); + + write(STDERR_FILENO, &digit, 1); +} + +internal void +write_string(s32 fd, const char *str) +{ + s32 len = 0; + while (str[len]) len++; + os_write(fd, str, len); +} + + #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 @@ #define BASE_OS_H internal string8 -load_file(const char *path) +load_file(mem_arena *arena, const char *path) { string8 result = {0}; struct stat sbuf = {0}; s32 file = open(path, O_RDONLY); - if(file == -1) return result; + if(file == -1) + { + warn("fialed to open file. path could be invalid"); + return (string8){0}; + } if(fstat(file, &sbuf) == -1) { - print("error: fstat failed"); + warn("error: fstat failed"); close(file); - return result; + return (string8){0}; } + + result = PushString(arena, sbuf.st_size); + result.size = (u64)sbuf.st_size; if(result.size != 0) { 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 @@ #ifndef BASE_STRING_H #define BASE_STRING_H -#define StringLit(string) \ - (string8){ .data = (u8 *)(string), .size = (sizeof(string) - 1) } - -#define PushString(arena, size) \ - ({ \ - string8 *result = PushStruct((arena), string8); \ - result->data = PushArray((arena), u8, (size)); \ - result->size = (u64)(size); \ - result; \ - }) - +#define PushString(arena, count) (string8){ .data = (PushArrayZero(arena, u8, (count))), .size = (count) } #define StringCast(data, size) (string8){(u8 *)(data), (u64)(size) } #define StringPCast(data, size) (string8 *){(u8 *)(data), (u64)(size) } 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 @@ +// TODO(nasr): metaprogram that takes an expected output and generates a test for that specified +// function +/* base library testing framework */ #ifndef BASE_TEST_H #define BASE_TEST_H -#define RED "\x1b[31m" -#define GREEN "\x1b[32m" -#define RESET "\x1b[0m" -#define BLUE "\x1b[34m" - -#define LEN(s) (sizeof(s) - 1) - -internal void -write_int(s32 num) -{ - - if (num < 0) - { - write(STDERR_FILENO, "-", 1); - num = -num; - } - if (num >= 10) - write_int(num / 10); - char digit = '0' + (num % 10); - - write(STDERR_FILENO, &digit, 1); -} - +// helper macro #define show \ do \ { \ @@ -45,7 +26,7 @@ write_int(s32 num) } \ } -#define check(expr) \ +#define verify(expr) \ { \ if ((expr) != 0) \ { \ @@ -62,13 +43,4 @@ write_int(s32 num) } \ } -#define checkpoint_output "<>\n" -#define checkpoint_end_output "^^^^^^^^^^^^^^\n\n\n" -#define checkpoint \ - { \ - write(STDERR_FILENO, BLUE checkpoint_output, LEN(BLUE checkpoint_output)); \ - show; \ - write(STDERR_FILENO, BLUE checkpoint_end_output, LEN(BLUE checkpoint_end_output)); \ - } - #endif /* BASE_TEST_H */ -- cgit v1.3