1 ! Copyright (C) 2006, 2008 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: word effect>string name>> ;
28 M: integer effect>string number>string ;
29 M: pair effect>string first2 [ effect>string ] bi@ ": " glue ;
31 : stack-picture ( seq -- string )
32 dup integer? [ "object" <repetition> ] when
33 [ [ effect>string % CHAR: \s , ] each ] "" make ;
35 M: effect effect>string ( effect -- string )
38 [ in>> stack-picture % "-- " % ]
39 [ out>> stack-picture % ]
40 [ terminated?>> [ "* " % ] when ]
45 GENERIC: stack-effect ( word -- effect/f )
48 { "declared-effect" "inferred-effect" }
49 swap props>> [ at ] curry map [ ] find nip ;
52 [ in>> clone ] [ out>> clone ] bi <effect> ;
54 : stack-height ( word -- n )
55 stack-effect effect-height ;
57 : split-shuffle ( stack shuffle -- stack1 stack2 )
60 : load-shuffle ( stack shuffle -- )
63 : shuffled-values ( shuffle -- values )
66 : shuffle ( stack shuffle -- newstack )
67 [ [ load-shuffle ] keep shuffled-values ] with-scope ;