X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=src%2Fscheme-primitives.4th;h=183efec11895d274e57b5d6ce5cfc39fbc1f7634;hb=6cb6a8d3e4449a1cf70ac4cbb0b88cf2c38d6434;hp=ba48b9e365a0f99ae7af8a6e241eecabb3d2e9d0;hpb=3055ae2e3d57ec91a4187a6c5cf8f05357d60810;p=scheme.forth.jl.git diff --git a/src/scheme-primitives.4th b/src/scheme-primitives.4th index ba48b9e..183efec 100644 --- a/src/scheme-primitives.4th +++ b/src/scheme-primitives.4th @@ -46,6 +46,10 @@ -rot 2drop boolean-type ; 1 make-fa-primitive procedure? +:noname ( args -- boolobj ) + port-type istype? -rot 2drop boolean-type +; 1 make-fa-primitive port? + \ }}} \ ==== Type conversions ==== {{{ @@ -153,6 +157,37 @@ charlist>symbol ; make-primitive string->symbol +:noname ( charlist -- string ) + 2dup 1 ensure-arg-count + + car nil? if + 2drop + nil nil cons + drop string-type + exit + then + + pair-type ensure-arg-type + + duplicate-charlist + drop string-type +; make-primitive list->string + +:noname ( string -- charlist ) + 2dup 1 ensure-arg-count + car string-type ensure-arg-type + + drop pair-type + + 2dup car nil? if + 2swap 2drop + else + 2drop + duplicate-charlist + then + +; make-primitive string->list + \ }}} \ ==== Numeric types ==== {{{ @@ -432,6 +467,62 @@ \ ==== Input/Output ==== {{{ +:noname ( -- port ) + console-i/o-port obj@ +; 0 make-fa-primitive console-i/o-port + +:noname ( -- port ) + current-input-port obj@ +; 0 make-fa-primitive current-input-port + +:noname ( args -- charobj ) + nil? if + 2drop current-input-port obj@ + else + car port-type ensure-arg-type + then + + read-char +; make-primitive read-char + +:noname ( args -- charobj ) + nil? if + 2drop current-input-port obj@ + else + car port-type ensure-arg-type + then + + peek-char +; make-primitive peek-char + +:noname ( args -- stringobj ) + nil? if + 2drop current-input-port obj@ + else + car port-type ensure-arg-type + then + + read-line +; make-primitive read-line + +: charlist>cstr ( charlist addr -- n ) + + dup 2swap ( origaddr addr charlist ) + + begin + nil? 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 - +; + :noname ( args -- finalResult ) drop pair-type pad charlist>cstr @@ -501,13 +592,13 @@ defer display \ ==== Evaluation ==== {{{ -:noname ( args -- result ) - 2dup car 2swap cdr - - nil? false = if car then ( proc argvals ) - - apply -; make-primitive apply +\ :noname ( args -- result ) +\ 2dup car 2swap cdr +\ +\ nil? false = if car then ( proc argvals ) +\ +\ apply +\ ; make-primitive apply \ }}} @@ -520,7 +611,17 @@ defer display nil? if ." Error." else - ." Error: " car display + ." Error:" + + 2dup car space display + cdr nil? invert if + begin + 2dup car space print + cdr nil? + until + then + + 2drop then reset-term