2010-01-14 10:10:13 -05:00
|
|
|
! Copyright (C) 2006, 2010 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2010-02-23 06:16:55 -05:00
|
|
|
USING: kernel math math.parser math.order namespaces make
|
|
|
|
sequences strings words assocs combinators accessors arrays
|
|
|
|
quotations ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: effects
|
|
|
|
|
2010-01-14 10:10:13 -05:00
|
|
|
TUPLE: effect
|
|
|
|
{ in array read-only }
|
|
|
|
{ out array read-only }
|
2010-03-05 16:30:10 -05:00
|
|
|
{ terminated? read-only }
|
|
|
|
{ in-var read-only }
|
|
|
|
{ out-var read-only } ;
|
|
|
|
|
|
|
|
: ?terminated ( out -- out terminated? )
|
|
|
|
dup { "*" } = [ drop { } t ] [ f ] if ;
|
2009-08-22 20:56:28 -04:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: <effect> ( in out -- effect )
|
2010-03-05 16:30:10 -05:00
|
|
|
?terminated f f effect boa ;
|
|
|
|
|
|
|
|
: <terminated-effect> ( in out terminated? -- effect )
|
|
|
|
f f effect boa ; inline
|
|
|
|
|
|
|
|
: <variable-effect> ( in-var in out-var out -- effect )
|
|
|
|
swap [ rot ] dip [ ?terminated ] 2dip effect boa ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: effect-height ( effect -- n )
|
2010-01-14 10:10:13 -05:00
|
|
|
[ out>> length ] [ in>> length ] bi - ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-03-11 04:37:33 -05:00
|
|
|
: variable-effect? ( effect -- ? )
|
|
|
|
[ in-var>> ] [ out-var>> ] bi or ;
|
|
|
|
: bivariable-effect? ( effect -- ? )
|
|
|
|
[ in-var>> ] [ out-var>> ] bi = not ;
|
|
|
|
|
2009-04-30 22:08:29 -04:00
|
|
|
: effect<= ( effect1 effect2 -- ? )
|
2007-09-20 18:09:08 -04:00
|
|
|
{
|
2008-06-08 16:32:55 -04:00
|
|
|
{ [ over terminated?>> ] [ t ] }
|
|
|
|
{ [ dup terminated?>> ] [ f ] }
|
2010-03-11 04:37:33 -05:00
|
|
|
{ [ 2dup [ bivariable-effect? ] either? ] [ f ] }
|
|
|
|
{ [ 2dup [ variable-effect? ] [ variable-effect? not ] bi* and ] [ f ] }
|
2010-01-14 10:10:13 -05:00
|
|
|
{ [ 2dup [ in>> length ] bi@ > ] [ f ] }
|
2008-03-29 21:36:58 -04:00
|
|
|
{ [ 2dup [ effect-height ] bi@ = not ] [ f ] }
|
2008-04-11 13:53:22 -04:00
|
|
|
[ t ]
|
2009-03-01 21:12:35 -05:00
|
|
|
} cond 2nip ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-04-30 22:08:29 -04:00
|
|
|
: effect= ( effect1 effect2 -- ? )
|
2010-01-14 10:10:13 -05:00
|
|
|
[ [ in>> length ] bi@ = ]
|
|
|
|
[ [ out>> length ] bi@ = ]
|
2009-04-30 22:08:29 -04:00
|
|
|
[ [ terminated?>> ] bi@ = ]
|
|
|
|
2tri and and ;
|
|
|
|
|
2008-07-18 20:22:59 -04:00
|
|
|
GENERIC: effect>string ( obj -- str )
|
|
|
|
M: string effect>string ;
|
2009-02-09 17:26:56 -05:00
|
|
|
M: object effect>string drop "object" ;
|
2008-07-18 20:22:59 -04:00
|
|
|
M: word effect>string name>> ;
|
2008-08-11 00:36:46 -04:00
|
|
|
M: integer effect>string number>string ;
|
2008-12-03 20:12:48 -05:00
|
|
|
M: pair effect>string first2 [ effect>string ] bi@ ": " glue ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: stack-picture ( seq -- string )
|
2008-07-18 20:22:59 -04:00
|
|
|
[ [ effect>string % CHAR: \s , ] each ] "" make ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2010-03-05 16:30:10 -05:00
|
|
|
: var-picture ( var -- string )
|
|
|
|
[ ".." " " surround ]
|
|
|
|
[ "" ] if* ;
|
|
|
|
|
2008-07-18 20:22:59 -04:00
|
|
|
M: effect effect>string ( effect -- string )
|
2007-09-20 18:09:08 -04:00
|
|
|
[
|
|
|
|
"( " %
|
2010-03-05 16:30:10 -05:00
|
|
|
dup in-var>> var-picture %
|
|
|
|
dup in>> stack-picture % "-- " %
|
|
|
|
dup out-var>> var-picture %
|
|
|
|
dup out>> stack-picture %
|
|
|
|
dup terminated?>> [ "* " % ] when
|
|
|
|
drop
|
2007-09-20 18:09:08 -04:00
|
|
|
")" %
|
|
|
|
] "" make ;
|
|
|
|
|
2009-09-01 15:39:22 -04:00
|
|
|
GENERIC: effect>type ( obj -- type )
|
|
|
|
M: object effect>type drop object ;
|
|
|
|
M: word effect>type ;
|
|
|
|
M: pair effect>type second effect>type ;
|
|
|
|
|
2010-01-14 10:10:13 -05:00
|
|
|
: effect-in-types ( effect -- input-types )
|
|
|
|
in>> [ effect>type ] map ;
|
|
|
|
|
|
|
|
: effect-out-types ( effect -- input-types )
|
|
|
|
out>> [ effect>type ] map ;
|
|
|
|
|
2008-02-04 20:38:31 -05:00
|
|
|
GENERIC: stack-effect ( word -- effect/f )
|
|
|
|
|
2010-02-23 06:16:55 -05:00
|
|
|
M: word stack-effect
|
|
|
|
[ "declared-effect" word-prop ]
|
|
|
|
[ parent-word dup [ stack-effect ] when ] bi or ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2011-10-18 16:18:42 -04:00
|
|
|
M: deferred stack-effect call-next-method ( -- * ) or ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: effect clone
|
2008-06-26 21:47:36 -04:00
|
|
|
[ in>> clone ] [ out>> clone ] bi <effect> ;
|
2007-09-29 19:43:03 -04:00
|
|
|
|
2008-07-18 20:22:59 -04:00
|
|
|
: stack-height ( word -- n )
|
|
|
|
stack-effect effect-height ;
|
|
|
|
|
2009-02-09 17:26:56 -05:00
|
|
|
: shuffle-mapping ( effect -- mapping )
|
|
|
|
[ out>> ] [ in>> ] bi [ index ] curry map ;
|
2007-09-29 19:43:03 -04:00
|
|
|
|
2008-09-05 21:39:45 -04:00
|
|
|
: shuffle ( stack shuffle -- newstack )
|
2009-02-09 17:26:56 -05:00
|
|
|
shuffle-mapping swap nths ;
|
2009-03-16 07:16:51 -04:00
|
|
|
|
|
|
|
: add-effect-input ( effect -- effect' )
|
2010-03-05 16:30:10 -05:00
|
|
|
[ in>> "obj" suffix ] [ out>> ] [ terminated?>> ] tri <terminated-effect> ;
|
2009-04-30 22:08:29 -04:00
|
|
|
|
|
|
|
: compose-effects ( effect1 effect2 -- effect' )
|
|
|
|
over terminated?>> [
|
|
|
|
drop
|
|
|
|
] [
|
2010-01-14 10:10:13 -05:00
|
|
|
[ [ [ in>> length ] [ out>> length ] bi ] [ in>> length ] bi* swap [-] + ]
|
|
|
|
[ [ out>> length ] [ [ in>> length ] [ out>> length ] bi ] bi* [ [-] ] dip + ]
|
2009-04-30 22:08:29 -04:00
|
|
|
[ nip terminated?>> ] 2tri
|
2010-01-14 10:10:13 -05:00
|
|
|
[ [ "x" <array> ] bi@ ] dip
|
2010-03-05 16:30:10 -05:00
|
|
|
<terminated-effect>
|
2009-04-30 22:08:29 -04:00
|
|
|
] if ; inline
|