Added more primitives.
authorTim Vaughan <tgvaughan@gmail.com>
Thu, 21 Jul 2016 08:29:22 +0000 (20:29 +1200)
committerTim Vaughan <tgvaughan@gmail.com>
Thu, 21 Jul 2016 08:29:22 +0000 (20:29 +1200)
scheme-primitives.4th
scheme.4th

index b268051..c99a0c0 100644 (file)
     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 )
         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 -
 
     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
index 72897be..9d86461 100644 (file)
@@ -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