diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 3055247..2c31a8d 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,4 +1,5 @@ add_library(imp + am.c memory_stream.c store.c token.c diff --git a/lib/am.c b/lib/am.c new file mode 100644 index 0000000..65ee9f1 --- /dev/null +++ b/lib/am.c @@ -0,0 +1,22 @@ +#include "am.h" + +#include +#include + +void am_init(am_t *am) +{ + memset(am, 0, sizeof(am_t)); + am->sp = am->stack + AM_STACK_SIZE - 1; +} + +void am_push(am_t *am) +{ + assert(am->sp >= am->stack); + *am->sp-- = am->expr; +} + +void am_pop(am_t *am) +{ + assert(am->sp < am->stack + AM_STACK_SIZE - 1); + am->expr = *++am->sp; +} diff --git a/lib/include/am.h b/lib/include/am.h new file mode 100644 index 0000000..eb96ddd --- /dev/null +++ b/lib/include/am.h @@ -0,0 +1,17 @@ +#ifndef AM_H +#define AM_H + +#include "expr.h" + +#define AM_STACK_SIZE 128U + +typedef struct { + expr_t *expr; + expr_t **sp, *stack[AM_STACK_SIZE]; +} am_t; + +void am_init(am_t *am); +void am_push(am_t *am); +void am_pop(am_t *am); + +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ddf0a5f..439cb1a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,6 +9,7 @@ function(add_test_suites) endfunction() add_test_suites( + am_tests.c store_tests.c token_tests.c ) diff --git a/tests/am_tests.c b/tests/am_tests.c new file mode 100644 index 0000000..a64f82c --- /dev/null +++ b/tests/am_tests.c @@ -0,0 +1,30 @@ +#include "am.h" +#include "unity.h" + +static am_t am; + +void setUp(void) +{ + am_init(&am); +} + +void tearDown(void) +{ +} + +static void test_expr_value_restored_after_push_modify_pop(void) +{ + expr_t a, b; + am.expr = &a; + am_push(&am); + am.expr = &b; + am_pop(&am); + TEST_ASSERT_EQUAL(&a, am.expr); +} + +int main(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_expr_value_restored_after_push_modify_pop); + return UNITY_END(); +}