working on template stuff, add log flush

This commit is contained in:
Krzosa Karol
2025-01-21 16:02:09 +01:00
parent 515737f477
commit adb30cf24d
8 changed files with 91 additions and 72 deletions

View File

@@ -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_comma; break;
case '@': token->kind = lex_kind_tag; break; case '@': token->kind = lex_kind_tag; break;
case '?': token->kind = lex_kind_question; 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; case '`': lex_eat_string(lex, token); break;
case '\'': lex_eat_string(lex, token); break; case '\'': lex_eat_string(lex, token); break;

View File

@@ -57,6 +57,7 @@ enum lex_kind_t {
X(lex_kind_arrow , "'->' arrow" , "->" )\ X(lex_kind_arrow , "'->' arrow" , "->" )\
X(lex_kind_question , "'?' question mark" , "?" )\ X(lex_kind_question , "'?' question mark" , "?" )\
X(lex_kind_tag , "'@' tag sign" , "@" )\ X(lex_kind_tag , "'@' tag sign" , "@" )\
X(lex_kind_escape , "'\' escape" , "\\" )\
X(lex_kind_preproc_null , "preproc_null" , "---" )\ X(lex_kind_preproc_null , "preproc_null" , "---" )\
X(lex_kind_preproc_define , "preproc_define" , "---" )\ X(lex_kind_preproc_define , "preproc_define" , "---" )\
X(lex_kind_preproc_ifdef , "preproc_ifdef" , "---" )\ X(lex_kind_preproc_ifdef , "preproc_ifdef" , "---" )\

View File

@@ -21,10 +21,12 @@ fn b32 os_vmem_decommit(void *p, usize size) {
fn void os_error_box(char *str) { fn void os_error_box(char *str) {
fprintf(stderr, "%s", str); fprintf(stderr, "%s", str);
fflush(stderr);
} }
fn void os_console_log(char *str) { fn void os_console_log(char *str) {
puts(str); fputs(str, stdout);
fflush(stdout);
} }
fn f64 os_parse_float(char *str) { fn f64 os_parse_float(char *str) {

View File

@@ -20,11 +20,13 @@ fn b32 os_vmem_decommit(void *p, usize size) {
fn void os_error_box(char *str) { fn void os_error_box(char *str) {
MessageBoxA(NULL, str, "fatal error", MB_OK); MessageBoxA(NULL, str, "fatal error", MB_OK);
fprintf(stderr, "%s", str); fprintf(stderr, "%s", str);
fflush(stderr);
} }
fn void os_console_log(char *str) { fn void os_console_log(char *str) {
OutputDebugStringA(str); OutputDebugStringA(str);
fputs(str, stdout); fputs(str, stdout);
fflush(stdout);
} }
fn f64 os_parse_float(char *str) { fn f64 os_parse_float(char *str) {

View File

@@ -132,15 +132,44 @@ fn s8_t mt_printf(ma_arena_t *arena, ast_t *n, char *str, ...) {
return result; 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); assert(sb->user_data);
ma_temp_t scratch = ma_begin_scratch1(sb->arena); 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); s8_t result = mt_print(sb->arena, (ast_t *)sb->user_data, string);
sb8_append(sb, result); sb8_append(sb, result);
ma_end_scratch(scratch); ma_end_scratch(scratch);
return result; 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) { 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); s8_t name_t = s8_printf(c->arena, "%S_t", decl);

View File

@@ -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_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); } 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()) #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_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); } 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()) #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) { fn void ui_assert_stacks_are_null(void) {
assert(ui->id_stack == NULL); assert(ui->id_stack == NULL);
assert(ui->lop_stack == NULL); assert(ui->lop_stack == NULL);

View File

@@ -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_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_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; }; 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 \ #define UI_DECL_BOX_MEMBERS \
f32 border_thickness;\ f32 border_thickness;\
ui_text_align_t text_align;\ ui_text_align_t text_align;\

View File

@@ -1,33 +1,34 @@
void mt_ui(ma_arena_t *arena) { void mt_ui(ma_arena_t *arena) {
typedef struct mt_ui_stacks_t mt_ui_stacks_t; ast_t *table = mtt_parse(arena, __FILE__, S8_CODE(
struct mt_ui_stacks_t { { type name skip_box_member }
s8_t type; { ui_id_t id 1 }
s8_t name; { ui_lop_t lop 1 }
b32 skip_box_member; { f32 border_thickness 0 }
{ ui_text_align_t text_align 0 }
s8_t stack; { f32 required_width 0 }
s8_t node; { f32 required_height 0 }
}; { v2f32_t padding 0 }
{ v4f32_t background_color 0 }
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")} ,
};
/////////////////////////////// ///////////////////////////////
// fill stack and node // create `stack` and `node` columns
for (mt_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { for (ast_t *it = table->first; it; it = it->next) {
s8_t core_type = it->type; s8_t type = mtts(it, "type");
if (s8_ends_with(core_type, s8_lit("_t"), false)) core_type = s8_chop(core_type, 2); if (s8_ends_with(type, s8_lit("_t"), false)) {
it->stack = s8_printf(arena, "%S_stack", it->name); type = s8_chop(type, 2);
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);
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); sb8_t *h = sb8_serial_begin(arena);
@@ -35,67 +36,51 @@ void mt_ui(ma_arena_t *arena) {
/////////////////////////////// ///////////////////////////////
// generate types // generate types
for (mt_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { for (ast_t *it = table->first; it; it = it->next) {
for (mt_ui_stacks_t *jt = it - 1; jt >= stacks; jt -= 1) if (s8_are_equal(it->type, jt->type)) goto type_already_gened; for (ast_t *prev = table->first; prev != it; prev = prev->next) {
sb8_stmtf(h, "typedef struct %S %S; struct %S { %S value; %S *next; };", it->node, it->node, it->node, it->type, it->node); 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:; type_already_gened:;
} }
sb8_printf(h, "\n");
/////////////////////////////// ///////////////////////////////
// generate field embeds // generate field embeds
sb8_stmtf(h, "#define UI_DECL_BOX_MEMBERS \\"); sb8_stmtf(h, "#define UI_DECL_BOX_MEMBERS \\");
for (mt_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { for (ast_t *it = table->first; it; it = it->next) {
if (it->skip_box_member) continue; if (mtt(it, "skip_box_member")->integer) continue;
sb8_stmtf(h, "%S %S;\\", it->type, it->name); h->user_data = it;
mt_stmtf(h, "@type @name;\\");
} }
sb8_printf(h, "\n"); sb8_printf(h, "\n");
sb8_stmtf(h, "#define UI_DECL_STACKS \\"); sb8_stmtf(h, "#define UI_DECL_STACKS \\");
for (mt_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { for (ast_t *it = table->first; it; it = it->next) {
sb8_stmtf(h, "%S *%S;\\", it->node, it->stack); h->user_data = it;
mt_stmtf(h, "@node *@stack;\\");
} }
sb8_printf(h, "\n"); sb8_printf(h, "\n");
/////////////////////////////// ///////////////////////////////
// generate stack functions // generate stack functions
for (mt_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { for (ast_t *it = table->first; it; it = it->next) {
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); c->user_data = it;
sb8_stmtf(c, "fn void ui_pop_%S(void) { SLLS_POP(ui->%S); }", it->name, it->stack); 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"
sb8_stmtf(c, "#define ui_set_%S(x) defer_block(ui_push_%S(x), ui_pop_%S())", it->name, it->name, it->name); "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) {"); sb8_stmtf(c, "fn void ui_assert_stacks_are_null(void) {");
for (mt_ui_stacks_t *it = stacks; it < stacks + lengthof(stacks); it += 1) { for (ast_t *it = table->first; it; it = it->next) {
sb8_stmtf(c, "assert(ui->%S == NULL);", it->stack); c->user_data = it;
mt_stmtf(c, "assert(ui->@stack == NULL);");
} }
sb8_stmtf(c, "}"); sb8_stmtf(c, "}");
///////////////////////////////
// write to disk
os_write_file(mt_cpath(arena), sb8_serial_end(arena, c)); os_write_file(mt_cpath(arena), sb8_serial_end(arena, c));
os_write_file(mt_hpath(arena), sb8_serial_end(arena, h)); 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
} }