Expose line, col in macros via (dyn :macro-form)

This exposes line and column indirectly via
tuple/sourcemap and allows interesting debug macros.
This commit is contained in:
Calvin Rose 2020-05-18 18:22:43 -05:00
parent 17cb0c1aee
commit 9e5f203302
3 changed files with 12 additions and 1 deletions

View File

@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file.
## Unreleased - ???
- Expose current macro form inside macros as (dyn :macro-form)
- Add `tracev` macro.
- Fix compiler bug that emitted incorrect code in some cases for while loops that create closures.
- Add `:fresh` option to `(import ...)` to overwrite the module cache.

View File

@ -950,9 +950,14 @@
"Print a value and a description of the form that produced that value to
stderr. Evaluates to x."
[x]
(def [l c] (tuple/sourcemap (dyn :macro-form ())))
(def cf (dyn :current-file))
(def fmt-1 (if cf (string/format "trace [%s]" cf) "trace"))
(def fmt-2 (if (or (neg? l) (neg? c)) ": " (string/format " on line %d, column %d:" l c)))
(def fmt (string fmt-1 fmt-2 " %j is "))
(def s (gensym))
~(let [,s ,x]
(,eprinf "trace %j is " ',x)
(,eprinf ,fmt ',x)
(,eprintf (,dyn :pretty-format "%q") ,s)
,s))
@ -1628,6 +1633,8 @@
See macex docs for info on on-binding."
[x &opt on-binding]
(setdyn :macro-form x)
(when on-binding
(when (symbol? x)
(break (on-binding x))))

View File

@ -596,8 +596,11 @@ static int macroexpand1(
/* Set env */
fiberp->env = c->env;
int lock = janet_gclock();
Janet mf_kw = janet_ckeywordv("macro-form");
janet_table_put(c->env, mf_kw, x);
Janet tempOut;
JanetSignal status = janet_continue(fiberp, janet_wrap_nil(), &tempOut);
janet_table_put(c->env, mf_kw, janet_wrap_nil());
janet_gcunlock(lock);
if (status != JANET_SIGNAL_OK) {
const uint8_t *es = janet_formatc("(macro) %V", tempOut);