1
0
mirror of https://github.com/janet-lang/janet synced 2025-02-08 21:10:02 +00:00

Allow adding name to short-fns.

When short-fn is used in a macro, it can be useful to
give the function a nicer name then a raw pointer.
This commit is contained in:
Calvin Rose 2022-08-18 14:33:59 -05:00
parent fe5ccb163e
commit ac2082e9b3
2 changed files with 6 additions and 5 deletions

View File

@ -2177,7 +2177,7 @@
|(+ $ $) # use pipe reader macro for terse function literals. |(+ $ $) # use pipe reader macro for terse function literals.
|(+ $&) # variadic functions |(+ $&) # variadic functions
``` ```
[arg] [arg &opt name]
(var max-param-seen -1) (var max-param-seen -1)
(var vararg false) (var vararg false)
(defn saw-special-arg (defn saw-special-arg
@ -2203,8 +2203,9 @@
x)) x))
x)) x))
(def expanded (macex arg on-binding)) (def expanded (macex arg on-binding))
(def name-splice (if name [name] []))
(def fn-args (seq [i :range [0 (+ 1 max-param-seen)]] (symbol '$ i))) (def fn-args (seq [i :range [0 (+ 1 max-param-seen)]] (symbol '$ i)))
~(fn [,;fn-args ,;(if vararg ['& '$&] [])] ,expanded)) ~(fn ,;name-splice [,;fn-args ,;(if vararg ['& '$&] [])] ,expanded))
### ###
### ###
@ -3886,7 +3887,7 @@
"E" (fn E-switch [i &] "E" (fn E-switch [i &]
(set no-file false) (set no-file false)
(def subargs (array/slice args (+ i 2))) (def subargs (array/slice args (+ i 2)))
(def src ~|,(parse (in args (+ i 1)))) (def src ~(short-fn ,(parse (in args (+ i 1))) E-expression))
(def thunk (compile src)) (def thunk (compile src))
(if (function? thunk) (if (function? thunk)
((thunk) ;subargs) ((thunk) ;subargs)

View File

@ -996,7 +996,7 @@ JanetCompileResult janet_compile(Janet source, JanetTable *env, const uint8_t *w
} }
/* C Function for compiling */ /* C Function for compiling */
JANET_CORE_FN(cfun, JANET_CORE_FN(cfun_compile,
"(compile ast &opt env source lints)", "(compile ast &opt env source lints)",
"Compiles an Abstract Syntax Tree (ast) into a function. " "Compiles an Abstract Syntax Tree (ast) into a function. "
"Pair the compile function with parsing functionality to implement " "Pair the compile function with parsing functionality to implement "
@ -1043,7 +1043,7 @@ JANET_CORE_FN(cfun,
void janet_lib_compile(JanetTable *env) { void janet_lib_compile(JanetTable *env) {
JanetRegExt cfuns[] = { JanetRegExt cfuns[] = {
JANET_CORE_REG("compile", cfun), JANET_CORE_REG("compile", cfun_compile),
JANET_REG_END JANET_REG_END
}; };
janet_core_cfuns_ext(env, NULL, cfuns); janet_core_cfuns_ext(env, NULL, cfuns);