1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-29 09:50:27 +00:00

Simplify eval.

Also add more conventional handling of nil to the `compile` function.
This commit is contained in:
Calvin Rose 2022-11-11 11:25:06 -06:00
parent 8d1cfe0c56
commit 6a557a73f5
2 changed files with 7 additions and 5 deletions

View File

@ -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))))

View File

@ -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));