Add register parameter to AM stack operations

This commit is contained in:
2025-08-10 20:38:04 +01:00
parent 57c39fd00b
commit 76efcef953
5 changed files with 29 additions and 19 deletions

View File

@@ -14,16 +14,16 @@ void am_init(am_t *am)
env_init(am);
}
void am_push(am_t *am)
void am_push(am_t *am, am_reg_t reg)
{
assert(am->sp >= am->stack);
*am->sp-- = AM_EXPR(am);
*am->sp-- = am->regs[reg];
}
void am_pop(am_t *am)
void am_pop(am_t *am, am_reg_t reg)
{
assert(am->sp < am->stack + AM_STACK_SIZE - 1);
AM_EXPR(am) = *++am->sp;
am->regs[reg] = *++am->sp;
}
void am_append_arg(am_t *am)

View File

@@ -25,22 +25,19 @@ static void eval_list(am_t *am)
AM_EXPR(am) = AM_EXPR(am)->pair.car;
while (!AM_UNEV(am)->is_atom) {
am_push(am);
am_push(am, EXPR);
AM_EXPR(am) = AM_UNEV(am)->pair.cdr;
am_push(am);
AM_EXPR(am) = AM_ARGL(am);
am_push(am);
am_push(am, EXPR);
am_push(am, ARGL);
AM_EXPR(am) = AM_UNEV(am)->pair.car;
eval(am);
am_pop(am);
AM_ARGL(am) = AM_EXPR(am);
am_pop(am, ARGL);
am_append_arg(am);
am_pop(am);
AM_UNEV(am) = AM_EXPR(am);
am_pop(am);
am_pop(am, UNEV);
am_pop(am, EXPR);
}
assert(AM_EXPR(am)->is_atom);

View File

@@ -29,8 +29,8 @@ typedef struct am {
} am_t;
void am_init(am_t *am);
void am_push(am_t *am);
void am_pop(am_t *am);
void am_push(am_t *am, am_reg_t reg);
void am_pop(am_t *am, am_reg_t reg);
void am_append_arg(am_t *am);
#endif

View File

@@ -67,7 +67,7 @@ parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
append(ctx, expr_symbol(ctx->am, &token->symbol));
break;
case TOKEN_TYPE_OPEN_PAREN:
am_push(ctx->am);
am_push(ctx->am, EXPR);
push_state(ctx, PARSE_STATE_LIST);
AM_EXPR(ctx->am) = expr_empty_list(ctx->am);
ctx->state = PARSE_STATE_LIST;
@@ -76,7 +76,7 @@ parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
ctx->state = pop_state(ctx);
if (ctx->state == PARSE_STATE_LIST) {
expr_t *expr = AM_EXPR(ctx->am);
am_pop(ctx->am);
am_pop(ctx->am, EXPR);
append(ctx, expr);
}
break;

View File

@@ -20,13 +20,25 @@ static void test_expr_value_restored_after_push_modify_pop(void)
expr_t a, b;
am.regs[EXPR] = &a;
am_push(&am);
am_push(&am, EXPR);
am.regs[EXPR] = &b;
am_pop(&am);
am_pop(&am, EXPR);
TEST_ASSERT_EQUAL(&a, am.regs[EXPR]);
}
static void test_argl_value_restored_after_push_modify_pop(void)
{
expr_t a, b;
am.regs[ARGL] = &a;
am_push(&am, ARGL);
am.regs[ARGL] = &b;
am_pop(&am, ARGL);
TEST_ASSERT_EQUAL(&a, am.regs[ARGL]);
}
static void test_append_arg_42_with_empty_argl(void)
{
am.regs[ARGL] = expr_empty_list(&am);
@@ -48,6 +60,7 @@ int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_expr_value_restored_after_push_modify_pop);
RUN_TEST(test_argl_value_restored_after_push_modify_pop);
RUN_TEST(test_append_arg_42_with_empty_argl);
return UNITY_END();
}