Add register parameters to AM append operation
This commit is contained in:
6
lib/am.c
6
lib/am.c
@@ -26,13 +26,13 @@ void am_pop(am_t *am, am_reg_t reg)
|
|||||||
am->regs[reg] = *++am->sp;
|
am->regs[reg] = *++am->sp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void am_append_arg(am_t *am)
|
void am_append(am_t *am, am_reg_t list_reg, am_reg_t item_reg)
|
||||||
{
|
{
|
||||||
expr_t *list = AM_ARGL(am);
|
expr_t *list = am->regs[list_reg];
|
||||||
while (!list->is_atom)
|
while (!list->is_atom)
|
||||||
list = list->pair.cdr;
|
list = list->pair.cdr;
|
||||||
|
|
||||||
list->is_atom = false;
|
list->is_atom = false;
|
||||||
list->pair.car = AM_VAL(am);
|
list->pair.car = am->regs[item_reg];
|
||||||
list->pair.cdr = expr_empty_list(am);
|
list->pair.cdr = expr_empty_list(am);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ static void eval_list(am_t *am)
|
|||||||
eval(am);
|
eval(am);
|
||||||
|
|
||||||
am_pop(am, ARGL);
|
am_pop(am, ARGL);
|
||||||
am_append_arg(am);
|
am_append(am, ARGL, VAL);
|
||||||
|
|
||||||
am_pop(am, UNEV);
|
am_pop(am, UNEV);
|
||||||
am_pop(am, EXPR);
|
am_pop(am, EXPR);
|
||||||
|
|||||||
@@ -31,6 +31,6 @@ typedef struct am {
|
|||||||
void am_init(am_t *am);
|
void am_init(am_t *am);
|
||||||
void am_push(am_t *am, am_reg_t reg);
|
void am_push(am_t *am, am_reg_t reg);
|
||||||
void am_pop(am_t *am, am_reg_t reg);
|
void am_pop(am_t *am, am_reg_t reg);
|
||||||
void am_append_arg(am_t *am);
|
void am_append(am_t *am, am_reg_t list_reg, am_reg_t item_reg);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
22
lib/parse.c
22
lib/parse.c
@@ -22,18 +22,6 @@ static parse_state_t pop_state(parse_ctx_t *ctx)
|
|||||||
return *++ctx->sp;
|
return *++ctx->sp;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void append(parse_ctx_t *ctx, expr_t *expr)
|
|
||||||
{
|
|
||||||
expr_t *list = AM_EXPR(ctx->am);
|
|
||||||
while (!list->is_atom)
|
|
||||||
list = list->pair.cdr;
|
|
||||||
assert(list->atom.type == ATOM_TYPE_EMPTY_LIST);
|
|
||||||
|
|
||||||
list->is_atom = false;
|
|
||||||
list->pair.car = expr;
|
|
||||||
list->pair.cdr = expr_empty_list(ctx->am);
|
|
||||||
}
|
|
||||||
|
|
||||||
parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
||||||
{
|
{
|
||||||
switch (ctx->state) {
|
switch (ctx->state) {
|
||||||
@@ -61,10 +49,12 @@ parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
|||||||
case PARSE_STATE_LIST:
|
case PARSE_STATE_LIST:
|
||||||
switch (token->type) {
|
switch (token->type) {
|
||||||
case TOKEN_TYPE_INTEGER:
|
case TOKEN_TYPE_INTEGER:
|
||||||
append(ctx, expr_integer(ctx->am, token->integer));
|
AM_VAL(ctx->am) = expr_integer(ctx->am, token->integer);
|
||||||
|
am_append(ctx->am, EXPR, VAL);
|
||||||
break;
|
break;
|
||||||
case TOKEN_TYPE_SYMBOL:
|
case TOKEN_TYPE_SYMBOL:
|
||||||
append(ctx, expr_symbol(ctx->am, &token->symbol));
|
AM_VAL(ctx->am) = expr_symbol(ctx->am, &token->symbol);
|
||||||
|
am_append(ctx->am, EXPR, VAL);
|
||||||
break;
|
break;
|
||||||
case TOKEN_TYPE_OPEN_PAREN:
|
case TOKEN_TYPE_OPEN_PAREN:
|
||||||
am_push(ctx->am, EXPR);
|
am_push(ctx->am, EXPR);
|
||||||
@@ -75,9 +65,9 @@ parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
|||||||
case TOKEN_TYPE_CLOSE_PAREN:
|
case TOKEN_TYPE_CLOSE_PAREN:
|
||||||
ctx->state = pop_state(ctx);
|
ctx->state = pop_state(ctx);
|
||||||
if (ctx->state == PARSE_STATE_LIST) {
|
if (ctx->state == PARSE_STATE_LIST) {
|
||||||
expr_t *expr = AM_EXPR(ctx->am);
|
AM_VAL(ctx->am) = AM_EXPR(ctx->am);
|
||||||
am_pop(ctx->am, EXPR);
|
am_pop(ctx->am, EXPR);
|
||||||
append(ctx, expr);
|
am_append(ctx->am, EXPR, VAL);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#define CAR(expr) (expr->pair.car)
|
#define CAR(expr) (expr->pair.car)
|
||||||
#define CDR(expr) (expr->pair.cdr)
|
#define CDR(expr) (expr->pair.cdr)
|
||||||
|
#define CADR(expr) CAR(CDR(expr))
|
||||||
|
|
||||||
static am_t am;
|
static am_t am;
|
||||||
|
|
||||||
@@ -39,11 +40,11 @@ static void test_argl_value_restored_after_push_modify_pop(void)
|
|||||||
TEST_ASSERT_EQUAL(&a, am.regs[ARGL]);
|
TEST_ASSERT_EQUAL(&a, am.regs[ARGL]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_append_arg_42_with_empty_argl(void)
|
static void test_append_val_42_to_empty_argl(void)
|
||||||
{
|
{
|
||||||
am.regs[ARGL] = expr_empty_list(&am);
|
am.regs[ARGL] = expr_empty_list(&am);
|
||||||
am.regs[VAL] = expr_integer(&am, 42);
|
am.regs[VAL] = expr_integer(&am, 42);
|
||||||
am_append_arg(&am);
|
am_append(&am, ARGL, VAL);
|
||||||
|
|
||||||
TEST_ASSERT_FALSE(am.regs[ARGL]->is_atom);
|
TEST_ASSERT_FALSE(am.regs[ARGL]->is_atom);
|
||||||
TEST_ASSERT_NOT_NULL(CAR(am.regs[ARGL]));
|
TEST_ASSERT_NOT_NULL(CAR(am.regs[ARGL]));
|
||||||
@@ -56,11 +57,36 @@ static void test_append_arg_42_with_empty_argl(void)
|
|||||||
TEST_ASSERT_EQUAL(ATOM_TYPE_EMPTY_LIST, CDR(am.regs[ARGL])->atom.type);
|
TEST_ASSERT_EQUAL(ATOM_TYPE_EMPTY_LIST, CDR(am.regs[ARGL])->atom.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_append_unev_2_to_expr_list_1(void)
|
||||||
|
{
|
||||||
|
am.regs[EXPR]
|
||||||
|
= expr_pair(&am, expr_integer(&am, 1), expr_empty_list(&am));
|
||||||
|
am.regs[UNEV] = expr_integer(&am, 2);
|
||||||
|
am_append(&am, EXPR, UNEV);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(am.regs[EXPR]);
|
||||||
|
TEST_ASSERT_FALSE(am.regs[EXPR]->is_atom);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CAR(am.regs[EXPR]));
|
||||||
|
TEST_ASSERT_TRUE(CAR(am.regs[EXPR])->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, CAR(am.regs[EXPR])->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(1, CAR(am.regs[EXPR])->atom.integer);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CDR(am.regs[EXPR]));
|
||||||
|
TEST_ASSERT_FALSE(CDR(am.regs[EXPR])->is_atom);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CADR(am.regs[EXPR]));
|
||||||
|
TEST_ASSERT_TRUE(CADR(am.regs[EXPR])->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, CADR(am.regs[EXPR])->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(2, CADR(am.regs[EXPR])->atom.integer);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
RUN_TEST(test_expr_value_restored_after_push_modify_pop);
|
RUN_TEST(test_expr_value_restored_after_push_modify_pop);
|
||||||
RUN_TEST(test_argl_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);
|
RUN_TEST(test_append_val_42_to_empty_argl);
|
||||||
|
RUN_TEST(test_append_unev_2_to_expr_list_1);
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user