Created
December 21, 2017 17:04
-
-
Save vyzo/a7bfde350f417692f3c032b14513d413 to your computer and use it in GitHub Desktop.
let-alist
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
(defsyntax (let-alist stx) | |
(syntax-case stx () | |
((macro expr body ...) | |
(with-syntax ((alist-ref (stx-identifier #'macro '%%ref))) | |
#'(let (alist expr) | |
(let-syntax | |
((var-ref | |
(syntax-rules () | |
((_ id) (%%ref id))))) | |
(let-syntax | |
((alist-ref | |
(lambda (stx) | |
(syntax-case stx () | |
((_ id) | |
(let (str (symbol->string (stx-e #'id))) | |
(if (eq? (string-ref str 0) #\.) | |
(let (sym (string->symbol (substring str 1 (string-length str)))) | |
(with-syntax ((elem sym)) | |
#'(cond | |
((assq 'elem alist) => cdr) | |
(else | |
(error "Missing alist element" alist 'elem))))) | |
#'(var-ref id)))))))) | |
body ...))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment