]> gitweb.factorcode.org Git - factor.git/blob - core/effects/effects.factor
change ERROR: words from throw-foo back to foo.
[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 [ rot ] dip [ ?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 first2-unsafe [ effect>string ] bi@ ": " glue ;
58
59 : stack-picture ( seq -- string )
60     [ [ effect>string % CHAR: \s , ] each ] "" make ;
61
62 : var-picture ( var -- string )
63     [ ".." " " surround ]
64     [ "" ] if* ;
65
66 M: effect effect>string ( effect -- string )
67     [
68         "( " %
69         dup in-var>> var-picture %
70         dup in>> stack-picture % "-- " %
71         dup out-var>> var-picture %
72         dup out>> stack-picture %
73         dup terminated?>> [ "* " % ] when
74         drop
75         ")" %
76     ] "" make ;
77
78 GENERIC: effect>type ( obj -- type )
79 M: object effect>type drop object ;
80 M: word effect>type ;
81 M: pair effect>type second-unsafe effect>type ;
82 M: classoid effect>type ;
83
84 : effect-in-types ( effect -- input-types )
85     in>> [ effect>type ] map ;
86
87 : effect-out-types ( effect -- input-types )
88     out>> [ effect>type ] map ;
89
90 GENERIC: stack-effect ( word -- effect/f )
91
92 M: word stack-effect
93     dup "declared-effect" word-prop [ nip ] [
94         parent-word dup [ stack-effect ] when
95     ] if* ;
96
97 M: deferred stack-effect call-next-method ( -- * ) or ;
98
99 M: effect clone
100     [ in>> clone ] [ out>> clone ] bi <effect> ;
101
102 : stack-height ( word -- n )
103     stack-effect effect-height ; inline
104
105 : shuffle-mapping ( effect -- mapping )
106     [ out>> ] [ in>> ] bi [ index ] curry map ;
107
108 : shuffle ( stack shuffle -- newstack )
109     shuffle-mapping swap nths ;
110
111 : add-effect-input ( effect -- effect' )
112     [ in>> "obj" suffix ] [ out>> ] [ terminated?>> ] tri
113     <terminated-effect> ;
114
115 : compose-effects ( effect1 effect2 -- effect' )
116     over terminated?>> [
117         drop
118     ] [
119         [ [ [ in>> length ] [ out>> length ] bi ] [ in>> length ] bi* swap [-] + ]
120         [ [ out>> length ] [ [ in>> length ] [ out>> length ] bi ] bi* [ [-] ] dip + ]
121         [ nip terminated?>> ] 2tri
122         [ [ "x" <array> ] bi@ ] dip
123         <terminated-effect>
124     ] if ; inline
125
126 : curry-effect ( effect -- effect' )
127     [ in>> length ] [ out>> length ] [ terminated?>> ] tri
128     pick 0 = [ [ 1 + ] dip ] [ [ 1 - ] 2dip ] if
129     [ [ "x" <array> ] bi@ ] dip <terminated-effect> ;
130
131 ERROR: bad-stack-effect word expected got ;
132
133 : check-stack-effect ( word effect -- )
134     over stack-effect 2dup effect=
135     [ 3drop ] [ bad-stack-effect ] if ;