1 \ ==== Type predilcates ==== {{{
3 :noname ( args -- boolobj )
4 nil objeq? boolean-type
5 ; 1 make-fa-primitive null?
7 :noname ( args -- boolobj )
8 boolean-type istype? -rot 2drop boolean-type
9 ; 1 make-fa-primitive boolean?
11 :noname ( args -- boolobj )
12 symbol-type istype? -rot 2drop boolean-type
13 ; 1 make-fa-primitive symbol?
15 :noname ( args -- boolobj )
16 fixnum-type istype? -rot 2drop boolean-type
17 ; 1 make-fa-primitive fixnum?
19 :noname ( args -- boolobj )
20 flonum-type istype? -rot 2drop boolean-type
21 ; 1 make-fa-primitive flonum?
23 :noname ( args -- boolobj )
24 ratnum-type istype? -rot 2drop boolean-type
25 ; 1 make-fa-primitive ratnum?
27 :noname ( args -- boolobj )
28 character-type istype? -rot 2drop boolean-type
29 ; 1 make-fa-primitive char?
31 :noname ( args -- boolobj )
32 string-type istype? -rot 2drop boolean-type
33 ; 1 make-fa-primitive string?
35 :noname ( args -- boolobj )
36 pair-type istype? -rot 2drop boolean-type
37 ; 1 make-fa-primitive pair?
39 :noname ( args -- boolobj )
40 primitive-proc-type istype? if
43 compound-proc-type istype?
46 -rot 2drop boolean-type
47 ; 1 make-fa-primitive procedure?
49 :noname ( args -- boolobj )
50 port-type istype? -rot 2drop boolean-type
51 ; 1 make-fa-primitive port?
55 \ ==== Type conversions ==== {{{
57 :noname ( args -- fixnum )
58 2dup 1 ensure-arg-count
59 car character-type ensure-arg-type
62 ; make-primitive char->integer
64 :noname ( args -- char )
65 2dup 1 ensure-arg-count
66 car fixnum-type ensure-arg-type
69 ; make-primitive integer->char
71 : fixnum-to-charlist ( fixnum -- charlist )
74 [char] 0 character-type nil cons
78 nil 2swap ( charlist fixnum )
83 2dup swap 10 mod swap ( charlist fixnum fixnummod )
84 2swap swap 10 / swap ( charlist fixnummod fixnumdiv )
85 -2rot ( fixnumdiv charlist fixnummod )
87 drop [char] 0 + character-type 2swap
88 cons ( fixnumdiv newcharlist )
96 :noname ( args -- string )
97 2dup 1 ensure-arg-count
98 car fixnum-type ensure-arg-type
102 fixnum-to-charlist ( fixnum charlist )
104 [char] - character-type 2swap cons
108 ; make-primitive number->string
110 :noname ( args -- symbol )
111 2dup 1 ensure-arg-count
112 car string-type ensure-arg-type
116 2dup car [char] - character-type objeq? if
120 2dup car [char] + character-type objeq? if
128 2dup nil objeq? false =
130 2dup car drop [char] 0 - -rot
131 2swap swap 10 * + -rot
140 ; make-primitive string->number
142 :noname ( args -- string )
143 2dup 1 ensure-arg-count
144 car symbol-type ensure-arg-type
149 ; make-primitive symbol->string
151 :noname ( args -- symbol )
152 2dup 1 ensure-arg-count
153 car string-type ensure-arg-type
158 ; make-primitive string->symbol
160 :noname ( charlist -- string )
161 2dup 1 ensure-arg-count
170 pair-type ensure-arg-type
174 ; make-primitive list->string
176 :noname ( string -- charlist )
177 2dup 1 ensure-arg-count
178 car string-type ensure-arg-type
189 ; make-primitive string->list
193 \ ==== Numeric types ==== {{{
197 :noname ( fixnum fixnum -- boolobj )
199 ; 2 make-fa-primitive fix:=
201 :noname ( fixnum fixnum -- boolobj )
202 drop swap drop < boolean-type
203 ; 2 make-fa-primitive fix:<
205 :noname ( fixnum fixnum -- boolobj )
206 drop swap drop > boolean-type
207 ; 2 make-fa-primitive fix:>
209 :noname ( fixnum fixnum -- boolobj )
210 drop swap drop <= boolean-type
211 ; 2 make-fa-primitive fix:<=
213 :noname ( fixnum fixnum -- boolobj )
214 drop swap drop >= boolean-type
215 ; 2 make-fa-primitive fix:>=
217 :noname ( fixnum -- boolobj )
219 ; 1 make-fa-primitive fix:zero?
221 :noname ( fixnum -- boolobj )
223 ; 1 make-fa-primitive fix:positive?
225 :noname ( fixnum -- boolobj )
227 ; 1 make-fa-primitive fix:negative?
229 :noname ( fixnum fixnum -- fixnum' )
230 drop swap drop + fixnum-type
231 ; 2 make-fa-primitive fix:+
233 :noname ( fixnum fixnum -- fixnum' )
234 drop swap drop - fixnum-type
235 ; 2 make-fa-primitive fix:-
237 :noname ( fixnum fixnum -- fixnum' )
238 drop swap drop * fixnum-type
239 ; 2 make-fa-primitive fix:*
241 :noname ( fixnum fixnum -- fixnum' )
242 drop swap drop / fixnum-type
243 ; 2 make-fa-primitive fix:quotient
245 :noname ( fixnum fixnum -- fixnum' )
246 drop swap drop mod fixnum-type
247 ; 2 make-fa-primitive fix:remainder
249 :noname ( fixnum -- fixnum+1 )
251 ; 1 make-fa-primitive fix:1+
253 :noname ( fixnum -- fixnum-1 )
255 ; 1 make-fa-primitive fix:-1+
257 :noname ( fixnum -- -fixnum )
259 ; 1 make-fa-primitive fix:neg
261 :noname ( fixnum -- -fixnum )
263 ; 1 make-fa-primitive fix:abs
265 :noname ( fixnum fixnum -- fixnum' )
266 drop swap drop gcd fixnum-type
267 ; 2 make-fa-primitive fix:gcd
271 :noname ( flonum flonum -- bool )
273 ; 2 make-fa-primitive flo:=
275 :noname ( flonum flonum -- bool )
276 drop swap drop f< boolean-type
277 ; 2 make-fa-primitive flo:<
279 :noname ( flonum flonum -- bool )
280 drop swap drop f> boolean-type
281 ; 2 make-fa-primitive flo:>
284 :noname ( flonum -- bool )
285 drop 0.0 = boolean-type
286 ; 1 make-fa-primitive flo:zero?
288 :noname ( flonum -- bool )
289 drop 0.0 f> boolean-type
290 ; 1 make-fa-primitive flo:positive?
292 :noname ( flonum -- bool )
293 drop 0.0 f< boolean-type
294 ; 1 make-fa-primitive flo:negative?
297 :noname ( flonum1 flonum2 -- flonum1+flonum2 )
298 drop swap drop f+ flonum-type
299 ; 2 make-fa-primitive flo:+
301 :noname ( flonum1 flonum2 -- flonum1-flonum2 )
302 drop swap drop f- flonum-type
303 ; 2 make-fa-primitive flo:-
305 :noname ( flonum1 flonum2 -- flonum1*flonum2 )
306 drop swap drop f* flonum-type
307 ; 2 make-fa-primitive flo:*
309 :noname ( flonum1 flonum2 -- flonum1/flonum2 )
310 drop swap drop f/ flonum-type
311 ; 2 make-fa-primitive flo:/
313 :noname ( flonum1 flonum2 -- flonum1/flonum2 )
314 drop swap drop f/ flonum-type
315 ; 2 make-fa-primitive flo:/
318 :noname ( flonum -- bool )
320 fnan? swap finf? or invert
321 ; 1 make-fa-primitive flo:finite?
324 :noname ( flonum -- flonum )
326 ; 1 make-fa-primitive flo:neg
328 :noname ( flonum -- flonum )
330 ; 1 make-fa-primitive flo:abs
332 :noname ( flonum -- flonum )
334 ; 1 make-fa-primitive flo:exp
336 :noname ( flonum -- flonum )
338 ; 1 make-fa-primitive flo:log
340 :noname ( flonum -- flonum )
342 ; 1 make-fa-primitive flo:sin
344 :noname ( flonum -- flonum )
346 ; 1 make-fa-primitive flo:cos
348 :noname ( flonum -- flonum )
350 ; 1 make-fa-primitive flo:tan
352 :noname ( flonum -- flonum )
354 ; 1 make-fa-primitive flo:asin
356 :noname ( flonum -- flonum )
358 ; 1 make-fa-primitive flo:acos
360 :noname ( flonum -- flonum )
362 ; 1 make-fa-primitive flo:atan
364 :noname ( flonum -- flonum )
366 ; 1 make-fa-primitive flo:sqrt
368 :noname ( flonum flonum -- flonum )
369 drop swap drop f^ flonum-type
370 ; 2 make-fa-primitive flo:expt
372 :noname ( flonum -- flonum )
374 ; 1 make-fa-primitive flo:floor
376 :noname ( flonum -- flonum )
378 ; 1 make-fa-primitive flo:ceiling
380 :noname ( flonum -- flonum )
382 ; 1 make-fa-primitive flo:truncate
384 :noname ( flonum -- flonum )
386 ; 1 make-fa-primitive flo:round
388 :noname ( flonum -- flonum )
389 drop floor f->i fixnum-type
390 ; 1 make-fa-primitive flo:floor->exact
392 :noname ( flonum -- flonum )
393 drop ceiling f->i fixnum-type
394 ; 1 make-fa-primitive flo:ceiling->exact
396 :noname ( flonum -- flonum )
397 drop truncate f->i fixnum-type
398 ; 1 make-fa-primitive flo:truncate->exact
400 :noname ( flonum -- flonum )
401 drop f->i fixnum-type
402 ; 1 make-fa-primitive flo:round->exact
404 :noname ( flonum flonum -- flonum )
405 drop swap drop f/ fatan flonum-type
406 ; 2 make-fa-primitive flo:atan2
410 ' make-rational 2 make-fa-primitive make-rational
412 :noname ( ratnum -- fixnum )
414 ; 1 make-fa-primitive rat:numerator
416 :noname ( ratnum -- fixnum )
418 ; 1 make-fa-primitive rat:denominator
422 :noname ( fixnum -- flonum )
423 drop i->f flonum-type
424 ; 1 make-fa-primitive fixnum->flonum
428 \ ==== Pairs and Lists ==== {{{
430 :noname ( arg1 arg2 -- pair )
432 ; 2 make-fa-primitive cons
434 :noname ( pair-obj -- obj )
436 ; pair-type 1 make-fa-type-primitive car
438 :noname ( args -- obj )
440 ; pair-type 1 make-fa-type-primitive cdr
442 :noname ( pair obj -- ok )
443 2swap pair-type ensure-arg-type
448 ; 2 make-fa-primitive set-car!
450 :noname ( pair obj -- ok )
451 2swap pair-type ensure-arg-type
456 ; 2 make-fa-primitive set-cdr!
460 \ ==== Polymorphic equality testing ==== {{{
462 :noname ( arg1 arg2 -- bool )
464 ; 2 make-fa-primitive eq?
468 \ ==== Input/Output ==== {{{
471 console-i/o-port obj@
472 ; 0 make-fa-primitive console-i/o-port
475 current-input-port obj@
476 ; 0 make-fa-primitive current-input-port
478 :noname ( args -- charobj )
480 2drop current-input-port obj@
482 car port-type ensure-arg-type
486 ; make-primitive read-char
488 :noname ( args -- charobj )
490 2drop current-input-port obj@
492 car port-type ensure-arg-type
496 ; make-primitive peek-char
498 :noname ( args -- stringobj )
500 2drop current-input-port obj@
502 car port-type ensure-arg-type
506 ; make-primitive read-line
508 : charlist>cstr ( charlist addr -- n )
510 dup 2swap ( origaddr addr charlist )
516 drop ( origaddr addr charlist char )
517 -rot 2swap ( origaddr charlist addr char )
519 1+ -rot ( origaddr nextaddr charlist )
522 2drop ( origaddr finaladdr )
526 :noname ( args -- finalResult )
530 ; string-type 1 make-fa-type-primitive load
532 :noname ( args -- obj )
534 ; 0 make-fa-primitive read
538 :noname ( obj -- none )
540 ; 1 make-fa-primitive write
542 : displaypair ( pairobj -- )
546 nil? if 2drop exit then
547 pair-type istype? if space recurse exit then
551 : displaychar ( charobj -- )
554 : (displaystring) ( charlist -- )
563 : displaystring ( stringobj -- )
564 drop pair-type (displaystring)
568 pair-type istype? if ." (" displaypair ." )" exit then
569 character-type istype? if displaychar exit then
570 string-type istype? if displaystring exit then
575 :noname ( stringobj -- none )
577 ; string-type 1 make-fa-type-primitive display-string
579 :noname ( charobj -- none )
581 ; character-type 1 make-fa-type-primitive display-character
583 :noname ( obj -- none )
585 ; 1 make-fa-primitive display
587 :noname ( args -- none )
589 ; 0 make-fa-primitive newline
593 \ ==== Evaluation ==== {{{
595 :noname ( args -- result )
598 nil? false = if car then ( proc argvals )
601 ; make-primitive apply
605 \ ==== Miscellaneous ==== {{{
607 ( Produce a recoverable exception. )
608 :noname ( args -- result )
614 ." Error: " car display
619 recoverable-exception throw
620 ; make-primitive error
622 ( Generate a temporary unique symbol. Used in the creation of hygienic macros. )
623 :noname ( args -- result )
624 [char] _ character-type nil cons
626 ; 0 make-fa-primitive gensym
628 ( Generate the NONE object indicating an unspecified return value. )
629 :noname ( args -- result )
631 ; 0 make-fa-primitive none