| 
									
										
										
										
											2008-07-14 14:37:24 -04:00
										 |  |  | ! Copyright (C) 2007, 2008 Daniel Ehrenberg | 
					
						
							| 
									
										
										
										
											2009-02-09 01:25:33 -05:00
										 |  |  | ! Portions copyright (C) 2009 Slava Pestov | 
					
						
							| 
									
										
										
										
											2007-11-28 10:49:43 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2009-02-10 17:42:35 -05:00
										 |  |  | USING: accessors arrays assocs classes.tuple definitions generic | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | generic.standard hashtables kernel lexer math parser | 
					
						
							|  |  |  | generic.parser sequences sets slots words words.symbol fry | 
					
						
							| 
									
										
										
										
											2009-03-07 01:49:57 -05:00
										 |  |  | compiler.units ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 10:49:43 -05:00
										 |  |  | IN: delegate | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | : protocol-words ( protocol -- words )
 | 
					
						
							|  |  |  |     \ protocol-words word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : protocol-consult ( protocol -- consulters )
 | 
					
						
							|  |  |  |     \ protocol-consult word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: group-words ( group -- words )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-09 01:25:33 -05:00
										 |  |  | M: standard-generic group-words | 
					
						
							|  |  |  |     dup "combination" word-prop #>> 2array 1array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | M: tuple-class group-words | 
					
						
							| 
									
										
										
										
											2008-07-14 14:37:24 -04:00
										 |  |  |     all-slots [ | 
					
						
							|  |  |  |         name>> | 
					
						
							|  |  |  |         [ reader-word 0 2array ] | 
					
						
							|  |  |  |         [ writer-word 0 2array ] bi
 | 
					
						
							|  |  |  |         2array
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  |     ] map concat ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Consultation | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | TUPLE: consultation group class quot loc ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <consultation> ( group class quot -- consultation )
 | 
					
						
							|  |  |  |     f consultation boa ;  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : create-consult-method ( word consultation -- method )
 | 
					
						
							|  |  |  |     [ class>> swap first create-method dup fake-definition ] keep
 | 
					
						
							|  |  |  |     [ drop ] [ "consultation" set-word-prop ] 2bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PREDICATE: consult-method < method-body "consultation" word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: consult-method reset-word | 
					
						
							|  |  |  |     [ call-next-method ] [ f "consultation" set-word-prop ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : consult-method-quot ( quot word -- object )
 | 
					
						
							|  |  |  |     [ second [ [ dip ] curry ] times ] [ first ] bi
 | 
					
						
							|  |  |  |     '[ _ call _ execute ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : consult-method ( word consultation -- )
 | 
					
						
							|  |  |  |     [ create-consult-method ] | 
					
						
							|  |  |  |     [ quot>> swap consult-method-quot ] 2bi
 | 
					
						
							| 
									
										
										
										
											2008-05-10 01:16:46 -04:00
										 |  |  |     define ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : change-word-prop ( word prop quot -- )
 | 
					
						
							| 
									
										
										
										
											2009-01-25 23:57:11 -05:00
										 |  |  |     [ swap props>> ] dip change-at ; inline
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | : each-generic ( consultation quot -- )
 | 
					
						
							|  |  |  |     [ [ group>> group-words ] keep ] dip curry each ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : register-consult ( consultation -- )
 | 
					
						
							|  |  |  |     [ group>> \ protocol-consult ] [ ] [ class>> ] tri
 | 
					
						
							|  |  |  |     '[ [ _ _ ] dip ?set-at ] change-word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : consult-methods ( consultation -- )
 | 
					
						
							|  |  |  |     [ consult-method ] each-generic ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : unregister-consult ( consultation -- )
 | 
					
						
							|  |  |  |     [ class>> ] [ group>> ] bi
 | 
					
						
							|  |  |  |     \ protocol-consult word-prop delete-at ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:49:57 -05:00
										 |  |  | : unconsult-method ( word consultation -- )
 | 
					
						
							|  |  |  |     [ class>> swap first method ] keep
 | 
					
						
							|  |  |  |     over [ | 
					
						
							|  |  |  |         over "consultation" word-prop eq?
 | 
					
						
							|  |  |  |         [ forget ] [ drop ] if
 | 
					
						
							|  |  |  |     ] [ 2drop ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | : unconsult-methods ( consultation -- )
 | 
					
						
							|  |  |  |     [ unconsult-method ] each-generic ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : define-consult ( consultation -- )
 | 
					
						
							|  |  |  |     [ register-consult ] [ consult-methods ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  | SYNTAX: CONSULT: | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  |     scan-word scan-word parse-definition <consultation> | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  |     [ save-location ] [ define-consult ] bi ;
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: consultation where loc>> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: consultation set-where (>>loc) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: consultation forget* | 
					
						
							|  |  |  |     [ unconsult-methods ] [ unregister-consult ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-05 00:14:40 -04:00
										 |  |  | ! Protocols | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2008-04-05 00:14:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : cross-2each ( seq1 seq2 quot -- )
 | 
					
						
							|  |  |  |     [ with each ] 2curry each ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : forget-all-methods ( classes words -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-10 01:16:46 -04:00
										 |  |  |     [ first method forget ] cross-2each ;
 | 
					
						
							| 
									
										
										
										
											2008-04-05 00:14:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : protocol-users ( protocol -- users )
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  |     protocol-consult keys ;
 | 
					
						
							| 
									
										
										
										
											2008-04-05 00:14:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | : lost-words ( protocol wordlist -- lost-words )
 | 
					
						
							| 
									
										
										
										
											2008-09-15 11:33:03 -04:00
										 |  |  |     [ protocol-words ] dip diff ;
 | 
					
						
							| 
									
										
										
										
											2008-04-05 00:14:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : forget-old-definitions ( protocol new-wordlist -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-10 02:14:36 -04:00
										 |  |  |     [ drop protocol-users ] [ lost-words ] 2bi
 | 
					
						
							|  |  |  |     forget-all-methods ;
 | 
					
						
							| 
									
										
										
										
											2008-04-05 00:14:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | : added-words ( protocol wordlist -- added-words )
 | 
					
						
							| 
									
										
										
										
											2008-05-10 01:16:46 -04:00
										 |  |  |     swap protocol-words diff ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : add-new-definitions ( protocol wordlist -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  |     [ drop protocol-consult values ] [ added-words ] 2bi
 | 
					
						
							|  |  |  |     [ swap consult-method ] cross-2each ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : initialize-protocol-props ( protocol wordlist -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-10 01:16:46 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         drop \ protocol-consult | 
					
						
							|  |  |  |         [ H{ } assoc-like ] change-word-prop | 
					
						
							|  |  |  |     ] [ { } like \ protocol-words set-word-prop ] 2bi ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 10:49:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-05 03:44:54 -04:00
										 |  |  | : fill-in-depth ( wordlist -- wordlist' )
 | 
					
						
							|  |  |  |     [ dup word? [ 0 2array ] when ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | : show-words ( wordlist' -- wordlist )
 | 
					
						
							|  |  |  |     [ dup second zero? [ first ] when ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | : define-protocol ( protocol wordlist -- )
 | 
					
						
							| 
									
										
										
										
											2009-01-16 17:39:24 -05:00
										 |  |  |     [ drop define-symbol ] [ | 
					
						
							|  |  |  |         fill-in-depth | 
					
						
							|  |  |  |         [ forget-old-definitions ] | 
					
						
							|  |  |  |         [ add-new-definitions ] | 
					
						
							|  |  |  |         [ initialize-protocol-props ] 2tri
 | 
					
						
							|  |  |  |     ] 2bi ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  | SYNTAX: PROTOCOL: | 
					
						
							|  |  |  |     CREATE-WORD parse-definition define-protocol ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 10:49:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-05 00:14:40 -04:00
										 |  |  | PREDICATE: protocol < word protocol-words ; ! Subclass of symbol? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: protocol forget* | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  |     [ f forget-old-definitions ] [ call-next-method ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-04-05 00:14:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-05 03:44:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: protocol definition protocol-words show-words ;
 | 
					
						
							| 
									
										
										
										
											2008-04-05 00:14:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: protocol definer drop \ PROTOCOL: \ ; ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | M: protocol group-words protocol-words ;
 | 
					
						
							| 
									
										
										
										
											2009-01-16 17:39:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  | SYNTAX: SLOT-PROTOCOL: | 
					
						
							| 
									
										
										
										
											2009-01-16 17:39:24 -05:00
										 |  |  |     CREATE-WORD ";" parse-tokens | 
					
						
							|  |  |  |     [ [ reader-word ] [ writer-word ] bi 2array ] map concat
 | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  |     define-protocol ;
 |