Created
January 17, 2016 00:42
-
-
Save yehohanan7/3b73f205a49e6f72b32b to your computer and use it in GitHub Desktop.
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
(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