Create environment module
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
add_library(imp
|
add_library(imp
|
||||||
am.c
|
am.c
|
||||||
|
env.c
|
||||||
memory_stream.c
|
memory_stream.c
|
||||||
parse.c
|
parse.c
|
||||||
store.c
|
store.c
|
||||||
|
|||||||
80
lib/env.c
Normal file
80
lib/env.c
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
#include "env.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static bool symbol_eq(symbol_t *s, symbol_t *t)
|
||||||
|
{
|
||||||
|
return s->len == t->len && memcmp(s->buf, t->buf, s->len) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static expr_t **lookup(am_t *am, bool *found)
|
||||||
|
{
|
||||||
|
assert(am->expr != NULL);
|
||||||
|
assert(am->expr->is_atom);
|
||||||
|
assert(am->expr->atom.type == ATOM_TYPE_SYMBOL);
|
||||||
|
|
||||||
|
if (am->env->is_atom) {
|
||||||
|
assert(am->env->atom.type == ATOM_TYPE_EMPTY_LIST);
|
||||||
|
*found = false;
|
||||||
|
return &am->env;
|
||||||
|
}
|
||||||
|
|
||||||
|
expr_t *prev;
|
||||||
|
for (expr_t *list = am->env; !list->is_atom; list = list->pair.cdr) {
|
||||||
|
assert(list != NULL);
|
||||||
|
|
||||||
|
expr_t *entry = list->pair.car;
|
||||||
|
assert(!entry->is_atom);
|
||||||
|
|
||||||
|
expr_t *key = entry->pair.car;
|
||||||
|
assert(key != NULL);
|
||||||
|
assert(key->is_atom);
|
||||||
|
assert(key->atom.type == ATOM_TYPE_SYMBOL);
|
||||||
|
|
||||||
|
if (symbol_eq(&am->expr->atom.symbol, &key->atom.symbol)) {
|
||||||
|
*found = true;
|
||||||
|
return &entry->pair.cdr;
|
||||||
|
}
|
||||||
|
|
||||||
|
prev = list;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(!prev->is_atom);
|
||||||
|
assert(prev->pair.cdr->is_atom);
|
||||||
|
assert(prev->pair.cdr->atom.type == ATOM_TYPE_EMPTY_LIST);
|
||||||
|
*found = false;
|
||||||
|
return &prev->pair.cdr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void env_init(am_t *am, store_t *store)
|
||||||
|
{
|
||||||
|
am->env = store_alloc(store);
|
||||||
|
am->env->is_atom = true;
|
||||||
|
am->env->atom.type = ATOM_TYPE_EMPTY_LIST;
|
||||||
|
}
|
||||||
|
|
||||||
|
void env_fetch(am_t *am)
|
||||||
|
{
|
||||||
|
bool found;
|
||||||
|
expr_t *val = *lookup(am, &found);
|
||||||
|
am->val = found ? val : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void env_set(am_t *am, store_t *store)
|
||||||
|
{
|
||||||
|
bool found;
|
||||||
|
expr_t **loc = lookup(am, &found);
|
||||||
|
if (found) {
|
||||||
|
*loc = am->val;
|
||||||
|
} else {
|
||||||
|
(*loc)->is_atom = false;
|
||||||
|
(*loc)->pair.cdr = store_alloc(store);
|
||||||
|
(*loc)->pair.cdr->is_atom = true;
|
||||||
|
(*loc)->pair.cdr->atom.type = ATOM_TYPE_EMPTY_LIST;
|
||||||
|
|
||||||
|
expr_t *entry = (*loc)->pair.car = store_alloc(store);
|
||||||
|
entry->pair.car = am->expr;
|
||||||
|
entry->pair.cdr = am->val;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
#define AM_STACK_SIZE 128U
|
#define AM_STACK_SIZE 128U
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
expr_t *expr;
|
expr_t *env, *expr, *val;
|
||||||
expr_t **sp, *stack[AM_STACK_SIZE];
|
expr_t **sp, *stack[AM_STACK_SIZE];
|
||||||
} am_t;
|
} am_t;
|
||||||
|
|
||||||
|
|||||||
11
lib/include/env.h
Normal file
11
lib/include/env.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef ENV_H
|
||||||
|
#define ENV_H
|
||||||
|
|
||||||
|
#include "am.h"
|
||||||
|
#include "store.h"
|
||||||
|
|
||||||
|
void env_init(am_t *am, store_t *store);
|
||||||
|
void env_fetch(am_t *am);
|
||||||
|
void env_set(am_t *am, store_t *store);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -10,6 +10,7 @@ endfunction()
|
|||||||
|
|
||||||
add_test_suites(
|
add_test_suites(
|
||||||
am_tests.c
|
am_tests.c
|
||||||
|
env_tests.c
|
||||||
parse_tests.c
|
parse_tests.c
|
||||||
store_tests.c
|
store_tests.c
|
||||||
token_tests.c
|
token_tests.c
|
||||||
|
|||||||
78
tests/env_tests.c
Normal file
78
tests/env_tests.c
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
#include "env.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static am_t am;
|
||||||
|
static store_t store;
|
||||||
|
|
||||||
|
static expr_t *integer(int64_t value)
|
||||||
|
{
|
||||||
|
expr_t *expr = store_alloc(&store);
|
||||||
|
expr->is_atom = true;
|
||||||
|
expr->atom.type = ATOM_TYPE_INTEGER;
|
||||||
|
expr->atom.integer = value;
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static expr_t *symbol(const char *s)
|
||||||
|
{
|
||||||
|
expr_t *expr = store_alloc(&store);
|
||||||
|
expr->is_atom = true;
|
||||||
|
expr->atom.type = ATOM_TYPE_SYMBOL;
|
||||||
|
memcpy(expr->atom.symbol.buf, s, strlen(s));
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUp(void)
|
||||||
|
{
|
||||||
|
am_init(&am);
|
||||||
|
store_init(&store);
|
||||||
|
env_init(&am, &store);
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_set_foo_to_42_then_fetch(void)
|
||||||
|
{
|
||||||
|
am.expr = symbol("foo");
|
||||||
|
am.val = integer(42);
|
||||||
|
env_set(&am, &store);
|
||||||
|
|
||||||
|
am.expr = symbol("foo");
|
||||||
|
am.val = NULL;
|
||||||
|
env_fetch(&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_update_foo_from_123_to_456_then_fetch(void)
|
||||||
|
{
|
||||||
|
am.expr = symbol("foo");
|
||||||
|
am.val = integer(123);
|
||||||
|
env_set(&am, &store);
|
||||||
|
am.val = integer(456);
|
||||||
|
env_set(&am, &store);
|
||||||
|
|
||||||
|
am.expr = symbol("foo");
|
||||||
|
am.val = NULL;
|
||||||
|
env_fetch(&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(456, am.val->atom.integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
RUN_TEST(test_set_foo_to_42_then_fetch);
|
||||||
|
RUN_TEST(test_update_foo_from_123_to_456_then_fetch);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user