From: Tim Vaughan Date: Tue, 19 Jul 2016 20:14:06 +0000 (+1200) Subject: Added predicate primitives. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=dc6e0cea1e1e982da8029c94d78ee66ab7e8fd82;p=scheme.forth.jl.git Added predicate primitives. --- diff --git a/scheme-primitives.4th b/scheme-primitives.4th new file mode 100644 index 0000000..3de4d37 --- /dev/null +++ b/scheme-primitives.4th @@ -0,0 +1,86 @@ +( = Type predicates = ) + +:noname ( args -- boolobj ) + 2dup 1 ensure-arg-count + + car nil objeq? boolean-type +; make-primitive null? + +:noname ( args -- boolobj ) + 2dup 1 ensure-arg-count + + car boolean-type istype? -rot 2drop boolean-type +; make-primitive boolean? + +:noname ( args -- boolobj ) + 2dup 1 ensure-arg-count + + car symbol-type istype? -rot 2drop boolean-type +; make-primitive symbol? + +:noname ( args -- boolobj ) + 2dup 1 ensure-arg-count + + car fixnum-type istype? -rot 2drop boolean-type +; make-primitive integer? + +:noname ( args -- boolobj ) + 2dup 1 ensure-arg-count + + car character-type istype? -rot 2drop boolean-type +; make-primitive char? + +:noname ( args -- boolobj ) + 2dup 1 ensure-arg-count + + car string-type istype? -rot 2drop boolean-type +; make-primitive string? + +:noname ( args -- boolobj ) + 2dup 1 ensure-arg-count + + car pair-type istype? -rot 2drop boolean-type +; make-primitive pair? + +:noname ( args -- boolobj ) + 2dup 1 ensure-arg-count + + car primitive-type istype? -rot 2drop boolean-type +; make-primitive procedure? + +( = Arithmeic = ) + +: add-prim ( args -- fixnum ) + 2dup nil objeq? if + 2drop + 0 fixnum-type + else + 2dup car drop + -rot cdr recurse drop + + fixnum-type + then +; +' add-prim make-primitive + + +:noname ( args -- fixnum ) + 2dup nil objeq? if + 2drop + 0 fixnum-type + else + 2dup car drop + -rot cdr add-prim drop + - fixnum-type + then +; make-primitive - + +:noname ( args -- fixnum ) + 2dup nil objeq? if + 2drop + 1 fixnum-type + else + 2dup car drop + -rot cdr recurse drop + * fixnum-type + then +; make-primitive * + diff --git a/scheme.4th b/scheme.4th index 52ce346..1953cf9 100644 --- a/scheme.4th +++ b/scheme.4th @@ -277,41 +277,26 @@ global-env setobj global-env fetchobj define-var ; -( = Arithmeic = ) - -: add-prim ( args -- ) - 2dup nil objeq? if - 2drop - 0 fixnum-type - else - 2dup car drop - -rot cdr recurse drop - + fixnum-type - then +: arg-count-error + bold fg red ." Incorrect argument count." reset-term cr + abort ; -' add-prim make-primitive + -:noname ( args -- ) - 2dup nil objeq? if - 2drop - 0 fixnum-type +: ensure-arg-count ( args n -- ) + dup 0= if + drop nil objeq? false = if + arg-count-error + then else - 2dup car drop - -rot cdr add-prim drop - - fixnum-type + -rot 2dup nil objeq? if + arg-count-error + then + + cdr rot 1- recurse then -; make-primitive - +; -:noname ( args -- ) - 2dup nil objeq? if - 2drop - 1 fixnum-type - else - 2dup car drop - -rot cdr recurse drop - * fixnum-type - then -; make-primitive * +include scheme-primitives.4th \ }}}