Split out the interpreter

This commit is contained in:
2017-05-21 12:47:09 +02:00
parent b31526d6ba
commit 455845d078
3 changed files with 77 additions and 57 deletions

54
ell.c Executable file → Normal file
View File

@@ -24,7 +24,6 @@
#include <errno.h>
#include <stdarg.h>
#include <assert.h>
#include <time.h>
#include <stdbool.h>
#include <setjmp.h>
@@ -1283,56 +1282,3 @@ init_runtime_library (struct context *ctx) {
&& native_register (ctx, "=", fn_equals)
&& native_register (ctx, "<", fn_less);
}
// --- Main --------------------------------------------------------------------
int
main (int argc, char *argv[]) {
FILE *fp = stdin;
if (argc > 1 && !(fp = fopen (argv[1], "rb"))) {
fprintf (stderr, "%s: %s\n", argv[1], strerror (errno));
return 1;
}
int c;
struct buffer buf = BUFFER_INITIALIZER;
while ((c = fgetc (fp)) != EOF)
buffer_append_c (&buf, c);
buffer_append_c (&buf, 0);
fclose (fp);
struct parser parser;
parser_init (&parser, buf.s, buf.len - 1);
const char *e = NULL;
struct item *program = parser_run (&parser, &e);
#ifndef NDEBUG
printf ("\x1b[1m%s\x1b[0m\n", buf.s);
print_tree (program, 0);
printf ("\n\n");
#endif
free (buf.s);
if (e) {
printf ("%s: %s\n", "parse error", e);
return 1;
}
parser_free (&parser);
struct context ctx;
context_init (&ctx);
if (!init_runtime_library (&ctx))
printf ("%s\n", "runtime library initialization failed");
struct item *result = NULL;
(void) execute (&ctx, program, &result);
item_free_list (result);
item_free_list (program);
const char *failure = ctx.error;
if (ctx.memory_failure)
failure = "memory allocation failure";
if (failure)
printf ("%s: %s\n", "runtime error", failure);
context_free (&ctx);
return 0;
}