Added predicate primitives.
authorTim Vaughan <tgvaughan@gmail.com>
Tue, 19 Jul 2016 20:14:06 +0000 (08:14 +1200)
committerTim Vaughan <tgvaughan@gmail.com>
Tue, 19 Jul 2016 20:14:20 +0000 (08:14 +1200)
scheme-primitives.4th [new file with mode: 0644]
scheme.4th

diff --git a/scheme-primitives.4th b/scheme-primitives.4th
new file mode 100644 (file)
index 0000000..3de4d37
--- /dev/null
@@ -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 *
+
index 52ce346..1953cf9 100644 (file)
@@ -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
 
 \ }}}