forked from Shen-Language/shen-sbcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.lsp
266 lines (205 loc) · 8.13 KB
/
primitives.lsp
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
"Copyright (c) 2010-2015, Mark Tarver
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of Mark Tarver may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Mark Tarver ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Mark Tarver BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
(SETQ *user-syntax-in* NIL)
(DEFMACRO if (X Y Z)
`(LET ((*C* ,X))
(COND ((EQ *C* 'true)
,Y)
((EQ *C* 'false)
,Z)
(T
(ERROR "~S is not a boolean~%" *C*)))))
(DEFMACRO and (X Y) `(if ,X (if ,Y 'true 'false) 'false))
(DEFMACRO or (X Y) `(if ,X 'true (if ,Y 'true 'false)))
(DEFUN set (X Y) (SET X Y))
(DEFUN value (X) (SYMBOL-VALUE X))
(DEFUN simple-error (String) (ERROR "~A" String))
(DEFMACRO trap-error (X F)
`(HANDLER-CASE ,X (ERROR (Condition) (FUNCALL ,F Condition))))
(DEFUN error-to-string (E)
(IF (TYPEP E 'CONDITION)
(FORMAT NIL "~A" E)
(ERROR "~S is not an exception~%" E)))
(DEFUN cons (X Y) (CONS X Y))
(DEFUN hd (X) (CAR X))
(DEFUN tl (X) (CDR X))
(DEFUN cons? (X) (IF (CONSP X) 'true 'false))
(DEFUN intern (String) (INTERN (shen.process-intern String)))
(DEFUN shen.process-intern (S)
(COND ((STRING-EQUAL S "")
S)
((STRING-EQUAL (pos S 0) "#")
(cn "_hash1957" (shen.process-intern (tlstr S))))
((STRING-EQUAL (pos S 0) "'")
(cn "_quote1957" (shen.process-intern (tlstr S))))
((STRING-EQUAL (pos S 0) "`")
(cn "_backquote1957" (shen.process-intern (tlstr S))))
((STRING-EQUAL (pos S 0) "|")
(cn "bar!1957" (shen.process-intern (tlstr S))))
(T
(cn (pos S 0) (shen.process-intern (tlstr S))))))
(DEFUN eval-kl (X)
(LET ((E (EVAL (shen.kl-to-lisp NIL X))))
(IF (AND (CONSP X) (EQ (CAR X) 'defun))
(COMPILE E)
E)))
(DEFMACRO lambda (X Y) `(FUNCTION (LAMBDA (,X) ,Y)))
(DEFMACRO let (X Y Z) `(LET ((,X ,Y)) ,Z))
(DECLAIM (INLINE write-byte))
(DEFUN write-byte (Byte S) (WRITE-BYTE Byte S))
(DEFUN shen.equal? (X Y)
(IF (shen.ABSEQUAL X Y) 'true 'false))
(DEFUN shen.ABSEQUAL (X Y)
(COND ((AND (CONSP X) (CONSP Y) (shen.ABSEQUAL (CAR X) (CAR Y)))
(shen.ABSEQUAL (CDR X) (CDR Y)))
((AND (STRINGP X) (STRINGP Y))
(STRING= X Y))
((AND (NUMBERP X) (NUMBERP Y))
(= X Y))
((AND (ARRAYP X) (ARRAYP Y))
(CF-VECTORS X Y (LENGTH X) (LENGTH Y)))
(T
(EQUAL X Y))))
(DEFUN CF-VECTORS (X Y LX LY)
(AND (= LX LY)
(CF-VECTORS-HELP X Y 0 (1- LX))))
(DEFUN CF-VECTORS-HELP (X Y COUNT MAX)
(COND ((= COUNT MAX)
(shen.ABSEQUAL (AREF X MAX) (AREF Y MAX)))
((shen.ABSEQUAL (AREF X COUNT) (AREF Y COUNT))
(CF-VECTORS-HELP X Y (1+ COUNT) MAX))
(T
NIL)))
(DEFMACRO freeze (X) `(FUNCTION (LAMBDA () ,X)))
(DEFUN absvector (N) (MAKE-ARRAY (LIST N)))
(DEFUN absvector? (X) (IF (ARRAYP X) 'true 'false))
(DEFUN address-> (Vector N Value) (SETF (SVREF Vector N) Value) Vector)
(DEFUN <-address (Vector N) (SVREF Vector N))
(DECLAIM (INLINE read-byte))
(DEFUN read-byte (S) (READ-BYTE S NIL -1))
(DEFUN open (String Direction)
(LET ((Path (FORMAT NIL "~A~A" *home-directory* String)))
(shen.openh Path Direction)))
(DEFUN shen.openh (Path Direction)
(COND ((EQ Direction 'in)
(OPEN Path
:DIRECTION :INPUT
; :ELEMENT-TYPE 'UNSIGNED-BYTE
:ELEMENT-TYPE :DEFAULT))
((EQ Direction 'out)
(OPEN Path
:DIRECTION :OUTPUT
; :ELEMENT-TYPE 'UNSIGNED-BYTE
:ELEMENT-TYPE :DEFAULT
:IF-EXISTS :SUPERSEDE))
(T
(ERROR "invalid direction"))))
(DEFUN type (X MyType) (DECLARE (IGNORE MyType)) X)
(DEFUN close (Stream) (CLOSE Stream) NIL)
(DEFUN pos (X N) (COERCE (LIST (CHAR X N)) 'STRING))
(DEFUN tlstr (X) (SUBSEQ X 1))
(DEFUN cn (Str1 Str2) (CONCATENATE 'STRING Str1 Str2))
(DEFUN string? (S) (IF (STRINGP S) 'true 'false))
(DEFUN n->string (N) (FORMAT NIL "~C" (CODE-CHAR N)))
(DEFUN string->n (S) (CHAR-CODE (CAR (COERCE S 'LIST))))
(DEFUN str (X)
(COND ((NULL X)
(ERROR "[] is not an atom in Shen; str cannot convert it to a string.~%"))
((SYMBOLP X)
(shen.process-string (SYMBOL-NAME X)))
((NUMBERP X)
(shen.process-number (FORMAT NIL "~A" X)))
((STRINGP X)
(FORMAT NIL "~S" X))
((STREAMP X)
(FORMAT NIL "~A" X))
((FUNCTIONP X)
(FORMAT NIL "~A" X))
(T
(ERROR "~S is not an atom, stream or closure; str cannot convert it to a string.~%" X))))
(DEFUN shen.process-number (S)
(COND ((STRING-EQUAL S "")
"")
((STRING-EQUAL (pos S 0) "d")
(IF (STRING-EQUAL (pos S 1) "0") "" (cn "e" (tlstr S))))
(T
(cn (pos S 0) (shen.process-number (tlstr S))))))
(DEFUN shen.process-string (X)
(COND ((STRING-EQUAL X "")
X)
((AND (> (LENGTH X) 8)
(STRING-EQUAL X "_hash1957" :END1 9))
(cn "#" (shen.process-string (SUBSEQ X 9))))
((AND (> (LENGTH X) 9)
(STRING-EQUAL X "_quote1957" :END1 10))
(cn "'" (shen.process-string (SUBSEQ X 10))))
((AND (> (LENGTH X) 13)
(STRING-EQUAL X "_backquote1957" :END1 14))
(cn "`" (shen.process-string (SUBSEQ X 14))))
((AND (> (LENGTH X) 7)
(STRING-EQUAL X "bar!1957" :END1 8))
(cn "|" (shen.process-string (SUBSEQ X 8))))
(T
(cn (pos X 0) (shen.process-string (tlstr X))))))
(DEFUN get-time (Time)
(COND ((EQ Time 'run)
(* 1.0 (/ (GET-INTERNAL-RUN-TIME) INTERNAL-TIME-UNITS-PER-SECOND)))
((EQ Time 'unix)
(- (GET-UNIVERSAL-TIME) 2208988800))
(T
(ERROR "get-time does not understand the parameter ~A~%" Time))))
(DEFUN shen.double-precision (X)
(IF (INTEGERP X) X (COERCE X 'DOUBLE-FLOAT)))
(DECLAIM (INLINE shen.double-precision))
(DEFUN shen.multiply (X Y)
(IF (OR (ZEROP X) (ZEROP Y))
0
(* (shen.double-precision X) (shen.double-precision Y))))
(DEFUN shen.add (X Y)
(+ (shen.double-precision X) (shen.double-precision Y)))
(DEFUN shen.subtract (X Y)
(- (shen.double-precision X) (shen.double-precision Y)))
(DEFUN shen.divide (X Y)
(LET ((Div (/ (shen.double-precision X)
(shen.double-precision Y))))
(IF (INTEGERP Div)
Div
(* (COERCE 1.0 'DOUBLE-FLOAT) Div))))
(DEFUN shen.greater? (X Y) (IF (> X Y) 'true 'false))
(DEFUN shen.less? (X Y) (IF (< X Y) 'true 'false))
(DEFUN shen.greater-than-or-equal-to? (X Y) (IF (>= X Y) 'true 'false))
(DEFUN shen.less-than-or-equal-to? (X Y) (IF (<= X Y) 'true 'false))
(DEFUN number? (N) (IF (NUMBERP N) 'true 'false))
(SETF *STANDARD-TWO-WAY* (MAKE-TWO-WAY-STREAM *STANDARD-INPUT* *STANDARD-OUTPUT*))
(SETQ *stinput* *STANDARD-TWO-WAY*)
(SETQ *stoutput* *STANDARD-OUTPUT*)
(SETQ *sterror* *ERROR-OUTPUT*)
(DEFUN SHEN-TOPLEVEL ()
(LET ((Args (CDR SB-EXT:*POSIX-ARGV*)))
(IF (CONSP Args)
(PROGN
(MAPC 'load Args)
(exit 0))
(HANDLER-CASE (shen.shen)
(SB-SYS:INTERACTIVE-INTERRUPT ()
(FORMAT T "~%Quit.~%")
(exit 0))))))