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
9 stack-checker.visitor ;
10 IN: stack-checker.row-polymorphism
12 : with-inner-d ( quot -- inner-d )
14 [ meta-d length inner-d-index set call ] dip
15 inner-d-index get [ min inner-d-index set ] keep ; inline
17 :: (effect-here) ( inner-d old-meta-d-length old-input-count -- effect )
18 old-meta-d-length inner-d - input-count get old-input-count - +
19 meta-d length inner-d -
20 [ "x" <array> ] bi@ terminated? get <terminated-effect> ; inline
22 : with-effect-here ( quot -- effect )
23 meta-d length input-count get
24 [ with-inner-d ] 2dip (effect-here) ; inline
26 : (diff-variable) ( diff variable vars -- diff' )
27 [ at* nip ] [ '[ _ _ at - ] ] [ '[ _ _ set-at 0 ] ] 2tri if ;
29 : (check-variable) ( actual-count declared-count variable vars -- diff ? )
30 [ - ] 2dip dupd '[ _ _ (diff-variable) t ] [ dup 0 <= ] if ;
32 : adjust-variable ( diff var vars -- )
33 pick 0 >= [ at+ ] [ 3drop ] if ; inline
35 :: check-variable ( vars declared actual slot var-slot -- diff ok? var )
36 actual declared [ slot call length ] bi@ declared var-slot call
37 [ vars (check-variable) ] keep ; inline
39 :: unify-variables ( in-diff in-ok? in-var out-diff out-ok? out-var vars -- ? )
40 { [ in-ok? ] [ out-ok? ] [ in-diff out-diff = ] } 0&& dup [
41 in-var [ in-diff swap vars adjust-variable ] when*
42 out-var [ out-diff swap vars adjust-variable ] when*
45 : (check-variables) ( vars declared actual -- ? )
46 [ [ in>> ] [ in-var>> ] check-variable ]
47 [ [ out>> ] [ out-var>> ] check-variable ]
48 [ 2drop ] 3tri unify-variables ;
50 : check-variables ( vars declared actual -- ? )
51 dup terminated?>> [ 3drop t ] [ (check-variables) ] if ;
53 : combinator-branches-effects ( branches -- quots declareds actuals )
54 [ [ known>callable ] { } map-as ]
55 [ [ effect>> ] { } map-as ]
56 [ [ actual>> ] { } map-as ] tri ;
58 : combinator-unbalanced-branches-error ( known -- * )
59 [ word>> ] [ branches>> <reversed> combinator-branches-effects ] bi
60 unbalanced-branches-error ;
62 : check-declared-effect ( known effect -- )
64 2dup [ [ variables>> ] [ effect>> ] bi ] dip check-variables
65 [ 2drop ] [ drop combinator-unbalanced-branches-error ] if ;