1 ! Copyright (C) 2007, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: fry accessors arrays kernel words sequences generic math
4 namespaces make quotations assocs combinators classes.tuple
5 classes.tuple.private effects summary hashtables classes generic
6 sets definitions generic.standard slots.private continuations
7 stack-checker.backend stack-checker.state stack-checker.visitor
8 stack-checker.errors stack-checker.values
9 stack-checker.recursive-state ;
10 IN: stack-checker.transforms
12 : give-up-transform ( word -- )
14 [ call-recursive-word ]
15 [ dup infer-word apply-word/effect ]
18 : ((apply-transform)) ( word quot values stack -- )
19 rot with-datastack first2
23 [ [ length meta-d shorten-by ] [ #drop, ] bi ] bi*
27 3drop give-up-transform
30 : (apply-transform) ( word quot n -- )
31 ensure-d dup [ known literal? ] all? [
33 recursive-state get 1array
36 [ [ literal value>> ] map ]
37 [ first literal recursion>> ] tri
41 ] [ 2drop give-up-transform ] if ;
43 : apply-transform ( word -- )
44 [ inlined-dependency depends-on ] [
46 [ "transform-quot" word-prop ]
47 [ "transform-n" word-prop ]
52 : apply-macro ( word -- )
53 [ inlined-dependency depends-on ] [
56 [ "declared-effect" word-prop in>> length ]
61 : define-transform ( word quot n -- )
62 [ drop "transform-quot" set-word-prop ]
63 [ nip "transform-n" set-word-prop ]
67 \ cond [ cond>quot ] 1 define-transform
74 dup peek swap but-last
81 \ cleave [ cleave>quot ] 1 define-transform
83 \ 2cleave [ 2cleave>quot ] 1 define-transform
85 \ 3cleave [ 3cleave>quot ] 1 define-transform
87 \ spread [ spread>quot ] 1 define-transform
89 \ (call-next-method) [
91 [ "method-class" word-prop ]
92 [ "method-generic" word-prop ] bi
93 [ inlined-dependency depends-on ] bi@
96 [ '[ _ no-next-method ] ] bi or
103 dup inlined-dependency depends-on
104 [ "boa-check" word-prop [ ] or ]
105 [ tuple-layout '[ _ <tuple-boa> ] ]
112 dup inlined-dependency depends-on
114 [ all-slots [ initial>> literalize , ] each ]
122 : bit-member-n 256 ; inline
124 : bit-member? ( seq -- ? )
125 #! Can we use a fast byte array test here?
127 { [ dup length 8 < ] [ f ] }
128 { [ dup [ integer? not ] any? ] [ f ] }
129 { [ dup [ 0 < ] any? ] [ f ] }
130 { [ dup [ bit-member-n >= ] any? ] [ f ] }
134 : bit-member-seq ( seq -- flags )
135 bit-member-n swap [ member? 1 0 ? ] curry B{ } map-as ;
137 : exact-float? ( f -- ? )
138 dup float? [ dup >integer >float = ] [ drop f ] if ; inline
140 : bit-member-quot ( seq -- newquot )
145 { [ over fixnum? ] [ ?nth 1 eq? ] }
146 { [ over bignum? ] [ ?nth 1 eq? ] }
147 { [ over exact-float? ] [ ?nth 1 eq? ] }
153 : member-quot ( seq -- newquot )
157 [ literalize [ t ] ] { } map>assoc
158 [ drop f ] suffix [ case ] curry
162 dup sequence? [ member-quot ] [ drop f ] if
165 : memq-quot ( seq -- newquot )
166 [ [ dupd eq? ] curry [ drop t ] ] { } map>assoc
167 [ drop f ] suffix [ cond ] curry ;
170 dup sequence? [ memq-quot ] [ drop f ] if