2006-05-02 06:05:58 -04:00
|
|
|
! Copyright (C) 2003, 2006 Slava Pestov.
|
2005-12-31 04:20:07 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2006-05-02 06:05:58 -04:00
|
|
|
IN: kernel-internals
|
2006-05-20 02:37:35 -04:00
|
|
|
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
|
2006-08-15 03:01:24 -04:00
|
|
|
: >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
|
2006-05-20 02:37:35 -04:00
|
|
|
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 ;
|
2006-08-15 03:01:24 -04:00
|
|
|
: ndrop ( -- ) namestack* pop* ;
|
2006-05-02 06:05:58 -04:00
|
|
|
: global ( -- g ) 4 getenv { hashtable } declare ; inline
|
2006-06-05 23:26:44 -04:00
|
|
|
: 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
|
2005-12-31 04:20:07 -05:00
|
|
|
|
2006-08-15 21:23:05 -04:00
|
|
|
: nest ( variable -- namespace )
|
2005-12-31 04:20:07 -05:00
|
|
|
dup namespace hash [ ] [ >r H{ } clone dup r> set ] ?if ;
|
|
|
|
|
2006-08-15 21:23:05 -04:00
|
|
|
: change ( variable quot -- )
|
2005-12-31 04:20:07 -05:00
|
|
|
>r dup get r> rot slip set ; inline
|
|
|
|
|
2006-08-15 21:23:05 -04:00
|
|
|
: +@ ( n variable -- ) [ [ 0 ] unless* + ] change ;
|
2005-12-31 04:20:07 -05:00
|
|
|
|
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
|
2005-12-31 04:20:07 -05:00
|
|
|
|
2006-08-15 21:23:05 -04:00
|
|
|
: bind ( ns quot -- ) swap >n call ndrop ; inline
|
2005-12-31 04:20:07 -05:00
|
|
|
|
2006-08-15 21:23:05 -04:00
|
|
|
: counter ( variable -- n ) global [ dup inc get ] bind ;
|
2006-01-22 16:40:18 -05:00
|
|
|
|
2005-12-31 04:20:07 -05:00
|
|
|
: 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
|
2005-04-25 03:33:33 -04:00
|
|
|
SYMBOL: building
|
2005-12-31 04:20:07 -05:00
|
|
|
|
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
|
2005-08-31 21:06:13 -04:00
|
|
|
|
2006-08-15 21:23:05 -04:00
|
|
|
: , ( elt -- ) building get push ;
|
2005-12-31 04:20:07 -05:00
|
|
|
|
|
|
|
: % ( 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 )
|
2006-05-18 22:20:23 -04:00
|
|
|
dup empty? [ [ [ % ] each ] over first make ] unless ;
|
|
|
|
|
2006-08-15 21:23:05 -04:00
|
|
|
: join ( seq glue -- newseq )
|
2006-05-18 22:20:23 -04:00
|
|
|
[ swap [ % ] [ dup % ] interleave drop ] over make ;
|