1 ! Copyright (C) 2006, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.parser namespaces make sequences strings
4 words assocs combinators accessors arrays ;
7 TUPLE: effect in out terminated? ;
9 : <effect> ( in out -- effect )
10 dup { "*" } sequence= [ drop { } t ] [ f ] if
13 : effect-height ( effect -- n )
14 [ out>> length ] [ in>> length ] bi - ;
16 : effect<= ( eff1 eff2 -- ? )
18 { [ over terminated?>> ] [ t ] }
19 { [ dup terminated?>> ] [ f ] }
20 { [ 2dup [ in>> length ] bi@ > ] [ f ] }
21 { [ 2dup [ effect-height ] bi@ = not ] [ f ] }
25 GENERIC: effect>string ( obj -- str )
26 M: string effect>string ;
27 M: object effect>string drop "object" ;
28 M: word effect>string name>> ;
29 M: integer effect>string number>string ;
30 M: pair effect>string first2 [ effect>string ] bi@ ": " glue ;
32 : stack-picture ( seq -- string )
33 dup integer? [ "object" <repetition> ] when
34 [ [ effect>string % CHAR: \s , ] each ] "" make ;
36 M: effect effect>string ( effect -- string )
39 [ in>> stack-picture % "-- " % ]
40 [ out>> stack-picture % ]
41 [ terminated?>> [ "* " % ] when ]
46 GENERIC: stack-effect ( word -- effect/f )
48 M: word stack-effect "declared-effect" word-prop ;
50 M: deferred stack-effect call-next-method (( -- * )) or ;
53 [ in>> clone ] [ out>> clone ] bi <effect> ;
55 : stack-height ( word -- n )
56 stack-effect effect-height ;
58 : split-shuffle ( stack shuffle -- stack1 stack2 )
61 : shuffle-mapping ( effect -- mapping )
62 [ out>> ] [ in>> ] bi [ index ] curry map ;
64 : shuffle ( stack shuffle -- newstack )
65 shuffle-mapping swap nths ;