aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2017-05-21 12:47:09 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2017-05-21 13:19:49 +0200
commit455845d078f39f3969139c6a0729e0b081cfa159 (patch)
tree9ea4b2ca263d99867ed8d99c7b8a8d48bb717f90
parentb31526d6ba610fbe355231339bb2b331bd4fb2af (diff)
downloadell-455845d078f39f3969139c6a0729e0b081cfa159.tar.gz
ell-455845d078f39f3969139c6a0729e0b081cfa159.tar.xz
ell-455845d078f39f3969139c6a0729e0b081cfa159.zip
Split out the interpreter
-rw-r--r--Makefile9
-rw-r--r--[-rwxr-xr-x]ell.c54
-rw-r--r--interpreter.c71
3 files changed, 77 insertions, 57 deletions
diff --git a/Makefile b/Makefile
index 5ad62ed..0b52f38 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,10 @@
CFLAGS = -std=c99 -Wall -Wextra -ggdb
-all: ell
-ell: ell.c
+
+all: interpreter
+interpreter: interpreter.c ell.c
+ $(CC) $(CFLAGS) $< -o $@
+repl: repl.c ell.c
$(CC) $(CFLAGS) $< -o $@
clean:
- rm ell
+ rm -f interpreter repl
.PHONY: all clean
diff --git a/ell.c b/ell.c
index 0a7859c..188672a 100755..100644
--- a/ell.c
+++ b/ell.c
@@ -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;
-}
-
diff --git a/interpreter.c b/interpreter.c
new file mode 100644
index 0000000..4159e08
--- /dev/null
+++ b/interpreter.c
@@ -0,0 +1,71 @@
+/*
+ * interpreter.c: test interpreter
+ *
+ * Copyright (c) 2017, Přemysl Janouch <p.janouch@gmail.com>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+
+#include "ell.c"
+
+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;
+}
+