3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
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)
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.
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.
22 (define-module (system il macros)
23 :use-module (ice-9 match))
25 (define (make-label) (gensym ":L"))
26 (define (make-sym) (gensym "_"))
32 (define (@import identifier)
33 `((@ System::Base::module::do-import) (@quote ,identifier)))
42 ;; (@if X (@and Y...) #f)
47 ((x . rest) `(@if ,x (@and ,@rest) #f))))
51 ;; (@let ((@_ X)) (@if @_ @_ (@or Y...)))
57 (let ((sym (make-sym)))
58 `(@let ((,sym ,x)) (@if ,sym ,sym (@or ,@rest)))))))
60 ;; (@while TEST BODY...) =>
64 ;; L1: (@if TEST (@goto L0) (@void))
66 (define (@while test . body)
67 (let ((L0 (make-label)) (L1 (make-label)))
71 (@label ,L1) (@if ,test (@goto ,L0) (@void)))))
73 ;; (@cond (TEST BODY...) ...) =>
78 (define (@cond . clauses)
79 (cond ((null? clauses) (error "missing clauses"))
80 ((pair? (car clauses))
81 (let ((c (car clauses)) (l (cdr clauses)))
82 (let ((rest (if (null? l) '(@void) `(@cond ,@l))))
83 (cond ((eq? (car c) '@else) `(@begin (@void) ,@(cdr c)))
84 ((null? (cdr c)) `(@or ,(car c) ,rest))
85 (else `(@if ,(car c) (@begin ,@(cdr c)) ,rest))))))
86 (else (error "bad clause:" (car clauses)))))
88 (define (@let* binds . body)
91 `(@let (,(car binds)) (@let* ,(cdr binds) ,@body))))
98 ;; 6. Standard procedures
100 ;;; 6.1 Equivalence predicates
102 (define (@eq? x y) `(@@ eq? ,x ,y))
103 (define (@eqv? x y) `(@@ eqv? ,x ,y))
104 (define (@equal? x y) `(@@ equal? ,x ,y))
108 (define (@number? x) `(@@ number? ,x))
109 (define (@complex? x) `(@@ complex? ,x))
110 (define (@real? x) `(@@ real? ,x))
111 (define (@rational? x) `(@@ rational? ,x))
112 (define (@integer? x) `(@@ integer? ,x))
114 (define (@exact? x) `(@@ exact? ,x))
115 (define (@inexact? x) `(@@ inexact? ,x))
117 (define (@= x y) `(@@ ee? ,x ,y))
118 (define (@< x y) `(@@ lt? ,x ,y))
119 (define (@> x y) `(@@ gt? ,x ,y))
120 (define (@<= x y) `(@@ le? ,x ,y))
121 (define (@>= x y) `(@@ ge? ,x ,y))
123 (define (@zero? x) `(@= ,x 0))
124 (define (@positive? x) `(@> ,x 0))
125 (define (@negative? x) `(@< ,x 0))
126 (define (@odd? x) `(@= (@modulo ,x 2) 1))
127 (define (@even? x) `(@= (@modulo ,x 2) 0))
129 (define (@max . args) `(@@ max ,@args))
130 (define (@min . args) `(@@ min ,@args))
136 ((x y) `(@@ add ,x ,y))
137 ((x y . rest) `(@@ add ,x (@+ ,y ,@rest)))))
143 ((x y) `(@@ mul ,x ,y))
144 ((x y . rest) `(@@ mul ,x (@* ,y ,@rest)))))
149 ((x y) `(@@ sub ,x ,y))
150 ((x y . rest) `(@@ sub ,x (@+ ,y ,@rest)))))
155 ((x y) `(@@ div ,x ,y))
156 ((x y . rest) `(@@ div ,x (@* ,y ,@rest)))))
161 (define (@remainder x y) `(@@ remainder ,x ,y))
202 ;;; 6.3 Other data types
206 (define (@not x) `(@@ not ,x))
207 (define (@boolean? x) `(@@ boolean? ,x))
209 ;;;; 6.3.2 Pairs and lists
211 (define (@pair? x) `(@@ pair? ,x))
212 (define (@cons x y) `(@@ cons ,x ,y))
214 (define (@car x) `(@@ car ,x))
215 (define (@cdr x) `(@@ cdr ,x))
216 (define (@set-car! x) `(@@ set-car! ,x))
217 (define (@set-cdr! x) `(@@ set-cdr! ,x))
219 (define (@caar x) `(@@ car (@@ car ,x)))
220 (define (@cadr x) `(@@ car (@@ cdr ,x)))
221 (define (@cdar x) `(@@ cdr (@@ car ,x)))
222 (define (@cddr x) `(@@ cdr (@@ cdr ,x)))
223 (define (@caaar x) `(@@ car (@@ car (@@ car ,x))))
224 (define (@caadr x) `(@@ car (@@ car (@@ cdr ,x))))
225 (define (@cadar x) `(@@ car (@@ cdr (@@ car ,x))))
226 (define (@caddr x) `(@@ car (@@ cdr (@@ cdr ,x))))
227 (define (@cdaar x) `(@@ cdr (@@ car (@@ car ,x))))
228 (define (@cdadr x) `(@@ cdr (@@ car (@@ cdr ,x))))
229 (define (@cddar x) `(@@ cdr (@@ cdr (@@ car ,x))))
230 (define (@cdddr x) `(@@ cdr (@@ cdr (@@ cdr ,x))))
231 (define (@caaaar x) `(@@ car (@@ car (@@ car (@@ car ,x)))))
232 (define (@caaadr x) `(@@ car (@@ car (@@ car (@@ cdr ,x)))))
233 (define (@caadar x) `(@@ car (@@ car (@@ cdr (@@ car ,x)))))
234 (define (@caaddr x) `(@@ car (@@ car (@@ cdr (@@ cdr ,x)))))
235 (define (@cadaar x) `(@@ car (@@ cdr (@@ car (@@ car ,x)))))
236 (define (@cadadr x) `(@@ car (@@ cdr (@@ car (@@ cdr ,x)))))
237 (define (@caddar x) `(@@ car (@@ cdr (@@ cdr (@@ car ,x)))))
238 (define (@cadddr x) `(@@ car (@@ cdr (@@ cdr (@@ cdr ,x)))))
239 (define (@cdaaar x) `(@@ cdr (@@ car (@@ car (@@ car ,x)))))
240 (define (@cdaadr x) `(@@ cdr (@@ car (@@ car (@@ cdr ,x)))))
241 (define (@cdadar x) `(@@ cdr (@@ car (@@ cdr (@@ car ,x)))))
242 (define (@cdaddr x) `(@@ cdr (@@ car (@@ cdr (@@ cdr ,x)))))
243 (define (@cddaar x) `(@@ cdr (@@ cdr (@@ car (@@ car ,x)))))
244 (define (@cddadr x) `(@@ cdr (@@ cdr (@@ car (@@ cdr ,x)))))
245 (define (@cdddar x) `(@@ cdr (@@ cdr (@@ cdr (@@ car ,x)))))
246 (define (@cddddr x) `(@@ cdr (@@ cdr (@@ cdr (@@ cdr ,x)))))
248 (define (@null? x) `(@@ null? ,x))
249 (define (@list? x) `(@@ list? ,x))
250 (define (@list . args) `(@@ list ,@args))
272 ;;;; 6.3.4 Characters
334 ;;;; 6.4 Control features
336 (define (@procedure? x) `(@@ procedure? x))
338 ;; (define (@apply proc . args) ...)
340 (define (@map f ls . more)
343 (@let map1 ((ls ,ls))
346 (@cons (f (car ls)) (map1 (cdr ls))))))
348 (@let map-more ((ls ,ls) (more ,more))
351 (@cons (@apply f (car ls) (map car more))
352 (map-more (cdr ls) (map cdr more))))))))
357 (do ((ls ls (cdr ls)) (more more (map cdr more)))
359 (apply f (car ls) (map car more))))
361 `(@apply (@~ system:il:base:for-each) args))))
363 (define (@force promise) `(@@ force promise))
365 (define (@call-with-current-continuation proc) `(@@ call/cc proc))
367 (define @call/cc @call-with-current-continuation)
376 ;;; scheme-report-environment
378 ;;; interaction-environment
380 ;;; 6.6 Input and output
384 ;;; call-with-input-file
385 ;;; call-with-output-file
389 ;;; current-input-port
390 ;;; current-output-port
392 ;;; with-input-from-file
393 ;;; with-output-to-file
398 ;;; close-output-port
415 ;;;; 6.6.4 System interface
423 ;;; Non-R5RS Procedures
429 ((x y) `(@cons ,x ,y))
430 ((x y . rest) `(@cons ,x (@cons* ,y ,@rest)))))
432 (define (@error . args) `(@@ display ,@args))
434 (define (@current-module)
435 `((@ System::Base::module::current-module)))