]> gitweb.factorcode.org Git - factor.git/blob - basis/stack-checker/row-polymorphism/row-polymorphism.factor
Add context-specific special object table, generalizing catchstack_save and current_c...
[factor.git] / basis / stack-checker / row-polymorphism / row-polymorphism.factor
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
5 stack-checker.backend
6 stack-checker.errors
7 stack-checker.state
8 stack-checker.values
9 stack-checker.visitor ;
10 IN: stack-checker.row-polymorphism
11
12 : with-inner-d ( quot -- inner-d )
13     inner-d-index get
14     [ meta-d length inner-d-index set call ] dip
15     inner-d-index get [ min inner-d-index set ] keep ; inline
16
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
21
22 : with-effect-here ( quot -- effect )
23     meta-d length input-count get
24     [ with-inner-d ] 2dip (effect-here) ; inline
25
26 : (diff-variable) ( diff variable vars -- diff' )
27     [ at* nip ] [ '[ _ _ at - ] ] [ '[ _ _ set-at 0 ] ] 2tri if ;
28
29 : (check-variable) ( actual-count declared-count variable vars -- diff ? )
30     [ - ] 2dip dupd '[ _ _ (diff-variable) t ] [ dup 0 <= ] if ;
31
32 : adjust-variable ( diff var vars -- )
33     pick 0 >= [ at+ ] [ 3drop ] if ; inline
34
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
38
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*
43     ] when ;
44
45 : (check-variables) ( vars declared actual -- ? )
46     [ [ in>>  ] [ in-var>>  ] check-variable ]
47     [ [ out>> ] [ out-var>> ] check-variable ]
48     [ 2drop ] 3tri unify-variables ;
49
50 : check-variables ( vars declared actual -- ? )
51     dup terminated?>> [ 3drop t ] [ (check-variables) ] if ;
52
53 : combinator-branches-effects ( branches -- quots declareds actuals )
54     [ [ known>callable ] { } map-as ]
55     [ [ effect>> ] { } map-as ]
56     [ [ actual>> ] { } map-as ] tri ;
57
58 : combinator-unbalanced-branches-error ( known -- * )
59     [ word>> ] [ branches>> <reversed> combinator-branches-effects ] bi
60     unbalanced-branches-error ;
61
62 : check-declared-effect ( known effect -- )
63     [ >>actual ] keep
64     2dup [ [ variables>> ] [ effect>> ] bi ] dip check-variables
65     [ 2drop ] [ drop combinator-unbalanced-branches-error ] if ;
66