factor/library/kernel.factor

116 lines
2.8 KiB
Factor
Raw Normal View History

2006-01-09 01:34:23 -05:00
! Copyright (C) 2004, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2004-12-24 02:52:02 -05:00
IN: kernel
USING: generic kernel-internals math math-internals ;
2004-12-24 02:52:02 -05:00
2005-08-07 00:00:57 -04:00
: 2swap ( x y z t -- z t x y ) rot >r rot r> ; inline
2006-08-15 21:23:05 -04:00
: clear ( -- ) V{ } set-datastack ;
2005-08-07 00:00:57 -04:00
GENERIC: hashcode ( obj -- n )
2004-12-18 23:35:20 -05:00
M: object hashcode drop 0 ;
2006-08-07 15:41:31 -04:00
GENERIC: equal? ( obj obj -- ? )
M: object equal? eq? ;
2006-08-15 21:23:05 -04:00
: = ( obj1 obj2 -- ? )
2006-08-07 15:41:31 -04:00
2dup eq? [ 2drop t ] [ equal? ] if ; inline
2004-12-18 23:35:20 -05:00
GENERIC: <=> ( obj1 obj2 -- n )
2006-01-09 01:34:23 -05:00
2006-08-15 21:23:05 -04:00
GENERIC: clone ( obj -- cloned )
2005-01-28 23:55:22 -05:00
M: object clone ;
2006-01-06 22:42:07 -05:00
: set-boot ( quot -- ) 8 setenv ;
2006-08-15 21:23:05 -04:00
: ? ( cond true false -- true/false )
rot [ drop ] [ nip ] if ; inline
2004-12-18 23:35:20 -05:00
2006-08-15 21:23:05 -04:00
: cpu ( -- cpu ) 7 getenv ; foldable
2006-03-26 21:07:23 -05:00
: os ( -- os ) 11 getenv ; foldable
2006-09-02 17:34:55 -04:00
: windows? ( -- ? ) os "windows" = ; foldable
: macosx? ( -- ? ) os "macosx" = ; foldable
2006-08-15 21:23:05 -04:00
: slip ( quot x -- x ) >r call r> ; inline
2005-08-07 00:00:57 -04:00
2006-08-15 21:23:05 -04:00
: 2slip ( quot x y -- x y ) >r >r call r> r> ; inline
2005-08-07 00:00:57 -04:00
2006-08-15 21:23:05 -04:00
: keep ( x quot -- x ) over >r call r> ; inline
2005-08-07 00:00:57 -04:00
2006-08-15 21:23:05 -04:00
: 2keep ( x y quot -- x y ) over >r pick >r call r> r> ; inline
2005-08-07 00:00:57 -04:00
2006-08-15 21:23:05 -04:00
: 3keep ( x y z quot -- x y z )
>r 3dup r> swap >r swap >r swap >r call r> r> r> ;
inline
2005-08-07 00:00:57 -04:00
2006-08-15 21:23:05 -04:00
: 2apply ( x y quot -- ) tuck 2slip call ; inline
2006-08-15 21:23:05 -04:00
: if* ( cond true false -- )
pick [ drop call ] [ 2nip call ] if ; inline
2005-08-07 00:00:57 -04:00
2006-08-15 21:23:05 -04:00
: ?if ( default cond true false -- )
>r >r [ nip r> r> drop call ] [ r> drop r> call ] if* ;
inline
2005-08-07 00:00:57 -04:00
2006-08-15 21:23:05 -04:00
: unless ( cond false -- ) [ ] swap if ; inline
2005-08-07 00:00:57 -04:00
2006-08-15 21:23:05 -04:00
: unless* ( cond false -- )
over [ drop ] [ nip call ] if ; inline
2005-08-07 00:00:57 -04:00
2006-08-15 21:23:05 -04:00
: when ( cond true -- ) [ ] if ; inline
2005-08-07 00:00:57 -04:00
2006-08-15 21:23:05 -04:00
: when* ( cond true -- ) dupd [ drop ] if ; inline
2006-07-22 17:17:21 -04:00
2006-08-15 21:23:05 -04:00
: >boolean ( obj -- ? ) t f ? ; inline
: and ( obj1 obj2 -- ? ) f ? ; inline
: or ( obj1 obj2 -- ? ) t swap ? ; inline
: xor ( obj1 obj2 -- ? ) [ not ] when ; inline
2005-08-07 00:00:57 -04:00
: with ( obj quot elt -- obj quot )
pick pick >r >r swap call r> r> ; inline
2006-08-15 21:23:05 -04:00
: keep-datastack ( quot -- )
datastack slip set-datastack drop ; inline
IN: kernel-internals
! These words are unsafe. Don't use them.
2006-08-15 21:23:05 -04:00
: declare ( spec -- ) drop ;
2006-08-15 21:23:05 -04:00
: array-capacity ( array -- n )
1 slot { fixnum } declare ; inline
: array-nth ( n array -- elt )
swap 2 fixnum+fast slot ; inline
: set-array-nth ( elt n array -- )
swap 2 fixnum+fast set-slot ; inline
2005-12-02 02:25:44 -05:00
! Some runtime implementation details
2006-08-15 21:23:05 -04:00
: num-types ( -- n ) 19 ; inline
2005-12-02 02:25:44 -05:00
: tag-mask BIN: 111 ; inline
: num-tags 8 ; inline
: tag-bits 3 ; inline
: fixnum-tag BIN: 000 ; inline
: bignum-tag BIN: 001 ; inline
2006-05-18 01:08:09 -04:00
: word-tag BIN: 010 ; inline
2005-12-02 02:25:44 -05:00
: object-tag BIN: 011 ; inline
: ratio-tag BIN: 100 ; inline
: float-tag BIN: 101 ; inline
: complex-tag BIN: 110 ; inline
2006-05-18 01:08:09 -04:00
: wrapper-tag BIN: 111 ; inline
2005-12-02 02:25:44 -05:00
2006-08-15 21:23:05 -04:00
: cell ( -- n ) 17 getenv ; foldable
2006-03-26 21:07:23 -05:00
IN: kernel
2006-09-02 17:34:55 -04:00
: win32? ( -- ? ) windows? cell 4 = and ; foldable
: win64? ( -- ? ) windows? cell 8 = and ; foldable
2006-03-27 23:03:25 -05:00
IN: memory
: generations ( -- n ) 15 getenv ;
: image ( -- path ) 16 getenv ;
: save ( -- ) image save-image ;