Created
April 23, 2010 17:30
LOL, defmacro!, defmacro-bang
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 mkstr (&rest args) | |
(with-output-to-string (s) | |
(dolist (a args)(princ a s)))) | |
(defun symb (&rest args) | |
(values (intern (apply #'mkstr args)))) | |
(defun flatten (x) | |
(labels ((rec (x acc) | |
(cond ((null x) acc) | |
((atom x) (cons x acc)) | |
(t (rec | |
(car x) | |
(rec (cdr x) acc)))))) | |
(rec x nil))) | |
(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)))) | |
(defmacro/g! nif (expr pos zero neg) | |
`(let ((,g!result ,expr)) | |
(cond ((plusp ,g!result) ,pos) | |
((zerop ,g!result) ,zero) | |
(t ,neg)))) | |
(macroexpand-1 '(defmacro/g! nif (expr pos zero neg) | |
`(let ((,g!result ,expr)) | |
(cond ((plusp ,g!result) ,pos) | |
((zerop ,g!result) ,zero) | |
(t ,neg))))) | |
(nif -1 1 0 -1) | |
; => -1 | |
(defun o!-symbol-p (s) | |
(and (symbolp s) | |
(> (length (symbol-name s)) 2) | |
(string= (symbol-name s) | |
"o!" | |
:start1 0 | |
:end1 2))) | |
(defun o!-symbol-to-g!-symbol (s) | |
(symb "G!" | |
(subseq (symbol-name s) 2))) | |
(defmacro defmacro! (name args &rest body) | |
(let* ((os (remove-if-not #'o!-symbol-p args)) | |
(gs (mapcar #'o!-symbol-to-g!-symbol os))) | |
`(defmacro/g! ,name ,args | |
`(let ,(mapcar #'list (list ,@gs)(list ,@os)) | |
,(progn ,@body))))) | |
(defmacro! square (o!x) | |
`(* ,g!x ,g!x)) | |
(macroexpand '(square (incf x))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment