X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=scheme.4th;h=74a5b45d939c2ab5b12c7cd6955e20f61967082c;hb=5241c399e654d473a6e86135937ed7af7965d0a2;hp=9a2a3ca1779e9fab029a6b6ef80893a09e621795;hpb=ce188b101cbb79f55249d3eb6ff06e44ef065096;p=scheme.forth.jl.git diff --git a/scheme.4th b/scheme.4th index 9a2a3ca..74a5b45 100644 --- a/scheme.4th +++ b/scheme.4th @@ -3,32 +3,66 @@ scheme definitions include term-colours.4th include defer-is.4th +include float.4th + +include debugging.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 @ , + 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 -create car-cells N allot -create car-type-cells N allot -create cdr-cells N allot -create cdr-type-cells N allot +1000 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 @ + @ + + dup scheme-memsize < if + nextfree ! + else + bold fg red + ." Out of memory! Aborting." + reset-term abort + then +; + : cons ( car-obj cdr-obj -- pair-obj ) cdr-type-cells nextfree @ + ! cdr-cells nextfree @ + ! @@ -36,8 +70,7 @@ variable nextfree car-cells nextfree @ + ! nextfree @ pair-type - - 1 nextfree +! + inc-nextfree ; : car ( pair-obj -- car-obj ) @@ -100,6 +133,71 @@ variable nextfree \ }}} +\ ---- Garbage Collection ---- {{{ + +variable gc-enabled +false gc-enabled ! + +: enable-gc + 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 +; + +defer gc-mark-trace +: gc-mark-obj ( obj -- ) + + gc-mark-trace + + 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 +; + +\ }}} + \ ---- Pre-defined symbols ---- {{{ objvar symbol-table @@ -327,6 +425,8 @@ global-env obj! bl word count + \ 2dup ." Defining primitive " type ." ..." cr + (create-symbol) drop symbol-type @@ -376,8 +476,6 @@ include scheme-primitives.4th \ ---- Read ---- {{{ -defer read - variable parse-idx variable stored-parse-idx create parse-str 161 allot @@ -497,13 +595,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 ) @@ -573,7 +710,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 +730,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 +865,12 @@ parse-idx-stack parse-idx-sp ! eatspaces fixnum? if - readnum + readfixnum + exit + then + + realnum? if + readrealnum exit then @@ -766,7 +926,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 @@ -785,11 +945,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 @@ -817,11 +976,23 @@ defer eval : definition? ( obj -- obj bool ) define-symbol tagged-list? ; +: make-lambda ( params body -- lambda-exp ) + lambda-symbol -2rot cons cons ; + : definition-var ( obj -- var ) - cdr car ; + cdr car + symbol-type istype? false = if car then +; : definition-val ( obj -- val ) - cdr cdr car ; + 2dup cdr car symbol-type istype? if + 2drop + cdr cdr car + else + cdr 2swap cdr cdr + make-lambda + then +; : assignment? ( obj -- obj bool ) set!-symbol tagged-list? ; @@ -933,6 +1104,15 @@ defer eval then ; +: procedure-params ( proc -- params ) + drop pair-type car ; + +: procedure-body ( proc -- body ) + drop pair-type cdr car ; + +: procedure-env ( proc -- body ) + drop pair-type cdr cdr car ; + : apply ( proc args ) 2swap dup case primitive-proc-type of @@ -940,7 +1120,27 @@ defer eval endof compound-proc-type of - ." Compound procedures not yet implemented." + 2dup procedure-body ( args proc body ) + -2rot 2dup procedure-params ( body args proc params ) + -2rot procedure-env ( body params args procenv ) + + extend-env ( body env ) + + 2swap ( env body ) + + begin + 2dup cdr 2dup nil objeq? false = + while + -2rot car 2over ( nextbody env exp env ) + eval + 2drop \ discard result + 2swap ( env nextbody ) + repeat + + 2drop ( env body ) + car 2swap ( exp env ) + + R> drop ['] eval goto-deferred \ Tail call optimization endof bold fg red ." Object not applicable. Aboring." reset-term cr @@ -988,12 +1188,13 @@ defer eval if-alternative then - 2swap ['] eval goto + 2swap + ['] eval goto-deferred then lambda? if - 2dup lambda-body - 2swap lambda-parameters + 2dup lambda-parameters + 2swap lambda-body 2rot make-procedure exit then @@ -1016,9 +1217,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 @@ -1083,7 +1284,8 @@ defer print 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 @@ -1099,18 +1301,69 @@ defer print \ }}} +\ ---- DEBUGGING ---- {{{ + +false value debug-mode + +:noname + debug-mode if + ." Object: " 2dup cr print cr + ." Pairlike: " pairlike? if + ." TRUE" + pairlike-marked? if + ." (Marked)" + else + ." (Unmarked)" + then + else + ." FALSE" + then + cr ." [paused]" + key drop cr + then +; is gc-mark-trace + +: gc-mark-sweep + gc-unmark + symbol-table obj@ gc-mark-obj + global-env obj@ gc-mark-obj + gc-sweep +; + +: gc-count-marked + 0 + scheme-memsize 0 do + nextfrees i + @ 0= if 1+ then + loop +; + +: gc-zero-unmarked + scheme-memsize 0 do + nextfrees i + @ 0<> if + 0 car-cells i + ! + 0 cdr-cells i + ! + then + loop +; + +\ }}} + + \ ---- REPL ---- : repl cr ." Welcome to scheme.forth.jl!" cr ." Use Ctrl-D to exit." cr + empty-parse-str begin cr bold fg green ." > " reset-term read + global-env obj@ eval + fg cyan ." ; " print reset-term again ;