Skip to content

Instantly share code, notes, and snippets.

@sjl

sjl/foo.lisp Secret

Created October 28, 2020 21:04
Show Gist options
  • Save sjl/73e749f35763958a0158f1bdd7bb3289 to your computer and use it in GitHub Desktop.
Save sjl/73e749f35763958a0158f1bdd7bb3289 to your computer and use it in GitHub Desktop.
(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