Handle argument evaluation

This commit is contained in:
2025-08-10 19:54:09 +01:00
parent 624311d04f
commit 662b99a40f
3 changed files with 48 additions and 2 deletions

View File

@@ -20,9 +20,29 @@ static void eval_atom(am_t *am)
static void eval_list(am_t *am)
{
am->argl = am->expr->pair.cdr;
am->argl = expr_empty_list(am);
am->unev = am->expr->pair.cdr;
am->expr = am->expr->pair.car;
while (!am->unev->is_atom) {
am_push(am);
am->expr = am->unev->pair.cdr;
am_push(am);
am->expr = am->argl;
am_push(am);
am->expr = am->unev->pair.car;
eval(am);
am_pop(am);
am->argl = am->expr;
am_append_arg(am);
am_pop(am);
am->unev = am->expr;
am_pop(am);
}
assert(am->expr->is_atom);
assert(am->expr->atom.type == ATOM_TYPE_SYMBOL);
env_fetch(am);

View File

@@ -7,7 +7,7 @@
#define AM_STACK_SIZE 128U
typedef struct am {
expr_t *argl, *env, *expr, *val;
expr_t *argl, *env, *expr, *unev, *val;
expr_t **sp, *stack[AM_STACK_SIZE];
store_t store;
} am_t;

View File

@@ -88,6 +88,31 @@ static void test_add_1_2_3_evals_to_6(void)
TEST_ASSERT_EQUAL(6, am.val->atom.integer);
}
static void test_add_1_mul_2_3_evals_to_7(void)
{
am.expr = expr_pair(
&am, expr_str_symbol(&am, "+"),
expr_pair(
&am, expr_integer(&am, 1),
expr_pair(
&am,
expr_pair(
&am, expr_str_symbol(&am, "*"),
expr_pair(
&am, expr_integer(&am, 2),
expr_pair(
&am, expr_integer(&am, 3),
expr_empty_list(&am)))),
expr_empty_list(&am))));
eval(&am);
TEST_ASSERT_NOT_NULL(am.val);
TEST_ASSERT_TRUE(am.val->is_atom);
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, am.val->atom.type);
TEST_ASSERT_EQUAL(7, am.val->atom.integer);
}
int main(void)
{
UNITY_BEGIN();
@@ -96,5 +121,6 @@ int main(void)
RUN_TEST(test_prim_proc_self_evals);
RUN_TEST(test_foo_evals_to_42_when_set_in_env);
RUN_TEST(test_add_1_2_3_evals_to_6);
RUN_TEST(test_add_1_mul_2_3_evals_to_7);
return UNITY_END();
}