2009-03-16 21:11:36 -04:00
|
|
|
! Copyright (C) 2007, 2009 Slava Pestov, Daniel Ehrenberg.
|
2008-07-20 05:24:37 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-03-16 21:11:36 -04:00
|
|
|
USING: fry accessors arrays kernel kernel.private combinators.private
|
2009-03-21 04:10:21 -04:00
|
|
|
words sequences generic math math.order namespaces make quotations assocs
|
|
|
|
combinators combinators.short-circuit classes.tuple
|
|
|
|
classes.tuple.private effects summary hashtables classes generic sets
|
|
|
|
definitions generic.standard slots.private continuations locals
|
|
|
|
generalizations stack-checker.backend stack-checker.state
|
|
|
|
stack-checker.visitor stack-checker.errors stack-checker.values
|
2008-11-13 01:07:45 -05:00
|
|
|
stack-checker.recursive-state ;
|
2008-07-20 05:24:37 -04:00
|
|
|
IN: stack-checker.transforms
|
|
|
|
|
2008-08-10 23:22:26 -04:00
|
|
|
: give-up-transform ( word -- )
|
2009-03-13 05:28:24 -04:00
|
|
|
{
|
|
|
|
{ [ dup "inferred-effect" word-prop ] [ cached-infer ] }
|
|
|
|
{ [ dup recursive-word? ] [ call-recursive-word ] }
|
|
|
|
[ dup infer-word apply-word/effect ]
|
|
|
|
} cond ;
|
2008-08-10 23:22:26 -04:00
|
|
|
|
2009-02-06 05:38:54 -05:00
|
|
|
:: ((apply-transform)) ( word quot values stack rstate -- )
|
|
|
|
rstate recursive-state
|
|
|
|
[ stack quot with-datastack first ] with-variable
|
|
|
|
[
|
|
|
|
word inlined-dependency depends-on
|
|
|
|
values [ length meta-d shorten-by ] [ #drop, ] bi
|
|
|
|
rstate infer-quot
|
|
|
|
] [ word give-up-transform ] if* ;
|
2008-08-10 23:22:26 -04:00
|
|
|
|
2009-02-06 11:21:55 -05:00
|
|
|
: literals? ( values -- ? ) [ literal-value? ] all? ;
|
|
|
|
|
2008-08-10 23:22:26 -04:00
|
|
|
: (apply-transform) ( word quot n -- )
|
2009-02-06 11:21:55 -05:00
|
|
|
ensure-d dup literals? [
|
2009-02-06 05:38:54 -05:00
|
|
|
dup empty? [ dup recursive-state get ] [
|
2008-08-14 00:52:49 -04:00
|
|
|
[ ]
|
2008-08-10 23:22:26 -04:00
|
|
|
[ [ literal value>> ] map ]
|
2008-08-14 00:52:49 -04:00
|
|
|
[ first literal recursion>> ] tri
|
2008-08-10 23:22:26 -04:00
|
|
|
] if
|
|
|
|
((apply-transform))
|
|
|
|
] [ 2drop give-up-transform ] if ;
|
2008-07-23 01:17:08 -04:00
|
|
|
|
|
|
|
: apply-transform ( word -- )
|
2009-02-06 05:38:54 -05:00
|
|
|
[ ] [ "transform-quot" word-prop ] [ "transform-n" word-prop ] tri
|
|
|
|
(apply-transform) ;
|
2008-07-23 01:17:08 -04:00
|
|
|
|
|
|
|
: apply-macro ( word -- )
|
2009-02-06 05:38:54 -05:00
|
|
|
[ ] [ "macro" word-prop ] [ "declared-effect" word-prop in>> length ] tri
|
|
|
|
(apply-transform) ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
|
|
|
: define-transform ( word quot n -- )
|
2008-08-31 20:17:04 -04:00
|
|
|
[ drop "transform-quot" set-word-prop ]
|
|
|
|
[ nip "transform-n" set-word-prop ]
|
2008-07-23 01:17:08 -04:00
|
|
|
3bi ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
|
|
|
! Combinators
|
|
|
|
\ cond [ cond>quot ] 1 define-transform
|
|
|
|
|
|
|
|
\ case [
|
2008-09-06 20:13:59 -04:00
|
|
|
[
|
|
|
|
[ no-case ]
|
2008-07-20 05:24:37 -04:00
|
|
|
] [
|
2009-02-04 03:41:30 -05:00
|
|
|
dup peek callable? [
|
2008-07-20 05:24:37 -04:00
|
|
|
dup peek swap but-last
|
|
|
|
] [
|
|
|
|
[ no-case ] swap
|
|
|
|
] if case>quot
|
2008-09-06 20:13:59 -04:00
|
|
|
] if-empty
|
2008-07-20 05:24:37 -04:00
|
|
|
] 1 define-transform
|
|
|
|
|
|
|
|
\ cleave [ cleave>quot ] 1 define-transform
|
|
|
|
|
|
|
|
\ 2cleave [ 2cleave>quot ] 1 define-transform
|
|
|
|
|
|
|
|
\ 3cleave [ 3cleave>quot ] 1 define-transform
|
|
|
|
|
|
|
|
\ spread [ spread>quot ] 1 define-transform
|
|
|
|
|
2008-08-10 23:22:26 -04:00
|
|
|
\ (call-next-method) [
|
2008-11-22 20:57:25 -05:00
|
|
|
[
|
|
|
|
[ "method-class" word-prop ]
|
|
|
|
[ "method-generic" word-prop ] bi
|
|
|
|
[ inlined-dependency depends-on ] bi@
|
2008-11-28 02:11:03 -05:00
|
|
|
] [
|
|
|
|
[ next-method-quot ]
|
|
|
|
[ '[ _ no-next-method ] ] bi or
|
|
|
|
] bi
|
2008-11-22 20:57:25 -05:00
|
|
|
] 1 define-transform
|
2008-08-10 23:22:26 -04:00
|
|
|
|
|
|
|
! Constructors
|
2008-07-20 05:24:37 -04:00
|
|
|
\ boa [
|
|
|
|
dup tuple-class? [
|
2008-08-30 03:31:27 -04:00
|
|
|
dup inlined-dependency depends-on
|
2008-09-19 01:26:27 -04:00
|
|
|
[ "boa-check" word-prop [ ] or ]
|
2008-09-10 23:11:40 -04:00
|
|
|
[ tuple-layout '[ _ <tuple-boa> ] ]
|
2008-07-20 05:24:37 -04:00
|
|
|
bi append
|
2008-08-10 23:22:26 -04:00
|
|
|
] [ drop f ] if
|
|
|
|
] 1 define-transform
|
|
|
|
|
|
|
|
\ new [
|
|
|
|
dup tuple-class? [
|
2008-08-30 03:31:27 -04:00
|
|
|
dup inlined-dependency depends-on
|
2008-09-03 04:46:56 -04:00
|
|
|
[
|
|
|
|
[ all-slots [ initial>> literalize , ] each ]
|
|
|
|
[ literalize , ] bi
|
|
|
|
\ boa ,
|
|
|
|
] [ ] make
|
2008-08-10 23:22:26 -04:00
|
|
|
] [ drop f ] if
|
|
|
|
] 1 define-transform
|
|
|
|
|
|
|
|
! Membership testing
|
2009-03-21 04:10:21 -04:00
|
|
|
CONSTANT: bit-member-max 256
|
2008-08-10 23:22:26 -04:00
|
|
|
|
|
|
|
: bit-member? ( seq -- ? )
|
|
|
|
#! Can we use a fast byte array test here?
|
|
|
|
{
|
2009-03-21 04:10:21 -04:00
|
|
|
[ length 4 > ]
|
|
|
|
[ [ integer? ] all? ]
|
|
|
|
[ [ 0 bit-member-max between? ] any? ]
|
|
|
|
} 1&& ;
|
2008-08-10 23:22:26 -04:00
|
|
|
|
|
|
|
: bit-member-seq ( seq -- flags )
|
2009-03-21 04:10:21 -04:00
|
|
|
[ supremum 1+ ] keep '[ _ member? 1 0 ? ] B{ } map-as ;
|
2008-08-10 23:22:26 -04:00
|
|
|
|
|
|
|
: bit-member-quot ( seq -- newquot )
|
2009-03-21 04:10:21 -04:00
|
|
|
bit-member-seq
|
|
|
|
'[
|
|
|
|
_ {
|
|
|
|
{ [ over fixnum? ] [ ?nth 1 eq? ] }
|
|
|
|
{ [ over bignum? ] [ ?nth 1 eq? ] }
|
|
|
|
[ 2drop f ]
|
|
|
|
} cond
|
|
|
|
] ;
|
2008-08-10 23:22:26 -04:00
|
|
|
|
|
|
|
: member-quot ( seq -- newquot )
|
|
|
|
dup bit-member? [
|
|
|
|
bit-member-quot
|
2008-07-20 05:24:37 -04:00
|
|
|
] [
|
2009-03-16 21:11:36 -04:00
|
|
|
dup length 4 <= [
|
|
|
|
[ drop f ] swap
|
|
|
|
[ literalize [ t ] ] { } map>assoc linear-case-quot
|
|
|
|
] [
|
|
|
|
unique [ key? ] curry
|
|
|
|
] if
|
2008-08-10 23:22:26 -04:00
|
|
|
] if ;
|
|
|
|
|
|
|
|
\ member? [
|
|
|
|
dup sequence? [ member-quot ] [ drop f ] if
|
2008-07-20 05:24:37 -04:00
|
|
|
] 1 define-transform
|
|
|
|
|
2008-08-10 23:22:26 -04:00
|
|
|
: memq-quot ( seq -- newquot )
|
|
|
|
[ [ dupd eq? ] curry [ drop t ] ] { } map>assoc
|
2008-08-14 00:52:49 -04:00
|
|
|
[ drop f ] suffix [ cond ] curry ;
|
2008-08-10 23:22:26 -04:00
|
|
|
|
|
|
|
\ memq? [
|
|
|
|
dup sequence? [ memq-quot ] [ drop f ] if
|
|
|
|
] 1 define-transform
|
2009-02-10 17:42:35 -05:00
|
|
|
|
2009-03-27 21:05:23 -04:00
|
|
|
! Index search
|
|
|
|
\ index [
|
|
|
|
dup sequence? [
|
|
|
|
dup length 4 >= [
|
|
|
|
dup length zip >hashtable '[ _ at ]
|
|
|
|
] [ drop f ] if
|
|
|
|
] [ drop f ] if
|
|
|
|
] 1 define-transform
|
|
|
|
|
2009-02-10 17:42:35 -05:00
|
|
|
! Shuffling
|
|
|
|
: nths-quot ( indices -- quot )
|
|
|
|
[ [ '[ _ swap nth ] ] map ] [ length ] bi
|
|
|
|
'[ _ cleave _ narray ] ;
|
|
|
|
|
|
|
|
\ shuffle [
|
|
|
|
shuffle-mapping nths-quot
|
|
|
|
] 1 define-transform
|