]> gitweb.factorcode.org Git - factor.git/blobdiff - core/kernel/kernel.factor
kernel: move recusrive-hashcode to math and add test
[factor.git] / core / kernel / kernel.factor
index c38206198c8178fe2bda8f999a90956f8801eb75..e5389f401f2117e1cd302362ac99cdea1923d769 100644 (file)
@@ -1,6 +1,8 @@
 ! Copyright (C) 2004, 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: kernel.private slots.private math.private ;
+USE: slots.private
+USE: kernel.private
+USE: math.private
 IN: kernel
 
 BUILTIN: callstack ;
@@ -57,7 +59,6 @@ PRIMITIVE: signal-handler ( -- )
 PRIMITIVE: special-object ( n -- obj )
 PRIMITIVE: strip-stack-traces ( -- )
 PRIMITIVE: tag ( object -- n )
-PRIMITIVE: unimplemented ( -- * )
 PRIMITIVE: unwind-native-frames ( -- )
 PRIVATE>
 
@@ -79,7 +80,7 @@ DEFER: if
 
 : ? ( ? true false -- true/false )
     ! 'if' and '?' can be defined in terms of each other
-    ! because the JIT special-cases an 'if' preceeded by
+    ! because the JIT special-cases an 'if' preceded by
     ! two literal quotations.
     rot [ drop ] [ nip ] if ; inline
 
@@ -117,6 +118,39 @@ DEFER: if
 
 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
 
+! Misfits
+: tuck ( x y -- y x y ) dup -rot ; inline
+
+: spin ( x y z -- z y x ) -rot swap ; inline
+
+: rotd ( w x y z -- x y w z ) [ rot ] dip ; inline
+
+: -rotd ( w x y z -- y w x z ) [ -rot ] dip ; inline
+
+: roll ( w x y z -- x y z w ) rotd swap ; inline
+
+: -roll ( w x y z -- z w x y ) swap -rotd ; inline
+
+: nipd ( x y z -- y z ) [ nip ] dip ; inline
+
+: overd ( x y z -- x y x z ) [ over ] dip ; inline
+
+: pickd ( w x y z -- w x y w z ) [ pick ] dip ; inline
+
+: 2nipd ( w x y z -- y z ) [ 2drop ] 2dip ; inline
+
+: 3nipd ( v w x y z -- y z ) [ 3drop ] 2dip ; inline
+
+: 3nip ( w x y z -- z ) 2nip nip ; inline
+
+: 4nip ( v w x y z -- z ) 2nip 2nip ; inline
+
+: 5nip ( u v w x y z -- z ) 3nip 2nip ; inline
+
+: 5drop ( v w x y z -- ) 4drop drop ; inline
+
+: reach ( w x y z -- w x y z w ) [ pick ] dip swap ; inline
+
 ! Keepers
 : keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
     over [ call ] dip ; inline
@@ -130,6 +164,15 @@ DEFER: if
 : 4keep ( ..a w x y z quot: ( ..a w x y z -- ..b ) -- ..b w x y z )
     [ 4dup ] dip 4dip ; inline
 
+: keepd ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x )
+    2keep drop ; inline
+
+: keepdd ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x )
+    3keep 2drop ; inline
+
+: 2keepd ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y )
+    3keep drop ; inline
+
 ! Cleavers
 : bi ( x p q -- )
     [ keep ] dip call ; inline
@@ -180,39 +223,42 @@ DEFER: if
     dup dup 2tri* ; inline
 
 ! Quotation building
-: 2curry ( obj1 obj2 quot -- curry )
+: 2curry ( obj1 obj2 quot -- curried )
     curry curry ; inline
 
-: 3curry ( obj1 obj2 obj3 quot -- curry )
+: 3curry ( obj1 obj2 obj3 quot -- curried )
     curry curry curry ; inline
 
-: with ( param obj quot -- obj curry )
+: with ( param obj quot -- obj curried )
     swapd [ swapd call ] 2curry ; inline
 
-: 2with ( param1 param2 obj quot -- obj curry )
+: 2with ( param1 param2 obj quot -- obj curried )
     with with ; inline
 
-: prepose ( quot1 quot2 -- compose )
+: withd ( param obj quot -- obj curried )
+    swapd [ -rotd call ] 2curry ; inline
+
+: prepose ( quot1 quot2 -- composed )
     swap compose ; inline
 
 ! Curried cleavers
 <PRIVATE
 
-: [curry] ( quot -- quot' ) [ curry ] curry ; inline
+: currier ( quot -- quot' ) [ curry ] curry ; inline
 
 PRIVATE>
 
-: bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
+: bi-curry ( x p q -- p' q' ) [ currier ] bi@ bi ; inline
 
-: tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
+: tri-curry ( x p q r -- p' q' r' ) [ currier ] tri@ tri ; inline
 
-: bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
+: bi-curry* ( x y p q -- p' q' ) [ currier ] bi@ bi* ; inline
 
-: tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
+: tri-curry* ( x y z p q r -- p' q' r' ) [ currier ] tri@ tri* ; inline
 
-: bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
+: bi-curry@ ( x y q -- p' q' ) currier bi@ ; inline
 
-: tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
+: tri-curry@ ( x y z q -- p' q' r' ) currier tri@ ; inline
 
 ! Booleans
 UNION: boolean POSTPONE: t POSTPONE: f ;
@@ -233,6 +279,8 @@ UNION: boolean POSTPONE: t POSTPONE: f ;
 
 : most ( x y quot -- z ) 2keep ? ; inline
 
+: negate ( quot -- quot' ) [ not ] compose ; inline
+
 ! Loops
 : loop ( ... pred: ( ... -- ... ? ) -- ... )
     [ call ] keep [ loop ] curry when ; inline recursive
@@ -243,11 +291,14 @@ UNION: boolean POSTPONE: t POSTPONE: f ;
 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
     swap do compose [ loop ] curry when ; inline
 
+: while* ( ..a pred: ( ..a -- ..b ? ) body: ( ..b ? -- ..a ) -- ..b )
+    [ [ dup ] compose ] dip while drop ; inline
+
 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
-    [ [ not ] compose ] dip while ; inline
+    [ negate ] dip while ; inline
 
 ! Object protocol
-GENERIC: hashcode* ( depth obj -- code )
+GENERIC: hashcode* ( depth obj -- code ) flushable
 
 M: object hashcode* 2drop 0 ; inline
 
@@ -255,6 +306,14 @@ M: f hashcode* 2drop 31337 ; inline
 
 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
 
+GENERIC: equal? ( obj1 obj2 -- ? )
+
+M: object equal? 2drop f ; inline
+
+TUPLE: identity-tuple ;
+
+M: identity-tuple equal? 2drop f ; inline
+
 : identity-hashcode ( obj -- code )
     dup tag 0 eq? [
         dup tag 1 eq? [ drop 0 ] [
@@ -265,14 +324,6 @@ M: f hashcode* 2drop 31337 ; inline
         ] if
     ] unless ; inline
 
-GENERIC: equal? ( obj1 obj2 -- ? )
-
-M: object equal? 2drop f ; inline
-
-TUPLE: identity-tuple ;
-
-M: identity-tuple equal? 2drop f ; inline
-
 M: identity-tuple hashcode* nip identity-hashcode ; inline
 
 : = ( obj1 obj2 -- ? )
@@ -309,15 +360,13 @@ ERROR: assert got expect ;
 
 ! Special object count and identifiers must be kept in sync with:
 !   vm/objects.hpp
-!   basis/bootstrap/image/image.factor
-CONSTANT: special-object-count 80
+CONSTANT: special-object-count 85
 
 CONSTANT: OBJ-WALKER-HOOK 3
 
 CONSTANT: OBJ-CALLCC-1 4
 
 CONSTANT: ERROR-HANDLER-QUOT 5
-CONSTANT: OBJ-ERROR 6
 
 CONSTANT: OBJ-CELL-SIZE 7
 CONSTANT: OBJ-CPU 8
@@ -350,7 +399,7 @@ CONSTANT: JIT-SAFEPOINT 30
 CONSTANT: JIT-EPILOG 31
 CONSTANT: JIT-RETURN 32
 CONSTANT: JIT-UNUSED 33
-CONSTANT: JIT-PUSH-IMMEDIATE 34
+CONSTANT: JIT-PUSH-LITERAL 34
 CONSTANT: JIT-DIP-WORD 35
 CONSTANT: JIT-DIP 36
 CONSTANT: JIT-2DIP-WORD 37
@@ -369,6 +418,8 @@ CONSTANT: SIGNAL-HANDLER-WORD 48
 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 49
 CONSTANT: WIN-EXCEPTION-HANDLER 50
 
+CONSTANT: OBJ-SAMPLE-CALLSTACKS 51
+
 CONSTANT: REDEFINITION-COUNTER 52
 
 CONSTANT: CALLBACK-STUB 53
@@ -409,6 +460,12 @@ CONSTANT: OBJ-VM-COMPILE-TIME 75
 CONSTANT: OBJ-VM-VERSION 76
 CONSTANT: OBJ-VM-GIT-LABEL 77
 
+CONSTANT: OBJ-CANONICAL-TRUE 78
+
+CONSTANT: OBJ-BIGNUM-ZERO 79
+CONSTANT: OBJ-BIGNUM-POS-ONE 80
+CONSTANT: OBJ-BIGNUM-NEG-ONE 81
+
 ! Context object count and identifiers must be kept in sync with:
 !   vm/contexts.hpp
 
@@ -423,7 +480,10 @@ CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3
 !   basis/debugger/debugger.factor
 !   vm/errors.hpp
 
-CONSTANT: kernel-error-count 21
+! VM adds this to kernel errors, so that user-space can identify them.
+CONSTANT: KERNEL-ERROR 0xfac7
+
+CONSTANT: kernel-error-count 20
 
 CONSTANT: ERROR-EXPIRED 0
 CONSTANT: ERROR-IO      1
@@ -433,19 +493,18 @@ CONSTANT: ERROR-DIVIDE-BY-ZERO 4
 CONSTANT: ERROR-SIGNAL 5
 CONSTANT: ERROR-ARRAY-SIZE 6
 CONSTANT: ERROR-OUT-OF-FIXNUM-RANGE 7
-CONSTANT: ERROR-C-STRING 8
-CONSTANT: ERROR-FFI 9
-CONSTANT: ERROR-UNDEFINED-SYMBOL 10
-CONSTANT: ERROR-DATASTACK-UNDERFLOW 11
-CONSTANT: ERROR-DATASTACK-OVERFLOW 12
-CONSTANT: ERROR-RETAINSTACK-UNDERFLOW 13
-CONSTANT: ERROR-RETAINSTACK-OVERFLOW 14
-CONSTANT: ERROR-CALLSTACK-UNDERFLOW 15
-CONSTANT: ERROR-CALLSTACK-OVERFLOW 16
-CONSTANT: ERROR-MEMORY 17
-CONSTANT: ERROR-FP-TRAP 18
-CONSTANT: ERROR-INTERRUPT 19
-CONSTANT: ERROR-CALLBACK-SPACE-OVERFLOW 20
+CONSTANT: ERROR-FFI 8
+CONSTANT: ERROR-UNDEFINED-SYMBOL 9
+CONSTANT: ERROR-DATASTACK-UNDERFLOW 10
+CONSTANT: ERROR-DATASTACK-OVERFLOW 11
+CONSTANT: ERROR-RETAINSTACK-UNDERFLOW 12
+CONSTANT: ERROR-RETAINSTACK-OVERFLOW 13
+CONSTANT: ERROR-CALLSTACK-UNDERFLOW 14
+CONSTANT: ERROR-CALLSTACK-OVERFLOW 15
+CONSTANT: ERROR-MEMORY 16
+CONSTANT: ERROR-FP-TRAP 17
+CONSTANT: ERROR-INTERRUPT 18
+CONSTANT: ERROR-CALLBACK-SPACE-OVERFLOW 19
 
 PRIMITIVE: callstack-for ( context -- array )
 PRIMITIVE: retainstack-for ( context -- array )