srfi-0.stk 8.69 KB
Newer Older
eg's avatar
eg committed
1 2 3
;;;;
;;;; srfi-0.stk 				-- SRFI-0 aka cond-expand
;;;; 
4
;;;; Copyright  1999-2007 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
eg's avatar
eg committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
;;;; 
;;;; 
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
;;;; 
;;;; 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, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
;;;; USA.
;;;; 
;;;;           Author: Erick Gallesio [eg@unice.fr]
;;;;    Creation date: 30-Aug-1999 16:26 (eg)
Erick Gallesio's avatar
Erick Gallesio committed
24
;;;; Last file update: 13-Dec-2007 12:26 (eg)
eg's avatar
eg committed
25 26
;;;;

27
(define-module SRFI-0
28
  (import SCHEME)
29 30
  (export srfi0-register-feature!
	  srfi-0-feature-implementation-file))
eg's avatar
eg committed
31

32 33
(select-module SRFI-0)
  
eg's avatar
eg committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
(define *all-features*
  '(stklos				; Of course
    STklos				; In case of cases-sensitive reader
    srfi-0 				; COND-EXPAND
    ((srfi-1 lists)	"srfi-1")	; List primitives
    ((srfi-2 and-let*)	"srfi-2")	; AND-LET*
    ;; srfi-3				; ....... withdrawn
    ((srfi-4 hvectors)	"srfi-4")	; Homogeneous vectors
    ;; srfi-5				; let with signature & rest args
    srfi-6				; String ports
    ((srfi-7 program)	"srfi-7")	; PROGRAM
    srfi-8				; RECEIVE
    ((srfi-9 records)	"srfi-9")	; Records
    srfi-10				; Sharp-Comma External Form
    (srfi-11		 "srfi-11")	; LET-VALUES and LET*-VALUES
    ;; srfi-12				; ....... withdrawn
    ((srfi-13 strings)	"srfi-13")	; String Library
    ((srfi-14 charsets)	"srfi-14")	; Character-Set Library
    ;; srfi-15				; ....... withdrawn
    ((srfi-16 case-lambda))		; CASE-LAMBDA
    srfi-17				; Generalized set!
Erick Gallesio's avatar
Erick Gallesio committed
55
    srfi-18				; Multithreading support
eg's avatar
eg committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    ;; (srfi-19 "srfi-19")		; Time Data Types and Procedures
    ;; srfi-20				; ....... withdrawn
    ;; srfi-21				; Real-time multithreading support
    srfi-22				; Unix Scheme Scripts
    ((srfi-23 error))			; Error reporting mechanism
    ;; srfi-24				; ....... withdrawn
    ;; srfi-25				; Multi-dimensional Arrays
    (srfi-26 "srfi-26")			; Specialize Param without Currying
    ((srfi-27 random))			; Sources of Random Bits
    srfi-28				; Basic Format Strings
    ;; srfi-29				; Localization
    srfi-30				; Nested Multi-line Comments
    srfi-31				; REC form
    ;; srfi-32				; ....... withdrawn
    ;; srfi-33				; ....... withdrawn
    srfi-34				; Exception Handling for Programs
    (srfi-35 "srfi-35")			; Conditions
    (srfi-36 "srfi-36")			; I/O Conditions
    (conditions "srfi-35" "srfi-36")
    ;; srfi-37				; Program Argument Processor
    srfi-38				; Ext. Rep. of shared structures
    ((srfi-39 parameters))		; Parameter Objects
    ;; srfi-40				; A Library of Streams
    ;; srfi-41				; ?????????? Disappeared
    ;; srfi-42				; Eager Comprehensions
    ;; srfi-43				; Vector library
    ;; srfi-44				; Collections
Erick Gallesio's avatar
Erick Gallesio committed
83
    (srfi-45 "srfi-45")			; primitives for lazy algorithms
Erick Gallesio's avatar
Erick Gallesio committed
84
    ;; srfi-46				; Basic Syntax-rules Extensions
eg's avatar
eg committed
85 86 87
    ;; srfi-47				; Array
    srfi-48				; Intermediate Format Strings
    ;; srfi-49				; Indentation-sensitive syntax (NEVER)
Erick Gallesio's avatar
Erick Gallesio committed
88
    ;; srfi-50				; ....... withdrawn
eg's avatar
eg committed
89 90 91 92 93
    ;; srfi-51				; Handling rest list
    ;; srfi-52				; ....... withdrawn
    ;; srfi-53				; ....... withdrawn
    ;; srfi-54				; Formatting
    srfi-55				; require-extension
Erick Gallesio's avatar
Erick Gallesio committed
94
    ;; srfi-56				; ....... withdrawn
eg's avatar
eg committed
95 96
    ;; srfi-57				; Records
    ;; srfi-58				; Array Notation
Erick Gallesio's avatar
Erick Gallesio committed
97
    (srfi-59 "srfi-59")				; Vicinity
eg's avatar
eg committed
98 99 100 101
    (srfi-60 "srfi-60")			; Integers as Bits
    ;; srfi-61				; A more general cond clause
    srfi-62				; S-expression comments
    ;; srfi-63				; Homogeneous and Heterogeneous Arrays
Erick Gallesio's avatar
Erick Gallesio committed
102 103
    ;; srfi-64			        ; A Scheme API for test suites
    ;; srfi-65				; ....... withdrawn
eg's avatar
eg committed
104 105
    (srfi-66 "srfi-66")			; Octet Vectors
    ;; srfi-67				; Compare Procedures
Erick Gallesio's avatar
Erick Gallesio committed
106
    ;; srfi-68				; ....... withdrawn
eg's avatar
eg committed
107 108 109 110
    ((srfi-69 hash-tables) "srfi-69")	; Basic Hash Tables
    srfi-70				; Numbers
    ;; srfi-71				; LET-syntax for multiple values
    ;; srfi-72				; Simple hygienic macros
Erick Gallesio's avatar
Erick Gallesio committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    ;; srfi-73				; ....... withdrawn
    ;; srfi-74				; Octet-Addressed Binary Blocks
    ;; srfi-75				; ....... withdrawn
    ;; srfi-76				; ....... withdrawn
    ;; srfi-77				; ....... withdrawn
    ;; srfi-78				; Lightweight testing
    ;; srfi-79
    ;; srfi-80
    ;; srfi-81
    ;; srfi-82
    ;; srfi-83				; ....... withdrawn
    ;; srfi-84
    ;; srfi-85				; ....... withdrawn
    ;; srfi-86
    ;; srfi-87				; => in case clauses
126 127
    srfi-88				; Keyword objects
    (srfi-89 "srfi-89")			; Optional Positional and Named Parameters
Erick Gallesio's avatar
Erick Gallesio committed
128 129 130 131 132 133
    ;; srfi-90
    ;; srfi-91
    ;; srfi-92
    ;; srfi-93				; ....... withdrawn
    ;; srfi-94
    ;; srfi-95
eg's avatar
eg committed
134 135
    ))

136 137 138 139 140
;; ----------------------------------------------------------------------
(define (srfi0-register-feature! feature)
  (set! *all-features* (cons feature *all-features*)))

;; ----------------------------------------------------------------------
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
(define (srfi-0-feature-implementation-file x)
  (let Loop ((l *all-features*))
    (cond
      ((null? l)			;; not found 
       #f)
      ((symbol? (car l))		; clause  srfi-x
       (if (eq? (car l) x)
	   #f
	   (Loop (cdr l))))
      ((symbol? (caar l))		; clause  (srfi-x ...)
       (if (eq? x (caar l))
           (cdar l)
           (Loop (cdr l))))
      (else				; clause ((srfi-x foo bar) ...)
       (if (memq x (caar l))
	   (cdar l)
           (Loop (cdr l)))))))

;; ----------------------------------------------------------------------
eg's avatar
eg committed
160
(define (%has-feature? x)
161 162 163
  (define (req files)
    (for-each require files) #t)

eg's avatar
eg committed
164 165 166 167 168 169 170 171
  (let Loop ((l *all-features*))
    (cond
      ((null? l)			;; not found 
       #f)
      ((symbol? (car l))		; clause  srfi-x
       (or (eq? (car l) x) (Loop (cdr l))))
      ((symbol? (caar l))		; clause  (srfi-x ...)
       (if (eq? x (caar l))
172 173
           (req (cdar l))
           (Loop (cdr l))))
eg's avatar
eg committed
174 175
      (else				; clause ((srfi-x foo bar) ...)
       (if (memq x (caar l))
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
           (req (cdar l))
           (Loop (cdr l))))))) 
;; ----------------------------------------------------------------------
(define (%srfi-0-expand  clauses)
  (cond
    ((null? clauses)
     (error 'cond-expand "no clause match"))
    ((not (pair? (car clauses)))
     (error 'cond-expand "bad clause ~S" (car clauses)))
    (else
     (let ((c (car clauses)))
       (cond
         ((eq? (car c) 'else)
          ;; Clause is (ELSE ...)
          `(begin ,@(cdr c)))
         
         ((pair? (car c))
          ;; Clause is ((AND..)..) ((OR..)..) ((NOT...))
          (let ((test (car c)))
            (cond
              ;; ---------- AND
              ((eq? (car test) 'and)
               (if (null? (cdr test))
                  `(begin ,@(cdr c))		   
                  `(cond-expand
                    (,(cadr test) (cond-expand
                                   ((and ,@(cddr test)) ,@(cdr c))
                                   ,@(cdr clauses)))
                    ,@(cdr clauses))))
              
              ;; ---------- OR
              ((eq? (car test) 'or)
               (if (null? (cdr test))
                   `(cond-expand ,@(cdr clauses))
                   `(cond-expand
                     (,(cadr test)       ,@(cdr c))
                     ((or ,@(cddr test)) ,@(cdr c))
                     ,@(cdr clauses))))
               
              ;; ---------- NOT 
              ((eq? (car test) 'not)
               (if (or (null? (cdr test))
                       (not (null? (cddr test))))
                   (error 'cond-expand "invalid 'not' clause")
                   `(cond-expand
                     (,(cadr test) (cond-expand ,@(cdr clauses)))
                     (else ,@(cdr c)))))
              ;; ----------
              (else 'cond-expand "bad clause ~S" c))))
         (else
          ;; Clause is (FEATURE ...)
          (if (%has-feature? (caar clauses))
              `(begin ,@(cdar clauses))
              `(cond-expand ,@(cdr clauses)))))))))
eg's avatar
eg committed
230 231


232 233 234 235 236
;; ----------------------------------------------------------------------
;;	cond-expand ...
;; ----------------------------------------------------------------------
(select-module STklos)
(import SRFI-0)
eg's avatar
eg committed
237 238

(define-macro (cond-expand . clauses)
239 240
  (let ((expander (in-module SRFI-0 %srfi-0-expand)))
    (expander clauses)))
eg's avatar
eg committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

(provide "srfi-0")


#|
Examples 

     (cond-expand
	((and srfi-1 srfi-10)
	 (write 1))
	((or srfi-1 srfi-10)
	 (write 2))
	(else))


      (cond-expand
	(command-line
	 (define (program-name) (car (argv)))))
|#