Shifting stream input into read.
[scheme.forth.jl.git] / scheme.4th
index 48ab564..524f3a1 100644 (file)
@@ -5,173 +5,223 @@ scheme definitions
 
 include term-colours.4th
 
-\ Cons cell memory
-1000 constant memsize
-create car memsize allot
-create cdr memsize allot
-create types memsize allot
+0 constant number-type
+1 constant boolean-type
+2 constant character-type
+: istype? ( obj -- obj b )
+    over = ;
 
-0 constant symbol-type
-1 constant int-type
-2 constant list-type
-3 constant bool-type
+\ ---- Read ----
 
-variable nextfree
-0 nextfree !
+variable parse-idx
+variable stored-parse-idx
+create parse-str 161 allot
+variable parse-str-span
 
-: make-bool 
-    nextfree @
+: append-newline
+    1 parse-str-span +!
+    '\n' parse-str parse-str-span @ + ! ;
 
-    car nextfree @ + !
-    cdr nextfree @ + 0 !
-    types nextfree @ + bool-type !
+: getline
+    parse-str 160 expect
+    span @ parse-str-span !
+    append-newline
+    0 parse-idx ! ;
 
-    1 nextfree +!
-;
+: inc-parse-idx
+    1 parse-idx +! ;
 
-: stack
-    create here 1+ , allot ;
+: dec-parse-idx
+    1 parse-idx -! ;
 
+: store-parse-idx
+    parse-idx @ stored-parse-idx !  ;
 
-: push ( st v -- )
-    over @ !
-    1 swap +!
-;
+: restore-parse-idx
+    stored-parse-idx @ parse-idx !  ;
 
-: pop ( st -- v )
-    dup @       ( s0 sp )
-    1-          ( so sp' )
 
-    2dup = abort" Stack underflow."
-    
-    dup @       ( s0 sp' v )
-    -rot swap   ( v sp' s0 )
-    !
-;
-
-100 stack parse-stack 
-variable parse-idx
-variable parse-str
-
-
-: inc-parse-idx parse-idx +! ;
-: dec-parse-idx parse-idx -! ;
-
-: ?charavailable ( -- bool )
-    parse-str @ @ parse-idx @ >
+: charavailable? ( -- bool )
+    parse-str-span @ parse-idx @ >
 ;
 
 : nextchar ( -- char )
-    ?charavailable if
-        parse-str @ 1+ parse-idx @ + @
-    else
-        0
-    then
-;
+    charavailable? false = if getline then
+    parse-str parse-idx @ + @ ;
 
-: ?whitespace ( -- bool )
+: whitespace? ( -- bool )
     nextchar BL = 
     nextchar '\n' = or
 ;
 
-: ?delim ( -- bool )
-    ?whitespace
+: delim? ( -- bool )
+    whitespace?
     nextchar [char] ( = or
     nextchar [char] ) = or
 ;
 
 : eatspaces
     begin
-        ?whitespace
+        whitespace?
     while
-            1 parse-idx +!
+        inc-parse-idx
     repeat
 ;
 
-: parsebool
-
-    nextchar [char] # <> if false exit then
+: digit? ( -- bool )
+    nextchar [char] 0 >=
+    nextchar [char] 9 <=
+    and ;
 
-    1 inc-parse-idx
+: minus? ( -- bool )
+    nextchar [char] - = ;
 
-    nextchar dup [char] t = swap [char] f = or
-    not if
-        1 dec-parse-idx
-        false exit
+: number? ( -- bool )
+    digit? minus? or false = if
+        false
+        exit
     then
 
-    1 inc-parse-idx
+    store-parse-idx
+    inc-parse-idx
+
+    begin digit? while
+        inc-parse-idx
+    repeat
 
-    ?delim not if
-        2 dec-parse-idx
-        false exit
+    delim? charavailable? false = or if
+        restore-parse-idx
+        true
     else
-        1 dec-parse-idx
-        nextchar [char] t = make-bool
-        1 inc-parse-idx
-        true exit
+        restore-parse-idx
+        false
+    then
+;
+
+: boolean? ( -- bool )
+    nextchar [char] # <> if false exit then
+
+    store-parse-idx
+    inc-parse-idx
+
+    nextchar [char] t <>
+    nextchar [char] f <>
+    and if restore-parse-idx false exit then
+
+    restore-parse-idx
+    true
+;
+
+: character? ( -- bool )
+    nextchar [char] # <> if false exit then
+
+    store-parse-idx
+    inc-parse-idx
+
+    nextchar [char] \ <> if restore-parse-idx false exit then
+
+    inc-parse-idx
+
+    charavailable? false = if restore-parse-idx false exit then
+
+    restore-parse-idx true
+;
+
+: readnum ( -- num-atom )
+    minus? dup if
+        inc-parse-idx
     then
+
+    0
+
+    begin digit? while
+        10 * nextchar [char] 0 - +
+        inc-parse-idx
+    repeat
+
+    swap if negate then
+
+    number-type
 ;
 
-\ Set cdr at i to j, leaving j on the stack
-: append ( i j -- j )
-    dup rot
-    dup 0> if
-        cdr + !
+: readbool ( -- bool-atom )
+    inc-parse-idx
+    
+    nextchar [char] f = if
+        false
     else
-        2drop
+        true
     then
+
+    boolean-type
 ;
 
-: parsetoken
+\ Parse a scheme expression
+: read ( -- obj )
 
     eatspaces
 
-    \ Parens
-
-    nextchar [char] ( = if
-        \ todo
+    number? if
+        readnum
         exit
     then
 
-    nextchar [char] ) = if
-        \ todo
+    boolean? if
+        readbool
         exit
     then
 
-    parsebool if
-        append
+    bold fg red ." Error parsing string starting at character '"
+    nextchar emit
+    ." '. Aborting." reset-term cr
+    abort
+;
+
+\ ---- Eval ----
+
+: self-evaluating? ( obj -- obj bool )
+    number-type istype? if true exit then
+    boolean-type istype? if true exit then
+    false ;
+
+: eval
+    self-evaluating? if
         exit
     then
+
+    bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
+    abort
 ;
 
-\ Parse a counted string into a scheme expression
-: parseexp ( straddr n -- exp )
-    0 parse-idx !
+\ ---- Print ----
 
-    begin
-        parsetoken
-    nextchar 0 =
-    until
+: printnum ( numobj -- ) drop . ;
+
+: printbool ( numobj -- )
+    drop if
+        ." #t"
+    else
+        ." #f"
+    then
 ;
 
-\ ---- REPL ----
+: print ( obj -- )
+    ." ; "
+    number-type istype? if ." => " printnum exit then
+    boolean-type istype? if ." => " printbool exit then
+;
 
-create repl-buffer 161 allot
+\ ---- REPL ----
 
 : repl
-    repl-buffer parse-str !
-
-    cr
+    cr ." Welcome to scheme.forth.jl!" cr
+       ." Use Ctrl-D to exit." cr
 
     begin
-        bold fg green ." => " reset-term
-
-        repl-buffer 1+ 160 expect cr
-        span @ repl-buffer !
-
-        parseexp
-        \ eval
+        cr bold fg green ." > " reset-term
+        read
+        eval
+        fg cyan print reset-term
     again
 ;