Implement atom evaluation

This commit is contained in:
2025-08-10 13:55:20 +01:00
parent 03bd0ff597
commit 3f871d2b43
5 changed files with 95 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
add_library(imp
am.c
env.c
eval.c
expr.c
memory_stream.c
parse.c

19
lib/eval.c Normal file
View File

@@ -0,0 +1,19 @@
#include "eval.h"
#include "env.h"
#include <assert.h>
void eval(am_t *am)
{
assert(am->expr->is_atom);
switch (am->expr->atom.type) {
case ATOM_TYPE_EMPTY_LIST:
case ATOM_TYPE_INTEGER:
am->val = am->expr;
break;
case ATOM_TYPE_SYMBOL:
env_fetch(am);
break;
}
}

8
lib/include/eval.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef EVAL_H
#define EVAL_H
#include "am.h"
void eval(am_t *am);
#endif

View File

@@ -11,6 +11,7 @@ endfunction()
add_test_suites(
am_tests.c
env_tests.c
eval_tests.c
expr_tests.c
parse_tests.c
store_tests.c

66
tests/eval_tests.c Normal file
View File

@@ -0,0 +1,66 @@
#include "am.h"
#include "env.h"
#include "eval.h"
#include "store.h"
#include "unity.h"
static am_t am;
static store_t store;
void setUp(void)
{
am_init(&am);
store_init(&store);
env_init(&am, &store);
}
void tearDown(void)
{
}
static void test_42_self_evals(void)
{
am.expr = expr_integer(&store, 42);
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(42, am.val->atom.integer);
}
static void test_empty_list_self_evals(void)
{
am.expr = expr_empty_list(&store);
eval(&am);
TEST_ASSERT_NOT_NULL(am.val);
TEST_ASSERT_TRUE(am.val->is_atom);
TEST_ASSERT_EQUAL(ATOM_TYPE_EMPTY_LIST, am.val->atom.type);
}
static void test_foo_evals_to_42_when_set_in_env(void)
{
am.expr = expr_str_symbol(&store, "foo");
am.val = expr_integer(&store, 42);
env_set(&am, &store);
am.val = NULL;
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(42, am.val->atom.integer);
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_42_self_evals);
RUN_TEST(test_empty_list_self_evals);
RUN_TEST(test_foo_evals_to_42_when_set_in_env);
return UNITY_END();
}