From adb30cf24d64703afdfa0ea4193431c538f7ab16 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 21 Jan 2025 16:02:09 +0100 Subject: [PATCH] working on template stuff, add log flush --- src/core/core_lexer.c | 1 + src/core/core_lexer.h | 1 + src/core/core_platform_unix.c | 4 +- src/core/core_platform_win32.c | 2 + src/meta/meta_table_format.c | 33 ++++++++- src/ui/ui.gen.c | 2 +- src/ui/ui.gen.h | 1 - src/ui/ui.meta.c | 119 ++++++++++++++------------------- 8 files changed, 91 insertions(+), 72 deletions(-) diff --git a/src/core/core_lexer.c b/src/core/core_lexer.c index 92df061..b1020fc 100644 --- a/src/core/core_lexer.c +++ b/src/core/core_lexer.c @@ -209,6 +209,7 @@ fn void lex_token_ex(lexer_t *lex, lex_t *token) { case ',': token->kind = lex_kind_comma; break; case '@': token->kind = lex_kind_tag; break; case '?': token->kind = lex_kind_question; break; + case '\\': token->kind = lex_kind_escape; break; case '"': lex_eat_string(lex, token); break; case '`': lex_eat_string(lex, token); break; case '\'': lex_eat_string(lex, token); break; diff --git a/src/core/core_lexer.h b/src/core/core_lexer.h index dfb2c9b..53b4464 100644 --- a/src/core/core_lexer.h +++ b/src/core/core_lexer.h @@ -57,6 +57,7 @@ enum lex_kind_t { X(lex_kind_arrow , "'->' arrow" , "->" )\ X(lex_kind_question , "'?' question mark" , "?" )\ X(lex_kind_tag , "'@' tag sign" , "@" )\ + X(lex_kind_escape , "'\' escape" , "\\" )\ X(lex_kind_preproc_null , "preproc_null" , "---" )\ X(lex_kind_preproc_define , "preproc_define" , "---" )\ X(lex_kind_preproc_ifdef , "preproc_ifdef" , "---" )\ diff --git a/src/core/core_platform_unix.c b/src/core/core_platform_unix.c index 9c67310..d30e0e1 100644 --- a/src/core/core_platform_unix.c +++ b/src/core/core_platform_unix.c @@ -21,10 +21,12 @@ fn b32 os_vmem_decommit(void *p, usize size) { fn void os_error_box(char *str) { fprintf(stderr, "%s", str); + fflush(stderr); } fn void os_console_log(char *str) { - puts(str); + fputs(str, stdout); + fflush(stdout); } fn f64 os_parse_float(char *str) { diff --git a/src/core/core_platform_win32.c b/src/core/core_platform_win32.c index f79caa4..fac687e 100644 --- a/src/core/core_platform_win32.c +++ b/src/core/core_platform_win32.c @@ -20,11 +20,13 @@ fn b32 os_vmem_decommit(void *p, usize size) { fn void os_error_box(char *str) { MessageBoxA(NULL, str, "fatal error", MB_OK); fprintf(stderr, "%s", str); + fflush(stderr); } fn void os_console_log(char *str) { OutputDebugStringA(str); fputs(str, stdout); + fflush(stdout); } fn f64 os_parse_float(char *str) { diff --git a/src/meta/meta_table_format.c b/src/meta/meta_table_format.c index a0b9b6e..44d1365 100644 --- a/src/meta/meta_table_format.c +++ b/src/meta/meta_table_format.c @@ -132,15 +132,44 @@ fn s8_t mt_printf(ma_arena_t *arena, ast_t *n, char *str, ...) { return result; } -fn s8_t mt_sbprintf(sb8_t *sb, char *str, ...) { +fn s8_t mt_sbprint(sb8_t *sb, s8_t string) { assert(sb->user_data); ma_temp_t scratch = ma_begin_scratch1(sb->arena); - S8_FMT(scratch.arena, str, string); s8_t result = mt_print(sb->arena, (ast_t *)sb->user_data, string); sb8_append(sb, result); ma_end_scratch(scratch); return result; } + +fn s8_t mt_sbprintf(sb8_t *sb, char *str, ...) { + ma_temp_t scratch = ma_begin_scratch1(sb->arena); + S8_FMT(scratch.arena, str, string); + s8_t result = mt_sbprint(sb, string); + ma_end_scratch(scratch); + return result; +} + +fn s8_t mt_stmtf(sb8_t *sb, char *str, ...) { + sb8_indent(sb); + ma_temp_t scratch = ma_begin_scratch1(sb->arena); + S8_FMT(scratch.arena, str, string); + s8_t result = mt_sbprint(sb, string); + ma_end_scratch(scratch); + return result; +} + +fn ast_t *mt_ast_string(ma_arena_t *arena, s8_t string) { + ast_t *n = mt_create_ast(arena, &lex_null, set_bit(ast_flag_string)); + n->string = string; + return n; +} + +fn ast_t *mt_kv(ma_arena_t *arena, s8_t key, s8_t value) { + ast_t *n = mt_ast_string(arena, key); + mt_ast_append(n, mt_ast_string(arena, value)); + return n; +} + fn void mtt_serialb(sb8_t *c, sb8_t *h, ast_t *table, s8_t decl) { s8_t name_t = s8_printf(c->arena, "%S_t", decl); diff --git a/src/ui/ui.gen.c b/src/ui/ui.gen.c index d181bc8..11c8ea9 100644 --- a/src/ui/ui.gen.c +++ b/src/ui/ui.gen.c @@ -1,4 +1,3 @@ - fn void ui_push_id(ui_id_t v) { ui_id_node_t *n = ma_push_type(tcx.temp, ui_id_node_t); n->value = v; SLLS_PUSH(ui->id_stack, n); } fn void ui_pop_id(void) { SLLS_POP(ui->id_stack); } #define ui_set_id(x) defer_block(ui_push_id(x), ui_pop_id()) @@ -23,6 +22,7 @@ fn void ui_pop_padding(void) { SLLS_POP(ui->padding_stack); } fn void ui_push_background_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->background_color_stack, n); } fn void ui_pop_background_color(void) { SLLS_POP(ui->background_color_stack); } #define ui_set_background_color(x) defer_block(ui_push_background_color(x), ui_pop_background_color()) + fn void ui_assert_stacks_are_null(void) { assert(ui->id_stack == NULL); assert(ui->lop_stack == NULL); diff --git a/src/ui/ui.gen.h b/src/ui/ui.gen.h index a9ebc8f..5e36393 100644 --- a/src/ui/ui.gen.h +++ b/src/ui/ui.gen.h @@ -5,7 +5,6 @@ typedef struct ui_f32_node_t ui_f32_node_t; struct ui_f32_node_t { f32 value; ui typedef struct ui_text_align_node_t ui_text_align_node_t; struct ui_text_align_node_t { ui_text_align_t value; ui_text_align_node_t *next; }; typedef struct ui_v2f32_node_t ui_v2f32_node_t; struct ui_v2f32_node_t { v2f32_t value; ui_v2f32_node_t *next; }; typedef struct ui_v4f32_node_t ui_v4f32_node_t; struct ui_v4f32_node_t { v4f32_t value; ui_v4f32_node_t *next; }; - #define UI_DECL_BOX_MEMBERS \ f32 border_thickness;\ ui_text_align_t text_align;\ diff --git a/src/ui/ui.meta.c b/src/ui/ui.meta.c index 2d6be45..8e79625 100644 --- a/src/ui/ui.meta.c +++ b/src/ui/ui.meta.c @@ -1,33 +1,34 @@ void mt_ui(ma_arena_t *arena) { - typedef struct mt_ui_stacks_t mt_ui_stacks_t; - struct mt_ui_stacks_t { - s8_t type; - s8_t name; - b32 skip_box_member; - - s8_t stack; - s8_t node; - }; - - mt_ui_stacks_t stacks[] = { - {s8_lit("ui_id_t") , s8_lit("id") , .skip_box_member = true} , - {s8_lit("ui_lop_t") , s8_lit("lop") , .skip_box_member = true} , - {s8_lit("f32") , s8_lit("border_thickness")} , - {s8_lit("ui_text_align_t") , s8_lit("text_align")} , - {s8_lit("f32") , s8_lit("required_width")} , - {s8_lit("f32") , s8_lit("required_height")} , - {s8_lit("v2f32_t") , s8_lit("padding")} , - {s8_lit("v4f32_t") , s8_lit("background_color")} , - }; + ast_t *table = mtt_parse(arena, __FILE__, S8_CODE( + { type name skip_box_member } + { ui_id_t id 1 } + { ui_lop_t lop 1 } + { f32 border_thickness 0 } + { ui_text_align_t text_align 0 } + { f32 required_width 0 } + { f32 required_height 0 } + { v2f32_t padding 0 } + { v4f32_t background_color 0 } + )); /////////////////////////////// - // fill stack and node - for (mt_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { - s8_t core_type = it->type; - if (s8_ends_with(core_type, s8_lit("_t"), false)) core_type = s8_chop(core_type, 2); - it->stack = s8_printf(arena, "%S_stack", it->name); - it->node = s8_printf(arena, "%S_node_t", core_type); - if (!s8_starts_with(it->node, s8_lit("ui_"), false)) it->node = s8_printf(arena, "ui_%S", it->node); + // create `stack` and `node` columns + for (ast_t *it = table->first; it; it = it->next) { + s8_t type = mtts(it, "type"); + if (s8_ends_with(type, s8_lit("_t"), false)) { + type = s8_chop(type, 2); + } + + s8_t ui_type = type; + if (!s8_starts_with(ui_type, s8_lit("ui_"), false)) { + ui_type = s8_printf(arena, "ui_%S", ui_type); + } + + s8_t node = s8_printf(arena, "%S_node_t", ui_type); + s8_t stack = s8_printf(arena, "%S_stack", mtts(it, "name")); + + mt_ast_append(it, mt_kv(arena, s8_lit("node"), node)); + mt_ast_append(it, mt_kv(arena, s8_lit("stack"), stack)); } sb8_t *h = sb8_serial_begin(arena); @@ -35,67 +36,51 @@ void mt_ui(ma_arena_t *arena) { /////////////////////////////// // generate types - for (mt_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { - for (mt_ui_stacks_t *jt = it - 1; jt >= stacks; jt -= 1) if (s8_are_equal(it->type, jt->type)) goto type_already_gened; - sb8_stmtf(h, "typedef struct %S %S; struct %S { %S value; %S *next; };", it->node, it->node, it->node, it->type, it->node); + for (ast_t *it = table->first; it; it = it->next) { + for (ast_t *prev = table->first; prev != it; prev = prev->next) { + if (s8_are_equal(mtts(it, "type"), mtts(prev, "type"))) goto type_already_gened; + } + + h->user_data = it; + mt_stmtf(h, "typedef struct @node @node; struct @node { @type value; @node *next; };"); + type_already_gened:; } - sb8_printf(h, "\n"); /////////////////////////////// // generate field embeds sb8_stmtf(h, "#define UI_DECL_BOX_MEMBERS \\"); - for (mt_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { - if (it->skip_box_member) continue; - sb8_stmtf(h, "%S %S;\\", it->type, it->name); + for (ast_t *it = table->first; it; it = it->next) { + if (mtt(it, "skip_box_member")->integer) continue; + h->user_data = it; + mt_stmtf(h, "@type @name;\\"); } sb8_printf(h, "\n"); sb8_stmtf(h, "#define UI_DECL_STACKS \\"); - for (mt_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { - sb8_stmtf(h, "%S *%S;\\", it->node, it->stack); + for (ast_t *it = table->first; it; it = it->next) { + h->user_data = it; + mt_stmtf(h, "@node *@stack;\\"); } sb8_printf(h, "\n"); /////////////////////////////// // generate stack functions - for (mt_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { - sb8_stmtf(c, "fn void ui_push_%S(%S v) { %S *n = ma_push_type(tcx.temp, %S); n->value = v; SLLS_PUSH(ui->%S, n); }", it->name, it->type, it->node, it->node, it->stack); - sb8_stmtf(c, "fn void ui_pop_%S(void) { SLLS_POP(ui->%S); }", it->name, it->stack); - sb8_stmtf(c, "#define ui_set_%S(x) defer_block(ui_push_%S(x), ui_pop_%S())", it->name, it->name, it->name); + for (ast_t *it = table->first; it; it = it->next) { + c->user_data = it; + mt_sbprintf(c, "fn void ui_push_@name(@type v) { @node *n = ma_push_type(tcx.temp, @node); n->value = v; SLLS_PUSH(ui->@stack, n); }\n" + "fn void ui_pop_@name(void) { SLLS_POP(ui->@stack); }\n" + "#define ui_set_@name(x) defer_block(ui_push_@name(x), ui_pop_@name())\n"); } sb8_stmtf(c, "fn void ui_assert_stacks_are_null(void) {"); - for (mt_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { - sb8_stmtf(c, "assert(ui->%S == NULL);", it->stack); + for (ast_t *it = table->first; it; it = it->next) { + c->user_data = it; + mt_stmtf(c, "assert(ui->@stack == NULL);"); } sb8_stmtf(c, "}"); - /////////////////////////////// - // write to disk + os_write_file(mt_cpath(arena), sb8_serial_end(arena, c)); os_write_file(mt_hpath(arena), sb8_serial_end(arena, h)); - - /////////////////////////////// - // idea - #if 0 - sb8_t *h = sb8_serial_begin(arena); - sb8_stmtf(h, "typedef struct node_type node_type; struct node_type { value_type value; node_type *next; };"); - - sb8_serial_end_use_template(arena, h, (variables_t){ - {"node_type", node_type_name}, - {"value_type", value_type_name}, - {0}. - }); - - /// - - variables_t vars = serial_vars_from_struct(it, type(mt_ui_stacks_t)); - sb8_tmplf(h, "typedef struct $node_type $node_type; struct $node_type { value_type value; $node_type *next; };", vars); - - //// - - sb8_stmtf(h, "typedef struct node_type node_type; struct node_type { value_type value; node_type *next; };", vars); - - #endif } \ No newline at end of file