| 
									
										
										
										
											2008-02-04 20:38:31 -05:00
										 |  |  | ! Copyright (C) 2006, 2008 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-09-10 21:07:00 -04:00
										 |  |  | USING: kernel math math.parser namespaces make sequences strings | 
					
						
							| 
									
										
										
										
											2008-08-11 00:36:46 -04:00
										 |  |  | words assocs combinators accessors arrays ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: effects | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: effect in out terminated? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <effect> ( in out -- effect )
 | 
					
						
							|  |  |  |     dup { "*" } sequence= [ drop { } t ] [ f ] if
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:09 -04:00
										 |  |  |     effect boa ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : effect-height ( effect -- n )
 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:32:55 -04:00
										 |  |  |     [ out>> length ] [ in>> length ] bi - ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : effect<= ( eff1 eff2 -- ? )
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-06-08 16:32:55 -04:00
										 |  |  |         { [ over terminated?>> ] [ t ] } | 
					
						
							|  |  |  |         { [ dup terminated?>> ] [ f ] } | 
					
						
							|  |  |  |         { [ 2dup [ in>> length ] bi@ > ] [ f ] } | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  |         { [ 2dup [ effect-height ] bi@ = not ] [ f ] } | 
					
						
							| 
									
										
										
										
											2008-04-11 13:53:22 -04:00
										 |  |  |         [ t ] | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     } cond 2nip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-18 20:22:59 -04:00
										 |  |  | GENERIC: effect>string ( obj -- str )
 | 
					
						
							|  |  |  | M: string effect>string ;
 | 
					
						
							|  |  |  | M: word effect>string name>> ;
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:36:46 -04:00
										 |  |  | M: integer effect>string number>string ;
 | 
					
						
							| 
									
										
										
										
											2008-07-18 20:22:59 -04:00
										 |  |  | M: pair effect>string first2 [ effect>string ] bi@ ": " swap 3append ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : stack-picture ( seq -- string )
 | 
					
						
							| 
									
										
										
										
											2008-08-11 03:49:43 -04:00
										 |  |  |     dup integer? [ "object" <repetition> ] when
 | 
					
						
							| 
									
										
										
										
											2008-07-18 20:22:59 -04:00
										 |  |  |     [ [ effect>string % CHAR: \s , ] each ] "" make ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-18 20:22:59 -04:00
										 |  |  | M: effect effect>string ( effect -- string )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         "( " % | 
					
						
							| 
									
										
										
										
											2008-06-08 16:32:55 -04:00
										 |  |  |         [ in>> stack-picture % "-- " % ] | 
					
						
							|  |  |  |         [ out>> stack-picture % ] | 
					
						
							|  |  |  |         [ terminated?>> [ "* " % ] when ] | 
					
						
							|  |  |  |         tri
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         ")" % | 
					
						
							|  |  |  |     ] "" make ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-04 20:38:31 -05:00
										 |  |  | GENERIC: stack-effect ( word -- effect/f )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-26 21:47:36 -04:00
										 |  |  | M: symbol stack-effect drop (( -- symbol )) ;
 | 
					
						
							| 
									
										
										
										
											2008-02-04 20:38:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: word stack-effect | 
					
						
							|  |  |  |     { "declared-effect" "inferred-effect" } | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  |     swap props>> [ at ] curry map [ ] find nip ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: effect clone | 
					
						
							| 
									
										
										
										
											2008-06-26 21:47:36 -04:00
										 |  |  |     [ in>> clone ] [ out>> clone ] bi <effect> ;
 | 
					
						
							| 
									
										
										
										
											2007-09-29 19:43:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-18 20:22:59 -04:00
										 |  |  | : stack-height ( word -- n )
 | 
					
						
							|  |  |  |     stack-effect effect-height ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-29 19:43:03 -04:00
										 |  |  | : split-shuffle ( stack shuffle -- stack1 stack2 )
 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:32:55 -04:00
										 |  |  |     in>> length cut* ;
 | 
					
						
							| 
									
										
										
										
											2007-09-29 19:43:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : load-shuffle ( stack shuffle -- )
 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:32:55 -04:00
										 |  |  |     in>> [ set ] 2each ;
 | 
					
						
							| 
									
										
										
										
											2007-09-29 19:43:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : shuffled-values ( shuffle -- values )
 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:32:55 -04:00
										 |  |  |     out>> [ get ] map ;
 | 
					
						
							| 
									
										
										
										
											2007-09-29 19:43:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 21:39:45 -04:00
										 |  |  | : shuffle ( stack shuffle -- newstack )
 | 
					
						
							| 
									
										
										
										
											2007-09-29 19:43:03 -04:00
										 |  |  |     [ [ load-shuffle ] keep shuffled-values ] with-scope ;
 |