Skip to content

Instantly share code, notes, and snippets.

@samth
Created November 24, 2011 00:02
Show Gist options
  • Select an option

  • Save samth/1390297 to your computer and use it in GitHub Desktop.

Select an option

Save samth/1390297 to your computer and use it in GitHub Desktop.
Some macro trickery
#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