callcc.stk 2.56 KB
Newer Older
separdau's avatar
separdau committed
1 2
;;;;
;;;; callcc.stk	-- R5RS call/cc function written in Scheme
3 4 5 6
;;;;
;;;; Copyright © 2000-2006 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;;
;;;;
separdau's avatar
separdau committed
7 8 9 10
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
11
;;;;
separdau's avatar
separdau committed
12 13 14 15
;;;; 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.
16
;;;;
separdau's avatar
separdau committed
17 18
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
19
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
separdau's avatar
separdau committed
20
;;;; USA.
21
;;;;
separdau's avatar
separdau committed
22 23
;;;;           Author: Erick Gallesio [eg@unice.fr]
;;;;    Creation date:  3-Apr-2006 12:27 (eg)
eg's avatar
eg committed
24
;;;; Last file update: 24-Oct-2006 17:33 (eg)
separdau's avatar
separdau committed
25 26 27 28 29 30 31 32 33 34
;;;;

(define (%call/cc proc)
  ;; A simple continuation without winding (accepts multiples values)
  (let ((k  (%make-continuation)))
    (if  (%fresh-continuation? k)
        (proc (lambda v
		(%restore-continuation k v)))
        (apply values k))))

eg's avatar
eg committed
35
;(define %dynamic-wind-stack (make-parameter (list #f)))
separdau's avatar
separdau committed
36 37 38 39

(define call/cc #f)
(define dynamic-wind #f)

eg's avatar
eg committed
40
(let ()
41

separdau's avatar
separdau committed
42
  (define (reroot! there)
eg's avatar
eg committed
43 44 45 46 47 48 49 50 51 52 53
      (unless (eq? (%thread-dynwind-stack) there)
	(reroot! (cdr there))
	(let ((before (caar there))
	      (after  (cdar there))
	      (here   (%thread-dynwind-stack)))
	  (set-car! here (cons after before))
	  (set-cdr! here  there)
	  (set-car! there #f)
	  (set-cdr! there '())
	  (%thread-dynwind-stack-set! there)
	  (before))))
separdau's avatar
separdau committed
54 55 56 57 58 59

  ;;
  ;; call/cc
  ;;
  (set! call/cc
    (lambda  (proc)
eg's avatar
eg committed
60
      (let ((here (%thread-dynwind-stack)))
separdau's avatar
separdau committed
61 62 63 64
	(%call/cc (lambda (cont)
		    (proc (lambda results
			    (reroot! here)
			    (apply cont results))))))))
65

separdau's avatar
separdau committed
66 67 68 69 70 71 72 73
  ;;
  ;; dynamic-wind
  ;;
  (set! dynamic-wind
    (lambda (before during after)
      (define (verify-proc p)
	(unless (procedure? p)  ; Should test it is a thunk
	  (error 'dynamic-wind "bad procedure ~S" p)))
74

separdau's avatar
separdau committed
75 76 77
      (verify-proc before)
      (verify-proc during)
      (verify-proc after)
78

eg's avatar
eg committed
79
      (let ((here (%thread-dynwind-stack)))
separdau's avatar
separdau committed
80 81 82 83 84 85 86
	(reroot! (cons (cons before after) here))
	(with-handler (lambda (c)
			(reroot! here)
			(raise c))
		      (call-with-values during
			(lambda results (reroot! here) (apply values results))))))))

eg's avatar
eg committed
87 88 89 90 91

;;
;; call-with-current-continuation
;;
(define call-with-current-continuation call/cc)