diff --git a/CHANGELOG.md b/CHANGELOG.md index 19a208e8..5ec37a1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 291c0c57..87aa8c60 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -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)))) diff --git a/src/core/compile.c b/src/core/compile.c index fb51c074..00c4dccd 100644 --- a/src/core/compile.c +++ b/src/core/compile.c @@ -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);