Created
November 24, 2011 00:02
-
-
Save samth/1390297 to your computer and use it in GitHub Desktop.
Some macro trickery
This file contains hidden or 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
| #lang racket | |
| (require (for-syntax syntax/parse)) | |
| (define-for-syntax (transform stx) | |
| (syntax-parse stx | |
| #:literals (and or) | |
| [(and rest ...) #'(p-and rest ...)] | |
| [(or rest ...) #'(p-or rest ...)] | |
| [_ stx])) | |
| (define-syntax (p-and stx) | |
| (syntax-parse stx | |
| [(_ arg ...) | |
| (with-syntax ([(arg* ...) (map transform (syntax->list #'(arg ...)))]) | |
| #'(list 'and arg* ...))])) | |
| (define-syntax (p-or stx) | |
| (syntax-parse stx | |
| [(_ arg ...) | |
| (with-syntax ([(arg* ...) (map transform (syntax->list #'(arg ...)))]) | |
| #'(list 'or arg* ...))])) | |
| (define-syntax (run stx) | |
| (syntax-parse stx | |
| [(_ forms ...) | |
| (with-syntax ([(arg* ...) (map transform (syntax->list #'(forms ...)))]) | |
| #'(list 'top-level arg* ...))])) | |
| (run (and (and 1 2)) (or (and (+ (if (and #t #T) 0 1))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment