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
|
|
|
|
|
|
|
: namestack* ( -- ns ) 3 getenv { vector } declare ; inline
|
2006-05-20 02:37:35 -04:00
|
|
|
: >n ( namespace -- n:namespace ) namestack* push ;
|
|
|
|
: n> ( n:namespace -- 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
|
|
|
|
2005-12-31 04:20:07 -05:00
|
|
|
: namestack ( -- ns ) namestack* clone ; inline
|
2006-05-02 06:05:58 -04:00
|
|
|
: set-namestack ( ns -- ) >vector 3 setenv ; inline
|
|
|
|
: namespace ( -- namespace ) namestack* peek ;
|
|
|
|
: ndrop ( n:namespace -- ) namestack* pop* ;
|
|
|
|
: global ( -- g ) 4 getenv { hashtable } declare ; inline
|
2005-12-31 04:20:07 -05:00
|
|
|
: get ( variable -- value ) namestack* hash-stack ; flushable
|
2006-05-02 06:05:58 -04:00
|
|
|
: set ( value variable -- ) namespace set-hash ; inline
|
2005-12-31 04:20:07 -05:00
|
|
|
: on ( var -- ) t swap set ; inline
|
|
|
|
: off ( var -- ) f swap set ; inline
|
2006-03-19 00:30:57 -05:00
|
|
|
: get-global ( var -- value ) global hash ; inline
|
2005-12-31 04:20:07 -05:00
|
|
|
: set-global ( value var -- ) global set-hash ; inline
|
|
|
|
|
|
|
|
: nest ( variable -- hash )
|
|
|
|
dup namespace hash [ ] [ >r H{ } clone dup r> set ] ?if ;
|
|
|
|
|
|
|
|
: change ( var quot -- quot: old -- new )
|
|
|
|
>r dup get r> rot slip set ; inline
|
|
|
|
|
2006-01-22 16:40:18 -05:00
|
|
|
: +@ ( n var -- ) [ [ 0 ] unless* + ] change ;
|
2005-12-31 04:20:07 -05:00
|
|
|
|
2006-01-22 16:40:18 -05:00
|
|
|
: inc ( var -- ) 1 swap +@ ; inline
|
2006-01-21 16:16:49 -05:00
|
|
|
|
2006-01-22 16:40:18 -05:00
|
|
|
: dec ( var -- ) -1 swap +@ ; inline
|
2005-12-31 04:20:07 -05:00
|
|
|
|
2006-04-17 17:17:34 -04:00
|
|
|
: bind ( namespace quot -- ) swap >n call ndrop ; inline
|
2005-12-31 04:20:07 -05:00
|
|
|
|
2006-01-22 16:40:18 -05:00
|
|
|
: counter ( var -- n ) global [ dup inc get ] bind ;
|
|
|
|
|
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
|
|
|
|
|
|
|
: make ( quot proto -- )
|
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
|
|
|
|
2005-12-31 04:20:07 -05:00
|
|
|
: , ( obj -- ) building get push ;
|
|
|
|
|
|
|
|
: % ( seq -- ) building get swap nappend ;
|
|
|
|
|
|
|
|
: # ( n -- ) number>string % ;
|
|
|
|
|
2006-02-01 20:10:08 -05:00
|
|
|
IN: sequences
|
|
|
|
|
|
|
|
: prune ( seq -- seq )
|
|
|
|
[ [ dup set ] each ] make-hash hash-keys ;
|
|
|
|
|
2006-05-18 22:20:23 -04:00
|
|
|
: concat ( seq -- seq )
|
|
|
|
dup empty? [ [ [ % ] each ] over first make ] unless ;
|
|
|
|
flushable
|
|
|
|
|
|
|
|
: join ( seq glue -- seq )
|
|
|
|
[ swap [ % ] [ dup % ] interleave drop ] over make ;
|
|
|
|
flushable
|
|
|
|
|
2005-11-12 00:37:24 -05:00
|
|
|
IN: kernel-internals
|
|
|
|
|
|
|
|
: init-namespaces ( -- ) global 1array >vector set-namestack ;
|