-
Notifications
You must be signed in to change notification settings - Fork 7
/
compatibility.lisp
253 lines (195 loc) · 8.26 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
;;; -*- 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)
;;; CCL, SBCL, and LispWorks have some minor differences which are resolved here
;;; 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))
;;; 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))
;;; SET-STREAM-LENGTH
#+CCL
(defun set-stream-length (stream new-length)
(ccl::stream-length stream new-length))
#+(or SBCL LispWorks)
(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))))
;;; 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
;;; ADD/REMOVE-AUTO-FLUSH-STREAM
;;; CCL provides ADD/REMOVE-AUTO-FLUSH-STREAM natively
#+(or SBCL LispWorks)
(defun add-auto-flush-stream (stream)
(declare (ignore stream))
nil)
#+(or SBCL LispWorks)
(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)
(cerror "Continue anyway" "NYI: ~S" '%address-of)
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))