2019-10-18 09:05:06 -04:00
|
|
|
! Copyright (C) 2003, 2007 Slava Pestov.
|
2005-12-31 04:20:07 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2019-10-18 09:05:06 -04:00
|
|
|
IN: namespaces
|
|
|
|
|
TUPLE: namespace-error object ;
|
|
|
|
|
|
2006-05-02 06:05:58 -04:00
|
|
|
IN: kernel-internals
|
2019-10-18 09:05:06 -04:00
|
|
|
USING: kernel vectors sequences hashtables errors ;
|
2006-05-02 06:05:58 -04:00
|
|
|
|
2006-08-15 21:23:05 -04:00
|
|
|
: namestack* ( -- namestack )
|
|
|
|
|
3 getenv { vector } declare ; inline
|
2019-10-18 09:05:06 -04:00
|
|
|
|
|
|
|
|
: >n ( namespace -- )
|
|
|
|
|
dup hashtable? [ <namespace-error> throw ] unless
|
|
|
|
|
namestack* push ;
|
|
|
|
|
|
2006-08-15 03:01:24 -04:00
|
|
|
: n> ( -- namespace ) namestack* pop ;
|
2006-05-02 06:05:58 -04:00
|
|
|
|
2004-07-16 02:26:21 -04:00
|
|
|
IN: namespaces
|
2019-10-18 09:05:06 -04:00
|
|
|
USING: arrays kernel-internals math strings words ;
|
|
|
|
|
|
|
|
|
|
: namespace ( -- namespace )
|
|
|
|
|
namestack* peek { hashtable } declare ;
|
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-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 ;
|
2019-10-18 09:05:06 -04:00
|
|
|
: set ( value variable -- ) namespace set-hash ;
|
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 )
|
2019-10-18 09:05:06 -04:00
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
1024 swap new-resizable [
|
|
|
|
|
building set call
|
|
|
|
|
] keep
|
|
|
|
|
] keep like
|
|
|
|
|
] 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
|
|
|
|
2019-10-18 09:05:06 -04:00
|
|
|
: % ( seq -- ) building get nappend ;
|
2005-12-31 04:20:07 -05:00
|
|
|
|
2006-11-05 21:37:22 -05:00
|
|
|
: init-namespaces ( -- ) global 1array set-namestack ;
|
2006-06-02 16:28:57 -04:00
|
|
|
|
2006-02-01 20:10:08 -05:00
|
|
|
IN: sequences
|
|
|
|
|
|
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 ;
|
2019-10-18 09:05:06 -04:00
|
|
|
|
|
|
|
|
: (prune) ( hash vec elt -- )
|
|
|
|
|
rot 2dup hash-member?
|
|
|
|
|
[ 3drop ] [ dupd dupd set-hash swap push ] if ; inline
|
|
|
|
|
|
|
|
|
|
: prune ( seq -- newseq )
|
|
|
|
|
dup length <hashtable> over length <vector>
|
|
|
|
|
rot [ >r 2dup r> (prune) ] each nip ;
|