aboutsummaryrefslogtreecommitdiff
path: root/repl.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2017-05-21 13:22:24 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2017-05-21 13:24:54 +0200
commitbbdd17885cbefeeb30cc6993b37d6e825c342e5a (patch)
tree74ec264b60979faf89e8d4b363eb1695907f6326 /repl.c
parenta529cf262e497a2517c182450e44d88e1e13860d (diff)
downloadell-bbdd17885cbefeeb30cc6993b37d6e825c342e5a.tar.gz
ell-bbdd17885cbefeeb30cc6993b37d6e825c342e5a.tar.xz
ell-bbdd17885cbefeeb30cc6993b37d6e825c342e5a.zip
repl: slight refactoring
Diffstat (limited to 'repl.c')
-rw-r--r--repl.c48
1 files changed, 26 insertions, 22 deletions
diff --git a/repl.c b/repl.c
index c2fbf02..b41f241 100644
--- a/repl.c
+++ b/repl.c
@@ -21,6 +21,27 @@
#include <readline/readline.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
main (int argc, char *argv[]) {
(void) argc;
@@ -43,31 +64,14 @@ main (int argc, char *argv[]) {
const char *e = NULL;
struct item *program = parser_run (&parser, &e);
free (line);
- if (e) {
+ if (e)
printf ("\x1b[31m%s: %s\x1b[0m\n", "parse error", e);
- parser_free (&parser);
- continue;
- }
+ else
+ run (&ctx, program);
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');
- item_free_list (result);
- }
}
+
+ putchar ('\n');
context_free (&ctx);
return 0;
}