* lib.scm: Move module the system directives `export',
[bpt/guile.git] / srfi / srfi-4.scm
CommitLineData
71ca65d9
MG
1;;;; srfi-4.scm --- Homogeneous numeric vector datatypes.
2;;;;
3;;;; Copyright (C) 2001 Free Software Foundation, Inc.
4;;;;
5;;;; This program is free software; you can redistribute it and/or
6;;;; modify it under the terms of the GNU General Public License as
7;;;; published by the Free Software Foundation; either version 2, or
8;;;; (at your option) 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 GNU
13;;;; General Public License for more details.
14;;;;
15;;;; You should have received a copy of the GNU General Public License
16;;;; along with this software; 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;;;; As a special exception, the Free Software Foundation gives permission
21;;;; for additional uses of the text contained in its release of GUILE.
22;;;;
23;;;; The exception is that, if you link the GUILE library with other files
24;;;; to produce an executable, this does not by itself cause the
25;;;; resulting executable to be covered by the GNU General Public License.
26;;;; Your use of that executable is in no way restricted on account of
27;;;; linking the GUILE library code into it.
28;;;;
29;;;; This exception does not however invalidate any other reasons why
30;;;; the executable file might be covered by the GNU General Public License.
31;;;;
32;;;; This exception applies only to the code released by the
33;;;; Free Software Foundation under the name GUILE. If you copy
34;;;; code from other Free Software Foundation releases into a copy of
35;;;; GUILE, as the General Public License permits, the exception does
36;;;; not apply to the code that you add in this way. To avoid misleading
37;;;; anyone as to the status of such modified files, you must delete
38;;;; this exception notice from them.
39;;;;
40;;;; If you write modifications of your own for GUILE, it is your choice
41;;;; whether to permit this exception to apply to your modifications.
42;;;; If you do not wish that, delete this exception notice.
43
44;;; Commentary:
45
46;;; This module implements homogeneous numeric vectors as defined in SRFI-4.
47
48;;; Code:
49
50;;; Author: Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
51
1a179b03
MD
52(define-module (srfi srfi-4)
53 :export (
71ca65d9
MG
54;;; Unsigned 8-bit vectors.
55 u8vector? make-u8vector u8vector u8vector-length u8vector-ref
56 u8vector-set! u8vector->list list->u8vector
57
58;;; Signed 8-bit vectors.
59 s8vector? make-s8vector s8vector s8vector-length s8vector-ref
60 s8vector-set! s8vector->list list->s8vector
61
62;;; Unsigned 16-bit vectors.
63 u16vector? make-u16vector u16vector u16vector-length u16vector-ref
64 u16vector-set! u16vector->list list->u16vector
65
66;;; Signed 16-bit vectors.
67 s16vector? make-s16vector s16vector s16vector-length s16vector-ref
68 s16vector-set! s16vector->list list->s16vector
69
70;;; Unsigned 32-bit vectors.
71 u32vector? make-u32vector u32vector u32vector-length u32vector-ref
72 u32vector-set! u32vector->list list->u32vector
73
74;;; Signed 32-bit vectors.
75 s32vector? make-s32vector s32vector s32vector-length s32vector-ref
76 s32vector-set! s32vector->list list->s32vector
77
78;;; Unsigned 64-bit vectors.
79 u64vector? make-u64vector u64vector u64vector-length u64vector-ref
80 u64vector-set! u64vector->list list->u64vector
81
82;;; Signed 64-bit vectors.
83 s64vector? make-s64vector s64vector s64vector-length s64vector-ref
84 s64vector-set! s64vector->list list->s64vector
85
86;;; 32-bit floating point vectors.
87 f32vector? make-f32vector f32vector f32vector-length f32vector-ref
88 f32vector-set! f32vector->list list->f32vector
89
90;;; 64-bit floating point vectors.
91 f64vector? make-f64vector f64vector f64vector-length f64vector-ref
92 f64vector-set! f64vector->list list->f64vector
1a179b03 93 ))
71ca65d9
MG
94
95
96;; Make 'srfi-4 available as a feature identifiere to `cond-expand'.
97;;
98(cond-expand-provide (current-module) '(srfi-4))
99
100
101;; Load the compiled primitives from the shared library.
102;;
103(load-extension "libguile-srfi-srfi-4" "scm_init_srfi_4")
104
105
106;; Reader extension for #f32() and #f64() vectors.
107;;
108(define (hash-f char port)
109 (if (or (char=? (peek-char port) #\3)
110 (char=? (peek-char port) #\6))
111 (let* ((obj (read port)))
112 (if (number? obj)
113 (cond ((= obj 32)
114 (let ((l (read port)))
115 (if (list? l)
116 (list->f32vector l)
117 (error "syntax error in #f32() vector literal"))))
118 ((= obj 64)
119 (let ((l (read port)))
120 (if (list? l)
121 (list->f64vector l)
122 (error "syntax error in #f64() vector literal"))))
123 (else
124 (error "syntax error in #f...() vector literal")))
125 (error "syntax error in #f...() vector literal")))
126 #f))
127
128
129;; Reader extension for #u8(), #u16(), #u32() and #u64() vectors.
130;;
131(define (hash-u char port)
132 (if (or (char=? (peek-char port) #\8)
133 (char=? (peek-char port) #\1)
134 (char=? (peek-char port) #\3)
135 (char=? (peek-char port) #\6))
136 (let ((obj (read port)))
137 (cond ((= obj 8)
138 (let ((l (read port)))
139 (if (list? l)
140 (list->u8vector l)
141 (error "syntax error in #u8() vector literal"))))
142 ((= obj 16)
143 (let ((l (read port)))
144 (if (list? l)
145 (list->u16vector l)
146 (error "syntax error in #u16() vector literal"))))
147 ((= obj 32)
148 (let ((l (read port)))
149 (if (list? l)
150 (list->u32vector l)
151 (error "syntax error in #u32() vector literal"))))
152 ((= obj 64)
153 (let ((l (read port)))
154 (if (list? l)
155 (list->u64vector l)
156 (error "syntax error in #u64() vector literal"))))
157 (else
158 (error "syntax error in #u...() vector literal"))))
159 (error "syntax error in #u...() vector literal")))
160
161
162;; Reader extension for #s8(), #s16(), #s32() and #s64() vectors.
163;;
164(define (hash-s char port)
165 (if (or (char=? (peek-char port) #\8)
166 (char=? (peek-char port) #\1)
167 (char=? (peek-char port) #\3)
168 (char=? (peek-char port) #\6))
169 (let ((obj (read port)))
170 (cond ((= obj 8)
171 (let ((l (read port)))
172 (if (list? l)
173 (list->s8vector l)
174 (error "syntax error in #s8() vector literal"))))
175 ((= obj 16)
176 (let ((l (read port)))
177 (if (list? l)
178 (list->s16vector l)
179 (error "syntax error in #s16() vector literal"))))
180 ((= obj 32)
181 (let ((l (read port)))
182 (if (list? l)
183 (list->s32vector l)
184 (error "syntax error in #s32() vector literal"))))
185 ((= obj 64)
186 (let ((l (read port)))
187 (if (list? l)
188 (list->s64vector l)
189 (error "syntax error in #s64() vector literal"))))
190 (else
191 (error "syntax error in #s...() vector literal"))))
192 (error "syntax error in #s...() vector literal")))
193
194
195;; Install the hash extensions.
196;;
197(read-hash-extend #\f hash-f)
198(read-hash-extend #\u hash-u)
199(read-hash-extend #\s hash-s)