39 lines
633 B
C
39 lines
633 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "evaluator.h"
|
|
#include "reader.h"
|
|
|
|
#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;
|
|
|
|
expression_t *expression;
|
|
const int used = parse_expression(&pool, buffer, len, &expression);
|
|
if (used != len) {
|
|
puts("Invalid expression");
|
|
return true;
|
|
}
|
|
|
|
const int result = evaluate(expression);
|
|
printf("%d\n", result);
|
|
|
|
return true;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
while (step_repl())
|
|
;
|
|
return EXIT_SUCCESS;
|
|
}
|