Hi all,
A while ago Doug ran across a caveat in the macro system. He had code
like the following in the 'pack' vocab,
MACRO: pack ( str -- quot )
[ pack-table at '[ _ execute ] ] { } map-as
'[ [ [ _ spread ] input<sequence ] B{ } append-outputs-as ] ;
So the macro converts a string into a sequence of quotations by
mapping characters to words using the 'pack-table', then fries this
sequence of quotations into a nested form involving three other
macros: spread, input<sequence and append-outputs-as.
This looks all fine and dandy, but Doug noticed that the macro didn't
expand at compile time. The reason is that the fry form
'[ [ [ _ spread ] input<sequence ] B{ } append-outputs-as ]
Is parsed as
[
[ [ spread ] curry input<sequence ] curry
B{ } append-outputs-as
] curry
However, since input<sequence and append-outputs-as were macros, they
were being called on the result of a 'curry', which is not a literal,
so those macros didn't expand at compile time. At the time, I didn't
think there would be a solution, so I explained this to Doug and we
settled for the slightly less pleasing code below, which does not
suffer from the problem:
MACRO: pack ( str -- quot )
[ pack-table at '[ _ execute ] ] { } map-as
'[ _ spread ]
'[ _ input<sequence ]
'[ _ B{ } append-outputs-as ] ;
However, the above is just a mechanical transformation of the above.
Indeed, even though the macros input<sequence and append-outputs-as
are called with the result of curry, and not with a literal value,
both inputs to the curry are literals, so all the information needed
to expand them at compile time is there.
To fix this, I made macro expansion more general, allowing the first
nested curry version as well as the flat second version. This means
you can also do stuff like this,
: my-combinator ( seq quot -- result ) [ dip append ] curry
input<sequence ; inline
{ "xyz" "foo" } [ reverse ] my-combinator .
=> "zyxfoo"
So this means you can now use fry to construct macro expansions
involving additional macros without worrying as much.
Slava
------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk