2008-07-20 05:24:37 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-07-24 00:50:21 -04:00
|
|
|
USING: fry accessors quotations kernel sequences namespaces assocs
|
|
|
|
words generic generic.standard generic.standard.engines arrays
|
|
|
|
kernel.private combinators vectors stack-checker
|
|
|
|
stack-checker.state stack-checker.visitor stack-checker.errors
|
|
|
|
stack-checker.backend compiler.tree ;
|
2008-07-20 05:24:37 -04:00
|
|
|
IN: compiler.tree.builder
|
|
|
|
|
2008-07-27 21:25:42 -04:00
|
|
|
: with-tree-builder ( quot -- nodes )
|
|
|
|
[ V{ } clone stack-visitor set ] prepose
|
|
|
|
with-infer ; inline
|
2008-07-24 00:50:21 -04:00
|
|
|
|
2008-07-27 21:25:42 -04:00
|
|
|
GENERIC# build-tree-with 1 ( quot stack -- nodes )
|
2008-07-24 00:50:21 -04:00
|
|
|
|
|
|
|
M: callable build-tree-with
|
|
|
|
#! Not safe to call from inference transforms.
|
|
|
|
[
|
|
|
|
>vector meta-d set
|
|
|
|
f infer-quot
|
|
|
|
] with-tree-builder nip ;
|
|
|
|
|
2008-07-27 21:25:42 -04:00
|
|
|
: build-tree ( quot -- nodes ) f build-tree-with ;
|
2008-07-24 00:50:21 -04:00
|
|
|
|
|
|
|
: (make-specializer) ( class picker -- quot )
|
|
|
|
swap "predicate" word-prop append ;
|
|
|
|
|
|
|
|
: make-specializer ( classes -- quot )
|
|
|
|
dup length <reversed>
|
|
|
|
[ (picker) 2array ] 2map
|
|
|
|
[ drop object eq? not ] assoc-filter
|
|
|
|
dup empty? [ drop [ t ] ] [
|
|
|
|
[ (make-specializer) ] { } assoc>map
|
|
|
|
unclip [ swap [ f ] \ if 3array append [ ] like ] reduce
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
: specializer-cases ( quot word -- default alist )
|
|
|
|
dup [ array? ] all? [ 1array ] unless [
|
|
|
|
[ make-specializer ] keep
|
|
|
|
'[ , declare ] pick append
|
|
|
|
] { } map>assoc ;
|
|
|
|
|
|
|
|
: method-declaration ( method -- quot )
|
|
|
|
dup "method-generic" word-prop dispatch# object <array>
|
|
|
|
swap "method-class" word-prop prefix ;
|
|
|
|
|
|
|
|
: specialize-method ( quot method -- quot' )
|
|
|
|
method-declaration '[ , declare ] prepend ;
|
|
|
|
|
|
|
|
: specialize-quot ( quot specializer -- quot' )
|
|
|
|
specializer-cases alist>quot ;
|
|
|
|
|
|
|
|
: standard-method? ( method -- ? )
|
|
|
|
dup method-body? [
|
|
|
|
"method-generic" word-prop standard-generic?
|
|
|
|
] [ drop f ] if ;
|
|
|
|
|
|
|
|
: specialized-def ( word -- quot )
|
|
|
|
dup def>> swap {
|
|
|
|
{ [ dup standard-method? ] [ specialize-method ] }
|
|
|
|
{
|
|
|
|
[ dup "specializer" word-prop ]
|
|
|
|
[ "specializer" word-prop specialize-quot ]
|
|
|
|
}
|
|
|
|
[ drop ]
|
|
|
|
} cond ;
|
|
|
|
|
2008-07-27 21:25:42 -04:00
|
|
|
: build-tree-from-word ( word -- effect nodes )
|
2008-07-24 00:50:21 -04:00
|
|
|
[
|
|
|
|
[
|
|
|
|
dup +cannot-infer+ word-prop [ cannot-infer-effect ] when
|
|
|
|
dup "no-compile" word-prop [ cannot-infer-effect ] when
|
|
|
|
dup specialized-def over dup 2array 1array infer-quot
|
|
|
|
finish-word
|
|
|
|
] maybe-cannot-infer
|
|
|
|
] with-tree-builder ;
|
|
|
|
|
|
|
|
: specialized-length ( specializer -- n )
|
|
|
|
dup [ array? ] all? [ first ] when length ;
|