-
Notifications
You must be signed in to change notification settings - Fork 7
/
compatibility.lisp
341 lines (272 loc) · 11.9 KB
/
compatibility.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
;;; -*- Syntax: Common-Lisp; Base: 10 -*-
;;;
;;; Copyright (c) 2024 Gary Palter
;;;
;;; Licensed under the MIT License;
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; https://opensource.org/license/mit
(in-package #:forth)
;;; Resolve minor differences between supported Lisp implementations
;;; SBCL's DEFCONSTANT will complain when the constant is a bytespec (i.e., (BYTE ...)) as it represents bytespecs
;;; as a CONS which are not EQL when the load-time value tries to replace the compile-time value. (SBCL is strictly
;;; adhering to the ANSI CL specification in this case.)
#+SBCL
(defmacro define-constant (name value &optional docstring)
(let ((value-var (gensym)))
`(defconstant ,name
(let ((,value-var ,value))
(if (and (boundp ',name) (equal (symbol-value ',name) ,value-var))
(symbol-value ',name)
,value-var))
,@(when docstring (list docstring)))))
#-SBCL
(defmacro define-constant (name value &optional docstring)
`(defconstant ,name ,value ,@(when docstring (list docstring))))
;;; ----------------------------------------------------------------------------
;;; NAMED-LAMBDA
#+CCL
(defmacro named-lambda (name arglist &body body)
`(ccl:nfunction ,name (lambda ,arglist ,@body)))
;;; SBCL provides NAMED-LAMBDA natively
#+LispWorks
(defmacro named-lambda (name arglist &body body)
`(lambda ,arglist
(declare (hcl:lambda-name ,name))
,@body))
#+ECL
(defmacro named-lambda (name arglist &body body)
`(ext:lambda-block ,name ,arglist ,@body))
;;; ----------------------------------------------------------------------------
;;; WHITESPACEP
;;; CCL provides WHITESPACEP natively
#-CCL
(declaim (inline whitespacep))
#+SBCL
(defun whitespacep (ch) (sb-unicode:whitespace-p ch))
#+LispWorks
(defun whitespacep (ch) (lw:whitespace-char-p ch))
#+ECL
(defun whitespacep (ch) (or (eql ch #\Space) (eql ch #\Tab)))
;;; ----------------------------------------------------------------------------
;;; SET-STREAM-LENGTH
#+CCL
(defun set-stream-length (stream new-length)
(ccl::stream-length stream new-length))
#+SBCL
(defun set-stream-length (stream new-length)
(if (sb-sys:fd-stream-p stream)
(let ((fd (sb-sys:fd-stream-fd stream)))
(sb-posix:ftruncate fd new-length))
(error 'file-error :pathname (pathname stream))))
#+(or LispWorks ECL)
(defun set-stream-length (stream new-length)
(declare (ignore new-length))
(error 'file-error :pathname (pathname stream)))
;;; ----------------------------------------------------------------------------
;;; MAKE-PIPED-STREAMS
#+CCL
(defclass unbuffered-fd-character-output-stream (ccl::fd-character-output-stream)
())
#+CCL
(defmethod ccl:stream-write-char :after ((st unbuffered-fd-character-output-stream) char)
(declare (ignore char))
(force-output st))
#+CCL
(defmethod ccl:stream-write-string :after ((st unbuffered-fd-character-output-stream) string &optional start end)
(declare (ignore string start end))
(force-output st))
#+CCL
(defmethod ccl:stream-terpri :after ((st unbuffered-fd-character-output-stream))
(force-output st))
#+CCL
;; Used by WRITE-SEQUENCE
(defmethod ccl:stream-write-vector :after ((st unbuffered-fd-character-output-stream) vector start end)
(declare (ignore vector start end))
(force-output st))
#+CCL
(defun make-piped-streams (&key name (element-type 'character) (external-format :default))
(declare (ignore name))
(let* ((char-p (or (eq element-type 'character) (subtypep element-type 'character)))
(real-external-format (when char-p
(ccl::normalize-external-format :pipe external-format)))
(encoding (when char-p (ccl:external-format-character-encoding real-external-format)))
(line-termination (when char-p (ccl:external-format-line-termination real-external-format))))
(multiple-value-bind (read-fd write-fd) (ccl::pipe)
(let ((is (ccl::make-fd-stream read-fd
:direction :input
:interactive t
:element-type element-type
:sharing :lock
:basic t
:encoding encoding
:line-termination line-termination
:auto-close t))
(os (ccl::make-fd-stream write-fd
:direction :output
:interactive t
:element-type element-type
:sharing :lock
:basic nil
:encoding encoding
:line-termination line-termination
:auto-close t)))
(change-class os 'unbuffered-fd-character-output-stream)
(values is os)))))
#+SBCL
(defun make-piped-streams (&key name (element-type 'character) (external-format :default))
(declare (ignore name))
(multiple-value-bind (read-fd write-fd) (sb-unix:unix-pipe)
(let ((is (sb-impl::make-fd-stream read-fd
:input t
:element-type element-type
:buffering :line
:external-format external-format
:auto-close t))
(os (sb-impl::make-fd-stream write-fd
:output t
:element-type element-type
:buffering :line
:external-format external-format
:auto-close t)))
(values is os))))
#+LispWorks
(defun make-piped-streams (&key name (element-type 'character) (external-format :default))
(assert (eq element-type 'character) () "~S only supports ~S ~S, not ~S"
'make-piped-streams :element-type 'character element-type)
(assert (eq external-format :default) () "~S only supports ~S ~S, not ~S"
'make-piped-streams :external-format :default external-format)
(let ((buffer (make-in-memory-buffer +default-in-memory-buffer-size+)))
(values (make-in-memory-character-input-stream buffer name)
(make-in-memory-character-output-stream buffer name))))
#+ECL
(defun make-piped-streams (&key name (element-type 'character) (external-format :default))
(declare (ignore name))
(assert (eq element-type 'character) () "~S only supports ~S ~S, not ~S"
'make-piped-streams :element-type 'character element-type)
(assert (eq external-format :default) () "~S only supports ~S ~S, not ~S"
'make-piped-streams :external-format :default external-format)
(let ((pipe (ext:make-pipe)))
(values (two-way-steram-input-stream pipe)
(two-way-steram-output-stream pipe))))
;;; ----------------------------------------------------------------------------
;;; PROCESS-RUN-FUNCTION, PROCESS-WAIT
;;; CCL provides PROCESS-RUN-FUNCTION and PROCESS-WAIT natively
#+SBCL
(defun process-run-function (name-or-keywords function &rest args)
(let ((name (if (listp name-or-keywords)
(destructuring-bind (&key (name "Anonymous") &allow-other-keys)
name-or-keywords
name)
name-or-keywords)))
(sb-thread:make-thread function :name name :arguments args)))
#+SBCL
(defun process-wait (whostate function &rest args)
(declare (dynamic-extent args) (ignore whostate))
(loop until (apply function args)
do (sleep 0.001)))
#+LispWorks
(defun process-run-function (name-or-keywords function &rest args)
(multiple-value-bind (name priority)
(if (listp name-or-keywords)
(destructuring-bind (&key (name "Anonymous") (priority mp:*default-process-priority*) &allow-other-keys)
name-or-keywords
(values name priority))
(values name-or-keywords mp:*default-process-priority*))
(apply #'mp:process-run-function name `(:priority ,priority) function args)))
;;; LispWorks provides PROCESS-WAIT natively
#+ECL
(defun process-run-function (name-or-keywords function &rest args)
(let ((name (if (listp name-or-keywords)
(destructuring-bind (&key (name "Anonymous") &allow-other-keys)
name-or-keywords
name)
name-or-keywords)))
(apply #'mp:process-run-function name function args)))
#+ECL
(defun process-wait (whostate function &rest args)
(declare (dynamic-extent args) (ignore whostate))
(loop until (apply function args)
do (sleep 0.001)))
;;; ----------------------------------------------------------------------------
;;; ADD/REMOVE-AUTO-FLUSH-STREAM
;;; CCL provides ADD/REMOVE-AUTO-FLUSH-STREAM natively
#-CCL
(defun add-auto-flush-stream (stream)
(declare (ignore stream))
nil)
#-CCL
(defun remove-auto-flush-stream (stream)
(declare (ignore stream))
nil)
;;; ----------------------------------------------------------------------------
;;; ADDRESS-POINTER, POINTER-ADDRESS, NULL-POINTER-P, %ADDRESS-OF
(declaim (inline address-pointer pointer-address null-pointer-p null-pointer %address-of))
(defun address-pointer (address)
(cffi:make-pointer address))
(defun pointer-address (pointer)
(cffi:pointer-address pointer))
(defun null-pointer-p (pointer)
(cffi:null-pointer-p pointer))
(defun null-pointer ()
(cffi:null-pointer))
#+CCL
(defun %address-of (object)
;; CCL's %ADDRESS-OF doesn't strip the tag from the address
(logand (ccl:%address-of object) (lognot 7)))
#+SBCL
(defun %address-of (object)
;; The OBJECT should be a (SIMPLE-ARRAY (UNSIGNED-BYTE 8) (*)) as that's how CL-Forth represents memory
(sb-sys:sap-int (sb-sys:vector-sap object)))
#+LispWorks
(defun %address-of (object)
;; The OBJECT should be a (SIMPLE-ARRAY (UNSIGNED-BYTE 8) (*)) as that's how CL-Forth represents memory
;; LispWorks returns the address of the object's header word, skip to the actual data
(+ (system:object-address object) 8))
#+ECL
(defun %address-of (object)
;; The OBJECT should be a (SIMPLE-ARRAY (UNSIGNED-BYTE 8) (*)) as that's how CL-Forth represents memory
;; ECL array objects contain a pointer to the actual data and SI:MAKE-FOREIGN-DATA-FROM-ARRAY extracts that pointer
(pointer-address (si:make-foreign-data-from-array object)))
;;; ----------------------------------------------------------------------------
;;; OBJECT-SIZE
(declaim (inline object-size))
#+CCL
(defun object-size (object)
(ccl:object-direct-size object))
#+SBCL
(defun object-size (object)
(sb-ext:primitive-object-size object))
#+LispWorks
(defun object-size (object)
(hcl:find-object-size object))
#+ECL
(defun object-size (object)
(declare (ignore object))
0)
;;; ----------------------------------------------------------------------------
;;; PARSE-BODY
(defun parse-body (body &key documentation whole) ;; from alexandria
"Parses BODY into (values remaining-forms declarations doc-string).
Documentation strings are recognized only if DOCUMENTATION is true.
Syntax errors in body are signalled and WHOLE is used in the signal
arguments when given."
(let ((doc nil)
(decls nil)
(current nil))
(tagbody
:declarations
(setf current (car body))
(when (and documentation (stringp current) (cdr body))
(cond ((eq documentation :multiple)
(push (pop body) doc))
(doc
(error "Too many documentation strings in ~S." (or whole body)))
(t
(setf doc (pop body))))
(go :declarations))
(when (and (listp current) (eql (first current) 'declare))
(push (pop body) decls)
(go :declarations)))
(values body (nreverse decls) (if (eq documentation :multiple) (nreverse doc) doc))))