X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=scheme.4th;h=89b316011ffb5a1b318ae46396f7f9075905e401;hb=1e77138f35f12ccc6fe20d943482ac6b57c3f7cd;hp=e1126ff0868bcc7a6bca3bdbefc1889d13a34ad1;hpb=7c4b983db5c13783ed581b302becb7e8419123dc;p=scheme.forth.jl.git diff --git a/scheme.4th b/scheme.4th index e1126ff..89b3160 100644 --- a/scheme.4th +++ b/scheme.4th @@ -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 @@ -51,6 +55,8 @@ variable nextfree nextfree @ pair-type 1 nextfree +! + + collect-garbage ; : car ( pair-obj -- car-obj ) @@ -113,6 +119,28 @@ variable nextfree \ }}} +\ ---- Garbage Collection ---- {{{ + +variable gc-enabled +false gc-enabled ! + +: gc-enable + true gc-enabled ! ; + +: gc-disable + false gc-enabled ! ; + +: gc-enabled? + gc-enabled @ ; + +:noname + gc-enabled? if + .s ." GC!" cr + then +; is collect-garbage + +\ }}} + \ ---- Pre-defined symbols ---- {{{ objvar symbol-table @@ -340,6 +368,8 @@ global-env obj! bl word count + \ 2dup ." Defining primitive " type ." ..." cr + (create-symbol) drop symbol-type @@ -508,13 +538,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 +653,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,6 +673,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 @@ -721,7 +808,12 @@ parse-idx-stack parse-idx-sp ! eatspaces fixnum? if - readnum + readfixnum + exit + then + + realnum? if + readrealnum exit then @@ -777,7 +869,7 @@ parse-idx-stack parse-idx-sp ! eof? if inc-parse-idx - bold fg blue ." Moriturus te saluto." reset-term ." ok" cr + bold fg blue ." Moriturus te saluto." reset-term cr quit then @@ -799,6 +891,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 +1160,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 +1227,8 @@ parse-idx-stack parse-idx-sp ! 2drop ." " ; :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 @@ -1154,12 +1250,17 @@ parse-idx-stack parse-idx-sp ! cr ." Welcome to scheme.forth.jl!" cr ." Use Ctrl-D to exit." cr + empty-parse-str + gc-enable + begin cr bold fg green ." > " reset-term read + global-env obj@ eval + fg cyan ." ; " print reset-term again ;