Implement atom evaluation
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
add_library(imp
|
add_library(imp
|
||||||
am.c
|
am.c
|
||||||
env.c
|
env.c
|
||||||
|
eval.c
|
||||||
expr.c
|
expr.c
|
||||||
memory_stream.c
|
memory_stream.c
|
||||||
parse.c
|
parse.c
|
||||||
|
|||||||
19
lib/eval.c
Normal file
19
lib/eval.c
Normal 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
8
lib/include/eval.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef EVAL_H
|
||||||
|
#define EVAL_H
|
||||||
|
|
||||||
|
#include "am.h"
|
||||||
|
|
||||||
|
void eval(am_t *am);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -11,6 +11,7 @@ endfunction()
|
|||||||
add_test_suites(
|
add_test_suites(
|
||||||
am_tests.c
|
am_tests.c
|
||||||
env_tests.c
|
env_tests.c
|
||||||
|
eval_tests.c
|
||||||
expr_tests.c
|
expr_tests.c
|
||||||
parse_tests.c
|
parse_tests.c
|
||||||
store_tests.c
|
store_tests.c
|
||||||
|
|||||||
66
tests/eval_tests.c
Normal file
66
tests/eval_tests.c
Normal 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();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user