1 ! (c)2010 Joe Groff bsd license
2 USING: accessors arrays assocs combinators combinators.short-circuit
3 continuations effects fry kernel locals math math.order namespaces
4 quotations sequences splitting
7 stack-checker.known-words
10 stack-checker.visitor ;
11 IN: stack-checker.row-polymorphism
13 : with-inner-d ( quot -- inner-d )
15 [ meta-d length inner-d-index set call ] dip
16 inner-d-index get [ min inner-d-index set ] keep ; inline
18 :: (effect-here) ( inner-d old-meta-d-length old-input-count -- effect )
19 old-meta-d-length inner-d - input-count get old-input-count - +
20 meta-d length inner-d -
21 [ "x" <array> ] bi@ terminated? get <terminated-effect> ; inline
23 : with-effect-here ( quot -- effect )
24 meta-d length input-count get
25 [ with-inner-d ] 2dip (effect-here) ; inline
27 : (diff-variable) ( diff variable vars -- diff' )
28 [ at* nip ] [ '[ _ _ at - ] ] [ '[ _ _ set-at 0 ] ] 2tri if ;
30 : (check-variable) ( actual-count declared-count variable vars -- diff ? )
31 [ - ] 2dip dupd '[ _ _ (diff-variable) t ] [ dup 0 <= ] if ;
33 : adjust-variable ( diff var vars -- )
34 pick 0 >= [ at+ ] [ 3drop ] if ; inline
36 :: check-variable ( vars declared actual slot var-slot -- diff ok? var )
37 actual declared [ slot call length ] bi@ declared var-slot call
38 [ vars (check-variable) ] keep ; inline
40 :: unify-variables ( in-diff in-ok? in-var out-diff out-ok? out-var vars -- ? )
41 { [ in-ok? ] [ out-ok? ] [ in-diff out-diff = ] } 0&& dup [
42 in-var [ in-diff swap vars adjust-variable ] when*
43 out-var [ out-diff swap vars adjust-variable ] when*
46 : (check-variables) ( vars declared actual -- ? )
47 [ [ in>> ] [ in-var>> ] check-variable ]
48 [ [ out>> ] [ out-var>> ] check-variable ]
49 [ 2drop ] 3tri unify-variables ;
51 : check-variables ( vars declared actual -- ? )
52 dup terminated?>> [ 3drop t ] [ (check-variables) ] if ;
54 : combinator-branches-effects ( branches -- quots declareds actuals )
55 [ [ known>callable ] { } map-as ]
56 [ [ effect>> ] { } map-as ]
57 [ [ actual>> ] { } map-as ] tri ;
59 : combinator-unbalanced-branches-error ( known -- * )
60 [ word>> ] [ branches>> <reversed> combinator-branches-effects ] bi
61 unbalanced-branches-error ;
63 : check-declared-effect ( known effect -- )
65 2dup [ [ variables>> ] [ effect>> ] bi ] dip check-variables
66 [ 2drop ] [ drop combinator-unbalanced-branches-error ] if ;