From: Tim Vaughan Date: Thu, 21 Jul 2016 08:29:22 +0000 (+1200) Subject: Added more primitives. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=8a9d83b586844a04f7ee19f98b56f460063bdcad;p=scheme.forth.jl.git Added more primitives. --- diff --git a/scheme-primitives.4th b/scheme-primitives.4th index b268051..c99a0c0 100644 --- a/scheme-primitives.4th +++ b/scheme-primitives.4th @@ -130,6 +130,24 @@ fixnum-type ; make-primitive string->number +:noname ( args -- string ) + 2dup 1 ensure-arg-count + car symbol-type ensure-arg-type + + drop pair-type + duplicate-charlist + drop string-type +; make-primitive symbol->string + +:noname ( args -- symbol ) + 2dup 1 ensure-arg-count + car string-type ensure-arg-type + + drop pair-type + duplicate-charlist + charlist>symbol +; make-primitive string->symbol + ( = Arithmetic = ) : add-prim ( args -- fixnum ) @@ -150,8 +168,14 @@ 0 fixnum-type else 2dup car drop - -rot cdr add-prim drop - - fixnum-type + -rot cdr + 2dup nil objeq? if + 2drop negate + else + add-prim drop + - + then + fixnum-type then ; make-primitive - @@ -166,3 +190,24 @@ then ; make-primitive * +:noname ( args -- fixnum ) + 2dup 2 ensure-arg-count + + 2dup car fixnum-type ensure-arg-type + 2swap cdr car fixnum-type ensure-arg-type + + drop swap drop + + / fixnum-type +; make-primitive quotient + +:noname ( args -- fixnum ) + 2dup 2 ensure-arg-count + + 2dup car fixnum-type ensure-arg-type + 2swap cdr car fixnum-type ensure-arg-type + + drop swap drop + + mod fixnum-type +; make-primitive remainder diff --git a/scheme.4th b/scheme.4th index 72897be..9d86461 100644 --- a/scheme.4th +++ b/scheme.4th @@ -103,6 +103,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 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 +; + + : (create-symbol) ( addr n -- symbol-obj ) dup 0= if 2drop nil @@ -599,60 +659,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