Skip to content

Instantly share code, notes, and snippets.

@jeapostrophe
Last active January 7, 2016 22:30
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 jeapostrophe/904bdaf376791fa14cea to your computer and use it in GitHub Desktop.
Save jeapostrophe/904bdaf376791fa14cea to your computer and use it in GitHub Desktop.
#lang racket/base
(require (for-syntax racket/base
syntax/parse)
racket/splicing)
(begin-for-syntax
(define special-define-set
(make-parameter (box '())))
(define (add-to-boxed-list! b v)
(set-box! b (cons v (unbox b)))))
(define-syntax (detect-special-defines stx)
(syntax-parse stx
[(_ body-expr ...)
(define the-b (box '()))
(define ctxt (syntax-local-context))
(printf "ctxt: ~v\n" ctxt)
(with-syntax ([(_ new-body-expr ...)
(parameterize ([special-define-set the-b])
(local-expand/capture-lifts
(syntax/loc stx
(begin body-expr ...))
ctxt
'()))])
(printf "box is: ~v\n" the-b)
(printf "nbe: ~v\n" (syntax->datum #'(new-body-expr ...)))
(with-syntax ([(d ...) (unbox the-b)])
(quasisyntax/loc stx
(begin
new-body-expr ...
(printf "Defined: ~a\n"
'(d ...))))))]))
(define-syntax (special-define stx)
(syntax-parse stx
[(_ x:id b:expr)
(add-to-boxed-list! (special-define-set) #'x)
(syntax/loc stx
(define x b))]))
(detect-special-defines
(special-define x 1)
(special-define y 2))
#lang racket/base
(require (for-syntax racket/base
syntax/parse)
racket/splicing)
(begin-for-syntax
(define special-define-set
(make-parameter (box '())))
(define (add-to-boxed-list! b v)
(set-box! b (cons v (unbox b)))))
(define-syntax (detect-special-defines stx)
(syntax-parse stx
[(_ body-expr ...)
(define the-b (box '()))
(define ctxt (syntax-local-context))
(printf "ctxt: ~v\n" ctxt)
(with-syntax ([((_ new-body-expr ...) ...)
(parameterize ([special-define-set the-b])
(for/list ([be (in-list (syntax->list #'(body-expr ...)))])
(local-expand/capture-lifts
be
ctxt
(list #'define-values))))])
(printf "nbe: ~v\n" (syntax->datum #'(new-body-expr ... ...)))
(with-syntax ([(d ...) (unbox the-b)])
(quasisyntax/loc stx
(begin
new-body-expr ... ...
(printf "Defined: ~a\n"
'(d ...))))))]))
(define-syntax (special-define stx)
(syntax-parse stx
[(_ x:id b:expr)
(printf "special-define ran!\n")
(add-to-boxed-list! (special-define-set) #'x)
(syntax/loc stx
(define x b))]))
(detect-special-defines
(special-define x 1)
(special-define y 2))
(+ x y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment