factor/core/kernel/kernel.factor

258 lines
5.6 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2009 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: kernel.private slots.private math.private ;
2007-09-20 18:09:08 -04:00
IN: kernel
DEFER: dip
DEFER: 2dip
DEFER: 3dip
2007-09-20 18:09:08 -04:00
! Stack stuff
2007-12-29 11:36:20 -05:00
: spin ( x y z -- z y x ) swap rot ; inline
: roll ( x y z t -- y z t x ) [ rot ] dip swap ; inline
2007-09-20 18:09:08 -04:00
: -roll ( x y z t -- t x y z ) swap [ -rot ] dip ; inline
2007-09-20 18:09:08 -04:00
2008-01-11 17:02:44 -05:00
: 2over ( x y z -- x y z x y ) pick pick ; inline
2007-09-20 18:09:08 -04:00
: clear ( -- ) { } set-datastack ;
! Combinators
GENERIC: call ( callable -- )
2007-09-20 18:09:08 -04:00
2009-03-16 21:11:36 -04:00
GENERIC: execute ( word -- )
GENERIC: ?execute ( word -- value )
M: object ?execute ;
2007-09-20 18:09:08 -04:00
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
#! two literal quotations.
rot [ drop ] [ nip ] if ; inline
: if ( ? true false -- ) ? call ;
2008-03-29 00:00:20 -04:00
! Single branch
: unless ( ? false -- )
2007-09-20 18:09:08 -04:00
swap [ drop ] [ call ] if ; inline
: when ( ? true -- )
2007-09-20 18:09:08 -04:00
swap [ call ] [ drop ] if ; inline
2008-03-29 00:00:20 -04:00
! Anaphoric
: if* ( ? true false -- )
2008-03-29 00:00:20 -04:00
pick [ drop call ] [ 2nip call ] if ; inline
: when* ( ? true -- )
2007-09-20 18:09:08 -04:00
over [ call ] [ 2drop ] if ; inline
: unless* ( ? false -- )
2008-03-29 00:00:20 -04:00
over [ drop ] [ nip call ] if ; inline
! Default
: ?if ( default cond true false -- )
pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
2008-03-29 00:00:20 -04:00
2008-12-04 07:02:49 -05:00
! Slippers and dippers.
! Not declared inline because the compiler special-cases them
2008-11-23 03:46:43 -05:00
: slip ( quot x -- x )
#! 'slip' and 'dip' can be defined in terms of each other
#! because the JIT special-cases a 'dip' preceeded by
#! a literal quotation.
[ call ] dip ;
: 2slip ( quot x y -- x y )
#! '2slip' and '2dip' can be defined in terms of each other
#! because the JIT special-cases a '2dip' preceeded by
#! a literal quotation.
[ call ] 2dip ;
: 3slip ( quot x y z -- x y z )
#! '3slip' and '3dip' can be defined in terms of each other
#! because the JIT special-cases a '3dip' preceeded by
#! a literal quotation.
[ call ] 3dip ;
2007-09-20 18:09:08 -04:00
2008-12-04 07:02:49 -05:00
: dip ( x quot -- x ) swap slip ;
2007-09-20 18:09:08 -04:00
2008-12-04 07:02:49 -05:00
: 2dip ( x y quot -- x y ) -rot 2slip ;
2008-05-22 16:45:30 -04:00
2008-12-04 07:02:49 -05:00
: 3dip ( x y z quot -- x y z ) -roll 3slip ;
2008-10-20 02:58:17 -04:00
: 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
2008-03-29 00:00:20 -04:00
! Keepers
: keep ( x quot -- x ) over slip ; inline
2007-09-20 18:09:08 -04:00
: 2keep ( x y quot -- x y ) [ 2dup ] dip 2dip ; inline
2007-09-20 18:09:08 -04:00
: 3keep ( x y z quot -- x y z ) [ 3dup ] dip 3dip ; inline
2007-09-20 18:09:08 -04:00
2008-03-29 00:00:20 -04:00
! Cleavers
2008-03-30 00:11:45 -04:00
: bi ( x p q -- )
[ keep ] dip call ; inline
2008-03-29 00:00:20 -04:00
2008-03-30 00:11:45 -04:00
: tri ( x p q r -- )
[ [ keep ] dip keep ] dip call ; inline
2008-03-29 00:00:20 -04:00
! Double cleavers
2008-03-30 00:11:45 -04:00
: 2bi ( x y p q -- )
[ 2keep ] dip call ; inline
2008-03-29 00:00:20 -04:00
2008-03-30 00:11:45 -04:00
: 2tri ( x y p q r -- )
[ [ 2keep ] dip 2keep ] dip call ; inline
2008-03-29 00:00:20 -04:00
! Triple cleavers
2008-03-30 00:11:45 -04:00
: 3bi ( x y z p q -- )
[ 3keep ] dip call ; inline
2008-03-29 00:00:20 -04:00
2008-03-30 00:11:45 -04:00
: 3tri ( x y z p q r -- )
[ [ 3keep ] dip 3keep ] dip call ; inline
2008-03-29 00:00:20 -04:00
! Spreaders
2008-03-30 00:11:45 -04:00
: bi* ( x y p q -- )
[ dip ] dip call ; inline
2008-03-29 00:00:20 -04:00
2008-03-30 00:11:45 -04:00
: tri* ( x y z p q r -- )
[ [ 2dip ] dip dip ] dip call ; inline
2008-03-29 00:00:20 -04:00
! Double spreaders
2008-03-30 00:11:45 -04:00
: 2bi* ( w x y z p q -- )
[ 2dip ] dip call ; inline
2008-03-29 00:00:20 -04:00
: 2tri* ( u v w x y z p q r -- )
[ 4dip ] 2dip 2bi* ; inline
2008-03-29 00:00:20 -04:00
! Appliers
2008-03-30 01:40:43 -04:00
: bi@ ( x y quot -- )
dup bi* ; inline
2008-03-29 00:00:20 -04:00
2008-03-30 01:40:43 -04:00
: tri@ ( x y z quot -- )
dup dup tri* ; inline
2008-03-29 00:00:20 -04:00
! Double appliers
2008-03-30 01:40:43 -04:00
: 2bi@ ( w x y z quot -- )
dup 2bi* ; inline
2007-09-20 18:09:08 -04:00
: 2tri@ ( u v w y x z quot -- )
dup dup 2tri* ; inline
2008-03-26 04:57:48 -04:00
! Quotation building
: 2curry ( obj1 obj2 quot -- curry )
curry curry ; inline
: 3curry ( obj1 obj2 obj3 quot -- curry )
curry curry curry ; inline
: with ( param obj quot -- obj curry )
swapd [ swapd call ] 2curry ; inline
2008-05-11 18:42:48 -04:00
: prepose ( quot1 quot2 -- compose )
swap compose ; inline
! Curried cleavers
<PRIVATE
: [curry] ( quot -- quot' ) [ curry ] curry ; inline
PRIVATE>
: bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
: tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
: bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
: tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
: bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
: tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
2008-03-26 04:57:48 -04:00
! Booleans
: not ( obj -- ? ) [ f ] [ t ] if ; inline
2007-09-20 18:09:08 -04:00
2008-07-25 03:07:45 -04:00
: and ( obj1 obj2 -- ? ) over ? ; inline
2007-09-20 18:09:08 -04:00
: >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
2007-09-20 18:09:08 -04:00
: or ( obj1 obj2 -- ? ) dupd ? ; inline
2008-08-01 18:22:58 -04:00
: xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
2007-09-20 18:09:08 -04:00
2008-03-29 00:00:20 -04:00
: both? ( x y quot -- ? ) bi@ and ; inline
2007-09-20 18:09:08 -04:00
2008-03-29 00:00:20 -04:00
: either? ( x y quot -- ? ) bi@ or ; inline
2007-09-20 18:09:08 -04:00
2009-02-17 19:56:54 -05:00
: most ( x y quot -- z ) 2keep ? ; inline
2007-09-20 18:09:08 -04:00
! Loops
: loop ( pred: ( -- ? ) -- )
[ call ] keep [ loop ] curry when ; inline recursive
: do ( pred body -- pred body )
dup 2dip ; inline
: while ( pred: ( -- ? ) body: ( -- ) -- )
swap do compose [ loop ] curry when ; inline
: until ( pred: ( -- ? ) body: ( -- ) -- )
[ [ not ] compose ] dip while ; inline
! Object protocol
GENERIC: hashcode* ( depth obj -- code )
M: object hashcode* 2drop 0 ;
M: f hashcode* 2drop 31337 ;
: hashcode ( obj -- code ) 3 swap hashcode* ; inline
GENERIC: equal? ( obj1 obj2 -- ? )
M: object equal? 2drop f ;
TUPLE: identity-tuple ;
M: identity-tuple equal? 2drop f ;
: = ( obj1 obj2 -- ? )
2dup eq? [ 2drop t ] [
2dup both-fixnums? [ 2drop f ] [ equal? ] if
] if ; inline
GENERIC: clone ( obj -- cloned )
M: object clone ;
M: callstack clone (clone) ;
! Tuple construction
GENERIC: new ( class -- tuple )
GENERIC: boa ( ... class -- tuple )
2007-09-20 18:09:08 -04:00
! Error handling -- defined early so that other files can
! throw errors before continuations are loaded
2009-03-16 21:11:36 -04:00
GENERIC: throw ( error -- * )
2007-09-20 18:09:08 -04:00
2008-07-28 23:03:13 -04:00
ERROR: assert got expect ;
: assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
2007-09-20 18:09:08 -04:00
<PRIVATE
: declare ( spec -- ) drop ;
2008-10-11 14:58:41 -04:00
: hi-tag ( obj -- n ) { hi-tag } declare 0 slot ; inline
2008-01-02 19:36:36 -05:00
: do-primitive ( number -- ) "Improper primitive call" throw ;
2007-09-20 18:09:08 -04:00
PRIVATE>