diff --git a/lib/am.c b/lib/am.c index acf203b..cf32605 100644 --- a/lib/am.c +++ b/lib/am.c @@ -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) diff --git a/lib/eval.c b/lib/eval.c index e6eebb8..386a4a2 100644 --- a/lib/eval.c +++ b/lib/eval.c @@ -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); diff --git a/lib/include/am.h b/lib/include/am.h index 8cd4d38..36240f0 100644 --- a/lib/include/am.h +++ b/lib/include/am.h @@ -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 diff --git a/lib/parse.c b/lib/parse.c index d8f103d..7622485 100644 --- a/lib/parse.c +++ b/lib/parse.c @@ -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; diff --git a/tests/am_tests.c b/tests/am_tests.c index dba4004..06ac962 100644 --- a/tests/am_tests.c +++ b/tests/am_tests.c @@ -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(); }