Skip to content

Instantly share code, notes, and snippets.

@felixr
Last active December 19, 2020 18:59
Show Gist options
  • Save felixr/49e492d48fb6eaa5d958b4b40d022233 to your computer and use it in GitHub Desktop.
Save felixr/49e492d48fb6eaa5d958b4b40d022233 to your computer and use it in GitHub Desktop.
Debug macro for janet
(defmacro dbg! [form]
(let [func (first form)
args (drop 1 form)]
(var accum @['do])
(var syms @[])
(each arg args
(let [sym (gensym)]
(array/push accum (tuple 'def sym arg))
(array/push syms [arg sym])))
(array/push accum (tuple 'def 'ret (tuple func ;(map last syms))))
(array/push accum (tuple 'printf (string/format "dbg!: %q => %%q" form) 'ret))
(each [a s] syms
(array/push accum (tuple 'printf " %s => %q" (string/format "%q" a) s)))
(array/push accum 'ret)
(tuple/slice accum 0)))
(var foo @{})
(each x [3 4]
(dbg! (put foo x (* x x))))
# dbg!: (put foo x (* x x)) => @{3 9}
# foo => @{3 9}
# x => 3
# (* x x) => 9))))
# dbg!: (put foo x (* x x)) => @{3 9 4 16}
# foo => @{3 9 4 16}
# x => 4
# (* x x) => 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment