Debian Codegraph @

scheme/ikarus.handlers.ss

raw
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 (library (ikarus system parameters)
17 (export make-parameter)
18 (import (except (ikarus) make-parameter))
19 (define make-parameter
20 (let ()
21 (import (ikarus))
22 (case-lambda
23 [(x guard) (make-parameter x guard)]
24 [(x) (make-parameter x)]))))
25
26 (library (ikarus.pointer-value)
27 (export pointer-value)
28 (import (only (ikarus) define import))
29 (define (pointer-value x)
30 (import (ikarus))
31 (pointer-value x)))
32
33
34 (library (ikarus system handlers)
35 (export
36 interrupt-handler $apply-nonprocedure-error-handler
37 $incorrect-args-error-handler $multiple-values-error $debug
38 $underflow-misaligned-error top-level-value-error car-error
39 cdr-error fxadd1-error fxsub1-error cadr-error fx+-type-error
40 fx+-types-error fx+-overflow-error $do-event engine-handler)
41 (import (except (ikarus) interrupt-handler engine-handler)
42 (only (ikarus system $interrupts) $interrupted? $unset-interrupted!)
43 (only (ikarus.posix) signal-code->signal-name))
44
45 (define interrupt-handler
46 (make-parameter
47 (lambda (signal)
48 ; FIXME
49 ;(set-port-output-index! (console-output-port) 0)
50 (raise-continuable
51 (condition
52 (make-interrupted-condition signal)
53 (make-message-condition "received an interrupt signal"))))
54 (lambda (x)
55 (if (procedure? x)
56 x
57 (die 'interrupt-handler "not a procedure" x)))))
58
59 (define engine-handler
60 (make-parameter
61 void
62 (lambda (x)
63 (if (procedure? x)
64 x
65 (die 'engine-handler "not a procedure" x)))))
66
67 (define $apply-nonprocedure-error-handler
68 (lambda (x)
69 (die 'apply "not a procedure" x)))
70
71 (define $incorrect-args-error-handler
72 (lambda (p . ls)
73 (apply die 'apply "incorrect number of arguments" p ls)))
74
75 (define $multiple-values-error
76 (lambda args
77 (die 'apply
78 "incorrect number of values returned to single value context"
79 args)))
80
81 (define $debug
82 (lambda (x)
83 (foreign-call "ik_error" (cons "DEBUG" x))))
84
85 (define $underflow-misaligned-error
86 (lambda ()
87 (foreign-call "ik_error" "misaligned")))
88
89 (define top-level-value-error
90 (lambda (x)
91 (cond
92 [(symbol? x)
93 (if (symbol-bound? x)
94 (die 'top-level-value-error "BUG: should not happen" x)
95 (die #f "unbound" (string->symbol (symbol->string x))))]
96 [else
97 (die 'top-level-value "not a symbol" x)])))
98
99 (define car-error
100 (lambda (x)
101 (die 'car "not a pair" x)))
102
103 (define cdr-error
104 (lambda (x)
105 (die 'cdr "not a pair" x)))
106
107 (define fxadd1-error
108 (lambda (x)
109 (if (fixnum? x)
110 (die 'fxadd1 "overflow")
111 (die 'fxadd1 "not a fixnum" x))))
112
113 (define fxsub1-error
114 (lambda (x)
115 (if (fixnum? x)
116 (die 'fxsub1 "underflow")
117 (die 'fxsub1 "not a fixnum" x))))
118
119 (define cadr-error
120 (lambda (x)
121 (die 'cadr "invalid list structure" x)))
122
123 (define fx+-type-error
124 (lambda (x)
125 (die 'fx+ "not a fixnum" x)))
126
127 (define fx+-types-error
128 (lambda (x y)
129 (die 'fx+ "not a fixnum"
130 (if (fixnum? x) y x))))
131
132 (define fx+-overflow-error
133 (lambda (x y)
134 (die 'fx+ "overflow")))
135
136 (define $do-event
137 (lambda ()
138 (cond
139 [($interrupted?)
140 (let ((signal (signal-code->signal-name
141 (foreign-call "ikrt_interrupted_signal"))))
142 ($unset-interrupted!)
143 ((interrupt-handler) signal))]
144 [else
145 ((engine-handler))])))
146
147 )