Skip to content

Commit

Permalink
Small improvements to optimizer
Browse files Browse the repository at this point in the history
* Added optimization passes limit
* Slightly modified formatting
  • Loading branch information
ChickChicky committed Nov 6, 2024
1 parent e34de1b commit 93c9e09
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions randomart.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Node *node_number_loc(const char *file, int line, Arena *arena, float number)
return node;
}
#define node_number(arena, number) node_number_loc(__FILE__, __LINE__, arena, number)
#define node_number_inline(value) (Node){ .kind = NK_NUMBER, .file = __FILE__, .line = __LINE__, .as.number = (value) }

Node *node_rule_loc(const char *file, int line, Arena *arena, int rule)
{
Expand All @@ -123,6 +124,7 @@ Node *node_boolean_loc(const char *file, int line, Arena *arena, bool boolean)
return node;
}
#define node_boolean(arena, boolean) node_boolean_loc(__FILE__, __LINE__, arena, boolean)
#define node_boolean_inline(value) (Node){ .kind = NK_BOOLEAN, .file = __FILE__, .line = __LINE__, .as.boolean = (value) }

#define node_x(arena) node_loc(__FILE__, __LINE__, arena, NK_X)
#define node_y(arena) node_loc(__FILE__, __LINE__, arena, NK_Y)
Expand Down Expand Up @@ -505,27 +507,29 @@ Node *gen_rule(Grammar grammar, Arena *arena, size_t rule, int depth)
return node;
}

bool get_number(Node *node, float *result) {
bool get_number(Node *node, float *result)
{
if (node->kind != NK_NUMBER)
return false;
if (result)
*result = node->as.number;
return true;
}

bool get_boolean(Node *node, bool *result) {
bool get_boolean(Node *node, bool *result)
{
if (node->kind != NK_BOOLEAN)
return false;
if (result)
*result = node->as.boolean;
return true;
}

#define node_number_inline(value) (Node){ .kind = NK_NUMBER, .file = __FILE__, .line = __LINE__, .as.number = (value) }
#define node_boolean_inline(value) (Node){ .kind = NK_BOOLEAN, .file = __FILE__, .line = __LINE__, .as.boolean = (value) }
#define OPTIMIZE_MAX_PASSES 100

// TODO: Probably try to free the discarded nodes
bool optimize_expr(Arena *arena, Node *expr) {
bool optimize_expr(Arena *arena, Node *expr)
{
switch (expr->kind) {
case NK_X:
case NK_Y:
Expand Down Expand Up @@ -589,8 +593,13 @@ bool optimize_expr(Arena *arena, Node *expr) {
}
}

void optimize_func(Arena *arena, Node *func) {
while (optimize_expr(arena, func));
bool optimize_func(Arena *arena, Node *func)
{
for (size_t i = 0; i < OPTIMIZE_MAX_PASSES; i++) {
if (!optimize_expr(arena, func))
return true;
}
return false;
}

size_t arch[] = {2, 28, 28, 9, 3};
Expand Down Expand Up @@ -649,7 +658,9 @@ int main()
fprintf(stderr, "ERROR: the crappy generation process could not terminate\n");
return 1;
}
optimize_func(&static_arena, f);
if (!optimize_func(&static_arena, f)) {
nob_log(WARNING, "Exceeded maximum optimization passes");
}
node_print_ln(f);

// bool ok = render_pixels(node_triple(node_x(), node_x(), node_x()));
Expand Down

0 comments on commit 93c9e09

Please sign in to comment.