Can now read and print reals.
[scheme.forth.jl.git] / scheme.4th
index cecacff..bac3283 100644 (file)
@@ -3,24 +3,38 @@ scheme definitions
 
 include term-colours.4th
 include defer-is.4th
+include throw-catch.4th
+include float.4th
+
+defer read
+defer eval
+defer print
 
 \ ------ Types ------
 
-0 constant fixnum-type
-1 constant boolean-type
-2 constant character-type
-3 constant string-type
-4 constant nil-type
-5 constant pair-type
-6 constant symbol-type
-7 constant primitive-proc-type
-8 constant compound-proc-type
+variable nexttype
+0 nexttype !
+: make-type
+    create nexttype @ ,
+    nexttype @ 1+ nexttype !
+    does> @ ;
+
+make-type fixnum-type
+make-type realnum-type
+make-type boolean-type
+make-type character-type
+make-type string-type
+make-type nil-type
+make-type pair-type
+make-type symbol-type
+make-type primitive-proc-type
+make-type compound-proc-type
 : istype? ( obj type -- obj bool )
     over = ;
 
 \ ------ Cons cell memory ------ {{{
 
-1000 constant N
+10000 constant N
 create car-cells N allot
 create car-type-cells N allot
 create cdr-cells N allot
@@ -376,8 +390,6 @@ include scheme-primitives.4th
 
 \ ---- Read ---- {{{
 
-defer read
-
 variable parse-idx
 variable stored-parse-idx
 create parse-str 161 allot
@@ -497,13 +509,48 @@ parse-idx-stack parse-idx-sp !
         inc-parse-idx
     repeat
 
-    delim? if
-        pop-parse-idx
-        true
-    else
-        pop-parse-idx
-        false
+    delim? pop-parse-idx
+;
+
+: realnum? ( -- bool )
+    \ Record starting parse idx:
+    \ Want to detect whether any characters were eaten.
+    parse-idx @
+
+    push-parse-idx
+
+    minus? plus? or if
+        inc-parse-idx
+    then
+
+    begin digit? while
+            inc-parse-idx
+    repeat
+
+    [char] . nextchar = if
+        inc-parse-idx
+        begin digit? while
+                inc-parse-idx
+        repeat
     then
+
+    [char] e nextchar = [char] E nextchar = or if
+        inc-parse-idx
+
+        minus? plus? or if
+            inc-parse-idx
+        then
+
+        begin digit? while
+                inc-parse-idx
+        repeat
+    then
+
+    \ This is a real number if characters were
+    \ eaten and the next characer is a delimiter.
+    parse-idx @ < delim? and
+
+    pop-parse-idx
 ;
 
 : boolean? ( -- bool )
@@ -573,7 +620,7 @@ parse-idx-stack parse-idx-sp !
 : string? ( -- bool )
     nextchar [char] " = ;
 
-: readnum ( -- num-atom )
+: readfixnum ( -- num-atom )
     plus? minus? or if
         minus?
         inc-parse-idx
@@ -593,6 +640,24 @@ parse-idx-stack parse-idx-sp !
     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-atom )
     inc-parse-idx
     
@@ -710,7 +775,12 @@ parse-idx-stack parse-idx-sp !
     eatspaces
 
     fixnum? if
-        readnum
+        readfixnum
+        exit
+    then
+
+    realnum? if
+        readrealnum
         exit
     then
 
@@ -785,11 +855,10 @@ parse-idx-stack parse-idx-sp !
 
 \ ---- Eval ---- {{{
 
-defer eval
-
 : 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
@@ -972,7 +1041,7 @@ defer eval
                 begin
                     2dup cdr 2dup nil objeq? false =
                 while
-                    -2rot car over ( nextbody env exp env )
+                    -2rot car 2over ( nextbody env exp env )
                     eval
                     2drop \ discard result
                     2swap ( env nextbody )
@@ -981,7 +1050,7 @@ defer eval
                 2drop ( env body )
                 car 2swap ( exp env )
 
-                eval \ TODO: tail call optimization
+                R> drop ['] eval goto-deferred  \ Tail call optimization
             endof
 
             bold fg red ." Object not applicable. Aboring." reset-term cr
@@ -1029,7 +1098,8 @@ defer eval
             if-alternative
         then
 
-        2swap ['] eval goto
+        2swap
+        ['] eval goto-deferred
     then
 
     lambda? if
@@ -1057,9 +1127,9 @@ defer eval
 
 \ ---- Print ---- {{{
 
-defer print
+: printfixnum ( fixnumobj -- ) drop 0 .R ;
 
-: printnum ( numobj -- ) drop 0 .R ;
+: printrealnum ( realnumobj -- ) drop float-print ;
 
 : printbool ( numobj -- )
     drop if
@@ -1124,7 +1194,8 @@ defer print
     2drop ." <compound procedure>" ;
 
 :noname ( obj -- )
-    fixnum-type istype? if printnum exit then
+    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