Hi,
This was really neat. In the spirit of trying things out, here is a
slightly different way I came up with. Disclaimer: I made no attempt
to factor the common patterns out of the code.
--------------
USING: combinators.cleave kernel parser quotations sequences make
syntax combinators ;
IN: cleave++
DEFER: $ delimiter
: >make-quote ( seq -- quot )
[ [ , ] each ] [ ] make ;
: \\ \ $ [ >make-quote ] parse-literal \ $ [ >make-quote ] parse-
literal ; parsing
: \\\ \ $ [ >make-quote ] parse-literal \ $ [ >make-quote ] parse-
literal \ $ [ >make-quote ] parse-literal ; parsing
--------------
The nice thing about the above is that you don't need any
redefinitions of bi, tri, etc... and this code works when you want to
put more than one operation in a quote.
For example:
TUPLE: example a b ;
C: <example> example
10 20 <example>
\\ a>> 1+ $ b>> 1- $ bi
versus:
10 20 <example>
[ a>> 1+ ] [ b>> 1- ] bi
I arbitrarily picked the $ as a delimiter so I'm not sure if it has
other meanings in Factor that would make it an inappropriate choice.
In playing around with the above it doesn't always seem to make the
code clearer. Part of the reason seems to be that code like:
[ [ xxx ] yyy ] [ [ zzz ] www ] bi
becomes:
\\ [ xxx ] yyy $ [ zzz ] www $ bi
Which doesn't look too bad, but here is some code from the nbody
benchmark that I translated.
Original:
: newton's-law ( other-body body -- e )
[ [ mass>> ] bi@ * ] [ [ location>> ] bi@ distance ] 2bi / ;
Translated:
: newton's-law ( other-body body -- e )
\\ [ mass>> ] bi@ * $ [ location>> ] bi@ distance $ 2bi / ;
To my eyes, the $ don't stand out in the same way to help identify
what is a part of a quote. Part of that is undoubtedly that its a
different syntax than I used, but for me it would take some playing
around with to see if it becomes easier to read. Maybe it's like fry?
'[ didn't stand out at first until I got used to looking for it.
Thanks,
Glenn
V. Glenn Tarcea
[EMAIL PROTECTED]
On Nov 28, 2008, at 1:57 PM, Eduardo Cavazos wrote:
> Hello,
>
> Marc Fauconneau (prunedtree on #concatenative) made a brilliant
> suggestion a
> while back. Have '\\' and '\\\' words and use them with enhanced
> versions of
> the cleavers/spreaders which accept words as well as quotations. E.g.:
>
> 10 \\ 1- 1+ bi ---> 9 11
>
> Which would currently be written:
>
> 10 [ 1- ] [ 1+ ] bi
>
> I've been trying out the above idiom and it's very very nice.
>
> Code below...
>
> Ed
>
> ----------------------------------------------------------------------
>
> USING: combinators.cleave fry kernel macros parser quotations ;
>
> IN: cleave-enhanced
>
> : \\
> scan-word literalize parsed
> scan-word literalize parsed ; parsing
>
> MACRO: bi ( p q -- quot )
> [ >quot ] dip
>> quot
> '[ _ _ [ keep ] dip call ] ;
>
> MACRO: tri ( p q r -- quot )
> [ >quot ] 2dip
> [ >quot ] dip
>> quot
> '[ _ _ _ [ [ keep ] dip keep ] dip call ] ;
>
> MACRO: bi* ( p q -- quot )
> [ >quot ] dip
>> quot
> '[ _ _ [ dip ] dip call ] ;
>
> MACRO: tri* ( p q r -- quot )
> [ >quot ] 2dip
> [ >quot ] dip
>> quot
> '[ _ _ _ [ [ 2dip ] dip dip ] dip call ] ;
>
> ----------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's
> challenge
> Build the coolest Linux based applications with Moblin SDK & win
> great prizes
> Grand prize is a trip for two to an Open Source event anywhere in
> the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> Factor-talk mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk