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 60
(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))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.