Add basic EOF handling to REPL
This commit is contained in:
12
lib/repl.c
12
lib/repl.c
@@ -7,12 +7,14 @@ void init_repl(repl_t *repl)
|
||||
init_memory_pool(&repl->pool);
|
||||
}
|
||||
|
||||
void step_repl(repl_t *repl)
|
||||
bool 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)
|
||||
if (EOF == byte)
|
||||
return false;
|
||||
else if ('\n' == byte)
|
||||
break;
|
||||
repl->line_buffer[len] = (char)byte;
|
||||
}
|
||||
@@ -21,10 +23,12 @@ void step_repl(repl_t *repl)
|
||||
const int result_len = snprintf(
|
||||
repl->result_buffer, REPL_RESULT_BUFFER_SIZE, "%d\n", result);
|
||||
repl->print(repl->result_buffer, result_len);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void run_repl(repl_t *repl)
|
||||
{
|
||||
while (1)
|
||||
step_repl(repl);
|
||||
while (step_repl(repl))
|
||||
;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user