public
Last active

tracing Outlet code

  • Download Gist
gistfile1.scm
Scheme
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
(trace-source (let ((i (+ 4 5)))
(+ i (* 2 (/ 3 4)))))
 
 
;; Output
 
(let
((i (+ 4 5)))
(+ i (* 2 (/ 3 4))))
||||(+ 4 5)
||||9
||||(+ i (* 2 (/ 3 4)))
||||||||(* 2 (/ 3 4))
||||||||||||(/ 3 4)
||||||||||||0.75
||||||||1.5
||||10.5
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)
(print-trace level src)
(set! level (+ level 1))
(let ((value (k)))
(set! level (- level 1))
(print-trace level value)
value))
 
(define (print-trace level obj)
(define (pad n)
(vector-for-each (lambda (_) (display "|"))
(make-vector (* n 4))))
 
(pad level)
(pretty obj)
(display "\n"))
 
(trace-source (let ((i (+ 4 5)))
(+ i (* 2 (/ 3 4)))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.