| 
									
										
										
										
											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. | 
					
						
							|  |  |  | USING: kernel vectors sequences hashtables | 
					
						
							|  |  |  | arrays kernel.private math strings assocs ;
 | 
					
						
							|  |  |  | IN: namespaces | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-18 05:06:00 -04:00
										 |  |  | : namestack* ( -- namestack ) 0 context-object { vector } declare ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : >n ( namespace -- ) namestack* push ;
 | 
					
						
							|  |  |  | : ndrop ( -- ) namestack* pop* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 ;
 | 
					
						
							| 
									
										
										
										
											2010-03-18 05:06:00 -04:00
										 |  |  | : set-namestack ( namestack -- ) >vector 0 set-context-object ;
 | 
					
						
							| 
									
										
										
										
											2010-01-13 00:08:18 -05:00
										 |  |  | : global ( -- g ) 21 special-object { hashtable } declare ; inline
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2008-01-18 02:39:24 -05:00
										 |  |  | : get-global ( variable -- value ) global at ;
 | 
					
						
							|  |  |  | : set-global ( value variable -- ) global set-at ;
 | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  | : change ( variable quot -- ) [ [ get ] keep ] dip dip set ; inline
 | 
					
						
							| 
									
										
										
										
											2009-04-30 22:38:14 -04:00
										 |  |  | : change-global ( variable quot -- ) [ global ] dip change-at ; 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
 | 
					
						
							| 
									
										
										
										
											2009-05-01 20:58:24 -04:00
										 |  |  | : counter ( variable -- n ) [ 0 or 1 + dup ] change-global ;
 | 
					
						
							| 
									
										
										
										
											2009-02-20 21:51:51 -05:00
										 |  |  | : make-assoc ( quot exemplar -- hash ) 20 swap new-assoc [ swap bind ] keep ; inline
 | 
					
						
							| 
									
										
										
										
											2009-03-31 09:16:04 -04:00
										 |  |  | : with-scope ( quot -- ) 5 <hashtable> swap bind ; inline
 | 
					
						
							| 
									
										
										
										
											2009-02-20 21:51:51 -05:00
										 |  |  | : with-variable ( value key quot -- ) [ associate ] dip bind ; inline
 | 
					
						
							| 
									
										
										
										
											2009-05-01 20:58:24 -04:00
										 |  |  | : initialize ( variable quot -- ) [ unless* ] curry change-global ; inline
 |