1 ! Copyright (C) 2008, 2009 Daniel Ehrenberg, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel parser words sequences quotations
4 combinators.short-circuit definitions ;
7 ! Mutating literals in word definitions is not really allowed,
8 ! and the deploy tool takes advantage of this fact to perform
9 ! some aggressive stripping and compression. However, this
10 ! breaks a naive implementation of values. We need to do two
12 ! 1) Store the value in a subclass of identity-tuple, so that
13 ! two quotations from different value words are never equal.
14 ! This avoids bogus merging of values.
15 ! 2) Set the "no-def-strip" word-prop, so that the shaker leaves
16 ! the def>> slot alone, allowing us to introspect it. Otherwise,
17 ! it will get set to [ ] and we would lose access to the
22 TUPLE: value-holder < identity-tuple obj ;
26 PREDICATE: value-word < word
29 [ first value-holder? ]
35 dup t "no-def-strip" set-word-prop
36 T{ value-holder } clone [ obj>> ] curry
37 (( -- value )) define-declared ; parsing
39 M: value-word definer drop \ VALUE: f ;
41 M: value-word definition drop f ;
43 : set-value ( value word -- )
47 scan-word literalize parsed
48 \ set-value parsed ; parsing
50 : get-value ( word -- value )
53 : change-value ( word quot -- )
54 [ [ get-value ] dip call ] [ drop ] 2bi set-value ; inline