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

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

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_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;\

View File

@@ -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
}