44 lines
721 B
C
44 lines
721 B
C
/*
|
|
* Copyright (c) Camden Dixie O'Brien
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
#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;
|
|
}
|