Skip to content

Instantly share code, notes, and snippets.

@yehohanan7
Created January 17, 2016 00:42
Show Gist options
  • Save yehohanan7/3b73f205a49e6f72b32b to your computer and use it in GitHub Desktop.
Save yehohanan7/3b73f205a49e6f72b32b to your computer and use it in GitHub Desktop.
(defun flatten (structure)
(cond ((null structure) nil)
((atom structure) (list structure))
(t (mapcan #'flatten structure))))
(defun g!-symbol-p (s)
(and (symbolp s)
(> (length (symbol-name s)) 2)
(string= (symbol-name s)
"G!"
:start1 0
:end1 2)))
(defmacro defmacro/g! (name args &rest body)
(let ((syms (remove-duplicates (remove-if-not #'g!-symbol-p (flatten body)))))
`(defmacro ,name ,args
(let ,(mapcar (lambda (s) `(,s (gensym ,(subseq (symbol-name s) 2)))) syms)
,@body))))
(macroexpand-1 '(defmacro/g! nif (expr pos zero neg)
`(let ((,g!result ,expr))
(cond ((plusp ,g!result) ,pos)
((zerop ,g!result) ,zero)
(t ,neg)))))
;; output
(DEFMACRO NIF (EXPR POS ZERO NEG)
(LET ()
`(LET ((,G!RESULT ,EXPR))
(COND ((PLUSP ,G!RESULT) ,POS) ((ZEROP ,G!RESULT) ,ZERO) (T ,NEG)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment