1 \ ==== Type predicates ==== {{{
3 :noname ( args -- boolobj )
4 2dup 1 ensure-arg-count
6 car nil objeq? boolean-type
9 :noname ( args -- boolobj )
10 2dup 1 ensure-arg-count
12 car boolean-type istype? -rot 2drop boolean-type
13 ; make-primitive boolean?
15 :noname ( args -- boolobj )
16 2dup 1 ensure-arg-count
18 car symbol-type istype? -rot 2drop boolean-type
19 ; make-primitive symbol?
21 :noname ( args -- boolobj )
22 2dup 1 ensure-arg-count
24 car fixnum-type istype? -rot 2drop boolean-type
25 ; make-primitive integer?
27 :noname ( args -- boolobj )
28 2dup 1 ensure-arg-count
30 car character-type istype? -rot 2drop boolean-type
31 ; make-primitive char?
33 :noname ( args -- boolobj )
34 2dup 1 ensure-arg-count
36 car string-type istype? -rot 2drop boolean-type
37 ; make-primitive string?
39 :noname ( args -- boolobj )
40 2dup 1 ensure-arg-count
42 car pair-type istype? -rot 2drop boolean-type
43 ; make-primitive pair?
45 :noname ( args -- boolobj )
46 2dup 1 ensure-arg-count
48 car primitive-proc-type istype? -rot 2drop boolean-type
49 ; make-primitive procedure?
53 \ ==== Type conversions ==== {{{
55 :noname ( args -- fixnum )
56 2dup 1 ensure-arg-count
57 car character-type ensure-arg-type
60 ; make-primitive char->integer
62 :noname ( args -- char )
63 2dup 1 ensure-arg-count
64 car fixnum-type ensure-arg-type
67 ; make-primitive integer->char
69 : fixnum-to-charlist ( fixnum -- charlist )
72 [char] 0 character-type nil cons
76 nil 2swap ( charlist fixnum )
81 2dup swap 10 mod swap ( charlist fixnum fixnummod )
82 2swap swap 10 / swap ( charlist fixnummod fixnumdiv )
83 -2rot ( fixnumdiv charlist fixnummod )
85 drop [char] 0 + character-type 2swap
86 cons ( fixnumdiv newcharlist )
94 :noname ( args -- string )
95 2dup 1 ensure-arg-count
96 car fixnum-type ensure-arg-type
100 fixnum-to-charlist ( fixnum charlist )
102 [char] - character-type 2swap cons
106 ; make-primitive number->string
108 :noname ( args -- symbol )
109 2dup 1 ensure-arg-count
110 car string-type ensure-arg-type
114 2dup car [char] - character-type objeq? if
118 2dup car [char] + character-type objeq? if
126 2dup nil objeq? false =
128 2dup car drop [char] 0 - -rot
129 2swap swap 10 * + -rot
138 ; make-primitive string->number
140 :noname ( args -- string )
141 2dup 1 ensure-arg-count
142 car symbol-type ensure-arg-type
147 ; make-primitive symbol->string
149 :noname ( args -- symbol )
150 2dup 1 ensure-arg-count
151 car string-type ensure-arg-type
156 ; make-primitive string->symbol
160 \ ==== Arithmetic ==== {{{
162 : add-prim ( args -- fixnum )
168 -rot cdr recurse drop
172 ' add-prim make-primitive +
174 :noname ( args -- fixnum )
191 :noname ( args -- fixnum )
197 -rot cdr recurse drop
202 :noname ( args -- fixnum )
203 2dup 2 ensure-arg-count
205 2dup car fixnum-type ensure-arg-type
206 2swap cdr car fixnum-type ensure-arg-type
211 ; make-primitive quotient
213 :noname ( args -- fixnum )
214 2dup 2 ensure-arg-count
216 2dup car fixnum-type ensure-arg-type
217 2swap cdr car fixnum-type ensure-arg-type
222 ; make-primitive remainder
226 : test-relation ( args -- bool )
230 true boolean-type exit
235 2dup car fixnum-type ensure-arg-type ( args arg0 )
236 2swap cdr ( arg0 args' )
240 true boolean-type exit
246 2dup nil objeq? false =
248 2dup car fixnum-type ensure-arg-type ( arg0 args' arg1 )
249 2rot 2swap 2dup 2rot 2swap ( args' arg1 arg1 arg0 )
250 relcfa @ execute false = if
252 false boolean-type exit
255 2swap cdr ( arg0 args'' )
262 : fixnum-lt ( obj1 obj2 -- bool )
267 ['] fixnum-lt relcfa !
271 : fixnum-gt ( obj1 obj2 -- bool )
276 ['] fixnum-gt relcfa !
280 : fixnum-eq ( obj1 obj2 -- bool )
285 ['] fixnum-eq relcfa !
293 \ ==== Pairs and Lists ==== {{{
295 :noname ( args -- pair )
296 2dup 2 ensure-arg-count
298 2dup car 2swap cdr car
300 ; make-primitive cons
302 :noname ( args -- list )
303 \ args is already a list!
304 ; make-primitive list
306 :noname ( args -- obj )
307 2dup 1 ensure-arg-count
308 car pair-type ensure-arg-type
313 :noname ( args -- obj )
314 2dup 1 ensure-arg-count
315 car pair-type ensure-arg-type
320 :noname ( args -- ok )
321 2dup 2 ensure-arg-count
323 2swap car pair-type ensure-arg-type
328 ; make-primitive set-car!
330 :noname ( args -- ok )
331 2dup 2 ensure-arg-count
333 2swap car pair-type ensure-arg-type
338 ; make-primitive set-cdr!
342 \ ==== Polymorphic equality testing ==== {{{
344 :noname ( args -- bool )
345 2dup 2 ensure-arg-count
354 \ ==== Input/Output ==== {{{
356 :noname ( args -- finalResult )
357 2dup 1 ensure-arg-count
358 car string-type ensure-arg-type
363 ; make-primitive load
365 :noname ( args -- obj )
368 ; make-primitive read
371 :noname ( args -- none )
372 2dup 1 ensure-arg-count
377 ; make-primitive write
379 : displaypair ( pairobj -- )
383 nil? if 2drop exit then
384 pair-type istype? if space recurse exit then
388 : displaychar ( charobj -- )
391 : (displaystring) ( charlist -- )
400 : displaystring ( stringobj -- )
401 drop pair-type (displaystring)
405 pair-type istype? if ." (" displaypair ." )" exit then
406 character-type istype? if displaychar exit then
407 string-type istype? if displaystring exit then
412 :noname ( args -- none )
413 2dup 1 ensure-arg-count
414 car string-type ensure-arg-type
419 ; make-primitive display-string
421 :noname ( args -- none )
422 2dup 1 ensure-arg-count
423 car character-type ensure-arg-type
428 ; make-primitive display-character
430 :noname ( args -- none )
431 2dup 1 ensure-arg-count
437 ; make-primitive display
439 :noname ( args -- none )
445 ; make-primitive newline
449 \ ==== Evaluation ==== {{{
451 :noname ( args -- result )
454 nil? false = if car then ( proc argvals )
457 ; make-primitive apply
461 \ ==== Miscellaneous ==== {{{
463 ( Produce a recoverable exception. )
464 :noname ( args -- result )
470 ." Error: " car display
475 recoverable-exception throw
476 ; make-primitive error
478 ( Generate a temporary unique symbol. Used in the creation of hygienic macros. )
479 :noname ( args -- result )
482 [char] _ character-type nil cons
484 ; make-primitive gensym
486 ( Generate the NONE object indicating an unspecified return value. )
487 :noname ( args -- result )
491 ; make-primitive none