| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  | ! Copyright (C) 2004, 2008 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | USING: kernel.private slots.private classes.tuple.private ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: kernel | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Stack stuff | 
					
						
							| 
									
										
										
										
											2007-12-29 11:36:20 -05:00
										 |  |  | : spin ( x y z -- z y x ) swap rot ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : roll ( x y z t -- y z t x ) >r rot r> swap ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : -roll ( x y z t -- t x y z ) swap >r -rot r> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2008-02-11 14:50:29 -05:00
										 |  |  | GENERIC: call ( callable -- )
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : unless ( cond false -- )
 | 
					
						
							|  |  |  |     swap [ drop ] [ call ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : when ( cond true -- )
 | 
					
						
							|  |  |  |     swap [ call ] [ drop ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  | ! Anaphoric | 
					
						
							|  |  |  | : if* ( cond true false -- )
 | 
					
						
							|  |  |  |     pick [ drop call ] [ 2nip call ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : when* ( cond true -- )
 | 
					
						
							|  |  |  |     over [ call ] [ 2drop ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  | : unless* ( cond false -- )
 | 
					
						
							|  |  |  |     over [ drop ] [ nip call ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Default | 
					
						
							|  |  |  | : ?if ( default cond true false -- )
 | 
					
						
							|  |  |  |     pick [ roll 2drop call ] [ 2nip call ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Slippers | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : slip ( quot x -- x ) >r call r> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 2slip ( quot x y -- x y ) >r >r call r> r> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 3slip ( quot x y z -- x y z ) >r >r >r call r> r> r> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-29 11:36:20 -05:00
										 |  |  | : dip ( obj quot -- obj ) swap slip ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  | ! Keepers | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : keep ( x quot -- x ) over slip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-11 17:02:44 -05:00
										 |  |  | : 2keep ( x y quot -- x y ) 2over 2slip ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : 3keep ( x y z quot -- x y z )
 | 
					
						
							|  |  |  |     >r 3dup r> -roll 3slip ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  | ! Cleavers | 
					
						
							| 
									
										
										
										
											2008-03-30 00:11:45 -04:00
										 |  |  | : bi ( x p q -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  |     >r keep r> call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 00:11:45 -04:00
										 |  |  | : tri ( x p q r -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  |     >r pick >r bi r> r> call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Double cleavers | 
					
						
							| 
									
										
										
										
											2008-03-30 00:11:45 -04:00
										 |  |  | : 2bi ( x y p q -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  |     >r 2keep r> call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 00:11:45 -04:00
										 |  |  | : 2tri ( x y p q r -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  |     >r >r 2keep r> 2keep r> call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Triple cleavers | 
					
						
							| 
									
										
										
										
											2008-03-30 00:11:45 -04:00
										 |  |  | : 3bi ( x y z p q -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  |     >r 3keep r> call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 00:11:45 -04:00
										 |  |  | : 3tri ( x y z p q r -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  |     >r >r 3keep r> 3keep r> call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Spreaders | 
					
						
							| 
									
										
										
										
											2008-03-30 00:11:45 -04:00
										 |  |  | : bi* ( x y p q -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  |     >r swap slip r> call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 00:11:45 -04:00
										 |  |  | : tri* ( x y z p q r -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  |     >r rot >r bi* r> r> call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Double spreaders | 
					
						
							| 
									
										
										
										
											2008-03-30 00:11:45 -04:00
										 |  |  | : 2bi* ( w x y z p q -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  |     >r -rot 2slip r> call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Appliers | 
					
						
							| 
									
										
										
										
											2008-03-30 01:40:43 -04:00
										 |  |  | : bi@ ( x y quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-02 01:28:07 -04:00
										 |  |  |     dup bi* ; inline
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 01:40:43 -04:00
										 |  |  | : tri@ ( x y z quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-02 01:28:07 -04:00
										 |  |  |     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 -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-02 01:28:07 -04:00
										 |  |  |     dup 2bi* ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-16 04:15:16 -04:00
										 |  |  | : while ( pred body tail -- )
 | 
					
						
							|  |  |  |     >r >r dup slip r> r> roll | 
					
						
							|  |  |  |     [ >r tuck 2slip r> while ] | 
					
						
							|  |  |  |     [ 2nip call ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Object protocol | 
					
						
							|  |  |  | GENERIC: hashcode* ( depth obj -- code )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: object hashcode* 2drop 0 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-05 08:35:51 -04:00
										 |  |  | M: f hashcode* 2drop 31337 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : hashcode ( obj -- code ) 3 swap hashcode* ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: equal? ( obj1 obj2 -- ? )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: object equal? 2drop f ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | TUPLE: identity-tuple ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: identity-tuple equal? 2drop f ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : = ( obj1 obj2 -- ? )
 | 
					
						
							|  |  |  |     2dup eq? [ 2drop t ] [ equal? ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: <=> ( obj1 obj2 -- n )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: clone ( obj -- cloned )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: object clone ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-03 16:56:49 -04:00
										 |  |  | M: callstack clone (clone) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Tuple construction | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | : construct-empty ( class -- tuple )
 | 
					
						
							|  |  |  |     tuple-layout <tuple> ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | : construct-boa ( ... class -- tuple )
 | 
					
						
							|  |  |  |     tuple-layout <tuple-boa> ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 3compose ( quot1 quot2 quot3 -- curry )
 | 
					
						
							|  |  |  |     compose compose ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Booleans | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : not ( obj -- ? ) f eq? ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >boolean ( obj -- ? ) t f ? ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : and ( obj1 obj2 -- ? ) over ? ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : or ( obj1 obj2 -- ? ) dupd ? ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : xor ( obj1 obj2 -- ? ) dup not swap ? ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-29 00:00:20 -04:00
										 |  |  | : compare ( obj1 obj2 quot -- n ) bi@ <=> ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : most ( x y quot -- z )
 | 
					
						
							|  |  |  |     >r 2dup r> call [ drop ] [ nip ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Error handling -- defined early so that other files can | 
					
						
							|  |  |  | ! throw errors before continuations are loaded | 
					
						
							| 
									
										
										
										
											2008-02-11 14:50:29 -05:00
										 |  |  | : throw ( error -- * ) 5 getenv [ die ] or 1 (throw) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-02 19:50:21 -04:00
										 |  |  | : hi-tag ( obj -- n ) 0 slot ; inline
 | 
					
						
							| 
									
										
										
										
											2008-04-02 01:28:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : declare ( spec -- ) drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-02 19:36:36 -05:00
										 |  |  | : do-primitive ( number -- ) "Improper primitive call" throw ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Deprecated | 
					
						
							|  |  |  | M: object delegate drop f ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC# get-slots 1 ( tuple slots -- ... )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC# set-slots 1 ( ... tuple slots -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : construct ( ... slots class -- tuple )
 | 
					
						
							|  |  |  |     construct-empty [ swap set-slots ] keep ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : construct-delegate ( delegate class -- tuple )
 | 
					
						
							|  |  |  |     >r { set-delegate } r> construct ; inline
 |