Move REPL logic into application
This commit is contained in:
30
app/main.c
30
app/main.c
@@ -1,12 +1,36 @@
|
||||
#include <stdio.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)
|
||||
{
|
||||
run_repl(&repl);
|
||||
while (step_repl())
|
||||
;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user