2010-03-04 22:30:08 -05:00
|
|
|
! (c)2010 Joe Groff bsd license
|
|
|
|
USING: accessors arrays assocs combinators combinators.short-circuit
|
|
|
|
continuations effects fry kernel locals math namespaces
|
2010-03-05 17:27:36 -05:00
|
|
|
quotations sequences splitting
|
2010-03-04 22:30:08 -05:00
|
|
|
stack-checker.backend
|
|
|
|
stack-checker.errors
|
|
|
|
stack-checker.known-words
|
|
|
|
stack-checker.values ;
|
|
|
|
IN: stack-checker.row-polymorphism
|
|
|
|
|
|
|
|
<PRIVATE
|
2010-03-05 00:51:49 -05:00
|
|
|
SYMBOLS: current-effect-variables current-effect current-meta-d ;
|
2010-03-04 22:30:08 -05:00
|
|
|
|
|
|
|
: quotation-effect? ( in -- ? )
|
|
|
|
dup pair? [ second effect? ] [ drop f ] if ;
|
|
|
|
|
2010-03-05 00:51:49 -05:00
|
|
|
SYMBOL: (unknown)
|
|
|
|
|
|
|
|
GENERIC: >error-quot ( known -- quot )
|
|
|
|
|
|
|
|
M: object >error-quot drop (unknown) ;
|
|
|
|
M: literal >error-quot value>> ;
|
|
|
|
M: composed >error-quot
|
|
|
|
[ quot1>> known >error-quot ] [ quot2>> known >error-quot ] bi
|
|
|
|
\ compose [ ] 3sequence ;
|
|
|
|
M: curried >error-quot
|
|
|
|
[ obj>> known >error-quot ] [ quot>> known >error-quot ] bi
|
|
|
|
\ curry [ ] 3sequence ;
|
|
|
|
|
|
|
|
: >error-branches-and-quots ( branch/values -- branches quots )
|
|
|
|
[ [ second ] [ known >error-quot ] bi* ] assoc-map unzip ;
|
|
|
|
|
|
|
|
: abandon-check ( -- * )
|
|
|
|
current-word get
|
|
|
|
current-effect get in>> current-meta-d get zip
|
|
|
|
[ first quotation-effect? ] filter
|
|
|
|
>error-branches-and-quots
|
|
|
|
invalid-quotation-input ;
|
2010-03-04 22:30:08 -05:00
|
|
|
|
|
|
|
:: check-variable ( actual-count declared-count variable -- difference )
|
|
|
|
actual-count declared-count -
|
|
|
|
variable [
|
2010-03-05 00:51:49 -05:00
|
|
|
variable current-effect-variables get at* nip
|
|
|
|
[ variable current-effect-variables get at - ]
|
|
|
|
[ variable current-effect-variables get set-at 0 ] if
|
2010-03-04 22:30:08 -05:00
|
|
|
] [
|
|
|
|
dup [ abandon-check ] unless-zero
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
: adjust-variable ( diff var -- )
|
|
|
|
over 0 >=
|
2010-03-05 00:51:49 -05:00
|
|
|
[ current-effect-variables get at+ ]
|
2010-03-04 22:30:08 -05:00
|
|
|
[ 2drop ] if ; inline
|
|
|
|
|
|
|
|
:: (check-input) ( declared actual -- )
|
2010-03-05 17:34:50 -05:00
|
|
|
actual declared [ in>> length ] bi@ declared in-var>>
|
|
|
|
[ check-variable ] keep :> ( in-diff in-var )
|
|
|
|
actual declared [ out>> length ] bi@ declared out-var>>
|
|
|
|
[ check-variable ] keep :> ( out-diff out-var )
|
2010-03-04 22:30:08 -05:00
|
|
|
{ [ in-var not ] [ out-var not ] [ in-diff out-diff = ] } 0||
|
|
|
|
[
|
|
|
|
in-var [ in-diff swap adjust-variable ] when*
|
|
|
|
out-var [ out-diff swap adjust-variable ] when*
|
|
|
|
] [
|
|
|
|
abandon-check
|
|
|
|
] if ;
|
|
|
|
|
2010-03-04 23:15:26 -05:00
|
|
|
GENERIC: (infer-known) ( known -- effect )
|
2010-03-04 22:30:08 -05:00
|
|
|
|
2010-03-04 23:15:26 -05:00
|
|
|
M: object (infer-known)
|
2010-03-04 22:30:08 -05:00
|
|
|
current-word get bad-macro-input ;
|
2010-03-04 23:15:26 -05:00
|
|
|
M: literal (infer-known)
|
2010-03-05 17:27:36 -05:00
|
|
|
value>> dup callable? [ (infer) ] [ abandon-check ] if ;
|
2010-03-04 23:15:26 -05:00
|
|
|
M: composed (infer-known)
|
|
|
|
[ quot1>> known (infer-known) ] [ quot2>> known (infer-known) ] bi compose-effects ;
|
|
|
|
M: curried (infer-known)
|
|
|
|
(( -- x )) swap quot>> known (infer-known) compose-effects ;
|
|
|
|
|
|
|
|
: infer-known ( value -- effect )
|
|
|
|
(infer-known) ; inline
|
2010-03-04 22:30:08 -05:00
|
|
|
|
|
|
|
: check-input ( in value -- )
|
|
|
|
over quotation-effect? [
|
|
|
|
[ second ] dip known infer-known (check-input)
|
|
|
|
] [ 2drop ] if ;
|
|
|
|
|
2010-03-04 23:15:26 -05:00
|
|
|
: normalize-variables ( -- variables' )
|
2010-03-05 00:51:49 -05:00
|
|
|
current-effect-variables get dup values [
|
2010-03-04 22:30:08 -05:00
|
|
|
infimum dup 0 <
|
|
|
|
[ '[ _ - ] assoc-map ] [ drop ] if
|
|
|
|
] unless-empty ;
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: infer-polymorphic-vars ( effect -- variables )
|
2010-03-05 00:51:49 -05:00
|
|
|
H{ } clone current-effect-variables set
|
|
|
|
dup current-effect set
|
|
|
|
in>> dup length ensure-d dup current-meta-d set
|
|
|
|
[ check-input ] 2each
|
2010-03-04 23:15:26 -05:00
|
|
|
normalize-variables ;
|
2010-03-04 22:30:08 -05:00
|
|
|
|
|
|
|
: check-polymorphic-effect ( word -- )
|
2010-03-04 23:15:26 -05:00
|
|
|
current-word get [
|
|
|
|
dup current-word set stack-effect infer-polymorphic-vars drop
|
|
|
|
] dip current-word set ;
|
2010-03-04 22:30:08 -05:00
|
|
|
|
|
|
|
SYMBOL: infer-polymorphic?
|