Skip to content

Instantly share code, notes, and snippets.

@kikairoya
Created December 13, 2013 04:07
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 kikairoya/7939648 to your computer and use it in GitHub Desktop.
Save kikairoya/7939648 to your computer and use it in GitHub Desktop.
(defun resolve-bind-args (bindexpr args)
(mapcar
(lambda (x)
(cond
((bind-subexpression-p x) (call (resolve-bind-args x args)))
((bind-placeholder-p x) (nth (1- (bind-placeholder-position x)) args))
(t x)))
(bind-subexpression-bounds bindexpr)))
(defmacro bind (&rest bounds)
`(let ((b ,(macroexpand (cons 'bind-internal bounds))))
(lambda (&rest args)
(call (resolve-bind-args b args)))))
(defmacro bind-internal (&rest bounds)
`(make-bind-subexpression
:bounds
(list
,@(mapcar
(lambda (x)
(cond
((and (consp x)
(symbolp (car x))
(string= (symbol-name (car x)) "bind"))
(macroexpand (cons 'bind-internal (cdr x))))
((and (symbolp x)
(char= (char (symbol-name x) 0) #\_)
(multiple-value-bind (n b)
(parse-integer (symbol-name x) :start 1 :radix 10)
(and b
(= b (length (symbol-name x)))
(make-bind-placeholder :position n)))))
(t x)))
bounds))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment