does> @ ;
make-type fixnum-type
-make-type realnum-type
make-type boolean-type
make-type character-type
make-type string-type
bl word
count
- \ 2dup ." Defining primitive " type ." ..." cr
-
cstr>charlist
charlist>symbol
then
;
+: ensure-arg-type-and-count ( tn tn-1 ... t2 t1 args n -- )
+ dup 0= if
+ drop nil objeq? false = if
+ recoverable-exception throw" Too many arguments for primitive procedure."
+ then
+ else
+ -rot nil? if
+ recoverable-exception throw" Too few arguments for primitive procedure."
+ then
+
+ 2dup cdr 2swap car ( ... t1 n args' arg1 )
+ 2rot 1- swap 2swap rot ( ... args' n-1 arg1 t1 )
+ istype? false = if
+ recoverable-exception throw" Incorrect type for primitive procedure."
+ then
+
+ 2drop recurse
+ then
+
+;
+
+: push-args-to-stack ( args -- arg1 arg2 ... argn )
+ begin
+ nil? false =
+ while
+ 2dup car 2swap cdr
+ repeat
+
+ 2drop
+;
+
+: add-fa-checks ( cfa n -- cfa' )
+ here current @ 1+ dup @ , !
+ 0 ,
+ here -rot
+ docol ,
+ ['] 2dup , ['] lit , , ['] ensure-arg-count ,
+ ['] push-args-to-stack ,
+ ['] lit , , ['] execute ,
+ ['] exit ,
+;
+
+: add-fa-type-checks ( cfa t1 t2 ... tn n -- cfa' )
+ here current @ 1+ dup @ , !
+ 0 ,
+ here >R
+ docol ,
+ ['] 2dup ,
+ ['] >R , ['] >R ,
+
+ dup ( cfa t1 t2 ... tn n m )
+
+ begin
+ ?dup 0>
+ while
+ rot ['] lit , , ( cfa t1 t2 ... tn-1 n m )
+ 1-
+ repeat
+
+ ['] R> , ['] R> ,
+
+ ['] lit , , ['] ensure-arg-type-and-count ,
+
+ ['] push-args-to-stack ,
+ ['] lit , , ['] execute ,
+ ['] exit ,
+
+ R>
+;
+
+: make-fa-primitive ( cfa n -- )
+ add-fa-checks make-primitive ;
+
+: make-fa-type-primitive ( cfa t1 t2 ... tn n -- )
+ add-fa-type-checks make-primitive ;
+
: arg-type-error
bold fg red ." Incorrect argument type." reset-term cr
abort
delim? pop-parse-idx
;
-: realnum? ( -- bool )
+: flonum? ( -- bool )
push-parse-idx
minus? plus? or if
fixnum-type
;
-: readrealnum ( -- realnum )
-
- \ Remember that at this point we're guaranteed to
- \ have a parsable real on this line.
-
- parse-str parse-idx @ +
-
- begin delim? false = while
- inc-parse-idx
- repeat
-
- parse-str parse-idx @ + over -
-
- float-parse
-
- realnum-type
-;
-
: readbool ( -- bool-obj )
inc-parse-idx
exit
then
- realnum? if
- readrealnum
- exit
- then
-
boolean? if
readbool
exit
: self-evaluating? ( obj -- obj bool )
boolean-type istype? if true exit then
fixnum-type istype? if true exit then
- realnum-type istype? if true exit then
character-type istype? if true exit then
string-type istype? if true exit then
nil-type istype? if true exit then
: printfixnum ( fixnumobj -- ) drop 0 .R ;
-: printrealnum ( realnumobj -- ) drop float-print ;
-
: printbool ( numobj -- )
drop if
." #t"
:noname ( obj -- )
fixnum-type istype? if printfixnum exit then
- realnum-type istype? if printrealnum exit then
boolean-type istype? if printbool exit then
character-type istype? if printchar exit then
string-type istype? if printstring exit then