]> gitweb.factorcode.org Git - factor.git/blob - core/effects/effects.factor
core: Add the shuffler words but without primitives.
[factor.git] / core / effects / effects.factor
1 ! Copyright (C) 2006, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays classes combinators kernel make math
4 math.order math.parser sequences sequences.private strings words ;
5 IN: effects
6
7 TUPLE: effect
8 { in array read-only }
9 { out array read-only }
10 { terminated? read-only }
11 { in-var read-only }
12 { out-var read-only } ;
13
14 : ?terminated ( out -- out terminated? )
15     dup { "*" } = [ drop { } t ] [ f ] if ;
16
17 : <effect> ( in out -- effect )
18     ?terminated f f effect boa ;
19
20 : <terminated-effect> ( in out terminated? -- effect )
21     f f effect boa ; inline
22
23 : <variable-effect> ( in-var in out-var out -- effect )
24     swap rotd [ ?terminated ] 2dip effect boa ;
25
26 : effect-height ( effect -- n )
27     [ out>> length ] [ in>> length ] bi - ; inline
28
29 : variable-effect? ( effect -- ? )
30     [ in-var>> ] [ out-var>> ] bi or ;
31
32 : bivariable-effect? ( effect -- ? )
33     [ in-var>> ] [ out-var>> ] bi = not ;
34
35 : effect<= ( effect1 effect2 -- ? )
36     {
37         { [ over terminated?>> ] [ t ] }
38         { [ dup terminated?>> ] [ f ] }
39         { [ 2dup [ bivariable-effect? ] either? ] [ f ] }
40         { [ 2dup [ variable-effect? ] [ variable-effect? not ] bi* and ] [ f ] }
41         { [ 2dup [ in>> length ] bi@ > ] [ f ] }
42         { [ 2dup [ effect-height ] same? not ] [ f ] }
43         [ t ]
44     } cond 2nip ; inline
45
46 : effect= ( effect1 effect2 -- ? )
47     [ [ in>> length ] same? ]
48     [ [ out>> length ] same? ]
49     [ [ terminated?>> ] same? ]
50     2tri and and ;
51
52 GENERIC: effect>string ( obj -- str )
53 M: string effect>string ;
54 M: object effect>string drop "object" ;
55 M: word effect>string name>> ;
56 M: integer effect>string number>string ;
57 M: pair effect>string
58     first2-unsafe over [
59         [ effect>string ] bi@ ": " glue
60     ] [
61         nip effect>string ":" prepend
62     ] if ;
63
64 : stack-picture ( seq -- string )
65     [ [ effect>string % CHAR: \s , ] each ] "" make ;
66
67 : var-picture ( var -- string )
68     [ ".." " " surround ]
69     [ "" ] if* ;
70
71 M: effect effect>string ( effect -- string )
72     [
73         "( " %
74         dup in-var>> var-picture %
75         dup in>> stack-picture % "-- " %
76         dup out-var>> var-picture %
77         dup out>> stack-picture %
78         dup terminated?>> [ "* " % ] when
79         drop
80         ")" %
81     ] "" make ;
82
83 GENERIC: effect>type ( obj -- type )
84 M: object effect>type drop object ;
85 M: word effect>type ;
86 M: pair effect>type second-unsafe effect>type ;
87 M: classoid effect>type ;
88
89 : effect-in-types ( effect -- input-types )
90     in>> [ effect>type ] map ;
91
92 : effect-out-types ( effect -- input-types )
93     out>> [ effect>type ] map ;
94
95 GENERIC: stack-effect ( word -- effect/f )
96
97 M: word stack-effect
98     dup "declared-effect" word-prop [ nip ] [
99         parent-word dup [ stack-effect ] when
100     ] if* ;
101
102 M: deferred stack-effect call-next-method ( -- * ) or ;
103
104 M: effect clone
105     [ in>> clone ] [ out>> clone ] bi <effect> ;
106
107 : stack-height ( word -- n )
108     stack-effect effect-height ; inline
109
110 : shuffle-mapping ( effect -- mapping )
111     [ out>> ] [ in>> ] bi [ index ] curry map ;
112
113 : shuffle ( stack shuffle -- newstack )
114     shuffle-mapping swap nths ;
115
116 : add-effect-input ( effect -- effect' )
117     [ in>> "obj" suffix ] [ out>> ] [ terminated?>> ] tri
118     <terminated-effect> ;
119
120 : compose-effects ( effect1 effect2 -- effect' )
121     over terminated?>> [
122         drop
123     ] [
124         [ [ [ in>> length ] [ out>> length ] bi ] [ in>> length ] bi* swap [-] + ]
125         [ [ out>> length ] [ [ in>> length ] [ out>> length ] bi ] bi* [ [-] ] dip + ]
126         [ nip terminated?>> ] 2tri
127         [ [ "x" <array> ] bi@ ] dip
128         <terminated-effect>
129     ] if ; inline
130
131 : curry-effect ( effect -- effect' )
132     [ in>> length ] [ out>> length ] [ terminated?>> ] tri
133     pick 0 = [ [ 1 + ] dip ] [ [ 1 - ] 2dip ] if
134     [ [ "x" <array> ] bi@ ] dip <terminated-effect> ;
135
136 ERROR: bad-stack-effect word got expected ;
137
138 : check-stack-effect ( word effect -- )
139     over stack-effect 2dup effect=
140     [ 3drop ] [ bad-stack-effect ] if ;