repl: slight refactoring

This commit is contained in:
Přemysl Eric Janouch 2017-05-21 13:22:24 +02:00
parent a529cf262e
commit bbdd17885c
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 26 additions and 22 deletions

44
repl.c
View File

@ -21,6 +21,27 @@
#include <readline/readline.h> #include <readline/readline.h>
#include <readline/history.h> #include <readline/history.h>
static void
run (struct context *ctx, struct item *program) {
struct item *result = NULL;
(void) execute (ctx, program, &result);
item_free_list (program);
const char *failure = ctx->error;
if (ctx->memory_failure)
failure = "memory allocation failure";
if (failure) {
printf ("\x1b[31m%s: %s\x1b[0m\n", "runtime error", failure);
free (ctx->error);
ctx->error = NULL;
ctx->memory_failure = false;
} else {
print_tree (result, 0);
putchar ('\n');
item_free_list (result);
}
}
int int
main (int argc, char *argv[]) { main (int argc, char *argv[]) {
(void) argc; (void) argc;
@ -43,31 +64,14 @@ main (int argc, char *argv[]) {
const char *e = NULL; const char *e = NULL;
struct item *program = parser_run (&parser, &e); struct item *program = parser_run (&parser, &e);
free (line); free (line);
if (e) { if (e)
printf ("\x1b[31m%s: %s\x1b[0m\n", "parse error", e); printf ("\x1b[31m%s: %s\x1b[0m\n", "parse error", e);
else
run (&ctx, program);
parser_free (&parser); parser_free (&parser);
continue;
} }
parser_free (&parser);
struct item *result = NULL;
(void) execute (&ctx, program, &result);
item_free_list (program);
const char *failure = ctx.error;
if (ctx.memory_failure)
failure = "memory allocation failure";
if (failure) {
printf ("\x1b[31m%s: %s\x1b[0m\n", "runtime error", failure);
free (ctx.error);
ctx.error = NULL;
ctx.memory_failure = false;
} else {
print_tree (result, 0);
putchar ('\n'); putchar ('\n');
item_free_list (result);
}
}
context_free (&ctx); context_free (&ctx);
return 0; return 0;
} }