Skip to content

Instantly share code, notes, and snippets.

@shirok
Last active December 14, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirok/5136967 to your computer and use it in GitHub Desktop.
Save shirok/5136967 to your computer and use it in GitHub Desktop.
(defmacro def-coords-macro (kind)
(let ((with-coords (intern (format nil "WITH-~a-COORDS" kind)))
(checking (intern (format nil "CHECKING-~a-COORDS" kind)))
(box (intern (format nil "BOX-~a" kind)))
(unbox (intern (format nil "UNBOX-~a" kind))))
`(progn
(defmacro ,box (v) `(cons ',',kind ,v))
(defmacro ,unbox (v)
(let ((vv (gensym)))
`(let ((,vv ,v))
(if (consp ,vv)
(if (eq (car ,vv) ',',kind)
(cdr ,vv)
(error "~a coordinate expected, but got ~s" ',',kind ,vv))
,vv))))
(defmacro ,with-coords ((x y) &body body)
`(let ((,x (,',unbox ,x))
(,y (,',unbox ,y)))
,@body))
(defmacro ,checking ((x y) (def name args &body body))
`(,def ,name ,args
(,',with-coords (,x ,y) ,@body))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment