| 
									
										
										
										
											2010-03-18 05:06:00 -04:00
										 |  |  | ! Copyright (C) 2003, 2010 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2011-11-30 19:02:37 -05:00
										 |  |  | USING: arrays assocs hashtables kernel kernel.private math | 
					
						
							|  |  |  | sequences vectors ;
 | 
					
						
							| 
									
										
										
										
											2011-11-27 13:43:23 -05:00
										 |  |  | SLOT: boxes | 
					
						
							|  |  |  | SLOT: value | 
					
						
							|  |  |  | FROM: accessors => boxes>> value>> value<< ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: namespaces | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 13:43:23 -05:00
										 |  |  | TUPLE: global-hashtable | 
					
						
							|  |  |  |     { boxes hashtable read-only } ;
 | 
					
						
							|  |  |  | TUPLE: global-box value ;
 | 
					
						
							| 
									
										
										
										
											2011-11-26 23:20:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 13:43:23 -05:00
										 |  |  | : (box-at) ( key globals -- box )
 | 
					
						
							|  |  |  |     boxes>> 2dup at
 | 
					
						
							|  |  |  |     [ 2nip ] [ [ f global-box boa ] 2dip [ set-at ] 2curry keep ] if* ; foldable
 | 
					
						
							| 
									
										
										
										
											2011-11-26 23:20:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 13:43:23 -05:00
										 |  |  | : box-at ( key globals -- box )
 | 
					
						
							|  |  |  |     (box-at) { global-box } declare ; inline
 | 
					
						
							| 
									
										
										
										
											2011-11-26 23:20:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 13:43:23 -05:00
										 |  |  | M: global-hashtable at* | 
					
						
							| 
									
										
										
										
											2012-07-20 18:09:06 -04:00
										 |  |  |     boxes>> at* [ | 
					
						
							|  |  |  |         { global-box } declare value>> dup
 | 
					
						
							|  |  |  |     ] [ drop f f ] if ; inline
 | 
					
						
							| 
									
										
										
										
											2011-11-26 23:20:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 13:43:23 -05:00
										 |  |  | M: global-hashtable set-at | 
					
						
							|  |  |  |     box-at value<< ; inline
 | 
					
						
							| 
									
										
										
										
											2011-11-26 23:20:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 13:43:23 -05:00
										 |  |  | M: global-hashtable delete-at | 
					
						
							|  |  |  |     box-at f swap value<< ; inline
 | 
					
						
							| 
									
										
										
										
											2011-11-26 23:20:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 13:43:23 -05:00
										 |  |  | : namestack* ( -- namestack )
 | 
					
						
							|  |  |  |     CONTEXT-OBJ-NAMESTACK context-object { vector } declare ; inline
 | 
					
						
							|  |  |  | : >n ( namespace -- ) namestack* push ;
 | 
					
						
							|  |  |  | : ndrop ( -- ) namestack* pop* ;
 | 
					
						
							| 
									
										
										
										
											2011-11-26 23:20:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-27 13:43:23 -05:00
										 |  |  | : global ( -- g ) OBJ-GLOBAL special-object { global-hashtable } declare ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-25 17:38:33 -04:00
										 |  |  | : namespace ( -- namespace ) namestack* last ; inline
 | 
					
						
							| 
									
										
										
										
											2008-01-18 02:39:24 -05:00
										 |  |  | : namestack ( -- namestack ) namestack* clone ;
 | 
					
						
							| 
									
										
										
										
											2011-11-02 15:54:31 -04:00
										 |  |  | : set-namestack ( namestack -- )
 | 
					
						
							|  |  |  |     >vector CONTEXT-OBJ-NAMESTACK set-context-object ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : init-namespaces ( -- ) global 1array set-namestack ;
 | 
					
						
							| 
									
										
										
										
											2008-12-06 19:37:28 -05:00
										 |  |  | : get ( variable -- value ) namestack* assoc-stack ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : set ( value variable -- ) namespace set-at ;
 | 
					
						
							|  |  |  | : on ( variable -- ) t swap set ; inline
 | 
					
						
							|  |  |  | : off ( variable -- ) f swap set ; inline
 | 
					
						
							| 
									
										
										
										
											2012-07-20 15:00:47 -04:00
										 |  |  | : is-global ( variable -- ? ) global boxes>> key? ; inline
 | 
					
						
							| 
									
										
										
										
											2012-07-20 18:09:06 -04:00
										 |  |  | : get-global ( variable -- value ) global box-at value>> ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-13 20:21:59 -04:00
										 |  |  | : set-global ( value variable -- ) global set-at ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  | : change ( variable quot -- ) [ [ get ] keep ] dip dip set ; inline
 | 
					
						
							| 
									
										
										
										
											2012-07-20 18:09:06 -04:00
										 |  |  | : change-global ( variable quot -- )
 | 
					
						
							|  |  |  |     [ [ get-global ] keep ] dip dip set-global ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-29 04:01:59 -04:00
										 |  |  | : toggle ( variable -- ) [ not ] change ; inline
 | 
					
						
							| 
									
										
										
										
											2011-10-13 20:21:59 -04:00
										 |  |  | : +@ ( n variable -- ) [ 0 or + ] change ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : inc ( variable -- ) 1 swap +@ ; inline
 | 
					
						
							|  |  |  | : dec ( variable -- ) -1 swap +@ ; inline
 | 
					
						
							| 
									
										
										
										
											2012-07-19 03:02:47 -04:00
										 |  |  | : with-variables ( ns quot -- ) swap >n call ndrop ; inline
 | 
					
						
							| 
									
										
										
										
											2012-08-02 18:14:21 -04:00
										 |  |  | : counter ( variable -- n ) [ 0 or 1 + dup ] change-global ; inline
 | 
					
						
							| 
									
										
										
										
											2012-07-19 03:02:47 -04:00
										 |  |  | : make-assoc ( quot exemplar -- hash ) 20 swap new-assoc [ swap with-variables ] keep ; inline
 | 
					
						
							|  |  |  | : with-scope ( quot -- ) 5 <hashtable> swap with-variables ; inline
 | 
					
						
							|  |  |  | : with-variable ( value key quot -- ) [ associate ] dip with-variables ; inline
 | 
					
						
							|  |  |  | : with-global ( quot -- ) [ global ] dip with-variables ; inline
 | 
					
						
							| 
									
										
										
										
											2009-05-01 20:58:24 -04:00
										 |  |  | : initialize ( variable quot -- ) [ unless* ] curry change-global ; inline
 |