Create REPL module

This commit is contained in:
2025-08-10 17:26:46 +01:00
parent 5c585ad35c
commit 9df53c63c5
3 changed files with 75 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ add_library(imp
prim.c prim.c
print.c print.c
read.c read.c
repl.c
store.c store.c
token.c token.c
) )

16
lib/include/repl.h Normal file
View File

@@ -0,0 +1,16 @@
#ifndef REPL_H
#define REPL_H
#include "am.h"
#define REPL_IO_BUFFER_SIZE 256U
typedef struct {
am_t am;
char io_buffer[REPL_IO_BUFFER_SIZE];
} repl_t;
void repl_init(repl_t *repl);
int repl_run(repl_t *repl);
#endif

58
lib/repl.c Normal file
View File

@@ -0,0 +1,58 @@
#include "repl.h"
#include "eval.h"
#include "memory_stream.h"
#include "print.h"
#include "read.h"
#include <assert.h>
#include <stdio.h>
#define PROMPT "> "
static int read_line(char buffer[REPL_IO_BUFFER_SIZE])
{
for (unsigned i = 0; i < REPL_IO_BUFFER_SIZE; ++i) {
const int c = getchar();
switch (c) {
case EOF:
return EOF;
case '\n':
return (int)i;
default:
buffer[i] = (char)c;
break;
}
}
assert(false);
return -2;
}
void repl_init(repl_t *repl)
{
am_init(&repl->am);
}
int repl_run(repl_t *repl)
{
int len;
memory_stream_t input;
while (true) {
fputs(PROMPT, stdout);
len = read_line(repl->io_buffer);
if (len < 0) {
fputc('\n', stdout);
return 0;
}
memory_stream_init(&input, (const uint8_t *)repl->io_buffer, len);
read(&repl->am, (stream_t *)&input);
eval(&repl->am);
len = print(&repl->am, repl->io_buffer, REPL_IO_BUFFER_SIZE - 1);
repl->io_buffer[len] = '\n';
fwrite(repl->io_buffer, 1, len + 1, stdout);
}
}