Add store into abstract machine struct
This commit is contained in:
@@ -4,13 +4,10 @@
|
||||
#include <string.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)
|
||||
@@ -19,11 +16,11 @@ void tearDown(void)
|
||||
|
||||
static void test_set_foo_to_42_then_fetch(void)
|
||||
{
|
||||
am.expr = expr_str_symbol(&store, "foo");
|
||||
am.val = expr_integer(&store, 42);
|
||||
env_set(&am, &store);
|
||||
am.expr = expr_str_symbol(&am, "foo");
|
||||
am.val = expr_integer(&am, 42);
|
||||
env_set(&am);
|
||||
|
||||
am.expr = expr_str_symbol(&store, "foo");
|
||||
am.expr = expr_str_symbol(&am, "foo");
|
||||
am.val = NULL;
|
||||
env_fetch(&am);
|
||||
|
||||
@@ -35,13 +32,13 @@ static void test_set_foo_to_42_then_fetch(void)
|
||||
|
||||
static void test_update_foo_from_123_to_456_then_fetch(void)
|
||||
{
|
||||
am.expr = expr_str_symbol(&store, "foo");
|
||||
am.val = expr_integer(&store, 123);
|
||||
env_set(&am, &store);
|
||||
am.val = expr_integer(&store, 456);
|
||||
env_set(&am, &store);
|
||||
am.expr = expr_str_symbol(&am, "foo");
|
||||
am.val = expr_integer(&am, 123);
|
||||
env_set(&am);
|
||||
am.val = expr_integer(&am, 456);
|
||||
env_set(&am);
|
||||
|
||||
am.expr = expr_str_symbol(&store, "foo");
|
||||
am.expr = expr_str_symbol(&am, "foo");
|
||||
am.val = NULL;
|
||||
env_fetch(&am);
|
||||
|
||||
|
||||
@@ -1,23 +1,18 @@
|
||||
#include "am.h"
|
||||
#include "env.h"
|
||||
#include "eval.h"
|
||||
#include "store.h"
|
||||
#include "unity.h"
|
||||
|
||||
static am_t am;
|
||||
static store_t store;
|
||||
|
||||
void test_prim_proc(am_t *am, store_t *store)
|
||||
void test_prim_proc(am_t *am)
|
||||
{
|
||||
(void)am;
|
||||
(void)store;
|
||||
}
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
am_init(&am);
|
||||
store_init(&store);
|
||||
env_init(&am, &store);
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
@@ -26,7 +21,7 @@ void tearDown(void)
|
||||
|
||||
static void test_42_self_evals(void)
|
||||
{
|
||||
am.expr = expr_integer(&store, 42);
|
||||
am.expr = expr_integer(&am, 42);
|
||||
|
||||
eval(&am);
|
||||
|
||||
@@ -38,7 +33,7 @@ static void test_42_self_evals(void)
|
||||
|
||||
static void test_empty_list_self_evals(void)
|
||||
{
|
||||
am.expr = expr_empty_list(&store);
|
||||
am.expr = expr_empty_list(&am);
|
||||
|
||||
eval(&am);
|
||||
|
||||
@@ -49,7 +44,7 @@ static void test_empty_list_self_evals(void)
|
||||
|
||||
static void test_prim_proc_self_evals(void)
|
||||
{
|
||||
am.expr = expr_prim_proc(&store, test_prim_proc);
|
||||
am.expr = expr_prim_proc(&am, test_prim_proc);
|
||||
|
||||
eval(&am);
|
||||
|
||||
@@ -61,9 +56,9 @@ static void test_prim_proc_self_evals(void)
|
||||
|
||||
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.expr = expr_str_symbol(&am, "foo");
|
||||
am.val = expr_integer(&am, 42);
|
||||
env_set(&am);
|
||||
am.val = NULL;
|
||||
|
||||
eval(&am);
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
#include "am.h"
|
||||
#include "expr.h"
|
||||
#include "store.h"
|
||||
#include "unity.h"
|
||||
|
||||
static store_t store;
|
||||
static am_t am;
|
||||
|
||||
void test_prim_proc(am_t *am, store_t *store)
|
||||
void test_prim_proc(am_t *am)
|
||||
{
|
||||
(void)am;
|
||||
(void)store;
|
||||
}
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
store_init(&store);
|
||||
am_init(&am);
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
@@ -22,7 +20,7 @@ void tearDown(void)
|
||||
|
||||
static void test_expr_integer_123(void)
|
||||
{
|
||||
expr_t *expr = expr_integer(&store, 123);
|
||||
expr_t *expr = expr_integer(&am, 123);
|
||||
TEST_ASSERT_NOT_NULL(expr);
|
||||
TEST_ASSERT_TRUE(expr->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, expr->atom.type);
|
||||
@@ -31,7 +29,7 @@ static void test_expr_integer_123(void)
|
||||
|
||||
static void test_expr_integer_456(void)
|
||||
{
|
||||
expr_t *expr = expr_integer(&store, 456);
|
||||
expr_t *expr = expr_integer(&am, 456);
|
||||
TEST_ASSERT_NOT_NULL(expr);
|
||||
TEST_ASSERT_TRUE(expr->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, expr->atom.type);
|
||||
@@ -41,7 +39,7 @@ static void test_expr_integer_456(void)
|
||||
static void test_expr_symbol_foo(void)
|
||||
{
|
||||
const symbol_t symbol = { .buf = "foo", .len = 3 };
|
||||
expr_t *expr = expr_symbol(&store, &symbol);
|
||||
expr_t *expr = expr_symbol(&am, &symbol);
|
||||
TEST_ASSERT_NOT_NULL(expr);
|
||||
TEST_ASSERT_TRUE(expr->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, expr->atom.type);
|
||||
@@ -52,7 +50,7 @@ static void test_expr_symbol_foo(void)
|
||||
static void test_expr_symbol_quux(void)
|
||||
{
|
||||
const symbol_t symbol = { .buf = "quux", .len = 4 };
|
||||
expr_t *expr = expr_symbol(&store, &symbol);
|
||||
expr_t *expr = expr_symbol(&am, &symbol);
|
||||
TEST_ASSERT_NOT_NULL(expr);
|
||||
TEST_ASSERT_TRUE(expr->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, expr->atom.type);
|
||||
@@ -62,7 +60,7 @@ static void test_expr_symbol_quux(void)
|
||||
|
||||
static void test_expr_str_symbol_foo(void)
|
||||
{
|
||||
expr_t *expr = expr_str_symbol(&store, "foo");
|
||||
expr_t *expr = expr_str_symbol(&am, "foo");
|
||||
TEST_ASSERT_NOT_NULL(expr);
|
||||
TEST_ASSERT_TRUE(expr->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, expr->atom.type);
|
||||
@@ -72,7 +70,7 @@ static void test_expr_str_symbol_foo(void)
|
||||
|
||||
static void test_expr_str_symbol_quux(void)
|
||||
{
|
||||
expr_t *expr = expr_str_symbol(&store, "quux");
|
||||
expr_t *expr = expr_str_symbol(&am, "quux");
|
||||
TEST_ASSERT_NOT_NULL(expr);
|
||||
TEST_ASSERT_TRUE(expr->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, expr->atom.type);
|
||||
@@ -82,7 +80,7 @@ static void test_expr_str_symbol_quux(void)
|
||||
|
||||
static void test_expr_empty_list(void)
|
||||
{
|
||||
expr_t *expr = expr_empty_list(&store);
|
||||
expr_t *expr = expr_empty_list(&am);
|
||||
TEST_ASSERT_NOT_NULL(expr);
|
||||
TEST_ASSERT_TRUE(expr->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_EMPTY_LIST, expr->atom.type);
|
||||
@@ -90,9 +88,9 @@ static void test_expr_empty_list(void)
|
||||
|
||||
static void test_expr_pair(void)
|
||||
{
|
||||
expr_t *car = expr_integer(&store, 123);
|
||||
expr_t *cdr = expr_integer(&store, 456);
|
||||
expr_t *expr = expr_pair(&store, car, cdr);
|
||||
expr_t *car = expr_integer(&am, 123);
|
||||
expr_t *cdr = expr_integer(&am, 456);
|
||||
expr_t *expr = expr_pair(&am, car, cdr);
|
||||
TEST_ASSERT_NOT_NULL(expr);
|
||||
TEST_ASSERT_FALSE(expr->is_atom);
|
||||
TEST_ASSERT_EQUAL(car, expr->pair.car);
|
||||
@@ -102,7 +100,7 @@ static void test_expr_pair(void)
|
||||
static void test_expr_prim_proc(void)
|
||||
{
|
||||
|
||||
expr_t *expr = expr_prim_proc(&store, test_prim_proc);
|
||||
expr_t *expr = expr_prim_proc(&am, test_prim_proc);
|
||||
TEST_ASSERT_NOT_NULL(expr);
|
||||
TEST_ASSERT_TRUE(expr->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_PRIM_PROC, expr->atom.type);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "parse.h"
|
||||
#include "unity.h"
|
||||
|
||||
static store_t store;
|
||||
static am_t am;
|
||||
static parse_ctx_t ctx;
|
||||
|
||||
@@ -9,9 +8,8 @@ static parse_ctx_t ctx;
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
store_init(&store);
|
||||
am_init(&am);
|
||||
parse_init(&am, &store, &ctx);
|
||||
parse_init(&am, &ctx);
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
|
||||
@@ -3,13 +3,10 @@
|
||||
#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)
|
||||
@@ -18,14 +15,14 @@ void tearDown(void)
|
||||
|
||||
static void test_add_empty_list_is_0(void)
|
||||
{
|
||||
am.expr = expr_str_symbol(&store, "+");
|
||||
am.expr = expr_str_symbol(&am, "+");
|
||||
env_fetch(&am);
|
||||
TEST_ASSERT_NOT_NULL(am.val);
|
||||
TEST_ASSERT_TRUE(am.val->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_PRIM_PROC, am.val->atom.type);
|
||||
|
||||
am.argl = expr_empty_list(&store);
|
||||
am.val->atom.prim_proc(&am, &store);
|
||||
am.argl = expr_empty_list(&am);
|
||||
am.val->atom.prim_proc(&am);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(am.val);
|
||||
TEST_ASSERT_TRUE(am.val->is_atom);
|
||||
@@ -35,19 +32,18 @@ static void test_add_empty_list_is_0(void)
|
||||
|
||||
static void test_add_1_2_3_is_6(void)
|
||||
{
|
||||
am.expr = expr_str_symbol(&store, "+");
|
||||
am.expr = expr_str_symbol(&am, "+");
|
||||
env_fetch(&am);
|
||||
TEST_ASSERT_NOT_NULL(am.val);
|
||||
TEST_ASSERT_TRUE(am.val->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_PRIM_PROC, am.val->atom.type);
|
||||
|
||||
am.argl = expr_pair(
|
||||
&store, expr_integer(&store, 1),
|
||||
&am, expr_integer(&am, 1),
|
||||
expr_pair(
|
||||
&store, expr_integer(&store, 2),
|
||||
expr_pair(
|
||||
&store, expr_integer(&store, 3), expr_empty_list(&store))));
|
||||
am.val->atom.prim_proc(&am, &store);
|
||||
&am, expr_integer(&am, 2),
|
||||
expr_pair(&am, expr_integer(&am, 3), expr_empty_list(&am))));
|
||||
am.val->atom.prim_proc(&am);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(am.val);
|
||||
TEST_ASSERT_TRUE(am.val->is_atom);
|
||||
@@ -57,14 +53,14 @@ static void test_add_1_2_3_is_6(void)
|
||||
|
||||
static void test_mul_empty_list_is_1(void)
|
||||
{
|
||||
am.expr = expr_str_symbol(&store, "*");
|
||||
am.expr = expr_str_symbol(&am, "*");
|
||||
env_fetch(&am);
|
||||
TEST_ASSERT_NOT_NULL(am.val);
|
||||
TEST_ASSERT_TRUE(am.val->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_PRIM_PROC, am.val->atom.type);
|
||||
|
||||
am.argl = expr_empty_list(&store);
|
||||
am.val->atom.prim_proc(&am, &store);
|
||||
am.argl = expr_empty_list(&am);
|
||||
am.val->atom.prim_proc(&am);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(am.val);
|
||||
TEST_ASSERT_TRUE(am.val->is_atom);
|
||||
@@ -74,19 +70,18 @@ static void test_mul_empty_list_is_1(void)
|
||||
|
||||
static void test_mul_2_3_4_is_24(void)
|
||||
{
|
||||
am.expr = expr_str_symbol(&store, "*");
|
||||
am.expr = expr_str_symbol(&am, "*");
|
||||
env_fetch(&am);
|
||||
TEST_ASSERT_NOT_NULL(am.val);
|
||||
TEST_ASSERT_TRUE(am.val->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_PRIM_PROC, am.val->atom.type);
|
||||
|
||||
am.argl = expr_pair(
|
||||
&store, expr_integer(&store, 2),
|
||||
&am, expr_integer(&am, 2),
|
||||
expr_pair(
|
||||
&store, expr_integer(&store, 3),
|
||||
expr_pair(
|
||||
&store, expr_integer(&store, 4), expr_empty_list(&store))));
|
||||
am.val->atom.prim_proc(&am, &store);
|
||||
&am, expr_integer(&am, 3),
|
||||
expr_pair(&am, expr_integer(&am, 4), expr_empty_list(&am))));
|
||||
am.val->atom.prim_proc(&am);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(am.val);
|
||||
TEST_ASSERT_TRUE(am.val->is_atom);
|
||||
@@ -96,15 +91,14 @@ static void test_mul_2_3_4_is_24(void)
|
||||
|
||||
static void test_sub_1_is_minus_1(void)
|
||||
{
|
||||
am.expr = expr_str_symbol(&store, "-");
|
||||
am.expr = expr_str_symbol(&am, "-");
|
||||
env_fetch(&am);
|
||||
TEST_ASSERT_NOT_NULL(am.val);
|
||||
TEST_ASSERT_TRUE(am.val->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_PRIM_PROC, am.val->atom.type);
|
||||
|
||||
am.argl = expr_pair(
|
||||
&store, expr_integer(&store, 1), expr_empty_list(&store));
|
||||
am.val->atom.prim_proc(&am, &store);
|
||||
am.argl = expr_pair(&am, expr_integer(&am, 1), expr_empty_list(&am));
|
||||
am.val->atom.prim_proc(&am);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(am.val);
|
||||
TEST_ASSERT_TRUE(am.val->is_atom);
|
||||
@@ -114,19 +108,18 @@ static void test_sub_1_is_minus_1(void)
|
||||
|
||||
static void test_sub_5_4_3_is_minus_2(void)
|
||||
{
|
||||
am.expr = expr_str_symbol(&store, "-");
|
||||
am.expr = expr_str_symbol(&am, "-");
|
||||
env_fetch(&am);
|
||||
TEST_ASSERT_NOT_NULL(am.val);
|
||||
TEST_ASSERT_TRUE(am.val->is_atom);
|
||||
TEST_ASSERT_EQUAL(ATOM_TYPE_PRIM_PROC, am.val->atom.type);
|
||||
|
||||
am.argl = expr_pair(
|
||||
&store, expr_integer(&store, 5),
|
||||
&am, expr_integer(&am, 5),
|
||||
expr_pair(
|
||||
&store, expr_integer(&store, 4),
|
||||
expr_pair(
|
||||
&store, expr_integer(&store, 3), expr_empty_list(&store))));
|
||||
am.val->atom.prim_proc(&am, &store);
|
||||
&am, expr_integer(&am, 4),
|
||||
expr_pair(&am, expr_integer(&am, 3), expr_empty_list(&am))));
|
||||
am.val->atom.prim_proc(&am);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(am.val);
|
||||
TEST_ASSERT_TRUE(am.val->is_atom);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#include "store.h"
|
||||
#include "am.h"
|
||||
#include "unity.h"
|
||||
|
||||
static store_t store;
|
||||
static am_t am;
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
store_init(&store);
|
||||
am_init(&am);
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
@@ -14,14 +14,14 @@ void tearDown(void)
|
||||
|
||||
static void test_alloc_returns_non_null_after_init(void)
|
||||
{
|
||||
const expr_t *const expr = store_alloc(&store);
|
||||
const expr_t *const expr = store_alloc(&am);
|
||||
TEST_ASSERT_NOT_NULL(expr);
|
||||
}
|
||||
|
||||
static void test_two_calls_to_alloc_return_distinct(void)
|
||||
{
|
||||
const expr_t *const a = store_alloc(&store);
|
||||
const expr_t *const b = store_alloc(&store);
|
||||
const expr_t *const a = store_alloc(&am);
|
||||
const expr_t *const b = store_alloc(&am);
|
||||
TEST_ASSERT_NOT_EQUAL(a, b);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user