factor/library/collections/namespaces.factor

70 lines
2.0 KiB
Factor
Raw Normal View History

2006-05-02 06:05:58 -04:00
! Copyright (C) 2003, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2006-05-02 06:05:58 -04:00
IN: kernel-internals
USING: vectors sequences ;
2006-05-02 06:05:58 -04:00
2006-08-15 21:23:05 -04:00
: namestack* ( -- namestack )
3 getenv { vector } declare ; inline
: >n ( namespace -- ) namestack* push ;
: n> ( -- namespace ) namestack* pop ;
2006-05-02 06:05:58 -04:00
2004-07-16 02:26:21 -04:00
IN: namespaces
USING: arrays hashtables kernel kernel-internals math strings
words ;
2004-07-16 02:26:21 -04:00
2006-08-15 21:23:05 -04:00
: namestack ( -- namestack ) namestack* clone ; inline
: set-namestack ( namestack -- ) >vector 3 setenv ; inline
2006-05-02 06:05:58 -04:00
: namespace ( -- namespace ) namestack* peek ;
: ndrop ( -- ) namestack* pop* ;
2006-05-02 06:05:58 -04:00
: global ( -- g ) 4 getenv { hashtable } declare ; inline
: get ( variable -- value ) namestack* hash-stack ;
2006-05-02 06:05:58 -04:00
: set ( value variable -- ) namespace set-hash ; inline
2006-08-15 21:23:05 -04:00
: on ( variable -- ) t swap set ; inline
: off ( variable -- ) f swap set ; inline
: get-global ( variable -- value ) global hash ; inline
: set-global ( value variable -- ) global set-hash ; inline
2006-08-15 21:23:05 -04:00
: nest ( variable -- namespace )
dup namespace hash [ ] [ >r H{ } clone dup r> set ] ?if ;
2006-08-15 21:23:05 -04:00
: change ( variable quot -- )
>r dup get r> rot slip set ; inline
2006-08-15 21:23:05 -04:00
: +@ ( n variable -- ) [ [ 0 ] unless* + ] change ;
2006-08-15 21:23:05 -04:00
: inc ( variable -- ) 1 swap +@ ; inline
2006-01-21 16:16:49 -05:00
2006-08-15 21:23:05 -04:00
: dec ( variable -- ) -1 swap +@ ; inline
2006-08-15 21:23:05 -04:00
: bind ( ns quot -- ) swap >n call ndrop ; inline
2006-08-15 21:23:05 -04:00
: counter ( variable -- n ) global [ dup inc get ] bind ;
: make-hash ( quot -- hash ) H{ } clone >n call n> ; inline
2006-04-17 17:17:34 -04:00
: with-scope ( quot -- ) H{ } clone >n call ndrop ; inline
2004-07-16 02:26:21 -04:00
2005-04-16 00:23:27 -04:00
! Building sequences
SYMBOL: building
2006-08-15 21:23:05 -04:00
: make ( quot exemplar -- seq )
2005-08-25 15:27:38 -04:00
[
2006-01-02 00:51:03 -05:00
dup thaw building set >r call building get r> like
2005-08-25 15:27:38 -04:00
] with-scope ; inline
2006-08-15 21:23:05 -04:00
: , ( elt -- ) building get push ;
: % ( seq -- ) building get swap nappend ;
: # ( n -- ) number>string % ;
2006-06-02 16:28:57 -04:00
: init-namespaces ( -- ) global 1array >vector set-namestack ;
2006-02-01 20:10:08 -05:00
IN: sequences
2006-08-15 21:23:05 -04:00
: concat ( seq -- newseq )
dup empty? [ [ [ % ] each ] over first make ] unless ;
2006-08-15 21:23:05 -04:00
: join ( seq glue -- newseq )
[ swap [ % ] [ dup % ] interleave drop ] over make ;