Add primitive procedures to expression type
This commit is contained in:
@@ -10,6 +10,7 @@ void eval(am_t *am)
|
|||||||
switch (am->expr->atom.type) {
|
switch (am->expr->atom.type) {
|
||||||
case ATOM_TYPE_EMPTY_LIST:
|
case ATOM_TYPE_EMPTY_LIST:
|
||||||
case ATOM_TYPE_INTEGER:
|
case ATOM_TYPE_INTEGER:
|
||||||
|
case ATOM_TYPE_PRIM_PROC:
|
||||||
am->val = am->expr;
|
am->val = am->expr;
|
||||||
break;
|
break;
|
||||||
case ATOM_TYPE_SYMBOL:
|
case ATOM_TYPE_SYMBOL:
|
||||||
|
|||||||
@@ -44,3 +44,12 @@ expr_t *expr_pair(store_t *store, expr_t *car, expr_t *cdr)
|
|||||||
expr->pair.cdr = cdr;
|
expr->pair.cdr = cdr;
|
||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expr_t *expr_prim_proc(store_t *store, prim_proc_t prim_proc)
|
||||||
|
{
|
||||||
|
expr_t *expr = store_alloc(store);
|
||||||
|
expr->is_atom = true;
|
||||||
|
expr->atom.type = ATOM_TYPE_PRIM_PROC;
|
||||||
|
expr->atom.prim_proc = prim_proc;
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,15 +7,21 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
struct am;
|
||||||
|
struct store;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char buf[MAX_SYMBOL_LEN];
|
char buf[MAX_SYMBOL_LEN];
|
||||||
size_t len;
|
size_t len;
|
||||||
} symbol_t;
|
} symbol_t;
|
||||||
|
|
||||||
|
typedef void (*prim_proc_t)(struct am *am, struct store *store);
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
ATOM_TYPE_EMPTY_LIST,
|
ATOM_TYPE_EMPTY_LIST,
|
||||||
ATOM_TYPE_INTEGER,
|
ATOM_TYPE_INTEGER,
|
||||||
ATOM_TYPE_SYMBOL,
|
ATOM_TYPE_SYMBOL,
|
||||||
|
ATOM_TYPE_PRIM_PROC,
|
||||||
} atom_type_t;
|
} atom_type_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -23,6 +29,7 @@ typedef struct {
|
|||||||
union {
|
union {
|
||||||
int64_t integer;
|
int64_t integer;
|
||||||
symbol_t symbol;
|
symbol_t symbol;
|
||||||
|
prim_proc_t prim_proc;
|
||||||
};
|
};
|
||||||
} atom_t;
|
} atom_t;
|
||||||
|
|
||||||
@@ -40,12 +47,11 @@ typedef struct expr {
|
|||||||
};
|
};
|
||||||
} expr_t;
|
} expr_t;
|
||||||
|
|
||||||
struct store;
|
|
||||||
|
|
||||||
expr_t *expr_integer(struct store *store, int64_t value);
|
expr_t *expr_integer(struct store *store, int64_t value);
|
||||||
expr_t *expr_symbol(struct store *store, const symbol_t *symbol);
|
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_str_symbol(struct store *store, const char *str);
|
||||||
expr_t *expr_empty_list(struct store *store);
|
expr_t *expr_empty_list(struct store *store);
|
||||||
expr_t *expr_pair(struct store *store, expr_t *car, expr_t *cdr);
|
expr_t *expr_pair(struct store *store, expr_t *car, expr_t *cdr);
|
||||||
|
expr_t *expr_prim_proc(struct store *store, prim_proc_t prim_proc);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -7,6 +7,12 @@
|
|||||||
static am_t am;
|
static am_t am;
|
||||||
static store_t store;
|
static store_t store;
|
||||||
|
|
||||||
|
void test_prim_proc(am_t *am, store_t *store)
|
||||||
|
{
|
||||||
|
(void)am;
|
||||||
|
(void)store;
|
||||||
|
}
|
||||||
|
|
||||||
void setUp(void)
|
void setUp(void)
|
||||||
{
|
{
|
||||||
am_init(&am);
|
am_init(&am);
|
||||||
@@ -41,6 +47,18 @@ static void test_empty_list_self_evals(void)
|
|||||||
TEST_ASSERT_EQUAL(ATOM_TYPE_EMPTY_LIST, am.val->atom.type);
|
TEST_ASSERT_EQUAL(ATOM_TYPE_EMPTY_LIST, am.val->atom.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_prim_proc_self_evals(void)
|
||||||
|
{
|
||||||
|
am.expr = expr_prim_proc(&store, test_prim_proc);
|
||||||
|
|
||||||
|
eval(&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);
|
||||||
|
TEST_ASSERT_EQUAL(test_prim_proc, am.val->atom.prim_proc);
|
||||||
|
}
|
||||||
|
|
||||||
static void test_foo_evals_to_42_when_set_in_env(void)
|
static void test_foo_evals_to_42_when_set_in_env(void)
|
||||||
{
|
{
|
||||||
am.expr = expr_str_symbol(&store, "foo");
|
am.expr = expr_str_symbol(&store, "foo");
|
||||||
@@ -61,6 +79,7 @@ int main(void)
|
|||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
RUN_TEST(test_42_self_evals);
|
RUN_TEST(test_42_self_evals);
|
||||||
RUN_TEST(test_empty_list_self_evals);
|
RUN_TEST(test_empty_list_self_evals);
|
||||||
|
RUN_TEST(test_prim_proc_self_evals);
|
||||||
RUN_TEST(test_foo_evals_to_42_when_set_in_env);
|
RUN_TEST(test_foo_evals_to_42_when_set_in_env);
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,16 @@
|
|||||||
|
#include "am.h"
|
||||||
#include "expr.h"
|
#include "expr.h"
|
||||||
#include "store.h"
|
#include "store.h"
|
||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
|
|
||||||
static store_t store;
|
static store_t store;
|
||||||
|
|
||||||
|
void test_prim_proc(am_t *am, store_t *store)
|
||||||
|
{
|
||||||
|
(void)am;
|
||||||
|
(void)store;
|
||||||
|
}
|
||||||
|
|
||||||
void setUp(void)
|
void setUp(void)
|
||||||
{
|
{
|
||||||
store_init(&store);
|
store_init(&store);
|
||||||
@@ -92,6 +99,16 @@ static void test_expr_pair(void)
|
|||||||
TEST_ASSERT_EQUAL(cdr, expr->pair.cdr);
|
TEST_ASSERT_EQUAL(cdr, expr->pair.cdr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_expr_prim_proc(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
expr_t *expr = expr_prim_proc(&store, test_prim_proc);
|
||||||
|
TEST_ASSERT_NOT_NULL(expr);
|
||||||
|
TEST_ASSERT_TRUE(expr->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_PRIM_PROC, expr->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(test_prim_proc, expr->atom.prim_proc);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
@@ -103,5 +120,6 @@ int main(void)
|
|||||||
RUN_TEST(test_expr_str_symbol_quux);
|
RUN_TEST(test_expr_str_symbol_quux);
|
||||||
RUN_TEST(test_expr_empty_list);
|
RUN_TEST(test_expr_empty_list);
|
||||||
RUN_TEST(test_expr_pair);
|
RUN_TEST(test_expr_pair);
|
||||||
|
RUN_TEST(test_expr_prim_proc);
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user