]> git.lizzy.rs Git - nothing.git/commitdiff
(#526) introduce (print) for REPL
authorrexim <reximkut@gmail.com>
Sun, 25 Nov 2018 18:01:48 +0000 (01:01 +0700)
committerrexim <reximkut@gmail.com>
Sun, 25 Nov 2018 18:02:15 +0000 (01:02 +0700)
CMakeLists.txt
src/ebisp/repl.c
src/ebisp/repl_runtime.c [new file with mode: 0644]
src/ebisp/repl_runtime.h [new file with mode: 0644]

index 2e2a7aada01a4253f35a5610f1c820331b1478e6..47ae3c126a4af8c74a2af0413f11775613363964 100644 (file)
@@ -139,6 +139,8 @@ add_executable(repl
   src/system/log.c
   src/system/nth_alloc.h
   src/system/nth_alloc.c
+  src/ebisp/repl_runtime.h
+  src/ebisp/repl_runtime.c
   )
 add_executable(nothing_test
   src/ebisp/builtins.c
index c542c155955109d5b974a08191c8b5652891cefe..539fd680a83742aae200647659701777aea79b70 100644 (file)
@@ -5,43 +5,10 @@
 #include "interpreter.h"
 #include "scope.h"
 #include "gc.h"
+#include "repl_runtime.h"
 
 #define REPL_BUFFER_MAX 1024
 
-static struct EvalResult gc_inspect_adapter(void *param, Gc *gc, struct Scope *scope, struct Expr args)
-{
-    assert(gc);
-    assert(scope);
-    (void) param;
-    (void) args;
-
-    gc_inspect(gc);
-
-    return eval_success(NIL(gc));
-}
-
-static struct EvalResult quit(void *param, Gc *gc, struct Scope *scope, struct Expr args)
-{
-    assert(gc);
-    assert(scope);
-    (void) args;
-    (void) param;
-
-    exit(0);
-
-    return eval_success(NIL(gc));
-}
-
-static struct EvalResult get_scope(void *param, Gc *gc, struct Scope *scope, struct Expr args)
-{
-    assert(gc);
-    assert(scope);
-    (void) param;
-    (void) args;
-
-    return eval_success(scope->expr);
-}
-
 static void eval_line(Gc *gc, Scope *scope, const char *line)
 {
     /* TODO(#465): eval_line could be implemented with read_all_exprs_from_string */
@@ -81,9 +48,7 @@ int main(int argc, char *argv[])
         .expr = CONS(gc, NIL(gc), NIL(gc))
     };
 
-    set_scope_value(gc, &scope, SYMBOL(gc, "quit"), NATIVE(gc, quit, NULL));
-    set_scope_value(gc, &scope, SYMBOL(gc, "gc-inspect"), NATIVE(gc, gc_inspect_adapter, NULL));
-    set_scope_value(gc, &scope, SYMBOL(gc, "scope"), NATIVE(gc, get_scope, NULL));
+    load_repl_runtime(gc, &scope);
 
     while (true) {
         printf("> ");
diff --git a/src/ebisp/repl_runtime.c b/src/ebisp/repl_runtime.c
new file mode 100644 (file)
index 0000000..729ddc5
--- /dev/null
@@ -0,0 +1,66 @@
+#include <assert.h>
+
+#include "scope.h"
+#include "interpreter.h"
+#include "gc.h"
+#include "expr.h"
+#include "repl_runtime.h"
+
+static struct EvalResult gc_inspect_adapter(void *param, Gc *gc, struct Scope *scope, struct Expr args)
+{
+    assert(gc);
+    assert(scope);
+    (void) param;
+    (void) args;
+
+    gc_inspect(gc);
+
+    return eval_success(NIL(gc));
+}
+
+static struct EvalResult quit(void *param, Gc *gc, struct Scope *scope, struct Expr args)
+{
+    assert(gc);
+    assert(scope);
+    (void) args;
+    (void) param;
+
+    exit(0);
+
+    return eval_success(NIL(gc));
+}
+
+static struct EvalResult get_scope(void *param, Gc *gc, struct Scope *scope, struct Expr args)
+{
+    assert(gc);
+    assert(scope);
+    (void) param;
+    (void) args;
+
+    return eval_success(scope->expr);
+}
+
+static struct EvalResult print(void *param, Gc *gc, struct Scope *scope, struct Expr args)
+{
+    assert(gc);
+    assert(scope);
+    (void) param;
+
+    const char *s = NULL;
+    struct EvalResult result = unpack_args(gc, "s", args, &s);
+    if (result.is_error) {
+        return result;
+    }
+
+    printf("%s\n", s);
+
+    return eval_success(NIL(gc));
+}
+
+void load_repl_runtime(Gc *gc, struct Scope *scope)
+{
+    set_scope_value(gc, scope, SYMBOL(gc, "quit"), NATIVE(gc, quit, NULL));
+    set_scope_value(gc, scope, SYMBOL(gc, "gc-inspect"), NATIVE(gc, gc_inspect_adapter, NULL));
+    set_scope_value(gc, scope, SYMBOL(gc, "scope"), NATIVE(gc, get_scope, NULL));
+    set_scope_value(gc, scope, SYMBOL(gc, "print"), NATIVE(gc, print, NULL));
+}
diff --git a/src/ebisp/repl_runtime.h b/src/ebisp/repl_runtime.h
new file mode 100644 (file)
index 0000000..1ff3fcb
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef REPL_RUNTIME_H_
+#define REPL_RUNTIME_H_
+
+typedef struct Gc Gc;
+struct Scope;
+
+void load_repl_runtime(Gc *gc, struct Scope *scope);
+
+#endif  // REPL_RUNTIME_H_