Created
August 8, 2017 21:01
-
-
Save iitalics/222230016b7e6a5862eaf8d90ad27bb4 to your computer and use it in GitHub Desktop.
Generates macro with absurd number of syntax/parse patterns
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
#lang racket/base | |
(require (for-syntax racket | |
racket/syntax | |
(only-in racket/function curryr) | |
syntax/parse | |
syntax/stx)) | |
(provide (all-from-out racket/base)) | |
(begin-for-syntax | |
; tweak these parameters to change resulting macro size | |
; will generate an expression with size around (expt splay height) | |
(define splay 4) | |
(define height 4) | |
; generates a huge syntax pattern that just matches pat | |
(define (generate-garbage pat [depth height]) | |
(syntax-parse (list depth pat) | |
[(0 pat) #'pat] | |
[(_ pat) | |
#:with (tmp ...) (map generate-temporary (range splay)) | |
#:with (X ...) (map generate-temporary (range splay)) | |
#:with (child ...) (stx-map (curryr generate-garbage (sub1 depth)) #'[tmp ...]) | |
#'(~or (~and child | |
((~datum X) . _) | |
(~not ((~datum X) . _)) | |
pat) ... | |
pat)])) | |
) | |
(define-syntax define-slow-macro | |
(syntax-parser | |
[(_ (name . pat) tem) | |
#:with pat+ (generate-garbage #'(_ . pat)) | |
#'(define-syntax (name stx) | |
(syntax-parse stx | |
[pat+ #'tem]))])) | |
; try it out: | |
; > (syntax->datum (expand-once #'(define-slow-macro (m e) e))) | |
; generate let using slow macro | |
(define-slow-macro (new-let ([x v] ...) e ...) | |
((λ (x ...) (begin e ...)) v ...)) | |
(provide new-let) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment