1 ! Copyright (C) 2003, 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: vectors sequences ;
6 : namestack* ( -- namestack )
7 3 getenv { vector } declare ; inline
8 : >n ( namespace -- ) namestack* push ;
9 : n> ( -- namespace ) namestack* pop ;
12 USING: arrays hashtables kernel kernel-internals math strings
15 : namestack ( -- namestack ) namestack* clone ; inline
16 : set-namestack ( namestack -- ) >vector 3 setenv ; inline
17 : namespace ( -- namespace ) namestack* peek ;
18 : ndrop ( -- ) namestack* pop* ;
19 : global ( -- g ) 4 getenv { hashtable } declare ; inline
20 : get ( variable -- value ) namestack* hash-stack ;
21 : set ( value variable -- ) namespace set-hash ; inline
22 : on ( variable -- ) t swap set ; inline
23 : off ( variable -- ) f swap set ; inline
24 : get-global ( variable -- value ) global hash ; inline
25 : set-global ( value variable -- ) global set-hash ; inline
27 : nest ( variable -- namespace )
28 dup namespace hash [ ] [ >r H{ } clone dup r> set ] ?if ;
30 : change ( variable quot -- )
31 >r dup get r> rot slip set ; inline
33 : +@ ( n variable -- ) [ [ 0 ] unless* + ] change ;
35 : inc ( variable -- ) 1 swap +@ ; inline
37 : dec ( variable -- ) -1 swap +@ ; inline
39 : bind ( ns quot -- ) swap >n call ndrop ; inline
41 : counter ( variable -- n ) global [ dup inc get ] bind ;
43 : make-hash ( quot -- hash ) H{ } clone >n call n> ; inline
45 : with-scope ( quot -- ) H{ } clone >n call ndrop ; inline
50 : make ( quot exemplar -- seq )
52 [ V{ } clone building set call building get ] with-scope
55 : , ( elt -- ) building get push ;
57 : % ( seq -- ) building get swap nappend ;
59 : init-namespaces ( -- ) global 1array set-namestack ;
63 : join ( seq glue -- newseq )
64 [ swap [ % ] [ dup % ] interleave drop ] over make ;