]> gitweb.factorcode.org Git - factor.git/blob - core/effects/effects.factor
Removing integers-as-sequences
[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: kernel math math.parser math.order namespaces make sequences strings
4 words assocs combinators accessors arrays quotations ;
5 IN: effects
6
7 TUPLE: effect
8 { in array read-only }
9 { out array read-only }
10 { terminated? read-only } ;
11
12 : <effect> ( in out -- effect )
13     dup { "*" } = [ drop { } t ] [ f ] if
14     effect boa ;
15
16 : effect-height ( effect -- n )
17     [ out>> length ] [ in>> length ] bi - ; inline
18
19 : effect<= ( effect1 effect2 -- ? )
20     {
21         { [ over terminated?>> ] [ t ] }
22         { [ dup terminated?>> ] [ f ] }
23         { [ 2dup [ in>> length ] bi@ > ] [ f ] }
24         { [ 2dup [ effect-height ] bi@ = not ] [ f ] }
25         [ t ]
26     } cond 2nip ; inline
27
28 : effect= ( effect1 effect2 -- ? )
29     [ [ in>> length ] bi@ = ]
30     [ [ out>> length ] bi@ = ]
31     [ [ terminated?>> ] bi@ = ]
32     2tri and and ;
33
34 GENERIC: effect>string ( obj -- str )
35 M: string effect>string ;
36 M: object effect>string drop "object" ;
37 M: word effect>string name>> ;
38 M: integer effect>string number>string ;
39 M: pair effect>string first2 [ effect>string ] bi@ ": " glue ;
40
41 : stack-picture ( seq -- string )
42     [ [ effect>string % CHAR: \s , ] each ] "" make ;
43
44 M: effect effect>string ( effect -- string )
45     [
46         "( " %
47         [ in>> stack-picture % "-- " % ]
48         [ out>> stack-picture % ]
49         [ terminated?>> [ "* " % ] when ]
50         tri
51         ")" %
52     ] "" make ;
53
54 GENERIC: effect>type ( obj -- type )
55 M: object effect>type drop object ;
56 M: word effect>type ;
57 M: pair effect>type second effect>type ;
58
59 : effect-in-types ( effect -- input-types )
60     in>> [ effect>type ] map ;
61
62 : effect-out-types ( effect -- input-types )
63     out>> [ effect>type ] map ;
64
65 GENERIC: stack-effect ( word -- effect/f )
66
67 M: word stack-effect "declared-effect" word-prop ;
68
69 M: deferred stack-effect call-next-method (( -- * )) or ;
70
71 M: effect clone
72     [ in>> clone ] [ out>> clone ] bi <effect> ;
73
74 : stack-height ( word -- n )
75     stack-effect effect-height ;
76
77 : split-shuffle ( stack shuffle -- stack1 stack2 )
78     in>> length cut* ;
79
80 : shuffle-mapping ( effect -- mapping )
81     [ out>> ] [ in>> ] bi [ index ] curry map ;
82
83 : shuffle ( stack shuffle -- newstack )
84     shuffle-mapping swap nths ;
85
86 : add-effect-input ( effect -- effect' )
87     [ in>> "obj" suffix ] [ out>> ] [ terminated?>> ] tri effect boa ;
88
89 : compose-effects ( effect1 effect2 -- effect' )
90     over terminated?>> [
91         drop
92     ] [
93         [ [ [ in>> length ] [ out>> length ] bi ] [ in>> length ] bi* swap [-] + ]
94         [ [ out>> length ] [ [ in>> length ] [ out>> length ] bi ] bi* [ [-] ] dip + ]
95         [ nip terminated?>> ] 2tri
96         [ [ "x" <array> ] bi@ ] dip
97         effect boa
98     ] if ; inline