Extract line reading logic to reader and remove REPL tests

Reading a line into the buffer was the only logic complex enough to be
worth testing really, and exposing the necessary parts of it to test
it effectively was a pain.  Makes more sense to move read_line out and
throw away most of the tests.
This commit is contained in:
2024-10-24 21:46:59 +01:00
parent ecd50c76c5
commit acb4cd38d7
9 changed files with 81 additions and 192 deletions

View File

@@ -1,5 +1,8 @@
#include "repl.h"
#include "evaluator.h"
#include "reader.h"
#include <stdio.h>
#include <string.h>
@@ -7,26 +10,17 @@ bool step_repl(repl_t *repl)
{
init_memory_pool(&repl->pool);
int len;
for (len = 0; len < REPL_LINE_BUFFER_SIZE; ++len) {
const int byte = repl->get_byte();
if (EOF == byte)
return false;
else if ('\n' == byte)
break;
repl->line_buffer[len] = (char)byte;
}
const expression_t *e = repl->read(&repl->pool, repl->line_buffer, len);
const int len = read_line(getchar, repl->buffer, REPL_BUFFER_SIZE);
if (len < 0)
return false;
const expression_t *e = read_expression(&repl->pool, repl->buffer, len);
if (NULL == e) {
const char *msg = "Invalid expression\n";
repl->print(msg, strlen(msg));
puts("Invalid expression\n");
return true;
}
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);
const int result = evaluate(e);
printf("%d\n", result);
return true;
}