Consolidate expression creation functions in expr module
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
add_library(imp
|
add_library(imp
|
||||||
am.c
|
am.c
|
||||||
env.c
|
env.c
|
||||||
|
expr.c
|
||||||
memory_stream.c
|
memory_stream.c
|
||||||
parse.c
|
parse.c
|
||||||
store.c
|
store.c
|
||||||
|
|||||||
13
lib/env.c
13
lib/env.c
@@ -49,9 +49,7 @@ static expr_t **lookup(am_t *am, bool *found)
|
|||||||
|
|
||||||
void env_init(am_t *am, store_t *store)
|
void env_init(am_t *am, store_t *store)
|
||||||
{
|
{
|
||||||
am->env = store_alloc(store);
|
am->env = expr_empty_list(store);
|
||||||
am->env->is_atom = true;
|
|
||||||
am->env->atom.type = ATOM_TYPE_EMPTY_LIST;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void env_fetch(am_t *am)
|
void env_fetch(am_t *am)
|
||||||
@@ -69,12 +67,7 @@ void env_set(am_t *am, store_t *store)
|
|||||||
*loc = am->val;
|
*loc = am->val;
|
||||||
} else {
|
} else {
|
||||||
(*loc)->is_atom = false;
|
(*loc)->is_atom = false;
|
||||||
(*loc)->pair.cdr = store_alloc(store);
|
(*loc)->pair.cdr = expr_empty_list(store);
|
||||||
(*loc)->pair.cdr->is_atom = true;
|
(*loc)->pair.car = expr_pair(store, am->expr, am->val);
|
||||||
(*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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
46
lib/expr.c
Normal file
46
lib/expr.c
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#include "expr.h"
|
||||||
|
#include "store.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
expr_t *expr_integer(store_t *store, 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
expr_t *expr_symbol(store_t *store, const symbol_t *symbol)
|
||||||
|
{
|
||||||
|
expr_t *expr = store_alloc(store);
|
||||||
|
expr->is_atom = true;
|
||||||
|
expr->atom.type = ATOM_TYPE_SYMBOL;
|
||||||
|
memcpy(&expr->atom.symbol, symbol, sizeof(symbol_t));
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
expr_t *expr_str_symbol(store_t *store, const char *str)
|
||||||
|
{
|
||||||
|
symbol_t symbol = { .len = strlen(str) };
|
||||||
|
memcpy(symbol.buf, str, symbol.len);
|
||||||
|
return expr_symbol(store, &symbol);
|
||||||
|
}
|
||||||
|
|
||||||
|
expr_t *expr_empty_list(store_t *store)
|
||||||
|
{
|
||||||
|
expr_t *expr = store_alloc(store);
|
||||||
|
expr->is_atom = true;
|
||||||
|
expr->atom.type = ATOM_TYPE_EMPTY_LIST;
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
expr_t *expr_pair(store_t *store, expr_t *car, expr_t *cdr)
|
||||||
|
{
|
||||||
|
expr_t *expr = store_alloc(store);
|
||||||
|
expr->is_atom = false;
|
||||||
|
expr->pair.car = car;
|
||||||
|
expr->pair.cdr = cdr;
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
@@ -40,4 +40,12 @@ typedef struct expr {
|
|||||||
};
|
};
|
||||||
} expr_t;
|
} expr_t;
|
||||||
|
|
||||||
|
struct store;
|
||||||
|
|
||||||
|
expr_t *expr_integer(struct store *store, int64_t value);
|
||||||
|
expr_t *expr_symbol(struct store *store, const symbol_t *symbol);
|
||||||
|
expr_t *expr_str_symbol(struct store *store, const char *str);
|
||||||
|
expr_t *expr_empty_list(struct store *store);
|
||||||
|
expr_t *expr_pair(struct store *store, expr_t *car, expr_t *cdr);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
#define STORE_SIZE 256U
|
#define STORE_SIZE 256U
|
||||||
|
|
||||||
typedef struct {
|
typedef struct store {
|
||||||
expr_t *free;
|
expr_t *free;
|
||||||
expr_t buffer[STORE_SIZE];
|
expr_t buffer[STORE_SIZE];
|
||||||
} store_t;
|
} store_t;
|
||||||
|
|||||||
55
lib/parse.c
55
lib/parse.c
@@ -23,33 +23,16 @@ static parse_state_t pop_state(parse_ctx_t *ctx)
|
|||||||
return *++ctx->sp;
|
return *++ctx->sp;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void load_integer(parse_ctx_t *ctx, expr_t **expr, int64_t integer)
|
static void append(parse_ctx_t *ctx, expr_t *expr)
|
||||||
{
|
{
|
||||||
*expr = store_alloc(ctx->store);
|
expr_t *list = ctx->am->expr;
|
||||||
(*expr)->is_atom = true;
|
while (!list->is_atom)
|
||||||
(*expr)->atom.type = ATOM_TYPE_INTEGER;
|
list = list->pair.cdr;
|
||||||
(*expr)->atom.integer = integer;
|
assert(list->atom.type == ATOM_TYPE_EMPTY_LIST);
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
list->is_atom = false;
|
||||||
load_symbol(parse_ctx_t *ctx, expr_t **expr, const symbol_t *symbol)
|
list->pair.car = expr;
|
||||||
{
|
list->pair.cdr = expr_empty_list(ctx->store);
|
||||||
*expr = store_alloc(ctx->store);
|
|
||||||
(*expr)->is_atom = true;
|
|
||||||
(*expr)->atom.type = ATOM_TYPE_SYMBOL;
|
|
||||||
memcpy(&(*expr)->atom.symbol, symbol, sizeof(symbol_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
static expr_t **append(parse_ctx_t *ctx, expr_t *expr)
|
|
||||||
{
|
|
||||||
while (!expr->is_atom)
|
|
||||||
expr = expr->pair.cdr;
|
|
||||||
assert(expr->atom.type == ATOM_TYPE_EMPTY_LIST);
|
|
||||||
expr->is_atom = false;
|
|
||||||
expr->pair.cdr = store_alloc(ctx->store);
|
|
||||||
expr->pair.cdr->is_atom = true;
|
|
||||||
expr->pair.cdr->atom.type = ATOM_TYPE_EMPTY_LIST;
|
|
||||||
return &expr->pair.car;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
||||||
@@ -58,18 +41,16 @@ parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
|||||||
case PARSE_STATE_INIT:
|
case PARSE_STATE_INIT:
|
||||||
switch (token->type) {
|
switch (token->type) {
|
||||||
case TOKEN_TYPE_INTEGER:
|
case TOKEN_TYPE_INTEGER:
|
||||||
load_integer(ctx, &ctx->am->expr, token->integer);
|
ctx->am->expr = expr_integer(ctx->store, token->integer);
|
||||||
ctx->state = PARSE_STATE_DONE;
|
ctx->state = PARSE_STATE_DONE;
|
||||||
break;
|
break;
|
||||||
case TOKEN_TYPE_SYMBOL:
|
case TOKEN_TYPE_SYMBOL:
|
||||||
load_symbol(ctx, &ctx->am->expr, &token->symbol);
|
ctx->am->expr = expr_symbol(ctx->store, &token->symbol);
|
||||||
ctx->state = PARSE_STATE_DONE;
|
ctx->state = PARSE_STATE_DONE;
|
||||||
break;
|
break;
|
||||||
case TOKEN_TYPE_OPEN_PAREN:
|
case TOKEN_TYPE_OPEN_PAREN:
|
||||||
push_state(ctx, PARSE_STATE_DONE);
|
push_state(ctx, PARSE_STATE_DONE);
|
||||||
ctx->am->expr = store_alloc(ctx->store);
|
ctx->am->expr = expr_empty_list(ctx->store);
|
||||||
ctx->am->expr->is_atom = true;
|
|
||||||
ctx->am->expr->atom.type = ATOM_TYPE_EMPTY_LIST;
|
|
||||||
ctx->state = PARSE_STATE_LIST;
|
ctx->state = PARSE_STATE_LIST;
|
||||||
break;
|
break;
|
||||||
case TOKEN_TYPE_CLOSE_PAREN:
|
case TOKEN_TYPE_CLOSE_PAREN:
|
||||||
@@ -80,21 +61,16 @@ parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
|||||||
|
|
||||||
case PARSE_STATE_LIST:
|
case PARSE_STATE_LIST:
|
||||||
switch (token->type) {
|
switch (token->type) {
|
||||||
expr_t **end_car;
|
|
||||||
case TOKEN_TYPE_INTEGER:
|
case TOKEN_TYPE_INTEGER:
|
||||||
end_car = append(ctx, ctx->am->expr);
|
append(ctx, expr_integer(ctx->store, token->integer));
|
||||||
load_integer(ctx, end_car, token->integer);
|
|
||||||
break;
|
break;
|
||||||
case TOKEN_TYPE_SYMBOL:
|
case TOKEN_TYPE_SYMBOL:
|
||||||
end_car = append(ctx, ctx->am->expr);
|
append(ctx, expr_symbol(ctx->store, &token->symbol));
|
||||||
load_symbol(ctx, end_car, &token->symbol);
|
|
||||||
break;
|
break;
|
||||||
case TOKEN_TYPE_OPEN_PAREN:
|
case TOKEN_TYPE_OPEN_PAREN:
|
||||||
am_push(ctx->am);
|
am_push(ctx->am);
|
||||||
push_state(ctx, PARSE_STATE_LIST);
|
push_state(ctx, PARSE_STATE_LIST);
|
||||||
ctx->am->expr = store_alloc(ctx->store);
|
ctx->am->expr = expr_empty_list(ctx->store);
|
||||||
ctx->am->expr->is_atom = true;
|
|
||||||
ctx->am->expr->atom.type = ATOM_TYPE_EMPTY_LIST;
|
|
||||||
ctx->state = PARSE_STATE_LIST;
|
ctx->state = PARSE_STATE_LIST;
|
||||||
break;
|
break;
|
||||||
case TOKEN_TYPE_CLOSE_PAREN:
|
case TOKEN_TYPE_CLOSE_PAREN:
|
||||||
@@ -102,8 +78,7 @@ parse_state_t parse_proc(parse_ctx_t *ctx, const token_t *token)
|
|||||||
if (ctx->state == PARSE_STATE_LIST) {
|
if (ctx->state == PARSE_STATE_LIST) {
|
||||||
expr_t *expr = ctx->am->expr;
|
expr_t *expr = ctx->am->expr;
|
||||||
am_pop(ctx->am);
|
am_pop(ctx->am);
|
||||||
end_car = append(ctx, ctx->am->expr);
|
append(ctx, expr);
|
||||||
*end_car = expr;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ endfunction()
|
|||||||
add_test_suites(
|
add_test_suites(
|
||||||
am_tests.c
|
am_tests.c
|
||||||
env_tests.c
|
env_tests.c
|
||||||
|
expr_tests.c
|
||||||
parse_tests.c
|
parse_tests.c
|
||||||
store_tests.c
|
store_tests.c
|
||||||
token_tests.c
|
token_tests.c
|
||||||
|
|||||||
@@ -6,24 +6,6 @@
|
|||||||
static am_t am;
|
static am_t am;
|
||||||
static store_t store;
|
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)
|
void setUp(void)
|
||||||
{
|
{
|
||||||
am_init(&am);
|
am_init(&am);
|
||||||
@@ -37,11 +19,11 @@ void tearDown(void)
|
|||||||
|
|
||||||
static void test_set_foo_to_42_then_fetch(void)
|
static void test_set_foo_to_42_then_fetch(void)
|
||||||
{
|
{
|
||||||
am.expr = symbol("foo");
|
am.expr = expr_str_symbol(&store, "foo");
|
||||||
am.val = integer(42);
|
am.val = expr_integer(&store, 42);
|
||||||
env_set(&am, &store);
|
env_set(&am, &store);
|
||||||
|
|
||||||
am.expr = symbol("foo");
|
am.expr = expr_str_symbol(&store, "foo");
|
||||||
am.val = NULL;
|
am.val = NULL;
|
||||||
env_fetch(&am);
|
env_fetch(&am);
|
||||||
|
|
||||||
@@ -53,13 +35,13 @@ static void test_set_foo_to_42_then_fetch(void)
|
|||||||
|
|
||||||
static void test_update_foo_from_123_to_456_then_fetch(void)
|
static void test_update_foo_from_123_to_456_then_fetch(void)
|
||||||
{
|
{
|
||||||
am.expr = symbol("foo");
|
am.expr = expr_str_symbol(&store, "foo");
|
||||||
am.val = integer(123);
|
am.val = expr_integer(&store, 123);
|
||||||
env_set(&am, &store);
|
env_set(&am, &store);
|
||||||
am.val = integer(456);
|
am.val = expr_integer(&store, 456);
|
||||||
env_set(&am, &store);
|
env_set(&am, &store);
|
||||||
|
|
||||||
am.expr = symbol("foo");
|
am.expr = expr_str_symbol(&store, "foo");
|
||||||
am.val = NULL;
|
am.val = NULL;
|
||||||
env_fetch(&am);
|
env_fetch(&am);
|
||||||
|
|
||||||
|
|||||||
107
tests/expr_tests.c
Normal file
107
tests/expr_tests.c
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
#include "expr.h"
|
||||||
|
#include "store.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
static store_t store;
|
||||||
|
|
||||||
|
void setUp(void)
|
||||||
|
{
|
||||||
|
store_init(&store);
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_expr_integer_123(void)
|
||||||
|
{
|
||||||
|
expr_t *expr = expr_integer(&store, 123);
|
||||||
|
TEST_ASSERT_NOT_NULL(expr);
|
||||||
|
TEST_ASSERT_TRUE(expr->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, expr->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(123, expr->atom.integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_expr_integer_456(void)
|
||||||
|
{
|
||||||
|
expr_t *expr = expr_integer(&store, 456);
|
||||||
|
TEST_ASSERT_NOT_NULL(expr);
|
||||||
|
TEST_ASSERT_TRUE(expr->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, expr->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(456, expr->atom.integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_expr_symbol_foo(void)
|
||||||
|
{
|
||||||
|
const symbol_t symbol = { .buf = "foo", .len = 3 };
|
||||||
|
expr_t *expr = expr_symbol(&store, &symbol);
|
||||||
|
TEST_ASSERT_NOT_NULL(expr);
|
||||||
|
TEST_ASSERT_TRUE(expr->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, expr->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(3, expr->atom.symbol.len);
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY("foo", expr->atom.symbol.buf, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_expr_symbol_quux(void)
|
||||||
|
{
|
||||||
|
const symbol_t symbol = { .buf = "quux", .len = 4 };
|
||||||
|
expr_t *expr = expr_symbol(&store, &symbol);
|
||||||
|
TEST_ASSERT_NOT_NULL(expr);
|
||||||
|
TEST_ASSERT_TRUE(expr->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, expr->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(4, expr->atom.symbol.len);
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY("quux", expr->atom.symbol.buf, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_expr_str_symbol_foo(void)
|
||||||
|
{
|
||||||
|
expr_t *expr = expr_str_symbol(&store, "foo");
|
||||||
|
TEST_ASSERT_NOT_NULL(expr);
|
||||||
|
TEST_ASSERT_TRUE(expr->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, expr->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(3, expr->atom.symbol.len);
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY("foo", expr->atom.symbol.buf, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_expr_str_symbol_quux(void)
|
||||||
|
{
|
||||||
|
expr_t *expr = expr_str_symbol(&store, "quux");
|
||||||
|
TEST_ASSERT_NOT_NULL(expr);
|
||||||
|
TEST_ASSERT_TRUE(expr->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, expr->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(4, expr->atom.symbol.len);
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY("quux", expr->atom.symbol.buf, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_expr_empty_list(void)
|
||||||
|
{
|
||||||
|
expr_t *expr = expr_empty_list(&store);
|
||||||
|
TEST_ASSERT_NOT_NULL(expr);
|
||||||
|
TEST_ASSERT_TRUE(expr->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_EMPTY_LIST, expr->atom.type);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
TEST_ASSERT_NOT_NULL(expr);
|
||||||
|
TEST_ASSERT_FALSE(expr->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(car, expr->pair.car);
|
||||||
|
TEST_ASSERT_EQUAL(cdr, expr->pair.cdr);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
RUN_TEST(test_expr_integer_123);
|
||||||
|
RUN_TEST(test_expr_integer_456);
|
||||||
|
RUN_TEST(test_expr_symbol_foo);
|
||||||
|
RUN_TEST(test_expr_symbol_quux);
|
||||||
|
RUN_TEST(test_expr_str_symbol_foo);
|
||||||
|
RUN_TEST(test_expr_str_symbol_quux);
|
||||||
|
RUN_TEST(test_expr_empty_list);
|
||||||
|
RUN_TEST(test_expr_pair);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user