Create basic REPL module

This commit is contained in:
2024-10-24 16:29:28 +01:00
parent 810feee55e
commit 8d5f5e4ede
5 changed files with 179 additions and 1 deletions

30
lib/repl.c Normal file
View File

@@ -0,0 +1,30 @@
#include "repl.h"
#include <stdio.h>
void init_repl(repl_t *repl)
{
init_memory_pool(&repl->pool);
}
void step_repl(repl_t *repl)
{
int len;
for (len = 0; len < REPL_LINE_BUFFER_SIZE; ++len) {
const int byte = repl->get_byte();
if ('\n' == byte)
break;
repl->line_buffer[len] = (char)byte;
}
const expression_t *e = repl->read(&repl->pool, repl->line_buffer, len);
const int result = repl->evaluate(e);
const int result_len = snprintf(
repl->result_buffer, REPL_RESULT_BUFFER_SIZE, "%d\n", result);
repl->print(repl->result_buffer, result_len);
}
void run_repl(repl_t *repl)
{
while (1)
step_repl(repl);
}