Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#618 alternative to dupn #2059

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,9 @@ static block bind_alternation_matchers(block matchers, block body) {
}

block gen_reduce(block source, block matcher, block init, block body) {
block res_var = gen_op_var_fresh(STOREV, "reduce");
block loop = BLOCK(gen_op_simple(DUPN),
block res_var = gen_op_var_fresh(STOREV, "*reduce");
block dot_var = gen_op_var_fresh(STOREV, "*dot");
block loop = BLOCK(gen_op_simple(DUP),
source,
bind_alternation_matchers(matcher,
BLOCK(gen_op_bound(LOADVN, res_var),
Expand All @@ -841,15 +842,23 @@ block gen_reduce(block source, block matcher, block init, block body) {
return BLOCK(gen_op_simple(DUP),
init,
res_var,
BLOCK(
// dummy null
gen_op_pushk_under(jv_null()),
// stores the actual input
dot_var
),
gen_op_target(FORK, loop),
//consumes dummy null and null-restores the actual input
gen_op_bound(LOADVN, dot_var),
loop,
//consumes dummy null and null-restores the result
gen_op_bound(LOADVN, res_var));
}

block gen_foreach(block source, block matcher, block init, block update, block extract) {
block output = gen_op_targetlater(JUMP);
block state_var = gen_op_var_fresh(STOREV, "foreach");
block loop = BLOCK(gen_op_simple(DUPN),
block loop = BLOCK(gen_op_simple(DUP),
// get a value from the source expression:
source,
// destructure the value into variable(s) for all the code
Expand All @@ -864,28 +873,11 @@ block gen_foreach(block source, block matcher, block init, block update, block e
// save new state
gen_op_bound(STOREV, state_var),
// extract an output...
extract,
// ...and output it by jumping
// past the BACKTRACK that comes
// right after the loop body,
// which in turn is there
// because...
//
// (Incidentally, extract can also
// backtrack, e.g., if it calls
// empty, in which case we don't
// get here.)
output)));
extract)));
block foreach = BLOCK(gen_op_simple(DUP),
init,
state_var,
gen_op_target(FORK, loop),
loop,
// ...at this point `foreach`'s original input
// will be on top of the stack, and we don't
// want to output it, so we backtrack.
gen_op_simple(BACKTRACK));
inst_set_target(output, foreach); // make that JUMP go bast the BACKTRACK at the end of the loop
loop);
return foreach;
}

Expand Down