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

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);
}
}
}