2009-02-06 05:38:54 -05:00
|
|
|
! Copyright (C) 2007, 2009 Slava Pestov.
|
2008-07-20 05:24:37 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: fry accessors arrays kernel words sequences generic math
|
2008-09-10 21:07:00 -04:00
|
|
|
namespaces make quotations assocs combinators classes.tuple
|
2008-07-20 05:24:37 -04:00
|
|
|
classes.tuple.private effects summary hashtables classes generic
|
2009-02-06 05:38:54 -05:00
|
|
|
sets definitions generic.standard slots.private continuations locals
|
2008-07-27 03:32:40 -04:00
|
|
|
stack-checker.backend stack-checker.state stack-checker.visitor
|
2008-11-13 01:07:45 -05:00
|
|
|
stack-checker.errors stack-checker.values
|
|
|
|
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 -- )
|
2008-11-12 20:08:40 -05:00
|
|
|
dup recursive-word?
|
2008-08-10 23:22:26 -04:00
|
|
|
[ call-recursive-word ]
|
|
|
|
[ dup infer-word apply-word/effect ]
|
|
|
|
if ;
|
|
|
|
|
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
|
|
|
|
|
|
|
: (apply-transform) ( word quot n -- )
|
2008-08-14 00:52:49 -04:00
|
|
|
ensure-d dup [ known literal? ] all? [
|
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
|
|
|
|
: bit-member-n 256 ; inline
|
|
|
|
|
|
|
|
: bit-member? ( seq -- ? )
|
|
|
|
#! Can we use a fast byte array test here?
|
|
|
|
{
|
|
|
|
{ [ dup length 8 < ] [ f ] }
|
2009-01-29 23:19:07 -05:00
|
|
|
{ [ dup [ integer? not ] any? ] [ f ] }
|
|
|
|
{ [ dup [ 0 < ] any? ] [ f ] }
|
|
|
|
{ [ dup [ bit-member-n >= ] any? ] [ f ] }
|
2008-08-10 23:22:26 -04:00
|
|
|
[ t ]
|
|
|
|
} cond nip ;
|
|
|
|
|
|
|
|
: bit-member-seq ( seq -- flags )
|
|
|
|
bit-member-n swap [ member? 1 0 ? ] curry B{ } map-as ;
|
|
|
|
|
|
|
|
: exact-float? ( f -- ? )
|
|
|
|
dup float? [ dup >integer >float = ] [ drop f ] if ; inline
|
|
|
|
|
|
|
|
: bit-member-quot ( seq -- newquot )
|
|
|
|
[
|
|
|
|
bit-member-seq ,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
{ [ over fixnum? ] [ ?nth 1 eq? ] }
|
|
|
|
{ [ over bignum? ] [ ?nth 1 eq? ] }
|
|
|
|
{ [ over exact-float? ] [ ?nth 1 eq? ] }
|
|
|
|
[ 2drop f ]
|
|
|
|
} cond
|
|
|
|
] %
|
|
|
|
] [ ] make ;
|
|
|
|
|
|
|
|
: member-quot ( seq -- newquot )
|
|
|
|
dup bit-member? [
|
|
|
|
bit-member-quot
|
2008-07-20 05:24:37 -04:00
|
|
|
] [
|
2008-08-10 23:22:26 -04:00
|
|
|
[ literalize [ t ] ] { } map>assoc
|
2008-08-12 04:18:15 -04:00
|
|
|
[ drop f ] suffix [ case ] curry
|
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
|