changeset 168:5697df53fe81

bin: Add interpreter binary src/bin/ylisp.
author Lewin Bormann <lbo@spheniscida.de>
date Tue, 03 Sep 2019 20:06:59 +0200
parents b28c56a172f7
children cdf06bdd9592
files doc/syntax.md src/bin/.clang src/bin/CMakeLists.txt src/bin/ylisp.c
diffstat 4 files changed, 46 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/doc/syntax.md	Tue Sep 03 20:06:37 2019 +0200
+++ b/doc/syntax.md	Tue Sep 03 20:06:59 2019 +0200
@@ -89,3 +89,5 @@
 ```
 
 Print the arguments formatted to stdout, separated by spaces.
+
+Strings are printed literally, without quotes.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/bin/.clang	Tue Sep 03 20:06:59 2019 +0200
@@ -0,0 +1,1 @@
+flags = -I../.. -I../../build
--- a/src/bin/CMakeLists.txt	Tue Sep 03 20:06:37 2019 +0200
+++ b/src/bin/CMakeLists.txt	Tue Sep 03 20:06:59 2019 +0200
@@ -2,3 +2,7 @@
 # REPL
 ADD_EXECUTABLE(repl repl.c)
 TARGET_LINK_LIBRARIES(repl core frontend)
+
+# Interpreter
+ADD_EXECUTABLE(ylisp ylisp.c)
+TARGET_LINK_LIBRARIES(ylisp core frontend)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/bin/ylisp.c	Tue Sep 03 20:06:59 2019 +0200
@@ -0,0 +1,39 @@
+#include <stdio.h>
+
+#include <src/parse.h>
+#include <src/preprocess.h>
+#include <src/eval.h>
+
+yexpr_t run(FILE* input, yeval_state_t* state, yvec_t** root) {
+    yexpr_t file = yexpr_new();
+    yexpr_t result = yexpr_new();
+    ystr_t error = ystr_new(NULL);
+    bool ok = yparse(input, &file, &error);
+    if (!ok) {
+        fprintf(stderr, "Parse error: %s\n", ystr_str(&error));
+        goto clean_all;
+    }
+    ypreprocess_resolve_builtins(&file);
+    ypreprocess_refs_repl(&file, root);
+    result = yeval_list_return_last(state, &file.value.list, 0);
+clean_all:
+    yexpr_destroy(&file);
+    ystr_destroy(&error);
+    return result;
+}
+
+int main(int argc, char** argv) {
+    yvec_t* root = NULL;
+    yeval_state_t state;
+    state.call_stack = YVEC_NEW(NULL, 16, yexpr_t);
+    yexpr_t result = yexpr_new();
+
+    if (argc == 1) {
+        result = run(stdin, &state, &root);
+    }
+
+    yvec_destroy(root);
+    yvec_destroy(&state.call_stack);
+    yexpr_destroy(&result);
+    return 0;
+}