]> gitweb.factorcode.org Git - factor.git/blob - basis/fry/fry.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / fry / fry.factor
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 ;
5 IN: fry
6
7 : _ ( -- * ) "Only valid inside a fry" throw ;
8 : @ ( -- * ) "Only valid inside a fry" throw ;
9
10 ERROR: >r/r>-in-fry-error ;
11
12 <PRIVATE
13
14 : [ncurry] ( n -- quot )
15     {
16         { 0 [ [ ] ] }
17         { 1 [ [ curry ] ] }
18         { 2 [ [ 2curry ] ] }
19         { 3 [ [ 3curry ] ] }
20         [ \ curry <repetition> ]
21     } case ;
22
23 M: >r/r>-in-fry-error summary
24     drop
25     "Explicit retain stack manipulation is not permitted in fried quotations" ;
26
27 : check-fry ( quot -- quot )
28     dup { >r r> load-locals get-local drop-locals } intersect
29     empty? [ >r/r>-in-fry-error ] unless ;
30
31 : shallow-fry ( quot -- quot' )
32     check-fry
33     [ dup \ @ = [ drop [ _ call ] ] [ 1array ] if ] map concat
34     { _ } split [ length 1- [ncurry] ] [ spread>quot ] bi prefix ;
35
36 PREDICATE: fry-specifier < word { _ @ } memq? ;
37
38 GENERIC: count-inputs ( quot -- n )
39
40 M: callable count-inputs [ count-inputs ] sigma ;
41 M: fry-specifier count-inputs drop 1 ;
42 M: object count-inputs drop 0 ;
43
44 PRIVATE>
45
46 : fry ( quot -- quot' )
47     [
48         [
49             dup callable? [
50                 [ count-inputs \ _ <repetition> % ] [ fry % ] bi
51             ] [ , ] if
52         ] each
53     ] [ ] make shallow-fry ;
54
55 : '[ \ ] parse-until fry over push-all ; parsing