2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2006-11-23 19:44:29 -05:00
|
|
|
|
2007-04-29 20:55:51 -04:00
|
|
|
(library (ikarus trace)
|
2008-07-12 01:31:40 -04:00
|
|
|
(export make-traced-procedure make-traced-macro)
|
|
|
|
(import (except (ikarus) make-traced-procedure make-traced-macro))
|
2007-04-29 20:55:51 -04:00
|
|
|
|
2006-11-23 19:44:29 -05:00
|
|
|
(define k* '())
|
|
|
|
|
|
|
|
(define display-prefix
|
|
|
|
(lambda (ls t)
|
|
|
|
(unless (null? ls)
|
|
|
|
(display (if t "|" " "))
|
|
|
|
(display-prefix (cdr ls) (not t)))))
|
|
|
|
|
|
|
|
(define display-trace
|
|
|
|
(lambda (k* v)
|
|
|
|
(display-prefix k* #t)
|
|
|
|
(write v)
|
|
|
|
(newline)))
|
|
|
|
|
2008-07-12 01:31:40 -04:00
|
|
|
|
2006-11-23 19:44:29 -05:00
|
|
|
(define make-traced-procedure
|
2007-12-11 17:41:48 -05:00
|
|
|
(case-lambda
|
|
|
|
[(name proc) (make-traced-procedure name proc (lambda (x) x))]
|
|
|
|
[(name proc filter)
|
|
|
|
(lambda args
|
|
|
|
(call/cf
|
|
|
|
(lambda (f)
|
|
|
|
(cond
|
|
|
|
[(memq f k*) =>
|
|
|
|
(lambda (ls)
|
|
|
|
(display-trace ls (filter (cons name args)))
|
|
|
|
(apply proc args))]
|
|
|
|
[else
|
|
|
|
(display-trace (cons 1 k*) (filter (cons name args)))
|
|
|
|
(dynamic-wind
|
|
|
|
(lambda () (set! k* (cons f k*)))
|
|
|
|
(lambda ()
|
|
|
|
(call-with-values
|
|
|
|
(lambda ()
|
|
|
|
(call/cf
|
|
|
|
(lambda (nf)
|
|
|
|
(set! f nf)
|
|
|
|
(set-car! k* nf)
|
|
|
|
(apply proc args))))
|
|
|
|
(lambda v*
|
|
|
|
(display-prefix k* #t)
|
|
|
|
(unless (null? v*)
|
|
|
|
(let ([v* (filter v*)])
|
|
|
|
(write (car v*))
|
|
|
|
(let f ([v* (cdr v*)])
|
2008-01-23 03:14:33 -05:00
|
|
|
(unless (null? v*)
|
|
|
|
(write-char #\space)
|
|
|
|
(write (car v*))
|
|
|
|
(f (cdr v*))))))
|
|
|
|
(newline)
|
2007-12-11 17:41:48 -05:00
|
|
|
(apply values v*))))
|
2008-07-12 01:31:40 -04:00
|
|
|
(lambda () (set! k* (cdr k*))))]))))]))
|
|
|
|
|
|
|
|
(define make-traced-macro
|
|
|
|
(lambda (name x)
|
|
|
|
(cond
|
|
|
|
[(procedure? x)
|
2008-07-12 13:40:22 -04:00
|
|
|
(make-traced-procedure name x syntax->datum)]
|
2008-07-12 01:31:40 -04:00
|
|
|
[(variable-transformer? x)
|
|
|
|
(make-variable-transformer
|
|
|
|
(make-traced-procedure name
|
|
|
|
(variable-transformer-procedure x)
|
|
|
|
syntax->datum))]
|
|
|
|
[else x]))))
|
2007-09-13 22:42:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
#!eof
|
|
|
|
|
|
|
|
(define traced-symbols '())
|
|
|
|
|
|
|
|
(define untrace-symbol!
|
|
|
|
(lambda (s)
|
|
|
|
(define loop
|
|
|
|
(lambda (ls)
|
|
|
|
(cond
|
|
|
|
[(null? ls) '()]
|
|
|
|
[(eq? s (caar ls))
|
|
|
|
(let ([a (cdar ls)])
|
|
|
|
(when (eq? (cdr a) (symbol-value s))
|
|
|
|
(set-symbol-value! s (car a)))
|
|
|
|
(cdr ls))]
|
|
|
|
[else (cons (car ls) (loop (cdr ls)))])))
|
|
|
|
(set! traced-symbols (loop traced-symbols))))
|
2006-11-23 19:44:29 -05:00
|
|
|
|
|
|
|
(define trace-symbol!
|
|
|
|
(lambda (s)
|
|
|
|
(cond
|
|
|
|
[(assq s traced-symbols) =>
|
|
|
|
(lambda (pr)
|
2007-05-06 18:52:19 -04:00
|
|
|
(let ([a (cdr pr)] [v (symbol-value s)])
|
2006-11-23 19:44:29 -05:00
|
|
|
(unless (eq? (cdr a) v)
|
|
|
|
(unless (procedure? v)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'trace
|
2007-10-25 14:32:26 -04:00
|
|
|
"the top-level value is not a procedure"
|
2006-11-23 19:44:29 -05:00
|
|
|
s v))
|
|
|
|
(let ([p (make-traced-procedure s v)])
|
|
|
|
(set-car! a v)
|
|
|
|
(set-cdr! a p)
|
2007-05-06 18:52:19 -04:00
|
|
|
(set-symbol-value! s p)))))]
|
2006-11-23 19:44:29 -05:00
|
|
|
[else
|
2007-05-06 18:52:19 -04:00
|
|
|
(unless (symbol-bound? s)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'trace "unbound" s))
|
2007-05-06 18:52:19 -04:00
|
|
|
(let ([v (symbol-value s)])
|
2006-11-23 19:44:29 -05:00
|
|
|
(unless (procedure? v)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'trace "the top-level value is not a procedure" s v))
|
2006-11-23 19:44:29 -05:00
|
|
|
(let ([p (make-traced-procedure s v)])
|
|
|
|
(set! traced-symbols
|
|
|
|
(cons (cons s (cons v p)) traced-symbols))
|
2007-05-06 18:52:19 -04:00
|
|
|
(set-symbol-value! s p)))])))
|
2006-12-05 13:28:23 -05:00
|
|
|
Try:
|
|
|
|
|
|
|
|
(trace-define fact
|
|
|
|
(lambda (n)
|
|
|
|
(if (zero? n)
|
|
|
|
1
|
|
|
|
(* n (fact (sub1 n))))))
|
|
|
|
(fact 5)
|
|
|
|
|
|
|
|
(trace-define (fact n m)
|
|
|
|
(cond
|
|
|
|
[(zero? n) m]
|
|
|
|
[else (fact (sub1 n) (* n m))]))
|
|
|
|
(fact 5 1)
|
|
|
|
|
2006-12-05 13:56:54 -05:00
|
|
|
|
|
|
|
(trace-define (fact n m k)
|
|
|
|
(cond
|
|
|
|
[(zero? n) (k m)]
|
|
|
|
[else (begin (fact (sub1 n) (* n m) k) 0)]))
|
2007-09-15 00:14:47 -04:00
|
|
|
|
|
|
|
|
2006-12-05 13:56:54 -05:00
|
|
|
(call/cc
|
|
|
|
(lambda (k)
|
|
|
|
(fact 6 1
|
|
|
|
(trace-lambda escape (v) (k v)))))
|
|
|
|
|
2007-09-15 00:14:47 -04:00
|
|
|
|
|
|
|
(call/cc
|
|
|
|
(lambda k*
|
|
|
|
(trace-define (fact n)
|
|
|
|
(cond
|
|
|
|
[(zero? n)
|
|
|
|
(call/cc
|
|
|
|
(lambda (k)
|
|
|
|
(set! k* (cons k k*))
|
|
|
|
1))]
|
|
|
|
[else (* n (fact (sub1 n)))]))
|
|
|
|
(fact 9)
|
|
|
|
(let ([k (car k*)])
|
|
|
|
(set! k* (cdr k*))
|
|
|
|
(k 100000))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-12-05 13:56:54 -05:00
|
|
|
(trace-define (infinite-loop n)
|
|
|
|
(infinite-loop (add1 n)))
|
|
|
|
(infinite-loop 0)
|
|
|
|
|