factor/extra/fry/fry.factor

67 lines
1.8 KiB
Factor
Raw Normal View History

2008-03-03 17:44:24 -05:00
! Copyright (C) 2008 Slava Pestov, Eduardo Cavazos.
! See http://factorcode.org/license.txt for BSD license.
2008-07-03 12:25:27 -04:00
USING: kernel sequences combinators parser splitting math
quotations arrays namespaces qualified sequences.deep sequences.lib ;
2008-03-11 04:30:35 -04:00
QUALIFIED: namespaces
2008-03-03 17:44:24 -05:00
IN: fry
2008-06-08 16:32:55 -04:00
: , ( -- * ) "Only valid inside a fry" throw ;
: @ ( -- * ) "Only valid inside a fry" throw ;
: _ ( -- * ) "Only valid inside a fry" throw ;
2008-03-03 17:44:24 -05:00
2008-04-22 17:29:20 -04:00
DEFER: (shallow-fry)
2008-07-02 16:57:07 -04:00
DEFER: shallow-fry
2008-03-03 17:44:24 -05:00
2008-04-22 17:29:20 -04:00
: ((shallow-fry)) ( accum quot adder -- result )
2008-07-02 16:57:07 -04:00
>r shallow-fry r>
2008-03-03 17:44:24 -05:00
append swap dup empty? [ drop ] [
[ prepose ] curry append
2008-03-03 17:44:24 -05:00
] if ; inline
2008-04-22 17:29:20 -04:00
: (shallow-fry) ( accum quot -- result )
2008-03-03 17:44:24 -05:00
dup empty? [
drop 1quotation
] [
unclip {
2008-04-22 17:29:20 -04:00
{ \ , [ [ curry ] ((shallow-fry)) ] }
{ \ @ [ [ compose ] ((shallow-fry)) ] }
2008-03-11 04:30:35 -04:00
! to avoid confusion, remove if fry goes core
2008-04-22 17:29:20 -04:00
{ \ namespaces:, [ [ curry ] ((shallow-fry)) ] }
2008-03-11 04:30:35 -04:00
2008-04-22 17:29:20 -04:00
[ swap >r suffix r> (shallow-fry) ]
2008-03-03 17:44:24 -05:00
} case
] if ;
2008-04-22 17:29:20 -04:00
: shallow-fry ( quot -- quot' ) [ ] swap (shallow-fry) ;
2008-03-03 17:44:24 -05:00
2008-07-03 12:25:27 -04:00
: deep-fry ( quot -- quot )
{ _ } last-split1 dup [
shallow-fry [ >r ] rot
deep-fry [ [ dip ] curry r> compose ] 4array concat
2008-03-03 17:44:24 -05:00
] [
2008-07-03 12:25:27 -04:00
drop shallow-fry
] if ;
2008-03-03 17:44:24 -05:00
2008-05-26 01:48:18 -04:00
: fry-specifier? ( obj -- ? ) { , namespaces:, @ } member? ;
: count-inputs ( quot -- n )
[
{
2008-07-03 12:25:27 -04:00
{ [ dup callable? ] [ count-inputs ] }
{ [ dup fry-specifier? ] [ drop 1 ] }
[ drop 0 ]
2008-05-26 01:48:18 -04:00
} cond
] map sum ;
2008-04-22 17:29:20 -04:00
: fry ( quot -- quot' )
[
[
dup callable? [
2008-05-26 01:48:18 -04:00
[ count-inputs \ , <repetition> % ] [ fry % ] bi
2008-04-22 17:29:20 -04:00
] [ namespaces:, ] if
] each
] [ ] make deep-fry ;
2008-03-03 17:44:24 -05:00
: '[ \ ] parse-until fry over push-all ; parsing