Simplify loop code

This commit is contained in:
Bruce Hill 2024-03-15 13:38:25 -04:00
parent 5cdaf3e3fa
commit bc92bb72c8

View File

@ -515,6 +515,9 @@ CORD compile_statement(env_t *env, ast_t *ast)
}; };
body_scope->loop_ctx = &loop_ctx; body_scope->loop_ctx = &loop_ctx;
CORD body = compile_statement(body_scope, for_->body); CORD body = compile_statement(body_scope, for_->body);
if (loop_ctx.skip_label)
body = CORD_all(body, "\n", loop_ctx.skip_label, ": continue;");
CORD stop = loop_ctx.stop_label ? CORD_all("\n", loop_ctx.stop_label, ":;") : CORD_EMPTY;
switch (iter_t->tag) { switch (iter_t->tag) {
case ArrayType: { case ArrayType: {
@ -522,10 +525,7 @@ CORD compile_statement(env_t *env, ast_t *ast)
CORD index = for_->index ? compile(env, for_->index) : "$i"; CORD index = for_->index ? compile(env, for_->index) : "$i";
CORD value = compile(env, for_->value); CORD value = compile(env, for_->value);
return CORD_all("$ARRAY_FOREACH(", compile(env, for_->iter), ", ", index, ", ", compile_type(item_t), ", ", value, ", ", return CORD_all("$ARRAY_FOREACH(", compile(env, for_->iter), ", ", index, ", ", compile_type(item_t), ", ", value, ", ",
body, body, ", ", for_->empty ? compile_statement(env, for_->empty) : "{}", ")", stop);
loop_ctx.skip_label ? CORD_all("\n", loop_ctx.skip_label, ":;") : CORD_EMPTY,
", ", for_->empty ? compile_statement(env, for_->empty) : "{}", ")",
loop_ctx.stop_label ? CORD_all("\n", loop_ctx.stop_label, ":;") : CORD_EMPTY);
} }
case TableType: { case TableType: {
type_t *key_t = Match(iter_t, TableType)->key_type; type_t *key_t = Match(iter_t, TableType)->key_type;
@ -539,17 +539,11 @@ CORD compile_statement(env_t *env, ast_t *ast)
value_offset += type_align(value_t) - (value_offset % type_align(value_t)); // padding value_offset += type_align(value_t) - (value_offset % type_align(value_t)); // padding
return CORD_all("$TABLE_FOREACH(", compile(env, for_->iter), ", ", compile_type(key_t), ", ", key, ", ", return CORD_all("$TABLE_FOREACH(", compile(env, for_->iter), ", ", compile_type(key_t), ", ", key, ", ",
compile_type(value_t), ", ", value, ", ", heap_strf("%zu", value_offset), compile_type(value_t), ", ", value, ", ", heap_strf("%zu", value_offset),
", ", body, ", ", body, ", ", for_->empty ? compile_statement(env, for_->empty) : "{}", ")", stop);
loop_ctx.skip_label ? CORD_all("\n", loop_ctx.skip_label, ":;") : CORD_EMPTY,
", ", for_->empty ? compile_statement(env, for_->empty) : "{}", ")",
loop_ctx.stop_label ? CORD_all("\n", loop_ctx.stop_label, ":;") : CORD_EMPTY);
} else { } else {
CORD key = compile(env, for_->value); CORD key = compile(env, for_->value);
return CORD_all("$ARRAY_FOREACH((", compile(env, for_->iter), ").entries, $i, ", compile_type(key_t), ", ", key, ", ", return CORD_all("$ARRAY_FOREACH((", compile(env, for_->iter), ").entries, $i, ", compile_type(key_t), ", ", key, ", ",
body, body, ", ", for_->empty ? compile_statement(env, for_->empty) : "{}", ")", stop);
loop_ctx.skip_label ? CORD_all("\n", loop_ctx.skip_label, ":;") : CORD_EMPTY,
", ", for_->empty ? compile_statement(env, for_->empty) : "{}", ")",
loop_ctx.stop_label ? CORD_all("\n", loop_ctx.stop_label, ":;") : CORD_EMPTY);
} }
} }
case IntType: { case IntType: {
@ -562,12 +556,9 @@ CORD compile_statement(env_t *env, ast_t *ast)
"int64_t $n = ", n, ";\n" "int64_t $n = ", n, ";\n"
"if ($n > 0) {\n" "if ($n > 0) {\n"
"for (int64_t ", index, " = 1, ", value, "; (", value, "=", index,") <= $n; ++", index, ") {\n" "for (int64_t ", index, " = 1, ", value, "; (", value, "=", index,") <= $n; ++", index, ") {\n"
"\t", body, "\t", body, "\n}"
loop_ctx.skip_label ? CORD_all("\n", loop_ctx.skip_label, ":;") : CORD_EMPTY,
"\n}"
"\n} else ", compile_statement(env, for_->empty), "\n} else ", compile_statement(env, for_->empty),
loop_ctx.stop_label ? CORD_all("\n", loop_ctx.stop_label, ":;") : CORD_EMPTY, stop, "\n}");
"\n}");
} else if (for_->empty) { } else if (for_->empty) {
return CORD_all( return CORD_all(
"{\n" "{\n"
@ -575,26 +566,23 @@ CORD compile_statement(env_t *env, ast_t *ast)
"if ($n > 0) {\n" "if ($n > 0) {\n"
"for (int64_t ", value, " = 1; ", value, " <= $n; ++", value, ") {\n" "for (int64_t ", value, " = 1; ", value, " <= $n; ++", value, ") {\n"
"\t", body, "\t", body,
loop_ctx.skip_label ? CORD_all("\n", loop_ctx.skip_label, ":;") : CORD_EMPTY,
"\n}" "\n}"
"\n} else ", compile_statement(env, for_->empty), "\n} else ", compile_statement(env, for_->empty),
loop_ctx.stop_label ? CORD_all("\n", loop_ctx.stop_label, ":;") : CORD_EMPTY, stop,
"\n}"); "\n}");
} else if (index) { } else if (index) {
return CORD_all( return CORD_all(
"for (int64_t ", value, ", ", index, " = 1, $n = ", n, "; (", value, "=", index,") <= $n; ++", value, ") {\n" "for (int64_t ", value, ", ", index, " = 1, $n = ", n, "; (", value, "=", index,") <= $n; ++", value, ") {\n"
"\t", body, "\t", body,
loop_ctx.skip_label ? CORD_all("\n", loop_ctx.skip_label, ":;") : CORD_EMPTY,
"\n}", "\n}",
loop_ctx.stop_label ? CORD_all("\n", loop_ctx.stop_label, ":;") : CORD_EMPTY, stop,
"\n"); "\n");
} else { } else {
return CORD_all( return CORD_all(
"for (int64_t ", value, " = 1, $n = ", compile(env, for_->iter), "; ", value, " <= $n; ++", value, ") {\n" "for (int64_t ", value, " = 1, $n = ", compile(env, for_->iter), "; ", value, " <= $n; ++", value, ") {\n"
"\t", body, "\t", body,
loop_ctx.skip_label ? CORD_all("\n", loop_ctx.skip_label, ":;") : CORD_EMPTY,
"\n}", "\n}",
loop_ctx.stop_label ? CORD_all("\n", loop_ctx.stop_label, ":;") : CORD_EMPTY, stop,
"\n"); "\n");
} }
} }