2005-01-28 23:55:22 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
2005-01-29 14:18:28 -05:00
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-12-24 02:52:02 -05:00
|
|
|
IN: kernel
|
2005-09-11 20:46:55 -04:00
|
|
|
USING: generic kernel-internals math-internals vectors ;
|
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
|
|
|
|
|
|
|
|
: clear ( -- )
|
|
|
|
#! Clear the datastack. For interactive use only; invoking
|
|
|
|
#! this from a word definition will clobber any values left
|
|
|
|
#! on the data stack by the caller.
|
|
|
|
{ } set-datastack ;
|
|
|
|
|
2005-08-19 22:22:15 -04:00
|
|
|
GENERIC: hashcode ( obj -- n ) flushable
|
2004-12-18 23:35:20 -05:00
|
|
|
M: object hashcode drop 0 ;
|
|
|
|
|
2005-08-19 22:22:15 -04:00
|
|
|
GENERIC: = ( obj obj -- ? ) flushable
|
2004-12-18 23:35:20 -05:00
|
|
|
M: object = eq? ;
|
|
|
|
|
2005-08-19 22:22:15 -04:00
|
|
|
GENERIC: clone ( obj -- obj ) flushable
|
2005-01-28 23:55:22 -05:00
|
|
|
M: object clone ;
|
2005-05-06 19:49:07 -04:00
|
|
|
|
2004-10-09 15:14:49 -04:00
|
|
|
: set-boot ( quot -- )
|
|
|
|
#! Set the boot quotation.
|
|
|
|
8 setenv ;
|
2004-12-15 16:57:29 -05:00
|
|
|
|
|
|
|
: num-types ( -- n )
|
|
|
|
#! One more than the maximum value from type primitive.
|
2005-09-09 17:32:38 -04:00
|
|
|
20 ; inline
|
2004-12-18 23:18:32 -05:00
|
|
|
|
2004-12-18 23:35:20 -05:00
|
|
|
: ? ( cond t f -- t/f )
|
|
|
|
#! Push t if cond is true, otherwise push f.
|
|
|
|
rot [ drop ] [ nip ] ifte ; inline
|
|
|
|
|
2005-08-03 23:56:28 -04:00
|
|
|
M: wrapper = ( obj wrapper -- ? )
|
2005-08-12 18:02:03 -04:00
|
|
|
over wrapper?
|
|
|
|
[ swap wrapped swap wrapped = ] [ 2drop f ] ifte ;
|
2005-08-03 23:56:28 -04:00
|
|
|
|
2005-05-14 17:18:45 -04:00
|
|
|
: >boolean t f ? ; inline
|
2004-12-18 23:35:20 -05:00
|
|
|
: and ( a b -- a&b ) f ? ; inline
|
2005-01-13 14:41:08 -05:00
|
|
|
: or ( a b -- a|b ) t swap ? ; inline
|
2005-04-27 01:47:57 -04:00
|
|
|
|
|
|
|
: cpu ( -- arch ) 7 getenv ;
|
|
|
|
: os ( -- os ) 11 getenv ;
|
|
|
|
: win32? ( -- ? ) os "win32" = ;
|
2005-04-30 00:43:39 -04:00
|
|
|
: unix? ( -- ? )
|
|
|
|
os "freebsd" =
|
|
|
|
os "linux" = or
|
|
|
|
os "macosx" = or ;
|
2005-05-06 19:49:07 -04:00
|
|
|
|
|
|
|
: tag-mask BIN: 111 ; inline
|
2005-08-15 03:25:39 -04:00
|
|
|
: num-tags 8 ; inline
|
2005-05-06 19:49:07 -04:00
|
|
|
: tag-bits 3 ; inline
|
|
|
|
|
|
|
|
: fixnum-tag BIN: 000 ; inline
|
|
|
|
: bignum-tag BIN: 001 ; inline
|
|
|
|
: cons-tag BIN: 010 ; inline
|
|
|
|
: object-tag BIN: 011 ; inline
|
2005-08-07 00:00:57 -04:00
|
|
|
|
|
|
|
: slip ( quot x -- x | quot: -- )
|
|
|
|
>r call r> ; inline
|
|
|
|
|
|
|
|
: 2slip ( quot x y -- x y | quot: -- )
|
|
|
|
>r >r call r> r> ; inline
|
|
|
|
|
|
|
|
: keep ( x quot -- x | quot: x -- )
|
|
|
|
over >r call r> ; inline
|
|
|
|
|
|
|
|
: 2keep ( x y quot -- x y | quot: x y -- )
|
|
|
|
over >r pick >r call r> r> ; inline
|
|
|
|
|
|
|
|
: 3keep ( x y z quot -- x y z | quot: x y z -- )
|
|
|
|
>r 3dup r> swap >r swap >r swap >r call r> r> r> ; inline
|
|
|
|
|
|
|
|
: ifte* ( cond true false -- | true: cond -- | false: -- )
|
|
|
|
#! [ X ] [ Y ] ifte* ==> dup [ X ] [ drop Y ] ifte
|
|
|
|
pick [ drop call ] [ 2nip call ] ifte ; inline
|
|
|
|
|
|
|
|
: ?ifte ( default cond true false -- )
|
|
|
|
#! [ X ] [ Y ] ?ifte ==> dup [ nip X ] [ drop Y ] ifte
|
|
|
|
>r >r dup [
|
|
|
|
nip r> r> drop call
|
|
|
|
] [
|
|
|
|
drop r> drop r> call
|
|
|
|
] ifte ; inline
|
|
|
|
|
|
|
|
: unless ( cond quot -- | quot: -- )
|
|
|
|
#! Execute a quotation only when the condition is f. The
|
|
|
|
#! condition is popped off the stack.
|
|
|
|
[ ] swap ifte ; inline
|
|
|
|
|
|
|
|
: unless* ( cond quot -- | quot: -- )
|
|
|
|
#! If cond is f, pop it off the stack and evaluate the
|
|
|
|
#! quotation. Otherwise, leave cond on the stack.
|
|
|
|
over [ drop ] [ nip call ] ifte ; inline
|
|
|
|
|
|
|
|
: when ( cond quot -- | quot: -- )
|
|
|
|
#! Execute a quotation only when the condition is not f. The
|
|
|
|
#! condition is popped off the stack.
|
|
|
|
[ ] ifte ; inline
|
|
|
|
|
|
|
|
: when* ( cond quot -- | quot: cond -- )
|
|
|
|
#! If the condition is true, it is left on the stack, and
|
|
|
|
#! the quotation is evaluated. Otherwise, the condition is
|
|
|
|
#! popped off the stack.
|
|
|
|
dupd [ drop ] ifte ; inline
|
|
|
|
|
|
|
|
: with ( obj quot elt -- obj quot )
|
|
|
|
#! Utility word for each-with, map-with.
|
|
|
|
pick pick >r >r swap call r> r> ; inline
|
|
|
|
|
|
|
|
: keep-datastack ( quot -- )
|
|
|
|
datastack slip set-datastack drop ;
|
2005-09-11 20:46:55 -04:00
|
|
|
|
|
|
|
IN: kernel-internals
|
|
|
|
|
|
|
|
! These words are unsafe. Don't use them.
|
|
|
|
|
|
|
|
: array-capacity ( a -- n ) 1 slot ; inline
|
|
|
|
: array-nth ( n a -- obj ) swap 2 fixnum+ slot ; inline
|
|
|
|
: set-array-nth ( obj n a -- ) swap 2 fixnum+ set-slot ; inline
|
|
|
|
|
|
|
|
: make-tuple ( class size -- tuple )
|
|
|
|
#! Internal allocation function. Do not call it directly,
|
|
|
|
#! since you can fool the runtime and corrupt memory by
|
|
|
|
#! specifying an incorrect size. Note that this word is also
|
|
|
|
#! handled specially by the compiler's type inferencer.
|
|
|
|
<tuple> [ 2 set-slot ] keep ; flushable
|