Skip to content

Instantly share code, notes, and snippets.

@jlongster
Created February 16, 2012 00:03
Show Gist options
  • Save jlongster/1840230 to your computer and use it in GitHub Desktop.
Save jlongster/1840230 to your computer and use it in GitHub Desktop.
tracing Outlet code
(trace-source (let ((i (+ 4 5)))
(+ i (* 2 (/ 3 4)))))
;; Output
(let ((i (+ 4 5)))
(+ i (* 2 (/ 3 4))))
-- (+ 4 5)
>> RESULT: 9
-- (+ i (* 2 (/ 3 4)))
---- (* 2 (/ 3 4))
------ (/ 3 4)
>>>>>> RESULT: 0.75
>>>> RESULT: 1.5
>> RESULT: 10.5
RESULT: 10.5
;; trace-source implementation (all of it!)
(define-expander (trace-source form e)
(define (trace-expander src e)
(lambda (x e1)
(if (and (list? x)
(subexpression? x src))
`(trace-form ,(e (list 'quote x) e)
(lambda () ,(e x e1)))
(e x e1))))
(define (subexpression? form src)
(or (equal? form src)
(and (list? src)
(or (subexpression? form (car src))
(subexpression? form (cdr src))))))
(let ((e1 (trace-expander (cadr form) e)))
(e1 (cadr form) e1)))
(define level 0)
(define (trace-form src k)
(define (pad n s)
(vector-for-each (lambda (_) (display s))
(make-vector (* n 2)))
(if (> n 0)
(display " ")))
(pad level "-")
(pretty src)
(display "\n")
(set! level (+ level 1))
(let ((value (k)))
(set! level (- level 1))
(pad level ">")
(display "RESULT: ")
(pretty value)
(display "\n")
value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment