Commit | Line | Data |
---|---|---|
17e90c5e KN |
1 | ;;; GHIL macros |
2 | ||
3 | ;; Copyright (C) 2001 Free Software Foundation, Inc. | |
4 | ||
5 | ;; This program is free software; you can redistribute it and/or modify | |
6 | ;; it under the terms of the GNU General Public License as published by | |
7 | ;; the Free Software Foundation; either version 2, or (at your option) | |
8 | ;; any later version. | |
9 | ;; | |
10 | ;; This program is distributed in the hope that it will be useful, | |
11 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | ;; GNU General Public License for more details. | |
14 | ;; | |
15 | ;; You should have received a copy of the GNU General Public License | |
16 | ;; along with this program; see the file COPYING. If not, write to | |
17 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | ;; Boston, MA 02111-1307, USA. | |
19 | ||
20 | ;;; Code: | |
21 | ||
22 | (define-module (system il macros) | |
23 | :use-module (ice-9 match)) | |
24 | ||
25 | (define (make-label) (gensym ":L")) | |
26 | (define (make-sym) (gensym "_")) | |
27 | ||
17e90c5e KN |
28 | \f |
29 | ;;; | |
30 | ;;; Syntax | |
31 | ;;; | |
32 | ||
33 | ;; (@and X Y...) => | |
34 | ;; | |
35 | ;; (@if X (@and Y...) #f) | |
36 | (define @and | |
37 | (match-lambda* | |
38 | (() #t) | |
39 | ((x) x) | |
40 | ((x . rest) `(@if ,x (@and ,@rest) #f)))) | |
41 | ||
42 | ;; (@or X Y...) => | |
43 | ;; | |
44 | ;; (@let ((@_ X)) (@if @_ @_ (@or Y...))) | |
45 | (define @or | |
46 | (match-lambda* | |
47 | (() #f) | |
48 | ((x) x) | |
49 | ((x . rest) | |
50 | (let ((sym (make-sym))) | |
51 | `(@let ((,sym ,x)) (@if ,sym ,sym (@or ,@rest))))))) | |
52 | ||
17e90c5e KN |
53 | ;; (@cond (TEST BODY...) ...) => |
54 | ;; | |
55 | ;; (@if TEST | |
56 | ;; (@begin BODY...) | |
57 | ;; (@cond ...)) | |
58 | (define (@cond . clauses) | |
59 | (cond ((null? clauses) (error "missing clauses")) | |
60 | ((pair? (car clauses)) | |
61 | (let ((c (car clauses)) (l (cdr clauses))) | |
62 | (let ((rest (if (null? l) '(@void) `(@cond ,@l)))) | |
63 | (cond ((eq? (car c) '@else) `(@begin (@void) ,@(cdr c))) | |
64 | ((null? (cdr c)) `(@or ,(car c) ,rest)) | |
65 | (else `(@if ,(car c) (@begin ,@(cdr c)) ,rest)))))) | |
66 | (else (error "bad clause:" (car clauses))))) | |
67 | ||
68 | (define (@let* binds . body) | |
69 | (if (null? binds) | |
70 | `(@begin ,@body) | |
71 | `(@let (,(car binds)) (@let* ,(cdr binds) ,@body)))) | |
72 | ||
73 | \f | |
74 | ;;; | |
75 | ;;; R5RS Procedures | |
76 | ;;; | |
77 | ||
78 | ;; 6. Standard procedures | |
79 | ||
80 | ;;; 6.1 Equivalence predicates | |
81 | ||
a80be762 KN |
82 | (define (@eq? x y) `(@@ eq? ,x ,y)) |
83 | (define (@eqv? x y) `(@@ eqv? ,x ,y)) | |
84 | (define (@equal? x y) `(@@ equal? ,x ,y)) | |
17e90c5e KN |
85 | |
86 | ;;; 6.2 Numbers | |
87 | ||
a80be762 KN |
88 | (define (@number? x) `((@ Core::number?) ,x)) |
89 | (define (@complex? x) `((@ Core::complex?) ,x)) | |
90 | (define (@real? x) `((@ Core::real?) ,x)) | |
91 | (define (@rational? x) `((@ Core::rational?) ,x)) | |
92 | (define (@integer? x) `((@ Core::integer?) ,x)) | |
17e90c5e | 93 | |
a80be762 KN |
94 | (define (@exact? x) `((@ Core::exact?) ,x)) |
95 | (define (@inexact? x) `((@ Core::inexact?) ,x)) | |
17e90c5e | 96 | |
a80be762 KN |
97 | (define (@= x y) `(@@ ee? ,x ,y)) |
98 | (define (@< x y) `(@@ lt? ,x ,y)) | |
99 | (define (@> x y) `(@@ gt? ,x ,y)) | |
100 | (define (@<= x y) `(@@ le? ,x ,y)) | |
101 | (define (@>= x y) `(@@ ge? ,x ,y)) | |
17e90c5e KN |
102 | |
103 | (define @+ | |
104 | (match-lambda* | |
105 | (() 0) | |
106 | ((x) x) | |
107 | ((x y) `(@@ add ,x ,y)) | |
108 | ((x y . rest) `(@@ add ,x (@+ ,y ,@rest))))) | |
109 | ||
110 | (define @* | |
111 | (match-lambda* | |
112 | (() 1) | |
113 | ((x) x) | |
114 | ((x y) `(@@ mul ,x ,y)) | |
115 | ((x y . rest) `(@@ mul ,x (@* ,y ,@rest))))) | |
116 | ||
117 | (define @- | |
118 | (match-lambda* | |
a80be762 | 119 | ((x) `(@@ sub 0 ,x)) |
17e90c5e KN |
120 | ((x y) `(@@ sub ,x ,y)) |
121 | ((x y . rest) `(@@ sub ,x (@+ ,y ,@rest))))) | |
122 | ||
123 | (define @/ | |
124 | (match-lambda* | |
a80be762 | 125 | ((x) `(@@ div 1 ,x)) |
17e90c5e KN |
126 | ((x y) `(@@ div ,x ,y)) |
127 | ((x y . rest) `(@@ div ,x (@* ,y ,@rest))))) | |
128 | ||
a80be762 KN |
129 | (define (@quotient x y) `(@@ quo ,x ,y)) |
130 | (define (@remainder x y) `(@@ rem ,x ,y)) | |
131 | (define (@modulo x y) `(@@ mod ,x ,y)) | |
46cd9a34 | 132 | |
17e90c5e KN |
133 | ;;; numerator |
134 | ;;; denominator | |
135 | ;;; | |
136 | ;;; floor | |
137 | ;;; ceiling | |
138 | ;;; truncate | |
139 | ;;; round | |
140 | ;;; | |
17e90c5e KN |
141 | ;;; exp |
142 | ;;; log | |
143 | ;;; sin | |
144 | ;;; cos | |
145 | ;;; tan | |
146 | ;;; asin | |
147 | ;;; acos | |
148 | ;;; atan | |
149 | ;;; | |
150 | ;;; sqrt | |
151 | ;;; expt | |
152 | ;;; | |
153 | ;;; make-rectangular | |
154 | ;;; make-polar | |
155 | ;;; real-part | |
156 | ;;; imag-part | |
157 | ;;; magnitude | |
158 | ;;; angle | |
159 | ;;; | |
160 | ;;; exact->inexact | |
161 | ;;; inexact->exact | |
162 | ;;; | |
163 | ;;; number->string | |
164 | ;;; string->number | |
165 | ||
166 | ;;; 6.3 Other data types | |
167 | ||
168 | ;;;; 6.3.1 Booleans | |
169 | ||
170 | (define (@not x) `(@@ not ,x)) | |
a80be762 | 171 | (define (@boolean? x) `((@ Core::boolean?) ,x)) |
17e90c5e KN |
172 | |
173 | ;;;; 6.3.2 Pairs and lists | |
174 | ||
175 | (define (@pair? x) `(@@ pair? ,x)) | |
176 | (define (@cons x y) `(@@ cons ,x ,y)) | |
177 | ||
178 | (define (@car x) `(@@ car ,x)) | |
179 | (define (@cdr x) `(@@ cdr ,x)) | |
180 | (define (@set-car! x) `(@@ set-car! ,x)) | |
181 | (define (@set-cdr! x) `(@@ set-cdr! ,x)) | |
182 | ||
183 | (define (@caar x) `(@@ car (@@ car ,x))) | |
184 | (define (@cadr x) `(@@ car (@@ cdr ,x))) | |
185 | (define (@cdar x) `(@@ cdr (@@ car ,x))) | |
186 | (define (@cddr x) `(@@ cdr (@@ cdr ,x))) | |
187 | (define (@caaar x) `(@@ car (@@ car (@@ car ,x)))) | |
188 | (define (@caadr x) `(@@ car (@@ car (@@ cdr ,x)))) | |
189 | (define (@cadar x) `(@@ car (@@ cdr (@@ car ,x)))) | |
190 | (define (@caddr x) `(@@ car (@@ cdr (@@ cdr ,x)))) | |
191 | (define (@cdaar x) `(@@ cdr (@@ car (@@ car ,x)))) | |
192 | (define (@cdadr x) `(@@ cdr (@@ car (@@ cdr ,x)))) | |
193 | (define (@cddar x) `(@@ cdr (@@ cdr (@@ car ,x)))) | |
194 | (define (@cdddr x) `(@@ cdr (@@ cdr (@@ cdr ,x)))) | |
195 | (define (@caaaar x) `(@@ car (@@ car (@@ car (@@ car ,x))))) | |
196 | (define (@caaadr x) `(@@ car (@@ car (@@ car (@@ cdr ,x))))) | |
197 | (define (@caadar x) `(@@ car (@@ car (@@ cdr (@@ car ,x))))) | |
198 | (define (@caaddr x) `(@@ car (@@ car (@@ cdr (@@ cdr ,x))))) | |
199 | (define (@cadaar x) `(@@ car (@@ cdr (@@ car (@@ car ,x))))) | |
200 | (define (@cadadr x) `(@@ car (@@ cdr (@@ car (@@ cdr ,x))))) | |
201 | (define (@caddar x) `(@@ car (@@ cdr (@@ cdr (@@ car ,x))))) | |
202 | (define (@cadddr x) `(@@ car (@@ cdr (@@ cdr (@@ cdr ,x))))) | |
203 | (define (@cdaaar x) `(@@ cdr (@@ car (@@ car (@@ car ,x))))) | |
204 | (define (@cdaadr x) `(@@ cdr (@@ car (@@ car (@@ cdr ,x))))) | |
205 | (define (@cdadar x) `(@@ cdr (@@ car (@@ cdr (@@ car ,x))))) | |
206 | (define (@cdaddr x) `(@@ cdr (@@ car (@@ cdr (@@ cdr ,x))))) | |
207 | (define (@cddaar x) `(@@ cdr (@@ cdr (@@ car (@@ car ,x))))) | |
208 | (define (@cddadr x) `(@@ cdr (@@ cdr (@@ car (@@ cdr ,x))))) | |
209 | (define (@cdddar x) `(@@ cdr (@@ cdr (@@ cdr (@@ car ,x))))) | |
210 | (define (@cddddr x) `(@@ cdr (@@ cdr (@@ cdr (@@ cdr ,x))))) | |
211 | ||
212 | (define (@null? x) `(@@ null? ,x)) | |
213 | (define (@list? x) `(@@ list? ,x)) | |
214 | (define (@list . args) `(@@ list ,@args)) | |
215 | ||
216 | ;;; length | |
217 | ;;; append | |
218 | ;;; reverse | |
17e90c5e KN |
219 | ;;; |
220 | ;;; memq | |
221 | ;;; memv | |
222 | ;;; member | |
223 | ;;; | |
224 | ;;; assq | |
225 | ;;; assv | |
226 | ;;; assoc | |
227 | ||
228 | ;;;; 6.3.3 Symbols | |
229 | ||
230 | ;;; symbol? | |
231 | ;;; symbol->string | |
232 | ;;; string->symbol | |
233 | ||
234 | ;;;; 6.3.4 Characters | |
235 | ||
236 | ;;; char? | |
237 | ;;; char=? | |
238 | ;;; char<? | |
239 | ;;; char>? | |
240 | ;;; char<=? | |
241 | ;;; char>=? | |
17e90c5e KN |
242 | ;;; char->integer |
243 | ;;; integer->char | |
17e90c5e KN |
244 | |
245 | ;;;; 6.3.5 Strings | |
246 | ||
247 | ;;; string? | |
248 | ;;; make-string | |
17e90c5e KN |
249 | ;;; string-length |
250 | ;;; string-ref | |
251 | ;;; string-set! | |
17e90c5e KN |
252 | |
253 | ;;;; 6.3.6 Vectors | |
254 | ||
255 | ;;; vector? | |
256 | ;;; make-vector | |
17e90c5e KN |
257 | ;;; vector-length |
258 | ;;; vector-ref | |
259 | ;;; vector-set! | |
17e90c5e KN |
260 | |
261 | ;;;; 6.4 Control features | |
262 | ||
a80be762 | 263 | ;; (define (@procedure? x) `(@@ procedure? x)) |
17e90c5e KN |
264 | |
265 | ;; (define (@apply proc . args) ...) | |
266 | ||
46cd9a34 | 267 | ;;; (define (@force promise) `(@@ force promise)) |
17e90c5e | 268 | |
a80be762 | 269 | ;;; (define (@call/cc proc) `(@@ call/cc proc)) |
17e90c5e KN |
270 | |
271 | ;;; values | |
272 | ;;; call-with-values | |
273 | ;;; dynamic-wind | |
274 | ||
275 | ;;; 6.5 Eval | |
276 | ||
17e90c5e KN |
277 | ;;; 6.6 Input and output |
278 | ||
279 | ;;;; 6.6.1 Ports | |
280 | ||
17e90c5e KN |
281 | ;;; input-port? |
282 | ;;; output-port? | |
283 | ;;; current-input-port | |
284 | ;;; current-output-port | |
285 | ;;; | |
17e90c5e KN |
286 | ;;; open-input-file |
287 | ;;; open-output-file | |
288 | ;;; close-input-port | |
289 | ;;; close-output-port | |
290 | ||
291 | ;;;; 6.6.2 Input | |
292 | ||
293 | ;;; read | |
294 | ;;; read-char | |
295 | ;;; peek-char | |
296 | ;;; eof-object? | |
297 | ;;; char-ready? | |
298 | ||
299 | ;;;; 6.6.3 Output | |
300 | ||
301 | ;;; write | |
302 | ;;; display | |
303 | ;;; newline | |
304 | ;;; write-char | |
305 | ||
306 | ;;;; 6.6.4 System interface | |
307 | ||
17e90c5e KN |
308 | \f |
309 | ;;; | |
310 | ;;; Non-R5RS Procedures | |
311 | ;;; | |
312 | ||
313 | (define @cons* | |
314 | (match-lambda* | |
315 | ((x) x) | |
316 | ((x y) `(@cons ,x ,y)) | |
317 | ((x y . rest) `(@cons ,x (@cons* ,y ,@rest))))) |