Last active
December 19, 2020 18:59
-
-
Save felixr/49e492d48fb6eaa5d958b4b40d022233 to your computer and use it in GitHub Desktop.
Debug macro for janet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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