Rename matcher_stack -> matcher_state_stack

It's not a stack of matchers, a thing we'll soon need. So let's rename
this.
This commit is contained in:
Laria 2022-11-06 17:12:03 +01:00
parent 0682b5464d
commit 0267faede3
3 changed files with 60 additions and 60 deletions

View file

@ -552,7 +552,7 @@ apfl_call_stack_entry_deinit(struct apfl_allocator allocator, struct call_stack_
case CSE_FUNCTION_DISPATCH:
break;
case CSE_MATCHER:
FREE_LIST(allocator, entry->matcher.matcher_stack, entry->matcher.matcher_stack_cap);
FREE_LIST(allocator, entry->matcher.matcher_state_stack, entry->matcher.matcher_state_stack_cap);
break;
}
}

View file

@ -57,7 +57,7 @@ enum matcher_mode {
MATCHER_MODE_LIST_UNDERFLOW,
};
struct matcher_stack_entry {
struct matcher_state {
enum matcher_mode mode;
size_t lower;
@ -68,9 +68,9 @@ struct matcher_call_stack_entry {
size_t pc;
struct matcher *matcher;
struct matcher_stack_entry *matcher_stack;
size_t matcher_stack_len;
size_t matcher_stack_cap;
struct matcher_state *matcher_state_stack;
size_t matcher_state_stack_len;
size_t matcher_state_stack_cap;
};
struct func_dispatch_call_stack_entry {

View file

@ -690,14 +690,14 @@ evaluate(apfl_ctx ctx, struct func_call_stack_entry *cse)
}
static void
matcher_stack_push(apfl_ctx ctx, struct matcher_call_stack_entry *cse, struct matcher_stack_entry entry)
matcher_state_push(apfl_ctx ctx, struct matcher_call_stack_entry *cse, struct matcher_state entry)
{
if (!apfl_resizable_append(
ctx->gc.allocator,
sizeof(struct matcher_stack_entry),
(void **)&cse->matcher_stack,
&cse->matcher_stack_len,
&cse->matcher_stack_cap,
sizeof(struct matcher_state),
(void **)&cse->matcher_state_stack,
&cse->matcher_state_stack_len,
&cse->matcher_state_stack_cap,
&entry,
1
)) {
@ -712,9 +712,9 @@ raise_invalid_matcher_state(apfl_ctx ctx)
}
static void
matcher_stack_drop(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
matcher_state_drop(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
{
if (cse->matcher_stack_len == 0) {
if (cse->matcher_state_stack_len == 0) {
raise_invalid_matcher_state(ctx);
}
@ -722,11 +722,11 @@ matcher_stack_drop(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
// We're shrinking, should not fail
apfl_resizable_resize(
ctx->gc.allocator,
sizeof(struct matcher_stack_entry),
(void **)&cse->matcher_stack,
&cse->matcher_stack_len,
&cse->matcher_stack_cap,
cse->matcher_stack_len-1
sizeof(struct matcher_state),
(void **)&cse->matcher_state_stack,
&cse->matcher_state_stack_len,
&cse->matcher_state_stack_cap,
cse->matcher_state_stack_len-1
)
);
}
@ -744,12 +744,12 @@ matcher_init_matching_inner(apfl_ctx ctx, struct matcher *matcher)
struct matcher_call_stack_entry matcher_cse = {
.pc = 0,
.matcher = matcher,
.matcher_stack = NULL,
.matcher_stack_len = 0,
.matcher_stack_cap = 0,
.matcher_state_stack = NULL,
.matcher_state_stack_len = 0,
.matcher_state_stack_cap = 0,
};
matcher_stack_push(ctx, &matcher_cse, (struct matcher_stack_entry) {
matcher_state_push(ctx, &matcher_cse, (struct matcher_state) {
.mode = MATCHER_MODE_VALUE,
});
@ -778,22 +778,22 @@ matcher_check_index(apfl_ctx ctx, size_t count, size_t index)
}
}
static struct matcher_stack_entry *
matcher_cur_stack_entry(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
static struct matcher_state *
matcher_cur_state(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
{
if (cse->matcher_stack_len == 0) {
if (cse->matcher_state_stack_len == 0) {
raise_invalid_matcher_state(ctx);
}
return &cse->matcher_stack[cse->matcher_stack_len-1];
return &cse->matcher_state_stack[cse->matcher_state_stack_len-1];
}
static bool
matcher_current_val_in_mstack(apfl_ctx ctx, struct matcher_stack_entry *mstack, struct apfl_value *value)
matcher_current_val_in_state(apfl_ctx ctx, struct matcher_state *state, struct apfl_value *value)
{
struct apfl_value cur;
switch (mstack->mode) {
switch (state->mode) {
case MATCHER_MODE_VALUE:
case MATCHER_MODE_LIST_REMAINING:
if (!apfl_stack_get(ctx, &cur, -1)) {
@ -811,10 +811,10 @@ matcher_current_val_in_mstack(apfl_ctx ctx, struct matcher_stack_entry *mstack,
if (cur.type != VALUE_LIST) {
raise_invalid_matcher_state(ctx);
}
if (mstack->lower >= cur.list->len) {
if (state->lower >= cur.list->len) {
return false;
}
*value = cur.list->items[mstack->lower];
*value = cur.list->items[state->lower];
return true;
case MATCHER_MODE_LIST_END:
if (!apfl_stack_get(ctx, &cur, -1)) {
@ -823,10 +823,10 @@ matcher_current_val_in_mstack(apfl_ctx ctx, struct matcher_stack_entry *mstack,
if (cur.type != VALUE_LIST) {
raise_invalid_matcher_state(ctx);
}
if (mstack->upper == 0) {
if (state->upper == 0) {
return NULL;
}
*value = cur.list->items[mstack->upper-1];
*value = cur.list->items[state->upper-1];
return true;
}
@ -836,19 +836,19 @@ matcher_current_val_in_mstack(apfl_ctx ctx, struct matcher_stack_entry *mstack,
static bool
matcher_current_val(apfl_ctx ctx, struct matcher_call_stack_entry *cse, struct apfl_value *value)
{
struct matcher_stack_entry *mstack = matcher_cur_stack_entry(ctx, cse);
return matcher_current_val_in_mstack(ctx, mstack, value);
struct matcher_state *state = matcher_cur_state(ctx, cse);
return matcher_current_val_in_state(ctx, state, value);
}
static bool
matcher_next(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
{
again:;
struct matcher_stack_entry *mstack = matcher_cur_stack_entry(ctx, cse);
struct matcher_state *state = matcher_cur_state(ctx, cse);
switch (mstack->mode) {
switch (state->mode) {
case MATCHER_MODE_VALUE:
mstack->mode = MATCHER_MODE_STOP;
state->mode = MATCHER_MODE_STOP;
if (!apfl_stack_drop(ctx, -1)) {
raise_invalid_matcher_state(ctx);
}
@ -858,20 +858,20 @@ again:;
raise_invalid_matcher_state(ctx);
return false;
case MATCHER_MODE_LIST_START:
mstack->lower++;
state->lower++;
return true;
case MATCHER_MODE_LIST_END:
if (mstack->upper <= mstack->lower) {
mstack->mode = MATCHER_MODE_LIST_UNDERFLOW;
if (state->upper <= state->lower) {
state->mode = MATCHER_MODE_LIST_UNDERFLOW;
return false;
}
mstack->upper--;
state->upper--;
return true;
case MATCHER_MODE_LIST_REMAINING:
if (!apfl_stack_drop(ctx, -1)) {
raise_invalid_matcher_state(ctx);
}
matcher_stack_drop(ctx, cse);
matcher_state_drop(ctx, cse);
goto again; // We also need to advance the previous stack entry,
// like we would do when doing a MATCHER_LEAVE_LIST
}
@ -882,9 +882,9 @@ again:;
static bool
matcher_enter_list(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
{
struct matcher_stack_entry *mstack = matcher_cur_stack_entry(ctx, cse);
struct matcher_state *state = matcher_cur_state(ctx, cse);
struct apfl_value cur;
if (!matcher_current_val_in_mstack(ctx, mstack, &cur)) {
if (!matcher_current_val_in_state(ctx, state, &cur)) {
return false;
}
if (cur.type != VALUE_LIST) {
@ -895,7 +895,7 @@ matcher_enter_list(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
apfl_stack_must_push(ctx, cur);
matcher_stack_push(ctx, cse, (struct matcher_stack_entry) {
matcher_state_push(ctx, cse, (struct matcher_state) {
.mode = MATCHER_MODE_LIST_START,
.lower = 0,
.upper = len,
@ -906,17 +906,17 @@ matcher_enter_list(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
static void
matcher_continue_from_end(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
{
struct matcher_stack_entry *mstack = matcher_cur_stack_entry(ctx, cse);
if (mstack->mode != MATCHER_MODE_LIST_START) {
struct matcher_state *state = matcher_cur_state(ctx, cse);
if (state->mode != MATCHER_MODE_LIST_START) {
raise_invalid_matcher_state(ctx);
}
mstack->mode = MATCHER_MODE_LIST_END;
state->mode = MATCHER_MODE_LIST_END;
}
static void
matcher_remainding(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
{
struct matcher_stack_entry *mstack = matcher_cur_stack_entry(ctx, cse);
struct matcher_state *state = matcher_cur_state(ctx, cse);
struct apfl_value cur;
if (!apfl_stack_get(ctx, &cur, -1)) {
@ -924,20 +924,20 @@ matcher_remainding(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
}
if (
(mstack->mode != MATCHER_MODE_LIST_START && mstack->mode != MATCHER_MODE_LIST_END)
(state->mode != MATCHER_MODE_LIST_START && state->mode != MATCHER_MODE_LIST_END)
|| cur.type != VALUE_LIST
) {
raise_invalid_matcher_state(ctx);
}
if (mstack->lower > mstack->upper) {
if (state->lower > state->upper) {
raise_invalid_matcher_state(ctx);
}
struct list_header *cur_list = cur.list;
assert(cur_list->len >= mstack->upper);
assert(cur_list->len >= state->upper);
size_t len = mstack->upper - mstack->lower;
size_t len = state->upper - state->lower;
apfl_list_create(ctx, len);
struct apfl_value new_val = apfl_stack_must_get(ctx, -1);
@ -946,7 +946,7 @@ matcher_remainding(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
struct list_header *new_list = new_val.list;
assert(new_list->cap == len);
assert(new_list->len == 0);
for (size_t i = mstack->lower; i < mstack->upper; i++) {
for (size_t i = state->lower; i < state->upper; i++) {
new_list->items[new_list->len++] = cur_list->items[i];
}
assert(new_list->len == len);
@ -955,18 +955,18 @@ matcher_remainding(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
raise_invalid_matcher_state(ctx);
}
mstack->mode = MATCHER_MODE_LIST_REMAINING;
state->mode = MATCHER_MODE_LIST_REMAINING;
}
static bool
matcher_leave_list(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
{
struct matcher_stack_entry *mstack = matcher_cur_stack_entry(ctx, cse);
if (mstack->mode != MATCHER_MODE_LIST_START) {
struct matcher_state *state = matcher_cur_state(ctx, cse);
if (state->mode != MATCHER_MODE_LIST_START) {
raise_invalid_matcher_state(ctx);
}
if (mstack->lower < mstack->upper) {
if (state->lower < state->upper) {
// List was not completely matched
return false;
}
@ -974,7 +974,7 @@ matcher_leave_list(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
if (!apfl_stack_drop(ctx, -1)) {
raise_invalid_matcher_state(ctx);
}
matcher_stack_drop(ctx, cse);
matcher_state_drop(ctx, cse);
return matcher_next(ctx, cse);
}
@ -1070,8 +1070,8 @@ evaluate_matcher(apfl_ctx ctx, struct matcher_call_stack_entry *cse)
return_from_matcher(
ctx,
// We've successfully matched everything, if there's only one stack element left and we're in stop mode
cse->matcher_stack_len == 1 && cse->matcher_stack[0].mode == MATCHER_MODE_STOP
// We've successfully matched everything, if there's only one stack element left and we're in stop state
cse->matcher_state_stack_len == 1 && cse->matcher_state_stack[0].mode == MATCHER_MODE_STOP
);
}