From 6a557a73f5fac223b72ef0bb6a783f13a96fe83e Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Fri, 11 Nov 2022 11:25:06 -0600 Subject: [PATCH] Simplify eval. Also add more conventional handling of nil to the `compile` function. --- src/boot/boot.janet | 4 ++-- src/core/compile.c | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index cb151205..4b0af9cd 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -2534,7 +2534,7 @@ (in env :exit-value env)) (defn quit - ``Tries to exit from the current repl or context. Does not always exit the application. + ``Tries to exit from the current repl or run-context. Does not always exit the application. Works by setting the :exit dynamic binding to true. Passing a non-nil `value` here will cause the outer run-context to return that value.`` [&opt value] @@ -2546,7 +2546,7 @@ ``Evaluates a form in the current environment. If more control over the environment is needed, use `run-context`.`` [form] - (def res (compile form (fiber/getenv (fiber/current)) :eval)) + (def res (compile form nil :eval)) (if (= (type res) :function) (res) (error (get res :error)))) diff --git a/src/core/compile.c b/src/core/compile.c index 44e5da99..ffdc98c6 100644 --- a/src/core/compile.c +++ b/src/core/compile.c @@ -1005,7 +1005,8 @@ JANET_CORE_FN(cfun_compile, "If a `lints` array is given, linting messages will be appended to the array. " "Each message will be a tuple of the form `(level line col message)`.") { janet_arity(argc, 1, 4); - JanetTable *env = argc > 1 ? janet_gettable(argv, 1) : janet_vm.fiber->env; + JanetTable *env = (argc > 1 && !janet_checktype(argv[1], JANET_NIL)) + ? janet_gettable(argv, 1) : janet_vm.fiber->env; if (NULL == env) { env = janet_table(0); janet_vm.fiber->env = env; @@ -1017,11 +1018,12 @@ JANET_CORE_FN(cfun_compile, source = janet_unwrap_string(x); } else if (janet_checktype(x, JANET_KEYWORD)) { source = janet_unwrap_keyword(x); - } else { + } else if (!janet_checktype(x, JANET_NIL)) { janet_panic_type(x, 2, JANET_TFLAG_STRING | JANET_TFLAG_KEYWORD); } } - JanetArray *lints = (argc >= 4) ? janet_getarray(argv, 3) : NULL; + JanetArray *lints = (argc >= 4 && !janet_checktype(argv[3], JANET_NIL)) + ? janet_getarray(argv, 3) : NULL; JanetCompileResult res = janet_compile_lint(argv[0], env, source, lints); if (res.status == JANET_COMPILE_OK) { return janet_wrap_function(janet_thunk(res.funcdef));