X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=scheme.4th;h=46c296958417263911bba06c9ca923cb34ee71d1;hb=90e92c642041f4a3174c29373d2dc4d366d746e1;hp=5c8f57dc4bdf91f878b132834784980b6b75cc64;hpb=e89a16664498692522a02e93de4493d93288748e;p=scheme.forth.jl.git diff --git a/scheme.4th b/scheme.4th index 5c8f57d..46c2969 100644 --- a/scheme.4th +++ b/scheme.4th @@ -3,23 +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 number-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-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 @@ -77,8 +92,8 @@ variable nextfree : type@ ( objvar -- type ) 1+ @ ; : value! ( newval objvar -- ) ! ; : type! ( newtype objvar -- ) 1+ ! ; -: setobj ( newobj objvar -- ) dup rot swap 1+ ! ! ; -: fetchobj ( objvar -- obj ) dup @ swap 1+ @ ; +: obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ; +: obj@ ( objvar -- obj ) dup @ swap 1+ @ ; : objeq? ( obj obj -- bool ) rot = -rot = and ; @@ -103,6 +118,66 @@ variable nextfree objvar symbol-table +: duplicate-charlist ( charlist -- copy ) + 2dup nil objeq? false = if + 2dup car 2swap cdr recurse cons + then ; + +: charlist-equiv ( charlist charlist -- bool ) + + 2over 2over + + \ One or both nil + nil? -rot 2drop + if + nil? -rot 2drop + if + 2drop 2drop true exit + else + 2drop 2drop false exit + then + else + nil? -rot 2drop + if + 2drop 2drop false exit + then + then + + 2over 2over + + \ Neither nil + car drop -rot car drop = if + cdr 2swap cdr recurse + else + 2drop 2drop false + then +; + +: charlist>symbol ( charlist -- symbol-obj ) + + symbol-table obj@ + + begin + nil? false = + while + 2over 2over + car drop pair-type + charlist-equiv if + 2swap 2drop + car + exit + else + cdr + then + repeat + + 2drop + drop symbol-type 2dup + symbol-table obj@ cons + symbol-table obj! +; + + : (create-symbol) ( addr n -- symbol-obj ) dup 0= if 2drop nil @@ -124,9 +199,9 @@ objvar symbol-table 2dup - symbol-table fetchobj + symbol-table obj@ cons - symbol-table setobj + symbol-table obj! create swap , , does> dup @ swap 1+ @ @@ -137,6 +212,8 @@ create-symbol define define-symbol create-symbol set! set!-symbol create-symbol ok ok-symbol create-symbol if if-symbol +create-symbol lambda lambda-symbol +create-symbol λ λ-symbol \ }}} @@ -175,19 +252,19 @@ objvar vars objvar vals : get-vars-vals-frame ( var frame -- bool ) - 2dup frame-vars vars setobj - frame-vals vals setobj + 2dup frame-vars vars obj! + frame-vals vals obj! begin - vars fetchobj nil objeq? false = + vars obj@ nil objeq? false = while - 2dup vars fetchobj car objeq? if + 2dup vars obj@ car objeq? if 2drop true exit then - vars fetchobj cdr vars setobj - vals fetchobj cdr vals setobj + vars obj@ cdr vars obj! + vals obj@ cdr vals obj! repeat 2drop false @@ -201,7 +278,7 @@ objvar vals 2over 2over first-frame get-vars-vals-frame if 2drop 2drop - vars fetchobj vals fetchobj true + vars obj@ vals obj@ true exit then @@ -236,15 +313,15 @@ hide vals objvar env : define-var ( var val env -- ) - env setobj + env obj! - 2over env fetchobj ( var val var env ) + 2over env obj@ ( var val var env ) get-vars-vals if 2swap 2drop ( var val vals ) set-car! 2drop else - env fetchobj + env obj@ first-frame ( var val frame ) add-binding then @@ -254,7 +331,7 @@ hide env objvar global-env nil nil nil extend-env -global-env setobj +global-env obj! \ }}} @@ -269,32 +346,50 @@ global-env setobj 2dup - symbol-table fetchobj + symbol-table obj@ cons - symbol-table setobj + symbol-table obj! - rot primitive-type ( var prim ) - global-env fetchobj define-var + rot primitive-proc-type ( var prim ) + global-env obj@ define-var ; -: add-prim ( args -- ) - nil objeq? if - 0 number-type +: 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 - 2dup cdr recurse drop - -rot car drop - + number-type + -rot 2dup nil objeq? if + arg-count-error + then + + cdr rot 1- recurse then ; -' add-prim make-primitive + +: 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 ---- {{{ -defer read - variable parse-idx variable stored-parse-idx create parse-str 161 allot @@ -357,12 +452,27 @@ parse-idx-stack parse-idx-sp ! nextchar [char] ) = or ; +: commentstart? ( -- bool ) + nextchar [char] ; = ; + : eatspaces + + false \ Indicates whether or not we're eating a comment + begin - whitespace? + dup whitespace? or commentstart? or while + dup nextchar '\n' = and if + invert \ Stop eating comment + else + dup false = commentstart? and if + invert \ Begin eating comment + then + then + inc-parse-idx repeat + drop ; : digit? ( -- bool ) @@ -373,10 +483,23 @@ parse-idx-stack parse-idx-sp ! : minus? ( -- bool ) nextchar [char] - = ; -: number? ( -- bool ) - digit? minus? or false = if - false - exit +: plus? ( -- bool ) + nextchar [char] + = ; + +: fixnum? ( -- bool ) + minus? plus? or if + inc-parse-idx + + delim? if + dec-parse-idx + false exit + else + dec-parse-idx + then + else + digit? false = if + false exit + then then push-parse-idx @@ -386,13 +509,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 ) @@ -462,9 +624,12 @@ parse-idx-stack parse-idx-sp ! : string? ( -- bool ) nextchar [char] " = ; -: readnum ( -- num-atom ) - minus? dup if +: readfixnum ( -- num-atom ) + plus? minus? or if + minus? inc-parse-idx + else + false then 0 @@ -476,7 +641,25 @@ parse-idx-stack parse-idx-sp ! swap if negate then - number-type + 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 ) @@ -548,60 +731,6 @@ parse-idx-stack parse-idx-sp ! cons ; -: charlist-equiv ( charlist charlist -- bool ) - - 2over 2over - - \ One or both nil - nil? -rot 2drop - if - nil? -rot 2drop - if - 2drop 2drop true exit - else - 2drop 2drop false exit - then - else - nil? -rot 2drop - if - 2drop 2drop false exit - then - then - - 2over 2over - - \ Neither nil - car drop -rot car drop = if - cdr 2swap cdr recurse - else - 2drop 2drop false - then -; - -: charlist>symbol ( charlist -- symbol-obj ) - - symbol-table fetchobj - - begin - nil? false = - while - 2over 2over - car drop pair-type - charlist-equiv if - 2swap 2drop - car - exit - else - cdr - then - repeat - - 2drop - drop symbol-type 2dup - symbol-table fetchobj cons - symbol-table setobj -; - : readpair ( -- pairobj ) eatspaces @@ -649,8 +778,13 @@ parse-idx-stack parse-idx-sp ! eatspaces - number? if - readnum + fixnum? if + readfixnum + exit + then + + realnum? if + readrealnum exit then @@ -705,6 +839,7 @@ parse-idx-stack parse-idx-sp ! then eof? if + inc-parse-idx bold fg blue ." Moriturus te saluto." reset-term ." ok" cr quit then @@ -712,17 +847,22 @@ parse-idx-stack parse-idx-sp ! \ Anything else is parsed as a symbol readsymbol charlist>symbol + \ Replace λ with lambda + 2dup λ-symbol objeq? if + 2drop lambda-symbol + then + + ; is read \ }}} \ ---- Eval ---- {{{ -defer eval - : self-evaluating? ( obj -- obj bool ) boolean-type istype? if true exit then - number-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 @@ -750,11 +890,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? ; @@ -821,7 +973,22 @@ defer eval : true? ( boolobj -- bool ) false? invert ; -: applicaion? ( obj -- obj bool) +: lambda? ( obj -- obj bool ) + lambda-symbol tagged-list? ; + +: lambda-parameters ( obj -- params ) + cdr car ; + +: lambda-body ( obj -- body ) + cdr cdr ; + +: make-procedure ( params body env -- proc ) + nil + cons cons cons + drop compound-proc-type +; + +: application? ( obj -- obj bool) pair-type istype? ; : operator ( obj -- operator ) @@ -831,7 +998,7 @@ defer eval cdr ; : nooperands? ( operands -- bool ) - cdr nil objeq? ; + nil objeq? ; : first-operand ( operands -- operand ) car ; @@ -840,7 +1007,59 @@ defer eval cdr ; : list-of-vals ( args env -- vals ) + 2swap + 2dup nooperands? if + 2swap 2drop + else + 2over 2over first-operand 2swap eval + -2rot rest-operands 2swap recurse + cons + 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 + drop execute + endof + + compound-proc-type of + 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 + abort + endcase ; :noname ( obj env -- result ) @@ -883,22 +1102,24 @@ defer eval if-alternative then - 2swap ['] eval goto + 2swap + ['] eval goto-deferred + then + + lambda? if + 2dup lambda-parameters + 2swap lambda-body + 2rot make-procedure + exit then application? if 2over 2over operator 2swap eval - - primitive-type istype? false = if - bold fg red ." Object not applicable. Aboring." reset-term cr - abort - then - -2rot operands 2swap list-of-vals - 2swap drop execute + apply exit then @@ -910,9 +1131,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 @@ -973,15 +1194,20 @@ defer print : printprim ( primobj -- ) 2drop ." " ; +: printcomp ( primobj -- ) + 2drop ." " ; + :noname ( obj -- ) - number-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 symbol-type istype? if printsymbol exit then nil-type istype? if printnil exit then pair-type istype? if ." (" printpair ." )" exit then - primitive-type istype? if printprim exit then + primitive-proc-type istype? if printprim exit then + compound-proc-type istype? if printcomp exit then bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr abort @@ -1000,7 +1226,7 @@ defer print begin cr bold fg green ." > " reset-term read - global-env fetchobj eval + global-env obj@ eval fg cyan ." ; " print reset-term again ;