Move REPL logic into application
This commit is contained in:
parent
acb4cd38d7
commit
b44bd100ca
30
app/main.c
30
app/main.c
@ -1,12 +1,36 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "repl.h"
|
#include "evaluator.h"
|
||||||
|
#include "reader.h"
|
||||||
|
|
||||||
static repl_t repl;
|
#define BUFFER_SIZE 128U
|
||||||
|
|
||||||
|
static memory_pool_t pool;
|
||||||
|
static char buffer[BUFFER_SIZE];
|
||||||
|
|
||||||
|
static bool step_repl(void)
|
||||||
|
{
|
||||||
|
init_memory_pool(&pool);
|
||||||
|
|
||||||
|
const int len = read_line(getchar, buffer, BUFFER_SIZE);
|
||||||
|
if (len < 0)
|
||||||
|
return false;
|
||||||
|
const expression_t *e = read_expression(&pool, buffer, len);
|
||||||
|
if (NULL == e) {
|
||||||
|
puts("Invalid expression\n");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int result = evaluate(e);
|
||||||
|
printf("%d\n", result);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
run_repl(&repl);
|
while (step_repl())
|
||||||
|
;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
#ifndef REPL_H
|
|
||||||
#define REPL_H
|
|
||||||
|
|
||||||
#include "memory_pool.h"
|
|
||||||
|
|
||||||
#define REPL_BUFFER_SIZE 128
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
memory_pool_t pool;
|
|
||||||
char buffer[REPL_BUFFER_SIZE];
|
|
||||||
} repl_t;
|
|
||||||
|
|
||||||
bool step_repl(repl_t *repl);
|
|
||||||
void run_repl(repl_t *repl);
|
|
||||||
|
|
||||||
#endif
|
|
32
lib/repl.c
32
lib/repl.c
@ -1,32 +0,0 @@
|
|||||||
#include "repl.h"
|
|
||||||
|
|
||||||
#include "evaluator.h"
|
|
||||||
#include "reader.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
bool step_repl(repl_t *repl)
|
|
||||||
{
|
|
||||||
init_memory_pool(&repl->pool);
|
|
||||||
|
|
||||||
const int len = read_line(getchar, repl->buffer, REPL_BUFFER_SIZE);
|
|
||||||
if (len < 0)
|
|
||||||
return false;
|
|
||||||
const expression_t *e = read_expression(&repl->pool, repl->buffer, len);
|
|
||||||
if (NULL == e) {
|
|
||||||
puts("Invalid expression\n");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int result = evaluate(e);
|
|
||||||
printf("%d\n", result);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void run_repl(repl_t *repl)
|
|
||||||
{
|
|
||||||
while (step_repl(repl))
|
|
||||||
;
|
|
||||||
}
|
|
@ -10,9 +10,7 @@ mkdir -p build
|
|||||||
clang $CFLAGS -c -o build/evaluator.o lib/evaluator.c
|
clang $CFLAGS -c -o build/evaluator.o lib/evaluator.c
|
||||||
clang $CFLAGS -c -o build/memory_pool.o lib/memory_pool.c
|
clang $CFLAGS -c -o build/memory_pool.o lib/memory_pool.c
|
||||||
clang $CFLAGS -c -o build/reader.o lib/reader.c
|
clang $CFLAGS -c -o build/reader.o lib/reader.c
|
||||||
clang $CFLAGS -c -o build/repl.o lib/repl.c
|
ar -crs build/lib.a build/evaluator.o build/memory_pool.o build/reader.o
|
||||||
ar -crs build/lib.a \
|
|
||||||
build/evaluator.o build/memory_pool.o build/reader.o build/repl.o
|
|
||||||
|
|
||||||
# Build tests
|
# Build tests
|
||||||
clang $CFLAGS -Itests -c -o build/testing.o tests/testing.c
|
clang $CFLAGS -Itests -c -o build/testing.o tests/testing.c
|
||||||
|
Loading…
x
Reference in New Issue
Block a user