factor/core/effects.factor

49 lines
1.3 KiB
Factor
Raw Normal View History

2006-08-15 16:29:35 -04:00
! Copyright (C) 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: words
USING: kernel math namespaces sequences strings generic ;
TUPLE: effect in out terminated? ;
C: effect
[
over { "*" } sequence=
[ nip t swap set-effect-terminated? ]
[ set-effect-out ] if
] keep
[ set-effect-in ] keep ;
: effect-height ( effect -- n )
dup effect-out length swap effect-in length - ;
: effect<= ( eff1 eff2 -- ? )
2dup [ effect-terminated? ] 2apply = >r
2dup [ effect-in length ] 2apply <= >r
[ effect-height ] 2apply number= r> and r> and ;
GENERIC: (stack-picture) ( obj -- str )
M: string (stack-picture) ;
M: word (stack-picture) word-name ;
M: integer (stack-picture) drop "object" ;
2006-08-15 16:29:35 -04:00
: stack-picture ( seq -- string )
[ [ (stack-picture) % CHAR: \s , ] each ] "" make ;
2006-08-15 16:29:35 -04:00
: effect>string ( effect -- string )
[
"( " %
dup effect-in stack-picture %
"-- " %
dup effect-out stack-picture %
effect-terminated? [ "* " % ] when
")" %
] "" make ;
2006-08-15 21:23:05 -04:00
: stack-effect ( word -- effect/f )
2006-08-18 18:47:41 -04:00
dup "declared-effect" word-prop [ ] [
2006-11-12 22:14:04 -05:00
dup "inferred-effect" word-prop [ ] [ drop f ] ?if
2006-08-15 16:29:35 -04:00
] ?if ;
2006-09-15 20:59:47 -04:00
M: effect clone
[ effect-in clone ] keep effect-out clone <effect> ;