2008-11-05 09:19:59 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! Copyright (C) 2008 James Cash
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! See http://factorcode.org/license.txt for BSD license.
							 | 
						
					
						
							
								
									
										
										
										
											2008-12-17 19:10:01 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								USING: kernel sequences fry words assocs linked-assocs tools.annotations
							 | 
						
					
						
							
								
									
										
										
										
											2009-04-17 12:01:58 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								coroutines lexer parser quotations arrays namespaces continuations
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								summary ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 09:19:59 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								IN: advice
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-11 01:31:22 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								SYMBOLS: before after around advised in-advice? ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-06 20:03:04 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-11 01:31:22 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: advised? ( word -- ? )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    advised word-prop ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								DEFER: make-advised
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 09:19:59 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-06 00:44:11 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								<PRIVATE
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-11 01:31:22 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: init-around-co ( quot -- coroutine )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    \ coreset suffix cocreate ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								PRIVATE>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 23:50:33 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: advise ( quot name word loc --  )
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-11 01:31:22 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    dup around eq? [ [ init-around-co ] 3dip ] when
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    over advised? [ over make-advised ] unless
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 23:50:33 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    word-prop set-at ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-11 01:31:22 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: advise-before ( quot name word --  ) before advise ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 23:50:33 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-11 01:31:22 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: advise-after ( quot name word --  ) after advise ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 23:50:33 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-11 01:31:22 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: advise-around ( quot name word --  ) around advise ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 23:50:33 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 09:19:59 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: get-advice ( word type -- seq )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    word-prop values ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: call-before ( word --  )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    before get-advice [ call ] each ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: call-after ( word --  )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    after get-advice [ call ] each ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 23:50:33 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: call-around ( main word --  )
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-11 01:31:22 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    t in-advice? [
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        around get-advice tuck 
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        [ [ coresume ] each ] [ call ] [ <reversed> [ coresume ] each ] tri*
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ] with-variable ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 09:19:59 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: remove-advice ( name word loc --  )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    word-prop delete-at ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 23:50:33 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-04-17 12:01:58 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								ERROR: ad-do-it-error ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								M: ad-do-it-error summary
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    drop "ad-do-it should only be called inside 'around' advice" ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 23:50:33 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: ad-do-it ( input -- result )
							 | 
						
					
						
							
								
									
										
										
										
											2009-04-17 12:01:58 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    in-advice? get [ ad-do-it-error ] unless coyield ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-05 09:19:59 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: make-advised ( word -- )
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-18 18:01:26 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ dup '[ [ _ ] dip over dup '[ _ call-before _ _ call-around _ call-after ] ] annotate ]
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-11 01:31:22 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ { before after around } [ <linked-hash> swap set-word-prop ] with each ] 
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-06 00:20:15 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ t advised set-word-prop ] tri ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-06 20:03:04 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: unadvise ( word --  )
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-11 01:31:22 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ reset ] [ { before after around advised } [ f swap set-word-prop ] with each ] bi ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								SYNTAX: ADVISE: ! word adname location => word adname quot loc
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    scan-word scan scan-word parse-definition swap [ spin ] dip advise ;
							 | 
						
					
						
							
								
									
										
										
										
											2008-11-11 01:31:22 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								SYNTAX: UNADVISE:    
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-28 14:38:27 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    scan-word suffix! \ unadvise suffix! ;
							 |