| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
(library (ikarus control) |
| 19 |
(export call/cf call/cc call-with-current-continuation dynamic-wind exit |
| 20 |
exit-handler) |
| 21 |
(import |
| 22 |
(ikarus system $stack) |
| 23 |
(except (ikarus) call/cf call/cc call-with-current-continuation |
| 24 |
dynamic-wind exit list-tail exit-handler)) |
| 25 |
|
| 26 |
(define primitive-call/cf |
| 27 |
(lambda (f) |
| 28 |
(if ($fp-at-base) |
| 29 |
(f ($current-frame)) |
| 30 |
($seal-frame-and-call f)))) |
| 31 |
|
| 32 |
(define call/cf |
| 33 |
(lambda (f) |
| 34 |
(if (procedure? f) |
| 35 |
(primitive-call/cf f) |
| 36 |
(die 'call/cf "not a procedure" f)))) |
| 37 |
|
| 38 |
(define primitive-call/cc |
| 39 |
(lambda (f) |
| 40 |
(primitive-call/cf |
| 41 |
(lambda (frm) |
| 42 |
(f ($frame->continuation frm)))))) |
| 43 |
|
| 44 |
(define winders '()) |
| 45 |
|
| 46 |
(define len |
| 47 |
(lambda (ls n) |
| 48 |
(if (null? ls) |
| 49 |
n |
| 50 |
(len (cdr ls) (fxadd1 n))))) |
| 51 |
|
| 52 |
(define list-tail |
| 53 |
(lambda (ls n) |
| 54 |
(if (fxzero? n) |
| 55 |
ls |
| 56 |
(list-tail (cdr ls) (fxsub1 n))))) |
| 57 |
|
| 58 |
(define drop-uncommon-heads |
| 59 |
(lambda (x y) |
| 60 |
(if (eq? x y) |
| 61 |
x |
| 62 |
(drop-uncommon-heads (cdr x) (cdr y))))) |
| 63 |
|
| 64 |
(define common-tail |
| 65 |
(lambda (x y) |
| 66 |
(let ([lx (len x 0)] [ly (len y 0)]) |
| 67 |
(let ([x (if (fx> lx ly) (list-tail x (fx- lx ly)) x)] |
| 68 |
[y (if (fx> ly lx) (list-tail y (fx- ly lx)) y)]) |
| 69 |
(if (eq? x y) |
| 70 |
x |
| 71 |
(drop-uncommon-heads (cdr x) (cdr y))))))) |
| 72 |
|
| 73 |
(define unwind* |
| 74 |
(lambda (ls tail) |
| 75 |
(unless (eq? ls tail) |
| 76 |
(set! winders (cdr ls)) |
| 77 |
((cdar ls)) |
| 78 |
(unwind* (cdr ls) tail)))) |
| 79 |
|
| 80 |
(define rewind* |
| 81 |
(lambda (ls tail) |
| 82 |
(unless (eq? ls tail) |
| 83 |
(rewind* (cdr ls) tail) |
| 84 |
((caar ls)) |
| 85 |
(set! winders ls)))) |
| 86 |
|
| 87 |
(define do-wind |
| 88 |
(lambda (new) |
| 89 |
(let ([tail (common-tail new winders)]) |
| 90 |
(unwind* winders tail) |
| 91 |
(rewind* new tail)))) |
| 92 |
|
| 93 |
(define call/cc |
| 94 |
(lambda (f) |
| 95 |
(unless (procedure? f) |
| 96 |
(die 'call/cc "not a procedure" f)) |
| 97 |
(primitive-call/cc |
| 98 |
(lambda (k) |
| 99 |
(let ([save winders]) |
| 100 |
(f (case-lambda |
| 101 |
[(v) (unless (eq? save winders) (do-wind save)) (k v)] |
| 102 |
[() (unless (eq? save winders) (do-wind save)) (k)] |
| 103 |
[(v1 v2 . v*) |
| 104 |
(unless (eq? save winders) (do-wind save)) |
| 105 |
(apply k v1 v2 v*)]))))))) |
| 106 |
|
| 107 |
(define call-with-current-continuation |
| 108 |
(lambda (f) |
| 109 |
(unless (procedure? f) |
| 110 |
(die 'call-with-current-continuation |
| 111 |
"not a procedure" f)) |
| 112 |
(call/cc f))) |
| 113 |
|
| 114 |
(define dynamic-wind |
| 115 |
(lambda (in body out) |
| 116 |
(unless (procedure? in) |
| 117 |
(die 'dynamic-wind "not a procedure" in)) |
| 118 |
(unless (procedure? body) |
| 119 |
(die 'dynamic-wind "not a procedure" body)) |
| 120 |
(unless (procedure? out) |
| 121 |
(die 'dynamic-wind "not a procedure" out)) |
| 122 |
(in) |
| 123 |
(set! winders (cons (cons in out) winders)) |
| 124 |
(call-with-values |
| 125 |
body |
| 126 |
(case-lambda |
| 127 |
[(v) (set! winders (cdr winders)) (out) v] |
| 128 |
[() (set! winders (cdr winders)) (out) (values)] |
| 129 |
[(v1 v2 . v*) |
| 130 |
(set! winders (cdr winders)) |
| 131 |
(out) |
| 132 |
(apply values v1 v2 v*)])))) |
| 133 |
|
| 134 |
(define exit-handler |
| 135 |
(make-parameter |
| 136 |
(lambda (status) (foreign-call "ikrt_exit" status)) |
| 137 |
(lambda (x) |
| 138 |
(if (procedure? x) |
| 139 |
x |
| 140 |
(die 'exit-handler "not a procedure" x))))) |
| 141 |
|
| 142 |
(define exit |
| 143 |
(case-lambda |
| 144 |
[() (exit 0)] |
| 145 |
[(status) |
| 146 |
(with-exception-handler |
| 147 |
(lambda (ex) |
| 148 |
(with-exception-handler ; Just in case. |
| 149 |
(lambda (_) (foreign-call "ikrt_exit" -4)) |
| 150 |
(lambda () |
| 151 |
(display "Exit-handler exception:\n" (console-error-port)) |
| 152 |
(print-condition ex (console-error-port)))) |
| 153 |
(foreign-call "ikrt_exit" -3)) |
| 154 |
(lambda () |
| 155 |
((exit-handler) status)))])) |
| 156 |
) |