]> gitweb.factorcode.org Git - factor.git/blob - core/effects/effects.factor
Fix permission bits
[factor.git] / core / effects / effects.factor
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 ;
5 IN: effects
6
7 TUPLE: effect in out terminated? ;
8
9 : <effect> ( in out -- effect )
10     dup { "*" } sequence= [ drop { } t ] [ f ] if
11     effect boa ;
12
13 : effect-height ( effect -- n )
14     [ out>> length ] [ in>> length ] bi - ;
15
16 : effect<= ( eff1 eff2 -- ? )
17     {
18         { [ over terminated?>> ] [ t ] }
19         { [ dup terminated?>> ] [ f ] }
20         { [ 2dup [ in>> length ] bi@ > ] [ f ] }
21         { [ 2dup [ effect-height ] bi@ = not ] [ f ] }
22         [ t ]
23     } cond 2nip ;
24
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@ ": " swap 3append ;
30
31 : stack-picture ( seq -- string )
32     dup integer? [ "object" <repetition> ] when
33     [ [ effect>string % CHAR: \s , ] each ] "" make ;
34
35 M: effect effect>string ( effect -- string )
36     [
37         "( " %
38         [ in>> stack-picture % "-- " % ]
39         [ out>> stack-picture % ]
40         [ terminated?>> [ "* " % ] when ]
41         tri
42         ")" %
43     ] "" make ;
44
45 GENERIC: stack-effect ( word -- effect/f )
46
47 M: symbol stack-effect drop (( -- symbol )) ;
48
49 M: word stack-effect
50     { "declared-effect" "inferred-effect" }
51     swap props>> [ at ] curry map [ ] find nip ;
52
53 M: effect clone
54     [ in>> clone ] [ out>> clone ] bi <effect> ;
55
56 : stack-height ( word -- n )
57     stack-effect effect-height ;
58
59 : split-shuffle ( stack shuffle -- stack1 stack2 )
60     in>> length cut* ;
61
62 : load-shuffle ( stack shuffle -- )
63     in>> [ set ] 2each ;
64
65 : shuffled-values ( shuffle -- values )
66     out>> [ get ] map ;
67
68 : shuffle ( stack shuffle -- newstack )
69     [ [ load-shuffle ] keep shuffled-values ] with-scope ;