DIP 50 - AST macros
deadalnix
deadalnix at gmail.com
Wed Nov 13 14:18:42 PST 2013
On Wednesday, 13 November 2013 at 19:25:18 UTC, Walter Bright
wrote:
> On 11/13/2013 11:24 AM, Walter Bright wrote:
>> Actually, there is a way to do this. I wrote an article years
>> back on how to
>> write "statement" workalikes using lazy function parameters. I
>> suppose I should
>> dig that up if I can find it.
>
> Ah, found the code:
>
> void ifthen(bool cond, lazy void dg)
> {
> if (cond)
> dg();
> }
>
> void ifthen(bool cond, lazy void dgthen, lazy void dgelse)
> {
> if (cond)
> dgthen();
> else
> dgelse();
> }
>
> void dotimes(int i, lazy int dg)
> {
> for (int j = 0; j < i; j++)
> dg();
> }
>
> void switcher(bool delegate()[] cases...)
> {
> foreach (c; cases)
> {
> if (c())
> break;
> }
> }
>
> bool scase(bool b, lazy void dg)
> {
> if (b)
> {
> dg();
> return true;
> }
> return false;
> }
>
> bool sdefault(lazy void dg)
> {
> dg();
> return true;
> }
>
> void whiler(lazy bool cond, lazy void bdy)
> {
> while (cond())
> bdy();
> }
>
> void test1()
> {
> int x = 3;
> dotimes(5, printf("%d\n", ++x));
>
> ifthen(true, printf("yes\n"));
> ifthen(false, printf("no\n"));
>
> ifthen(true, printf("yes\n"), printf("no\n"));
> ifthen(false, printf("yes\n"), printf("no\n"));
>
> int v = 2;
> switcher(
> scase(v == 1, printf("it is 1\n")),
> scase(v == 2, printf("it is 2\n")),
> scase(v == 3, printf("it is 3\n")),
> sdefault( printf("it is default\n"))
> );
>
> whiler( x < 100,
> (printf("%d\n", x), x *= 2)
> );
> }
Was is missing is the capability to look into the lazy
parameter's ast and act accordingly.
Here you can wrap something around code, not generation code
depending on passed code.
More information about the Digitalmars-d
mailing list