Handle NULL result from read_expression() in REPL

This commit is contained in:
2024-10-24 20:58:08 +01:00
parent 10803109db
commit 9f54c3552b
2 changed files with 29 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
#include "repl.h"
#include <stdio.h>
#include <string.h>
void init_repl(repl_t *repl)
{
@@ -19,6 +20,12 @@ bool step_repl(repl_t *repl)
repl->line_buffer[len] = (char)byte;
}
const expression_t *e = repl->read(&repl->pool, repl->line_buffer, len);
if (NULL == e) {
const char *msg = "Invalid expression\n";
repl->print(msg, strlen(msg));
return true;
}
const int result = repl->evaluate(e);
const int result_len = snprintf(
repl->result_buffer, REPL_RESULT_BUFFER_SIZE, "%d\n", result);