diff --git a/lib/include/expression.h b/lib/include/expression.h new file mode 100644 index 0000000..e31a448 --- /dev/null +++ b/lib/include/expression.h @@ -0,0 +1,27 @@ +#ifndef EXPRESSION_H +#define EXPRESSION_H + +#include + +typedef enum { + OPERATOR_ADD, + OPERATOR_SUBTRACT, + OPERATOR_MULTIPLY, + OPERATOR_DIVIDE, +} operator_t; + +struct _expression_t; +typedef struct { + operator_t operator; + const struct _expression_t *operands[2]; +} application_t; + +typedef struct _expression_t { + bool is_number; + union { + application_t application; + int number; + }; +} expression_t; + +#endif diff --git a/lib/include/memory_pool.h b/lib/include/memory_pool.h new file mode 100644 index 0000000..48e12f3 --- /dev/null +++ b/lib/include/memory_pool.h @@ -0,0 +1,16 @@ +#ifndef MEMORY_POOL_H +#define MEMORY_POOL_H + +#include "expression.h" + +#define MEMORY_POOL_SIZE 128U + +typedef struct { + expression_t buffer[MEMORY_POOL_SIZE]; + expression_t *free_pointer; +} memory_pool_t; + +void init_memory_pool(memory_pool_t *pool); +expression_t *allocate_expression(memory_pool_t *pool); + +#endif diff --git a/lib/memory_pool.c b/lib/memory_pool.c new file mode 100644 index 0000000..d1f8d4a --- /dev/null +++ b/lib/memory_pool.c @@ -0,0 +1,15 @@ +#include "memory_pool.h" + +#include + +void init_memory_pool(memory_pool_t *pool) +{ + pool->free_pointer = &pool->buffer[0]; +} + +expression_t *allocate_expression(memory_pool_t *pool) +{ + if (pool->free_pointer > pool->buffer + MEMORY_POOL_SIZE) + return NULL; + return pool->free_pointer++; +} diff --git a/scripts/build.sh b/scripts/build.sh index 594d166..890313d 100644 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -2,12 +2,18 @@ cd "$(git rev-parse --show-toplevel)" CFLAGS="$CFLAGS -std=c11 -pedantic -Wall -Wextra" CFLAGS="$CFLAGS -Og -ggdb" +CFLAGS="$CFLAGS -Ilib/include" mkdir -p build +# Build library +clang $CFLAGS -c -o build/memory_pool.o lib/memory_pool.c +clang $CFLAGS -c -o build/reader.o lib/reader.c +ar -crs build/lib.a build/memory_pool.o build/reader.o + # Build tests clang $CFLAGS -Itests -c -o build/testing.o tests/testing.c # Build application clang $CFLAGS -c -o build/main.o app/main.c -clang $CFLAGS -o build/infix-calculator build/main.o +clang $CFLAGS -o build/infix-calculator build/main.o build/lib.a