-
-
Save sjl/73e749f35763958a0158f1bdd7bb3289 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
(require :sb-cltl2) | |
(defun foo () | |
(print 'hello)) | |
(defun rep (n thunk) | |
(loop :repeat n :do (funcall thunk))) | |
(define-compiler-macro rep (&whole form n thunk &environment env) | |
(if (constantp n env) | |
(let ((x (eval (sb-cltl2:macroexpand-all n env)))) | |
(if (< x 5) | |
(let ((thunk% (gensym "thunk"))) | |
(format t "unrolling loop to become: ") | |
(print `(let ((,thunk% ,thunk)) | |
,@(loop :repeat x :collect `(funcall ,thunk%))))) | |
form)) ; don't unroll TOO much | |
form)) | |
(defun a () | |
(rep 4 #'foo)) | |
(defun b () | |
(rep (+ 1 2) #'foo)) | |
(defun c () | |
(symbol-macrolet ((x '(2))) | |
(rep (car x) #'foo))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment