Load now returns result of last expression.
[scheme.forth.jl.git] / scheme.4th
index e1126ff..7626be8 100644 (file)
@@ -3,23 +3,27 @@ scheme definitions
 
 include term-colours.4th
 include defer-is.4th
-include throw-catch.4th
+include float.4th
+
+include debugging.4th
 
 defer read
 defer eval
 defer print
 
+defer collect-garbage
+
 \ ------ Types ------
 
 variable nexttype
 0 nexttype !
 : make-type
     create nexttype @ ,
-    nexttype @ 1+ nexttype !
+    1 nexttype +!
     does> @ ;
 
 make-type fixnum-type
-make-type real-type
+make-type realnum-type
 make-type boolean-type
 make-type character-type
 make-type string-type
@@ -28,20 +32,44 @@ make-type pair-type
 make-type symbol-type
 make-type primitive-proc-type
 make-type compound-proc-type
+make-type fileport-type
 : istype? ( obj type -- obj bool )
     over = ;
 
-\ ------ Cons cell memory ------ {{{
+\ ------ List-structured memory ------ {{{
 
-10000 constant N
-create car-cells N allot
-create car-type-cells N allot
-create cdr-cells N allot
-create cdr-type-cells N allot
+10000 constant scheme-memsize
 
+create car-cells scheme-memsize allot
+create car-type-cells scheme-memsize allot
+create cdr-cells scheme-memsize allot
+create cdr-type-cells scheme-memsize allot
+
+create nextfrees scheme-memsize allot
+:noname
+    scheme-memsize 0 do
+        i 1+ nextfrees i + !
+    loop
+; execute
+        
 variable nextfree
 0 nextfree !
 
+: inc-nextfree
+    nextfrees nextfree @ + @
+    nextfree !
+
+    nextfree @ scheme-memsize >= if
+        collect-garbage
+    then
+
+    nextfree @ scheme-memsize >= if
+        fg red bold
+        ." Out of memory! Aborting."
+        reset-term abort
+    then
+;
+
 : cons ( car-obj cdr-obj -- pair-obj )
     cdr-type-cells nextfree @ + !
     cdr-cells nextfree @ + !
@@ -49,8 +77,7 @@ variable nextfree
     car-cells nextfree @ + !
 
     nextfree @ pair-type
-
-    1 nextfree +!
+    inc-nextfree
 ;
 
 : car ( pair-obj -- car-obj )
@@ -113,6 +140,37 @@ variable nextfree
 
 \ }}}
 
+\ ---- Port I/O ----  {{{
+
+: fileport>fid ( fileport -- fid )
+    drop ;
+
+: fid>fileport ( fid -- fileport )
+    fileport-type ;
+
+: open-input-file ( addr n -- fileport )
+    r/o open-file drop fid>fileport
+;
+
+: close-port ( fileport -- )
+    fileport>fid close-file drop
+;
+
+objvar console-i/o-port
+0 fileport-type console-i/o-port obj!
+
+objvar current-input-port
+console-i/o-port obj@ current-input-port obj!
+
+: read-port ( fileport -- obj )
+    current-input-port obj!
+    read ;
+
+: read-console ( -- obj )
+    console-i/o-port obj@ read-port ;
+
+\ }}}
+
 \ ---- Pre-defined symbols ---- {{{
 
 objvar symbol-table
@@ -177,7 +235,7 @@ objvar symbol-table
 ;
 
 
-: (create-symbol) ( addr n -- symbol-obj )
+: cstr>charlist ( addr n -- symbol-obj )
     dup 0= if
         2drop nil
     else
@@ -193,7 +251,7 @@ objvar symbol-table
     bl word
     count
 
-    (create-symbol)
+    cstr>charlist
     drop symbol-type
 
     2dup
@@ -334,59 +392,6 @@ global-env obj!
 
 \ }}}
 
-\ ---- Primitives ---- {{{
-
-: make-primitive ( cfa -- )
-    bl word
-    count
-
-    (create-symbol)
-    drop symbol-type
-    
-    2dup
-
-    symbol-table obj@
-    cons
-    symbol-table obj!
-
-    rot primitive-proc-type ( var prim )
-    global-env obj@ define-var
-;
-
-: arg-count-error
-            bold fg red ." Incorrect argument count." reset-term cr
-            abort
-;
-
-: ensure-arg-count ( args n -- )
-    dup 0= if
-        drop nil objeq? false = if
-            arg-count-error
-        then
-    else
-        -rot 2dup nil objeq? if
-            arg-count-error
-        then
-        
-        cdr rot 1- recurse
-    then
-;
-
-: arg-type-error
-            bold fg red ." Incorrect argument type." reset-term cr
-            abort
-;
-
-: ensure-arg-type ( arg type -- arg )
-    istype? false = if
-        arg-type-error
-    then
-;
-
-include scheme-primitives.4th
-
-\ }}}
-
 \ ---- Read ---- {{{
 
 variable parse-idx
@@ -415,13 +420,24 @@ parse-idx-stack parse-idx-sp !
     '\n' parse-str parse-str-span @ + !
     1 parse-str-span +! ;
 
+: append-eof
+    4 parse-str parse-str-span @ + !
+    1 parse-str-span +!  ;
+
 : empty-parse-str
     0 parse-str-span !
     0 parse-idx ! ;
 
 : getline
-    parse-str 160 expect cr
-    span @ parse-str-span !
+    current-input-port obj@ console-i/o-port obj@ objeq? if
+        parse-str 160 expect cr
+        span @ parse-str-span !
+    else
+        parse-str 160 current-input-port obj@ fileport>fid read-line
+        drop swap parse-str-span !
+
+        parse-str-span @ 0= and if append-eof then
+    then
     append-newline
     0 parse-idx ! ;
 
@@ -438,12 +454,16 @@ parse-idx-stack parse-idx-sp !
     charavailable? false = if getline then
     parse-str parse-idx @ + @ ;
 
+: '\t' 9 ;
 : whitespace? ( -- bool )
     nextchar BL = 
-    nextchar '\n' = or ;
+    nextchar '\n' =
+    nextchar '\t' =
+    or or ;
 
+: EOF 4 ; 
 : eof? ( -- bool )
-    nextchar 4 = ;
+    nextchar EOF = ;
 
 : delim? ( -- bool )
     whitespace?
@@ -508,13 +528,52 @@ 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 )
+    push-parse-idx
+
+    minus? plus? or if
+        inc-parse-idx
     then
+
+    \ Record starting parse idx:
+    \ Want to detect whether any characters (following +/-) were eaten.
+    parse-idx @
+
+    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
+
+        digit? invert if
+            drop pop-parse-idx false exit
+        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 )
@@ -584,7 +643,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
@@ -604,7 +663,25 @@ parse-idx-stack parse-idx-sp !
     fixnum-type
 ;
 
-: readbool ( -- bool-atom )
+: 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
     
     nextchar [char] f = if
@@ -618,7 +695,7 @@ parse-idx-stack parse-idx-sp !
     boolean-type
 ;
 
-: readchar ( -- char-atom )
+: readchar ( -- char-obj )
     inc-parse-idx
     inc-parse-idx
 
@@ -721,7 +798,12 @@ parse-idx-stack parse-idx-sp !
     eatspaces
 
     fixnum? if
-        readnum
+        readfixnum
+        exit
+    then
+
+    realnum? if
+        readrealnum
         exit
     then
 
@@ -776,9 +858,9 @@ parse-idx-stack parse-idx-sp !
     then
 
     eof? if
+        EOF character-type
         inc-parse-idx
-        bold fg blue ." Moriturus te saluto." reset-term ."  ok" cr
-        quit
+        exit
     then
 
     \ Anything else is parsed as a symbol
@@ -799,6 +881,7 @@ 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
@@ -1067,7 +1150,9 @@ parse-idx-stack parse-idx-sp !
 
 \ ---- Print ---- {{{
 
-: printnum ( numobj -- ) drop 0 .R ;
+: printfixnum ( fixnumobj -- ) drop 0 .R ;
+
+: printrealnum ( realnumobj -- ) drop float-print ;
 
 : printbool ( numobj -- )
     drop if
@@ -1132,7 +1217,8 @@ parse-idx-stack parse-idx-sp !
     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
@@ -1148,6 +1234,207 @@ parse-idx-stack parse-idx-sp !
 
 \ }}}
 
+\ ---- Garbage Collection ---- {{{
+
+variable gc-enabled
+false gc-enabled !
+
+variable gc-stack-depth
+
+: enable-gc
+    depth gc-stack-depth !
+    true gc-enabled ! ;
+
+: disable-gc
+    false gc-enabled ! ;
+
+: gc-enabled?
+    gc-enabled @ ;
+
+: pairlike? ( obj -- obj bool )
+    pair-type istype? if true exit then
+    string-type istype? if true exit then
+    symbol-type istype? if true exit then
+    compound-proc-type istype? if true exit then
+
+    false
+;
+
+: pairlike-marked? ( obj -- obj bool )
+    over nextfrees + @ 0=
+;
+
+: mark-pairlike ( obj -- obj )
+        over nextfrees + 0 swap !
+;
+
+: gc-unmark ( -- )
+    scheme-memsize 0 do
+        1 nextfrees i + !
+    loop
+;
+
+: gc-mark-obj ( obj -- )
+
+    pairlike? invert if 2drop exit then
+    pairlike-marked? if 2drop exit then
+
+    mark-pairlike
+
+    drop pair-type 2dup
+
+    car recurse
+    cdr recurse
+;
+
+: gc-sweep
+    scheme-memsize nextfree !
+    0 scheme-memsize 1- do
+        nextfrees i + @ 0<> if
+            nextfree @ nextfrees i + !
+            i nextfree !
+        then
+    -1 +loop
+;
+
+\ Following a GC, this gives the amount of free memory
+: gc-count-marked
+    0
+    scheme-memsize 0 do
+        nextfrees i + @ 0= if 1+ then
+    loop
+;
+
+\ Debugging word - helps spot memory that is retained
+: gc-zero-unmarked
+    scheme-memsize 0 do
+        nextfrees i + @ 0<> if
+            0 car-cells i + !
+            0 cdr-cells i + !
+        then
+    loop
+;
+
+:noname
+    \ ." GC! "
+
+    gc-unmark
+
+    symbol-table obj@ gc-mark-obj
+    global-env obj@ gc-mark-obj
+
+    depth gc-stack-depth @ do
+        PSP0 i + 1 + @
+        PSP0 i + 2 + @
+
+        gc-mark-obj
+    2 +loop
+
+    gc-sweep
+
+    \ ." (" gc-count-marked . ." pairs marked as used.)" cr
+; is collect-garbage
+
+\ }}}
+
+\ ---- Primitives ---- {{{
+
+: make-primitive ( cfa -- )
+    bl word
+    count
+
+    \ 2dup ." Defining primitive " type ." ..." cr
+
+    cstr>charlist
+    drop symbol-type
+    
+    2dup
+
+    symbol-table obj@
+    cons
+    symbol-table obj!
+
+    rot primitive-proc-type ( var prim )
+    global-env obj@ define-var
+;
+
+: arg-count-error
+            bold fg red ." Incorrect argument count." reset-term cr
+            abort
+;
+
+: ensure-arg-count ( args n -- )
+    dup 0= if
+        drop nil objeq? false = if
+            arg-count-error
+        then
+    else
+        -rot 2dup nil objeq? if
+            arg-count-error
+        then
+        
+        cdr rot 1- recurse
+    then
+;
+
+: arg-type-error
+            bold fg red ." Incorrect argument type." reset-term cr
+            abort
+;
+
+: ensure-arg-type ( arg type -- arg )
+    istype? false = if
+        arg-type-error
+    then
+;
+
+include scheme-primitives.4th
+
+\ }}}
+
+\ ---- Loading files ---- {{{
+
+: charlist>cstr ( charlist addr -- n )
+
+    dup 2swap ( origaddr addr charlist )
+
+    begin 
+        2dup nil objeq? false =
+    while
+        2dup cdr 2swap car 
+        drop ( origaddr addr charlist char )
+        -rot 2swap ( origaddr charlist addr char )
+        over !
+        1+ -rot ( origaddr nextaddr charlist )
+    repeat
+
+    2drop ( origaddr finaladdr ) 
+    swap -
+;
+
+: load ( addr n -- finalResult )
+    open-input-file
+
+    empty-parse-str
+
+    ok-symbol ( port res )
+
+    begin
+        2over read-port ( port res obj )
+
+        2dup EOF character-type objeq? if
+            2drop 2swap close-port
+            exit
+        then
+
+        2swap 2drop ( port obj )
+
+        global-env obj@ eval ( port res )
+    again
+;
+
+\ }}}
+
 \ ---- REPL ----
 
 : repl
@@ -1156,14 +1443,26 @@ parse-idx-stack parse-idx-sp !
 
     empty-parse-str
 
+    enable-gc
+
     begin
         cr bold fg green ." > " reset-term
-        read
+        read-console
+
+        2dup EOF character-type objeq? if
+            bold fg blue ." Moriturus te saluto." reset-term cr
+            exit
+        then
+
         global-env obj@ eval
+
         fg cyan ." ; " print reset-term
     again
 ;
 
+: test s" fact.scm" ;
+test load 2drop
+
 forth definitions
 
 \ vim:fdm=marker