Create read module
This commit is contained in:
@@ -7,6 +7,7 @@ add_library(imp
|
|||||||
parse.c
|
parse.c
|
||||||
prim.c
|
prim.c
|
||||||
print.c
|
print.c
|
||||||
|
read.c
|
||||||
store.c
|
store.c
|
||||||
token.c
|
token.c
|
||||||
)
|
)
|
||||||
|
|||||||
9
lib/include/read.h
Normal file
9
lib/include/read.h
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef READ_H
|
||||||
|
#define READ_H
|
||||||
|
|
||||||
|
#include "am.h"
|
||||||
|
#include "stream.h"
|
||||||
|
|
||||||
|
void read(am_t *am, stream_t *stream);
|
||||||
|
|
||||||
|
#endif
|
||||||
22
lib/read.c
Normal file
22
lib/read.c
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#include "read.h"
|
||||||
|
|
||||||
|
#include "parse.h"
|
||||||
|
#include "token.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
void read(am_t *am, stream_t *stream)
|
||||||
|
{
|
||||||
|
parse_ctx_t ctx;
|
||||||
|
token_t token;
|
||||||
|
|
||||||
|
parse_init(am, &ctx);
|
||||||
|
|
||||||
|
while (ctx.state != PARSE_STATE_DONE) {
|
||||||
|
const token_status_t token_status = token_read(stream, &token);
|
||||||
|
assert(token_status == TOKEN_OK);
|
||||||
|
|
||||||
|
parse_proc(&ctx, &token);
|
||||||
|
assert(ctx.state != PARSE_STATE_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@ add_test_suites(
|
|||||||
parse_tests.c
|
parse_tests.c
|
||||||
prim_tests.c
|
prim_tests.c
|
||||||
print_tests.c
|
print_tests.c
|
||||||
|
read_tests.c
|
||||||
store_tests.c
|
store_tests.c
|
||||||
token_tests.c
|
token_tests.c
|
||||||
)
|
)
|
||||||
|
|||||||
98
tests/read_tests.c
Normal file
98
tests/read_tests.c
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
#include "memory_stream.h"
|
||||||
|
#include "read.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define CAR(expr) (expr->pair.car)
|
||||||
|
#define CDR(expr) (expr->pair.cdr)
|
||||||
|
#define CADR(expr) CAR(CDR(expr))
|
||||||
|
#define CDDR(expr) CDR(CDR(expr))
|
||||||
|
#define CADDR(expr) CAR(CDDR(expr))
|
||||||
|
#define CDDDR(expr) CDR(CDDR(expr))
|
||||||
|
#define CAADDR(expr) CAR(CADDR(expr))
|
||||||
|
#define CDADDR(expr) CDR(CADDR(expr))
|
||||||
|
#define CADADDR(expr) CAR(CDADDR(expr))
|
||||||
|
#define CDDADDR(expr) CDR(CDADDR(expr))
|
||||||
|
#define CADDADDR(expr) CAR(CDDADDR(expr))
|
||||||
|
#define CDDDADDR(expr) CDR(CDDADDR(expr))
|
||||||
|
|
||||||
|
static am_t am;
|
||||||
|
|
||||||
|
void setUp(void)
|
||||||
|
{
|
||||||
|
am_init(&am);
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_nested_expression(void)
|
||||||
|
{
|
||||||
|
const char *input = "(+ 1 (* 2 3))";
|
||||||
|
memory_stream_t stream;
|
||||||
|
memory_stream_init(&stream, (const uint8_t *)input, strlen(input));
|
||||||
|
|
||||||
|
read(&am, (stream_t *)&stream);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(am.expr);
|
||||||
|
TEST_ASSERT_FALSE(am.expr->is_atom);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CAR(am.expr));
|
||||||
|
TEST_ASSERT_TRUE(CAR(am.expr)->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, CAR(am.expr)->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(1, CAR(am.expr)->atom.symbol.len);
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY("+", CAR(am.expr)->atom.symbol.buf, 1);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CDR(am.expr));
|
||||||
|
TEST_ASSERT_FALSE(CDR(am.expr)->is_atom);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CADR(am.expr));
|
||||||
|
TEST_ASSERT_TRUE(CADR(am.expr)->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, CADR(am.expr)->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(1, CADR(am.expr)->atom.integer);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CDDR(am.expr));
|
||||||
|
TEST_ASSERT_FALSE(CDDR(am.expr)->is_atom);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CADDR(am.expr));
|
||||||
|
TEST_ASSERT_FALSE(CADDR(am.expr)->is_atom);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CAADDR(am.expr));
|
||||||
|
TEST_ASSERT_TRUE(CAADDR(am.expr)->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_SYMBOL, CAADDR(am.expr)->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(1, CAADDR(am.expr)->atom.symbol.len);
|
||||||
|
TEST_ASSERT_EQUAL_MEMORY("*", CAADDR(am.expr)->atom.symbol.buf, 1);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CDADDR(am.expr));
|
||||||
|
TEST_ASSERT_FALSE(CDADDR(am.expr)->is_atom);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CADADDR(am.expr));
|
||||||
|
TEST_ASSERT_TRUE(CADADDR(am.expr)->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, CADADDR(am.expr)->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(2, CADADDR(am.expr)->atom.integer);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CDDADDR(am.expr));
|
||||||
|
TEST_ASSERT_FALSE(CDDADDR(am.expr)->is_atom);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CADDADDR(am.expr));
|
||||||
|
TEST_ASSERT_TRUE(CADDADDR(am.expr)->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_INTEGER, CADDADDR(am.expr)->atom.type);
|
||||||
|
TEST_ASSERT_EQUAL(3, CADDADDR(am.expr)->atom.integer);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CDDDADDR(am.expr));
|
||||||
|
TEST_ASSERT_TRUE(CDDDADDR(am.expr)->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_EMPTY_LIST, CDDDADDR(am.expr)->atom.type);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(CDDDR(am.expr));
|
||||||
|
TEST_ASSERT_TRUE(CDDDR(am.expr)->is_atom);
|
||||||
|
TEST_ASSERT_EQUAL(ATOM_TYPE_EMPTY_LIST, CDDDR(am.expr)->atom.type);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
RUN_TEST(test_nested_expression);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user