1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: fry arrays generic io io.streams.string kernel math namespaces
4 parser sequences strings vectors words quotations effects classes
5 continuations assocs combinators compiler.errors accessors math.order
6 definitions sets hints macros stack-checker.state
7 stack-checker.visitor stack-checker.errors stack-checker.values
8 stack-checker.recursive-state stack-checker.dependencies summary ;
9 IN: stack-checker.backend
11 : push-d ( obj -- ) meta-d push ;
13 : introduce-values ( values -- )
14 [ [ [ input-parameter ] dip set-known ] each ]
15 [ length input-count +@ ]
20 meta-d [ <value> dup 1array introduce-values ] [ pop ] if-empty ;
22 : peek-d ( -- obj ) pop-d dup push-d ;
24 : make-values ( n -- values )
25 [ <value> ] replicate ;
27 : ensure-d ( n -- values )
28 meta-d 2dup length > [
30 [ nip >array ] [ length - make-values ] [ nip delete-all ] 2tri
31 [ introduce-values ] [ meta-d push-all ] bi
35 : shorten-by ( n seq -- )
36 [ length swap - ] keep shorten ; inline
38 : consume-d ( n -- seq )
39 [ ensure-d ] [ meta-d shorten-by ] bi ;
41 : output-d ( values -- ) meta-d push-all ;
43 : produce-d ( n -- values )
44 make-values dup meta-d push-all ;
46 : push-r ( obj -- ) meta-r push ;
50 [ too-many-r> ] [ pop ] if ;
52 : consume-r ( n -- seq )
55 [ swap tail* ] [ shorten-by ] 2bi ;
57 : output-r ( seq -- ) meta-r push-all ;
59 : push-literal ( obj -- )
62 : pop-literal ( -- rstate obj )
66 [ literal [ recursion>> ] [ value>> ] bi ] bi
67 ] [ pop recursive-state get swap ] if-empty ;
69 : literals-available? ( n -- literals ? )
70 literals get 2dup length <=
71 [ [ swap tail* ] [ shorten-by ] 2bi t ] [ 2drop f f ] if ;
73 GENERIC: apply-object ( obj -- )
75 M: wrapper apply-object
77 [ dup word? [ called-dependency depends-on ] [ drop ] if ]
81 M: object apply-object push-literal ;
84 terminated? on meta-d clone meta-r clone #terminate, ;
87 meta-r empty? [ too-many->r ] unless ;
89 : infer-quot-here ( quot -- )
91 V{ } clone \ meta-r set
92 [ apply-object terminated? get not ] all?
93 [ commit-literals check->r ] [ literals get delete-all ] if
96 : infer-quot ( quot rstate -- )
100 ] dip recursive-state set ;
102 : time-bomb ( error -- )
103 '[ _ throw ] infer-quot-here ;
105 ERROR: bad-call obj ;
108 drop "call must be given a callable" ;
110 : infer-literal-quot ( literal -- )
111 dup recursive-quotation? [
112 value>> recursive-quotation-error
114 dup value>> callable? [
116 [ [ recursion>> ] keep add-local-quotation ]
119 value>> \ bad-call boa time-bomb
124 consume-d dup copy-values [ nip output-r ] [ #>r, ] 2bi ;
127 consume-r dup copy-values [ nip output-d ] [ #r>, ] 2bi ;
129 : consume/produce ( effect quot: ( inputs outputs -- ) -- )
130 '[ [ in>> length consume-d ] [ out>> length produce-d ] bi @ ]
131 [ terminated?>> [ terminate ] when ]
134 : apply-word/effect ( word effect -- )
135 swap '[ _ #call, ] consume/produce ;
138 meta-d clone #return, ;
140 : required-stack-effect ( word -- effect )
141 dup stack-effect [ ] [ missing-effect ] ?if ;
143 : infer-word ( word -- )
145 { [ dup macro? ] [ do-not-compile ] }
146 { [ dup "no-compile" word-prop ] [ do-not-compile ] }
147 [ dup required-stack-effect apply-word/effect ]
150 : with-infer ( quot -- effect visitor )
159 ] with-scope ; inline