X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=src%2Fscheme-primitives.4th;h=f94701f743c2d21222167450094cc8f6a4b8a7e2;hb=899e3f7a10cbb8ecd03aa2dfe3ad08bf4638a324;hp=928931901cfe3a387dbdf0cfeca28ce2a0d4d963;hpb=631c05ef31816dfd2c7e8ea0f19a008a1e732605;p=scheme.forth.jl.git diff --git a/src/scheme-primitives.4th b/src/scheme-primitives.4th index 9289319..f94701f 100644 --- a/src/scheme-primitives.4th +++ b/src/scheme-primitives.4th @@ -20,6 +20,10 @@ flonum-type istype? -rot 2drop boolean-type ; 1 make-fa-primitive flonum? +:noname ( args -- boolobj ) + ratnum-type istype? -rot 2drop boolean-type +; 1 make-fa-primitive ratnum? + :noname ( args -- boolobj ) character-type istype? -rot 2drop boolean-type ; 1 make-fa-primitive char? @@ -42,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 ==== {{{ @@ -149,9 +157,40 @@ 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 + \ }}} -\ ==== Primitivle Arithmetic ==== {{{ +\ ==== Numeric types ==== {{{ \ --- Fixnums --- @@ -219,10 +258,13 @@ swap negate swap ; 1 make-fa-primitive fix:neg -( Find the GCD of n1 and n2 where n2 < n1. ) -: gcd ( n1 n2 -- m ) - -; +:noname ( fixnum -- -fixnum ) + swap abs swap +; 1 make-fa-primitive fix:abs + +:noname ( fixnum fixnum -- fixnum' ) + drop swap drop gcd fixnum-type +; 2 make-fa-primitive fix:gcd \ --- Flonums --- @@ -238,6 +280,13 @@ drop swap drop f> boolean-type ; 2 make-fa-primitive flo:> +:noname ( flonum flonum -- bool ) + drop swap drop f<= boolean-type +; 2 make-fa-primitive flo:<= + +:noname ( flonum flonum -- bool ) + drop swap drop f>= boolean-type +; 2 make-fa-primitive flo:>= :noname ( flonum -- bool ) drop 0.0 = boolean-type @@ -279,6 +328,10 @@ ; 1 make-fa-primitive flo:finite? +:noname ( flonum -- flonum ) + swap -1.0 f* swap +; 1 make-fa-primitive flo:neg + :noname ( flonum -- flonum ) swap fabs swap ; 1 make-fa-primitive flo:abs @@ -327,6 +380,56 @@ swap floor swap ; 1 make-fa-primitive flo:floor +:noname ( flonum -- flonum ) + swap ceiling swap +; 1 make-fa-primitive flo:ceiling + +:noname ( flonum -- flonum ) + swap truncate swap +; 1 make-fa-primitive flo:truncate + +:noname ( flonum -- flonum ) + swap fround swap +; 1 make-fa-primitive flo:round + +:noname ( flonum -- flonum ) + drop floor f->i fixnum-type +; 1 make-fa-primitive flo:floor->exact + +:noname ( flonum -- flonum ) + drop ceiling f->i fixnum-type +; 1 make-fa-primitive flo:ceiling->exact + +:noname ( flonum -- flonum ) + drop truncate f->i fixnum-type +; 1 make-fa-primitive flo:truncate->exact + +:noname ( flonum -- flonum ) + drop f->i fixnum-type +; 1 make-fa-primitive flo:round->exact + +:noname ( flonum flonum -- flonum ) + drop swap drop f/ fatan flonum-type +; 2 make-fa-primitive flo:atan2 + +\ --- Rationals --- + +' make-rational 2 make-fa-primitive make-rational + +:noname ( ratnum -- fixnum ) + drop pair-type car +; 1 make-fa-primitive rat:numerator + +:noname ( ratnum -- fixnum ) + drop pair-type cdr +; 1 make-fa-primitive rat:denominator + +\ --- Conversion --- + +:noname ( fixnum -- flonum ) + drop i->f flonum-type +; 1 make-fa-primitive fixnum->flonum + \ }}} \ ==== Pairs and Lists ==== {{{ @@ -371,6 +474,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 @@ -442,12 +601,22 @@ defer display :noname ( args -- result ) 2dup car 2swap cdr - + nil? false = if car then ( proc argvals ) - - apply + + 2swap apply ; make-primitive apply +: make-continuation + \ TODO: Capture parameter and return stacks in continuation +; + +:noname ( args -- result ) + make-continuation nil cons + 2swap apply + +; 1 make-fa-primitive call-with-current-continuation + \ }}} \ ==== Miscellaneous ==== {{{ @@ -459,7 +628,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