| 
									
										
										
										
											2008-07-14 14:37:24 -04:00
										 |  |  | ! Copyright (C) 2007, 2008 Daniel Ehrenberg | 
					
						
							| 
									
										
										
										
											2007-11-28 10:49:43 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-07-05 01:59:39 -04:00
										 |  |  | USING: accessors parser generic kernel classes classes.tuple | 
					
						
							|  |  |  | words slots assocs sequences arrays vectors definitions | 
					
						
							| 
									
										
										
										
											2008-09-10 21:07:00 -04:00
										 |  |  | prettyprint math hashtables sets macros namespaces make ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 10:49:43 -05:00
										 |  |  | IN: delegate | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : consult-method ( word class quot -- )
 | 
					
						
							|  |  |  |     [ drop swap first create-method ] | 
					
						
							| 
									
										
										
										
											2008-06-13 02:51:46 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         nip
 | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             over second saver % | 
					
						
							|  |  |  |             % | 
					
						
							|  |  |  |             dup second restorer % | 
					
						
							|  |  |  |             first , | 
					
						
							|  |  |  |         ] [ ] make | 
					
						
							|  |  |  |     ] 3bi
 | 
					
						
							| 
									
										
										
										
											2008-05-10 01:16:46 -04:00
										 |  |  |     define ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : change-word-prop ( word prop quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  |     rot props>> swap change-at ; inline
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : register-protocol ( group class quot -- )
 | 
					
						
							|  |  |  |     rot \ protocol-consult [ swapd ?set-at ] change-word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : define-consult ( group class quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-10 01:16:46 -04:00
										 |  |  |     [ register-protocol ] | 
					
						
							|  |  |  |     [ rot group-words -rot [ consult-method ] 2curry each ] | 
					
						
							|  |  |  |     3bi ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : CONSULT: | 
					
						
							|  |  |  |     scan-word scan-word parse-definition define-consult ; parsing | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-05 00:14:40 -04:00
										 |  |  | ! Protocols | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 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-04-18 00:20:17 -04:00
										 |  |  |     >r protocol-words r> 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 -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-10 02:14:36 -04:00
										 |  |  |     [ drop protocol-consult >alist ] [ added-words ] 2bi
 | 
					
						
							| 
									
										
										
										
											2008-05-10 01:16:46 -04:00
										 |  |  |     [ swap first2 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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | : define-protocol ( protocol wordlist -- )
 | 
					
						
							|  |  |  |     fill-in-depth | 
					
						
							|  |  |  |     [ forget-old-definitions ] | 
					
						
							|  |  |  |     [ add-new-definitions ] | 
					
						
							|  |  |  |     [ initialize-protocol-props ] 2tri ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-28 10:49:43 -05:00
										 |  |  | : PROTOCOL: | 
					
						
							| 
									
										
										
										
											2008-04-05 00:14:40 -04:00
										 |  |  |     CREATE-WORD | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  |     [ define-symbol ] | 
					
						
							|  |  |  |     [ f "inline" set-word-prop ] | 
					
						
							|  |  |  |     [ parse-definition define-protocol ] tri ; parsing | 
					
						
							| 
									
										
										
										
											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
										 |  |  | : show-words ( wordlist' -- wordlist )
 | 
					
						
							|  |  |  |     [ dup second zero? [ first ] when ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: protocol definition protocol-words show-words ;
 | 
					
						
							| 
									
										
										
										
											2008-04-05 00:14:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: protocol definer drop \ PROTOCOL: \ ; ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: protocol synopsis* word-synopsis ; ! Necessary? | 
					
						
							| 
									
										
										
										
											2007-11-28 10:49:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | M: protocol group-words protocol-words ;
 |