Skip to content

Instantly share code, notes, and snippets.

@jeapostrophe
Created June 1, 2010 20:20
Show Gist options
  • Save jeapostrophe/421425 to your computer and use it in GitHub Desktop.
Save jeapostrophe/421425 to your computer and use it in GitHub Desktop.
(define-syntax-rule (code/size s c ...)
(parameterize ([current-font-size s])
(code c ...)))
(define opt-code
(code/size
20
(define y 5)
(define (g z)
(+ z 3))
(define (f x)
(g (+ x y)))))
(define opt-code-mod (racket-module opt-code "example.rkt" 320 440))
(define (crossout p)
(lt-superimpose p (hline (pict-width p) (pict-height p))))
(require racket/stxparam)
(define-syntax-parameter cl-before
(λ (stx) (raise-syntax-error 'cl-before "..." stx)))
(define-syntax-parameter cl-only
(λ (stx) (raise-syntax-error 'cl-only "..." stx)))
(define-syntax-rule (code-line step code-sample)
(let ([sample-pict (code/size 20 code-sample)])
(cl-before step
(ghost sample-pict)
(cl-only step
sample-pict
(crossout sample-pict)))))
(define-syntax-rule (with-steps* steps . rest)
(with-steps
steps
(syntax-parameterize
([cl-before (make-rename-transformer #'before)]
[cl-only (make-rename-transformer #'only)])
. rest)))
(require (for-syntax racket/local
racket/list))
(define-syntax-parameter code-step
(λ (stx) (raise-syntax-error 'code-step "..." stx)))
; JM: This is not for the faint of heart. It will break if e's
expansion (in particular the number and order of code-step occurrences
is not deterministic. If you write macros like *this*, it won't be
deterministic, so beware.
(define-syntax (with-code-steps stx)
(syntax-case stx ()
[(_ e)
(local
[(define steps empty)
(define (code-first-pass stx)
(syntax-case stx ()
[(_ l)
(with-syntax ([(this-step) (generate-temporaries #'(l))])
(set! steps (list* #'this-step steps))
#'1)]))
(define i 0)
(define (code-second-pass stx)
(syntax-case stx ()
[(_ l)
(with-syntax ([this-step (list-ref steps i)])
(set! i (add1 i))
#'(code-line this-step l))]))]
(local-expand #`(syntax-parameterize ([code-step #,code-first-pass])
e)
'expression empty)
(set! steps (reverse steps))
#`(syntax-parameterize ([code-step #,code-second-pass])
(with-steps* #,steps
e)))]))
(void
(with-code-steps
(slide
#:title "Optimizations Need Information"
(vl-append
(refocus opt-code-mod opt-code)
(code-step (f 7))
(code-step (g (+ 7 y)))
(code-step (g (+ 7 5)))
(code-step (g 12))
(code-step (+ 12 3))
(code-step 15)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment