| 
									
										
										
										
											2008-09-10 21:07:00 -04:00
										 |  |  | ! Copyright (C) 2003, 2008 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: kernel vectors sequences hashtables | 
					
						
							|  |  |  | arrays kernel.private math strings assocs ;
 | 
					
						
							|  |  |  | IN: namespaces | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-10 21:07:00 -04:00
										 |  |  | : namestack* ( -- namestack ) 0 getenv { vector } declare ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : >n ( namespace -- ) namestack* push ;
 | 
					
						
							|  |  |  | : ndrop ( -- ) namestack* pop* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : namespace ( -- namespace ) namestack* peek ;
 | 
					
						
							| 
									
										
										
										
											2008-01-18 02:39:24 -05:00
										 |  |  | : namestack ( -- namestack ) namestack* clone ;
 | 
					
						
							|  |  |  | : set-namestack ( namestack -- ) >vector 0 setenv ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : global ( -- g ) 21 getenv { hashtable } declare ; inline
 | 
					
						
							|  |  |  | : init-namespaces ( -- ) global 1array set-namestack ;
 | 
					
						
							|  |  |  | : get ( variable -- value ) namestack* assoc-stack ; flushable
 | 
					
						
							|  |  |  | : set ( value variable -- ) namespace set-at ;
 | 
					
						
							|  |  |  | : on ( variable -- ) t swap set ; inline
 | 
					
						
							|  |  |  | : off ( variable -- ) f swap set ; inline
 | 
					
						
							| 
									
										
										
										
											2008-01-18 02:39:24 -05:00
										 |  |  | : get-global ( variable -- value ) global at ;
 | 
					
						
							|  |  |  | : set-global ( value variable -- ) global set-at ;
 | 
					
						
							| 
									
										
										
										
											2008-09-10 21:07:00 -04:00
										 |  |  | : change ( variable quot -- ) >r dup get r> rot slip set ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : +@ ( n variable -- ) [ 0 or + ] change ;
 | 
					
						
							|  |  |  | : inc ( variable -- ) 1 swap +@ ; inline
 | 
					
						
							|  |  |  | : dec ( variable -- ) -1 swap +@ ; inline
 | 
					
						
							|  |  |  | : bind ( ns quot -- ) swap >n call ndrop ; inline
 | 
					
						
							|  |  |  | : counter ( variable -- n ) global [ dup inc get ] bind ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : make-assoc ( quot exemplar -- hash )
 | 
					
						
							|  |  |  |     20 swap new-assoc [ >n call ndrop ] keep ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-scope ( quot -- )
 | 
					
						
							|  |  |  |     H{ } clone >n call ndrop ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-variable ( value key quot -- )
 | 
					
						
							|  |  |  |     >r associate >n r> call ndrop ; inline
 |