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/>.
|
|
|
|
|
2007-04-30 00:55:13 -04:00
|
|
|
|
2007-05-05 03:16:26 -04:00
|
|
|
|
2007-04-30 01:02:08 -04:00
|
|
|
(library (ikarus control)
|
2007-05-11 22:02:49 -04:00
|
|
|
(export call/cf call/cc call-with-current-continuation dynamic-wind exit)
|
2007-05-05 01:56:44 -04:00
|
|
|
(import
|
2007-05-06 18:10:51 -04:00
|
|
|
(ikarus system $stack)
|
2007-05-11 22:02:49 -04:00
|
|
|
(except (ikarus) call/cf call/cc call-with-current-continuation
|
2007-06-13 10:42:04 -04:00
|
|
|
dynamic-wind exit list-tail))
|
2007-04-30 01:02:08 -04:00
|
|
|
|
2007-05-05 01:56:44 -04:00
|
|
|
(define primitive-call/cf
|
2007-04-30 00:55:13 -04:00
|
|
|
(lambda (f)
|
|
|
|
(if ($fp-at-base)
|
|
|
|
(f ($current-frame))
|
|
|
|
($seal-frame-and-call f))))
|
|
|
|
|
2007-05-05 01:56:44 -04:00
|
|
|
(define call/cf
|
|
|
|
(lambda (f)
|
|
|
|
(if (procedure? f)
|
|
|
|
(primitive-call/cf f)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'call/cf "not a procedure" f))))
|
2007-05-05 01:56:44 -04:00
|
|
|
|
2007-04-30 00:55:13 -04:00
|
|
|
(define primitive-call/cc
|
|
|
|
(lambda (f)
|
2007-05-05 01:56:44 -04:00
|
|
|
(primitive-call/cf
|
2007-04-30 00:55:13 -04:00
|
|
|
(lambda (frm)
|
|
|
|
(f ($frame->continuation frm))))))
|
|
|
|
|
2007-05-05 01:56:44 -04:00
|
|
|
(define winders '())
|
2007-04-30 00:55:13 -04:00
|
|
|
|
|
|
|
(define len
|
|
|
|
(lambda (ls n)
|
|
|
|
(if (null? ls)
|
|
|
|
n
|
|
|
|
(len (cdr ls) (fxadd1 n)))))
|
|
|
|
|
|
|
|
(define list-tail
|
|
|
|
(lambda (ls n)
|
|
|
|
(if (fxzero? n)
|
|
|
|
ls
|
|
|
|
(list-tail (cdr ls) (fxsub1 n)))))
|
|
|
|
|
|
|
|
(define drop-uncommon-heads
|
|
|
|
(lambda (x y)
|
|
|
|
(if (eq? x y)
|
|
|
|
x
|
|
|
|
(drop-uncommon-heads (cdr x) (cdr y)))))
|
|
|
|
|
|
|
|
(define common-tail
|
|
|
|
(lambda (x y)
|
|
|
|
(let ([lx (len x 0)] [ly (len y 0)])
|
|
|
|
(let ([x (if (fx> lx ly) (list-tail x (fx- lx ly)) x)]
|
|
|
|
[y (if (fx> ly lx) (list-tail y (fx- ly lx)) y)])
|
|
|
|
(if (eq? x y)
|
|
|
|
x
|
|
|
|
(drop-uncommon-heads (cdr x) (cdr y)))))))
|
|
|
|
|
|
|
|
(define unwind*
|
|
|
|
(lambda (ls tail)
|
|
|
|
(unless (eq? ls tail)
|
|
|
|
(set! winders (cdr ls))
|
|
|
|
((cdar ls))
|
|
|
|
(unwind* (cdr ls) tail))))
|
|
|
|
|
|
|
|
(define rewind*
|
|
|
|
(lambda (ls tail)
|
|
|
|
(unless (eq? ls tail)
|
|
|
|
(rewind* (cdr ls) tail)
|
|
|
|
((caar ls))
|
|
|
|
(set! winders ls))))
|
|
|
|
|
|
|
|
(define do-wind
|
|
|
|
(lambda (new)
|
|
|
|
(let ([tail (common-tail new winders)])
|
|
|
|
(unwind* winders tail)
|
|
|
|
(rewind* new tail))))
|
|
|
|
|
|
|
|
(define call/cc
|
|
|
|
(lambda (f)
|
2007-09-04 21:01:30 -04:00
|
|
|
(unless (procedure? f)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'call/cc "not a procedure" f))
|
2007-05-05 01:56:44 -04:00
|
|
|
(primitive-call/cc
|
2007-04-30 00:55:13 -04:00
|
|
|
(lambda (k)
|
|
|
|
(let ([save winders])
|
|
|
|
(f (case-lambda
|
|
|
|
[(v) (unless (eq? save winders) (do-wind save)) (k v)]
|
|
|
|
[() (unless (eq? save winders) (do-wind save)) (k)]
|
|
|
|
[(v1 v2 . v*)
|
|
|
|
(unless (eq? save winders) (do-wind save))
|
|
|
|
(apply k v1 v2 v*)])))))))
|
|
|
|
|
2007-05-11 22:02:49 -04:00
|
|
|
(define call-with-current-continuation
|
2007-09-04 21:01:30 -04:00
|
|
|
(lambda (f)
|
|
|
|
(unless (procedure? f)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'call-with-current-continuation
|
2007-10-25 14:32:26 -04:00
|
|
|
"not a procedure" f))
|
2007-09-04 21:01:30 -04:00
|
|
|
(call/cc f)))
|
2007-05-11 22:02:49 -04:00
|
|
|
|
2007-04-30 00:55:13 -04:00
|
|
|
(define dynamic-wind
|
|
|
|
(lambda (in body out)
|
2007-09-04 21:01:30 -04:00
|
|
|
(unless (procedure? in)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'dynamic-wind "not a procedure" in))
|
2007-09-04 21:01:30 -04:00
|
|
|
(unless (procedure? body)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'dynamic-wind "not a procedure" body))
|
2007-09-04 21:01:30 -04:00
|
|
|
(unless (procedure? out)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'dynamic-wind "not a procedure" out))
|
2007-04-30 00:55:13 -04:00
|
|
|
(in)
|
|
|
|
(set! winders (cons (cons in out) winders))
|
|
|
|
(call-with-values
|
|
|
|
body
|
|
|
|
(case-lambda
|
|
|
|
[(v) (set! winders (cdr winders)) (out) v]
|
|
|
|
[() (set! winders (cdr winders)) (out) (values)]
|
|
|
|
[(v1 v2 . v*)
|
|
|
|
(set! winders (cdr winders))
|
|
|
|
(out)
|
2007-05-05 03:16:26 -04:00
|
|
|
(apply values v1 v2 v*)]))))
|
|
|
|
|
|
|
|
(define exit
|
|
|
|
(case-lambda
|
|
|
|
[() (exit 0)]
|
2007-10-15 10:41:08 -04:00
|
|
|
[(status) (foreign-call "ikrt_exit" status)]))
|
2007-05-05 03:16:26 -04:00
|
|
|
)
|