| 
									
										
										
										
											2008-07-14 14:37:24 -04:00
										 |  |  | ! Copyright (C) 2007, 2008 Daniel Ehrenberg | 
					
						
							| 
									
										
										
										
											2010-03-23 01:32:00 -04:00
										 |  |  | ! Portions copyright (C) 2009, 2010 Slava Pestov, Joe Groff | 
					
						
							| 
									
										
										
										
											2007-11-28 10:49:43 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2010-03-23 01:32:00 -04:00
										 |  |  | USING: accessors arrays assocs classes.tuple definitions effects 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 | 
					
						
							| 
									
										
										
										
											2010-09-04 21:48:54 -04:00
										 |  |  | compiler.units make ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 10:49:43 -05:00
										 |  |  | IN: delegate | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-23 01:32:00 -04:00
										 |  |  | ERROR: broadcast-words-must-have-no-outputs group ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | : protocol-words ( protocol -- words )
 | 
					
						
							| 
									
										
										
										
											2016-04-14 23:19:54 -04:00
										 |  |  |     "protocol-words" word-prop ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : protocol-consult ( protocol -- consulters )
 | 
					
						
							| 
									
										
										
										
											2016-04-14 23:19:54 -04:00
										 |  |  |     "protocol-consult" word-prop ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | GENERIC: group-words ( group -- words )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-09 01:25:33 -05:00
										 |  |  | M: standard-generic group-words | 
					
						
							|  |  |  |     dup "combination" word-prop #>> 2array 1array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-04 21:48:54 -04:00
										 |  |  | : slot-words, ( slot-spec -- )
 | 
					
						
							|  |  |  |     [ name>> reader-word 0 2array , ] | 
					
						
							| 
									
										
										
										
											2010-06-08 15:52:46 -04:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2010-09-04 21:48:54 -04:00
										 |  |  |         dup read-only>> [ drop ] [ | 
					
						
							|  |  |  |             name>> writer-word 0 2array , | 
					
						
							|  |  |  |         ] if
 | 
					
						
							|  |  |  |     ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : slot-group-words ( slots -- words )
 | 
					
						
							|  |  |  |     [ [ slot-words, ] each ] { } make ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-08 15:52:46 -04:00
										 |  |  | M: tuple-class group-words | 
					
						
							|  |  |  |     all-slots slot-group-words ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-23 01:32:00 -04:00
										 |  |  | : check-broadcast-group ( group -- group )
 | 
					
						
							|  |  |  |     dup group-words [ first stack-effect out>> empty? ] all?
 | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |     [ broadcast-words-must-have-no-outputs ] unless ;
 | 
					
						
							| 
									
										
										
										
											2010-03-23 01:32:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | ! Consultation | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | TUPLE: consultation group class quot loc ;
 | 
					
						
							| 
									
										
										
										
											2016-04-14 23:19:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-23 01:32:00 -04:00
										 |  |  | TUPLE: broadcast < consultation ;
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <consultation> ( group class quot -- consultation )
 | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  |     f consultation boa ;
 | 
					
						
							| 
									
										
										
										
											2016-04-14 23:19:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-23 01:32:00 -04:00
										 |  |  | : <broadcast> ( group class quot -- consultation )
 | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  |     [ check-broadcast-group ] 2dip f broadcast boa ;
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : create-consult-method ( word consultation -- method )
 | 
					
						
							|  |  |  |     [ class>> swap first create-method dup fake-definition ] keep
 | 
					
						
							|  |  |  |     [ drop ] [ "consultation" set-word-prop ] 2bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-29 13:52:46 -04:00
										 |  |  | PREDICATE: consult-method < method | 
					
						
							|  |  |  |     "consultation" word-prop >boolean ;
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: consult-method reset-word | 
					
						
							|  |  |  |     [ call-next-method ] [ f "consultation" set-word-prop ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-23 01:32:00 -04:00
										 |  |  | GENERIC# (consult-method-quot) 2 ( consultation quot word -- object )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: consultation (consult-method-quot) | 
					
						
							|  |  |  |     '[ _ call _ execute ] nip ;
 | 
					
						
							| 
									
										
										
										
											2016-04-14 23:19:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-23 01:32:00 -04:00
										 |  |  | M: broadcast (consult-method-quot) | 
					
						
							|  |  |  |     '[ _ call [ _ execute ] each ] nip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : consult-method-quot ( consultation word -- object )
 | 
					
						
							|  |  |  |     [ dup quot>> ] dip
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  |     [ second [ [ dip ] curry ] times ] [ first ] bi
 | 
					
						
							| 
									
										
										
										
											2010-03-23 01:32:00 -04:00
										 |  |  |     (consult-method-quot) ;
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-23 19:51:26 -04:00
										 |  |  | : define-consult-method ( word consultation -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  |     [ create-consult-method ] | 
					
						
							| 
									
										
										
										
											2010-03-23 01:32:00 -04:00
										 |  |  |     [ swap consult-method-quot ] 2bi
 | 
					
						
							| 
									
										
										
										
											2008-05-10 01:16:46 -04:00
										 |  |  |     define ;
 | 
					
						
							| 
									
										
										
										
											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 -- )
 | 
					
						
							| 
									
										
										
										
											2016-04-14 23:19:54 -04:00
										 |  |  |     [ group>> "protocol-consult" ] [ ] [ class>> ] tri
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  |     '[ [ _ _ ] dip ?set-at ] change-word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : consult-methods ( consultation -- )
 | 
					
						
							| 
									
										
										
										
											2013-03-23 19:51:26 -04:00
										 |  |  |     [ define-consult-method ] each-generic ;
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : unregister-consult ( consultation -- )
 | 
					
						
							|  |  |  |     [ class>> ] [ group>> ] bi
 | 
					
						
							| 
									
										
										
										
											2016-04-14 23:19:54 -04:00
										 |  |  |     "protocol-consult" word-prop delete-at ;
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:49:57 -05:00
										 |  |  | : unconsult-method ( word consultation -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-03 19:42:24 -04:00
										 |  |  |     [ class>> swap first ?lookup-method ] keep
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:49:57 -05:00
										 |  |  |     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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-23 01:32:00 -04:00
										 |  |  | SYNTAX: BROADCAST: | 
					
						
							|  |  |  |     scan-word scan-word parse-definition <broadcast> | 
					
						
							|  |  |  |     [ save-location ] [ define-consult ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | M: consultation where loc>> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-05 16:52:54 -04:00
										 |  |  | M: consultation set-where loc<< ;
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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
										 |  |  | 
 | 
					
						
							|  |  |  | : forget-all-methods ( classes words -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-03 19:42:24 -04:00
										 |  |  |     [ first ?lookup-method forget ] cartesian-each ;
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2013-03-23 19:51:26 -04:00
										 |  |  |     [ swap define-consult-method ] cartesian-each ;
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : initialize-protocol-props ( protocol wordlist -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-10 01:16:46 -04:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2016-04-14 23:19:54 -04:00
										 |  |  |         drop "protocol-consult" | 
					
						
							| 
									
										
										
										
											2008-05-10 01:16:46 -04:00
										 |  |  |         [ H{ } assoc-like ] change-word-prop | 
					
						
							| 
									
										
										
										
											2016-04-14 23:19:54 -04:00
										 |  |  |     ] [ { } 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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-18 19:29:38 -04:00
										 |  |  | ERROR: not-a-generic word ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : check-generic ( generic -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-18 23:40:14 -04:00
										 |  |  |     dup array? [ first ] when
 | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |     dup generic? [ drop ] [ not-a-generic ] if ;
 | 
					
						
							| 
									
										
										
										
											2011-10-18 19:29:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 01:42:43 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-17 23:39:25 -04:00
										 |  |  | : define-protocol ( protocol wordlist -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-18 19:29:38 -04:00
										 |  |  |     dup [ check-generic ] each
 | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							| 
									
										
										
										
											2011-09-27 16:20:07 -04:00
										 |  |  |     scan-new-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: | 
					
						
							| 
									
										
										
										
											2011-09-27 16:20:07 -04:00
										 |  |  |     scan-new-word ";" | 
					
						
							| 
									
										
										
										
											2010-03-01 01:06:47 -05:00
										 |  |  |     [ [ reader-word ] [ writer-word ] bi 2array ] | 
					
						
							|  |  |  |     map-tokens concat define-protocol ;
 |