factor/basis/compiler/tree/checker/checker.factor

73 lines
2.0 KiB
Factor
Raw Normal View History

2008-08-10 00:00:27 -04:00
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: sequences kernel sets namespaces accessors assocs
2008-08-14 00:52:49 -04:00
arrays combinators continuations columns math
2008-08-10 00:00:27 -04:00
compiler.tree
compiler.tree.def-use
compiler.tree.combinators ;
IN: compiler.tree.checker
! Check some invariants.
ERROR: check-use-error value message ;
: check-use ( value uses -- )
[ empty? [ "No use" check-use-error ] [ drop ] if ]
[ all-unique? [ drop ] [ "Uses not all unique" check-use-error ] if ] 2bi ;
: check-def-use ( -- )
def-use get [ uses>> check-use ] assoc-each ;
2008-08-14 00:52:49 -04:00
GENERIC: check-node* ( node -- )
2008-08-10 00:00:27 -04:00
2008-08-14 00:52:49 -04:00
M: #shuffle check-node*
2008-08-10 00:00:27 -04:00
[ [ mapping>> values ] [ in-d>> ] bi subset? [ "Bad mapping inputs" throw ] unless ]
[ [ mapping>> keys ] [ out-d>> ] bi set= [ "Bad mapping outputs" throw ] unless ]
bi ;
: check-lengths ( seq -- )
[ length ] map all-equal? [ "Bad lengths" throw ] unless ;
2008-08-14 00:52:49 -04:00
M: #copy check-node* inputs/outputs 2array check-lengths ;
2008-08-10 00:00:27 -04:00
2008-08-14 00:52:49 -04:00
M: #>r check-node* inputs/outputs 2array check-lengths ;
2008-08-10 00:00:27 -04:00
2008-08-14 00:52:49 -04:00
M: #r> check-node* inputs/outputs 2array check-lengths ;
2008-08-10 00:00:27 -04:00
2008-08-14 00:52:49 -04:00
M: #return-recursive check-node* inputs/outputs 2array check-lengths ;
2008-08-10 00:00:27 -04:00
2008-08-14 00:52:49 -04:00
M: #phi check-node*
2008-08-10 00:00:27 -04:00
{
2008-08-14 00:52:49 -04:00
[ [ phi-in-d>> <flipped> ] [ out-d>> ] bi 2array check-lengths ]
[ [ phi-in-r>> <flipped> ] [ out-r>> ] bi 2array check-lengths ]
2008-08-10 00:00:27 -04:00
[ phi-in-d>> check-lengths ]
[ phi-in-r>> check-lengths ]
} cleave ;
2008-08-14 00:52:49 -04:00
M: #enter-recursive check-node*
2008-08-10 00:00:27 -04:00
[ [ in-d>> ] [ out-d>> ] bi 2array check-lengths ]
2008-08-13 15:17:04 -04:00
[ recursive-phi-in check-lengths ]
2008-08-10 00:00:27 -04:00
bi ;
2008-08-14 00:52:49 -04:00
M: #push check-node*
2008-08-10 00:00:27 -04:00
out-d>> length 1 = [ "Bad #push" throw ] unless ;
2008-08-14 00:52:49 -04:00
M: node check-node* drop ;
: check-values ( seq -- )
[ integer? ] all? [ "Bad values" throw ] unless ;
2008-08-10 00:00:27 -04:00
ERROR: check-node-error node error ;
2008-08-14 00:52:49 -04:00
: check-node ( node -- )
[
[ node-uses-values check-values ]
[ node-defs-values check-values ]
[ check-node* ]
tri
] [ check-node-error ] recover ;
2008-08-10 00:00:27 -04:00
: check-nodes ( nodes -- )
compute-def-use
check-def-use
2008-08-14 00:52:49 -04:00
[ check-node ] each-node ;