cgen, stb_truetype defines

This commit is contained in:
Krzosa Karol
2025-01-18 09:18:57 +01:00
parent 63dda7bf13
commit 7e52440e18
7 changed files with 121 additions and 48 deletions

View File

@@ -8,59 +8,20 @@
#include "src/meta/build_tool.c" #include "src/meta/build_tool.c"
#include "src/meta/parser.c" #include "src/meta/parser.c"
#include "src/meta/serialize.c" #include "src/meta/serialize.c"
#include "src/meta/cgen.c"
#include "src/app/app.meta.c" #include "src/app/app.meta.c"
#include "src/wasm_app/ui.meta.c" #include "src/wasm_app/ui.meta.c"
void list_files_recursive(sb8_t *sb, s8_t path) {
for (OS_FileIter iter = OS_IterateFiles(&Perm, path); OS_IsValid(iter); OS_Advance(&iter)) {
if (iter.is_directory) {
list_files_recursive(sb, iter.absolute_path);
} else {
sb8_append(sb, iter.absolute_path);
}
}
}
typedef struct c_file_t c_file_t;
struct c_file_t {
c_file_t *next;
s8_t path;
s8_t content;
};
typedef struct c_files_t c_files_t;
struct c_files_t {
c_file_t *first;
c_file_t *last;
};
int main(int argc, char **argv) { int main(int argc, char **argv) {
cache_init(&Perm, s8_lit("cache_build_file")); cache_init(&Perm, s8_lit("cache_build_file"));
int ok = 0; int ok = 0;
ma_arena_t *arena = ma_create(ma_default_reserve_size); ma_arena_t *arena = ma_create(ma_default_reserve_size);
sb8_t *include_paths = sb8(arena);
sb8_append(include_paths, OS_GetAbsolutePath(&Perm, s8_lit("../src")));
c_files_t files = {0}; cg_files_t files = cg_lex_files(arena, s8_lit("../src/wasm_app/main.c"), include_paths);
{
sb8_t *c = &(sb8_t){.arena = arena};
list_files_recursive(c, s8_lit(".."));
for (sb8_node_t *it = c->first; it; it = it->next) {
s8_t abs = it->string;
b32 is_c_file = s8_ends_with(abs, s8_lit(".c"), true) || s8_ends_with(abs, s8_lit(".cpp"), true) || s8_ends_with(abs, s8_lit(".h"), true) || s8_ends_with(abs, s8_lit(".hpp"), true);
b32 is_build_file = s8_ends_with(abs, s8_lit("build_file.c"), true);
if (!is_c_file) continue;
if (is_build_file) continue;
c_file_t *file = ma_push_type(arena, c_file_t);
file->path = abs;
file->content = OS_ReadFile(&Perm, abs);
SLLQ_APPEND(files.first, files.last, file);
lex_tokens(arena, file->path.str, file->content);
}
}
meta_app(arena); meta_app(arena);
meta_ui(arena); meta_ui(arena);
@@ -70,15 +31,14 @@ int main(int argc, char **argv) {
os_set_working_dir("../../build"); os_set_working_dir("../../build");
} }
b32 execute_python_snippets = false; // make sure to not abuse just for quick maths b32 execute_python_snippets = false; // make sure to not abuse, just for quick maths
b32 run_server = false; b32 run_server = false;
b32 core_test_target = false; b32 core_test_target = false;
b32 win32_target = true; b32 win32_target = true;
b32 wasm_target = false; b32 wasm_target = false;
if (execute_python_snippets) { if (execute_python_snippets) {
for (c_file_t *it = files.first; it; it = it->next) { for (cg_file_t *it = files.first; it; it = it->next) {
i64 idx = s8_find(it->content, s8_lit("/*#"), 0); i64 idx = s8_find(it->content, s8_lit("/*#"), 0);
if (idx != -1) os_systemf("\"D:\\dev\\apps\\bin\\pyorun.bat\" %s", it->path.str); if (idx != -1) os_systemf("\"D:\\dev\\apps\\bin\\pyorun.bat\" %s", it->path.str);
} }

View File

@@ -31,5 +31,7 @@ f32 f32_round(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_roundf(x), roundf
f64 f64_round(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_round(x), round(x)); } f64 f64_round(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_round(x), round(x)); }
f64 f64_mod(f64 a, f64 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_fmod(a, b), fmod(a, b)); } f64 f64_mod(f64 a, f64 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_fmod(a, b), fmod(a, b)); }
f32 f32_mod(f32 a, f32 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_fmodf(a, b), fmodf(a, b)); } f32 f32_mod(f32 a, f32 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_fmodf(a, b), fmodf(a, b)); }
f32 f32_pow(f32 a, f32 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_powf(a, b), powf(a, b)); }
f64 f64_pow(f64 a, f64 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_pow(a, b), pow(a, b)); }

View File

@@ -352,7 +352,6 @@ fn lex_t lex_token(lexer_t *lex) {
return result; return result;
} }
// @todo: use s8_t instead
fn lex_array_t lex_tokens(ma_arena_t *arena, char *file_name, s8_t stream) { fn lex_array_t lex_tokens(ma_arena_t *arena, char *file_name, s8_t stream) {
usize align = arena->align; usize align = arena->align;
arena->align = 0; arena->align = 0;

View File

@@ -122,6 +122,7 @@ fn s8_t s8_printf(ma_arena_t *ma, const char *str, ...);
// //
// string builder // string builder
#define sb8(ARENA) &(sb8_t){.arena = arena}
#define sb8_serial_begin(ARENA) &(sb8_t){.arena = ARENA} #define sb8_serial_begin(ARENA) &(sb8_t){.arena = ARENA}
#define sb8_serial_end(ARENA, SB) sb8_merge(ARENA, SB) #define sb8_serial_end(ARENA, SB) sb8_merge(ARENA, SB)

View File

@@ -1,6 +1,5 @@
#include "core/core_inc.h" #include "core/core_inc.h"
#include "core/core_inc.c" #include "core/core_inc.c"
#include "core/core_clexer.c"
void test_s8(void) { void test_s8(void) {
ma_arena_t *arena = ma_create(ma_default_reserve_size); ma_arena_t *arena = ma_create(ma_default_reserve_size);

98
src/meta/cgen.c Normal file
View File

@@ -0,0 +1,98 @@
typedef struct cg_file_t cg_file_t;
struct cg_file_t {
cg_file_t *next;
s8_t path;
s8_t content;
lex_array_t tokens;
};
typedef struct cg_files_t cg_files_t;
struct cg_files_t {
cg_file_t *first;
cg_file_t *last;
};
fn s8_t cg_resolve_path(ma_arena_t *arena, sb8_t *include_paths, s8_t filename, s8_t parent_file, b32 is_system_include) {
if (OS_IsAbsolute(filename) && os_file_exists(filename)) {
return filename;
}
// 1) (QUOTED_FORM) In the same directory as the file that contains the #include statement.
if (!is_system_include && parent_file.len) {
s8_t path = s8_printf(arena, "%S/%S", s8_chop_last_slash(parent_file), filename);
if (os_file_exists(path)) {
return OS_GetAbsolutePath(&Perm, path);
}
}
// 2) (QUOTED FORM) In the directories of the currently opened include files, in the reverse order in which they were opened. The search begins in the directory of the parent include file and continues upward through the directories of any grandparent include files.
// 3) (BOTH FORMS) Along the path that's specified by each /I (or INCLUDE enviroment variable) compiler option.
for (sb8_node_t *it = include_paths->first; it; it = it->next) {
s8_t path = s8_printf(arena, "%S/%S", it->string, filename);
if (os_file_exists(path)) {
return OS_GetAbsolutePath(&Perm, path);
}
}
return s8_null;
}
fn cg_file_t *cg_find_file_exact(cg_files_t *root, s8_t path) {
for (cg_file_t *it = root->first; it; it = it->next) {
if (s8_are_equal(path, it->path)) return it;
}
return NULL;
}
fn cg_file_t *cg_find_file(cg_files_t *root, s8_t name) {
for (cg_file_t *it = root->first; it; it = it->next) {
if (s8_ends_with(name, it->path, false)) return it;
}
return NULL;
}
fn void cg__lex_files(ma_arena_t *arena, cg_files_t *root, s8_t path, sb8_t *include_paths) {
s8_t content = OS_ReadFile(&Perm, path);
if (content.len == 0) {
return;
}
lex_array_t array = lex_tokens(arena, path.str, content);
cg_file_t *file = ma_push_type(arena, cg_file_t);
file->tokens = array;
file->content = content;
file->path = path;
SLLQ_APPEND(root->first, root->last, file);
for (i32 i = 0; i < array.len; i += 1) {
lex_t *token = array.data + i;
if (token->kind != lex_kind_preproc_include) {
continue;
}
s8_t inc_path = cg_resolve_path(arena, include_paths, token->string, s8_from_char(token->file), token->system_include);
if (inc_path.len && cg_find_file_exact(root, inc_path) == NULL) {
// debugf("%s:%d %S, %S", token->file, token->line + 1, token->string, inc_path);
cg__lex_files(arena, root, inc_path, include_paths);
}
}
}
fn cg_files_t cg_lex_files(ma_arena_t *arena, s8_t path, sb8_t *include_paths) {
cg_files_t files = {0};
path = OS_GetAbsolutePath(&Perm, path);
cg__lex_files(arena, &files, path, include_paths);
return files;
}
fn void cg_list_files_recursive(sb8_t *sb, s8_t path) {
for (OS_FileIter iter = OS_IterateFiles(&Perm, path); OS_IsValid(iter); OS_Advance(&iter)) {
if (iter.is_directory) {
cg_list_files_recursive(sb, iter.absolute_path);
} else {
sb8_append(sb, iter.absolute_path);
}
}
}

View File

@@ -6,6 +6,20 @@
#include "app/app.c" #include "app/app.c"
#define STB_TRUETYPE_IMPLEMENTATION #define STB_TRUETYPE_IMPLEMENTATION
#define STBTT_ifloor(x) ((int)f64_floor(x))
#define STBTT_iceil(x) ((int)f64_ceil(x))
#define STBTT_sqrt(x) (f64_sqrt(x))
#define STBTT_pow(x,y) (f64_pow(x,y))
#define STBTT_fmod(x,y) (f64_mod(x,y))
#define STBTT_cos(x) (f64_cos(x))
#define STBTT_acos(x) (f64_acos(x))
#define STBTT_fabs(x) (f64_abs(x))
#define STBTT_assert(x) (assert(x))
#define STBTT_malloc(x,u) (ma_push_size(tcx.temp, x))
#define STBTT_free(x,u)
#define STBTT_strlen(x) (str_len(x))
#define STBTT_memcpy memory_copy
#define STBTT_memset memory_set
#include "render/stb_truetype.h" #include "render/stb_truetype.h"
#include "render/backup_font.c" #include "render/backup_font.c"
#include "render/font.c" #include "render/font.c"