X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=scheme.4th;h=8bc44d3ad9693c76273660e20f42cac8b5ac9941;hb=215a272cf2521c34e9f8d2be62db1a35ac0a9809;hp=19848b60409e957933ac482f72e87dc803f7a6b2;hpb=af4ada618d8d096645f783f40508a1bd6ad0d95d;p=scheme.forth.jl.git diff --git a/scheme.4th b/scheme.4th index 19848b6..8bc44d3 100644 --- a/scheme.4th +++ b/scheme.4th @@ -3,6 +3,11 @@ scheme definitions include term-colours.4th include defer-is.4th +include catch-throw.4th + +defer read +defer eval +defer print \ ------ Types ------ @@ -13,13 +18,14 @@ include defer-is.4th 4 constant nil-type 5 constant pair-type 6 constant symbol-type -7 constant primitive-type +7 constant primitive-proc-type +8 constant 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 +83,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 +109,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 +190,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 +203,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 +243,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 +269,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 +304,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 +322,7 @@ hide env objvar global-env nil nil nil extend-env -global-env setobj +global-env obj! \ }}} @@ -269,12 +337,12 @@ 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 ; : arg-count-error @@ -313,8 +381,6 @@ include scheme-primitives.4th \ ---- Read ---- {{{ -defer read - variable parse-idx variable stored-parse-idx create parse-str 161 allot @@ -377,12 +443,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 ) @@ -584,60 +665,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 @@ -741,6 +768,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 @@ -748,14 +776,18 @@ 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 fixnum-type istype? if true exit then @@ -786,11 +818,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? ; @@ -857,6 +901,21 @@ defer eval : true? ( boolobj -- bool ) false? invert ; +: 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? ; @@ -887,6 +946,50 @@ 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 + 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 ) 2swap @@ -927,22 +1030,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 @@ -954,8 +1059,6 @@ defer eval \ ---- Print ---- {{{ -defer print - : printnum ( numobj -- ) drop 0 .R ; : printbool ( numobj -- ) @@ -1017,6 +1120,9 @@ defer print : printprim ( primobj -- ) 2drop ." " ; +: printcomp ( primobj -- ) + 2drop ." " ; + :noname ( obj -- ) fixnum-type istype? if printnum exit then boolean-type istype? if printbool exit then @@ -1025,7 +1131,8 @@ defer print 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 @@ -1044,7 +1151,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 ;