Added draft readflonum.
[scheme.forth.jl.git] / src / scheme.4th
index f8dd089..c448b02 100644 (file)
@@ -24,7 +24,7 @@ variable nexttype
     does> @ ;
 
 make-type fixnum-type
-make-type realnum-type
+make-type flonum-type
 make-type boolean-type
 make-type character-type
 make-type string-type
@@ -436,8 +436,6 @@ global-env obj!
     bl word
     count
 
-    \ 2dup ." Defining primitive " type ." ..." cr
-
     cstr>charlist
     charlist>symbol
   
@@ -459,6 +457,82 @@ global-env obj!
     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
@@ -667,7 +741,7 @@ parse-idx-stack parse-idx-sp !
     delim? pop-parse-idx
 ;
 
-: realnum? ( -- bool )
+: flonum? ( -- bool )
     push-parse-idx
 
     minus? plus? or if
@@ -779,7 +853,7 @@ parse-idx-stack parse-idx-sp !
 : string? ( -- bool )
     nextchar [char] " = ;
 
-: readfixnum ( -- num-atom )
+: readfixnum ( -- fixnum )
     plus? minus? or if
         minus?
         inc-parse-idx
@@ -799,22 +873,24 @@ parse-idx-stack parse-idx-sp !
     fixnum-type
 ;
 
-: readrealnum ( -- realnum )
+: readflonum ( -- flonum )
+    \ DRAFT!!!
+    readfixnum drop i->f
 
-    \ Remember that at this point we're guaranteed to
-    \ have a parsable real on this line.
-
-    parse-str parse-idx @ +
-
-    begin delim? false = while
+    [char] . netchar = if
+        10 i->f
+        begin digit? while
+            nextchar [char] 0 - i->f over f/ f+
             inc-parse-idx
-    repeat
-
-    parse-str parse-idx @ + over -
+        repeat
+    then
 
-    float-parse
+    [char] e nextchar = [char] E nextchar = or if
+        readfixnum drop i->f
+        f^
+    then
 
-    realnum-type
+    flonum-type
 ;
 
 : readbool ( -- bool-obj )
@@ -938,11 +1014,6 @@ parse-idx-stack parse-idx-sp !
         exit
     then
 
-    realnum? if
-        readrealnum
-        exit
-    then
-
     boolean? if
         readbool
         exit
@@ -1032,7 +1103,6 @@ parse-idx-stack parse-idx-sp !
 : 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
@@ -1524,8 +1594,6 @@ hide env
 
 : printfixnum ( fixnumobj -- ) drop 0 .R ;
 
-: printrealnum ( realnumobj -- ) drop float-print ;
-
 : printbool ( numobj -- )
     drop if
         ." #t"
@@ -1596,7 +1664,6 @@ hide env
 
 :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