1 ! Copyright (C) 2008 Slava Pestov, Eduardo Cavazos.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences combinators parser splitting math
4 quotations arrays make words locals.backend summary sets ;
7 : _ ( -- * ) "Only valid inside a fry" throw ;
8 : @ ( -- * ) "Only valid inside a fry" throw ;
10 ERROR: >r/r>-in-fry-error ;
14 : [ncurry] ( n -- quot )
20 [ \ curry <repetition> ]
23 M: >r/r>-in-fry-error summary
25 "Explicit retain stack manipulation is not permitted in fried quotations" ;
27 : check-fry ( quot -- quot )
28 dup { load-local load-locals get-local drop-locals } intersect
29 empty? [ >r/r>-in-fry-error ] unless ;
31 PREDICATE: fry-specifier < word { _ @ } memq? ;
33 GENERIC: count-inputs ( quot -- n )
35 M: callable count-inputs [ count-inputs ] sigma ;
36 M: fry-specifier count-inputs drop 1 ;
37 M: object count-inputs drop 0 ;
39 GENERIC: deep-fry ( obj -- )
41 : shallow-fry ( quot -- quot' curry# )
43 [ [ deep-fry ] each ] [ ] make
44 [ dup \ @ = [ drop [ _ call ] ] [ 1array ] if ] map concat
45 { _ } split [ spread>quot ] [ length 1- ] bi ;
49 : fry ( quot -- quot' ) shallow-fry [ncurry] swap prefix ;
52 [ count-inputs \ _ <repetition> % ] [ fry % ] bi ;
54 M: object deep-fry , ;
56 : '[ \ ] parse-until fry over push-all ; parsing