Hi

I'm meditating about a macrosystem which is working after compilation and before execution

The idea is to flag subs as :macro and and to replace every call with the string the sub returns.

use B::MyMacro; sub foo :macro { return "bar()" }
so that
print; foo(); print;
will be replaced with
print; do { bar() }; print;

Of course eval "string" needs also to be overridden, such that the macroreplacement will also be executed after eval's compilation.

This mechanism can already be realised using B::Deparse, RegEx and evals, but with rather fragile dependencies. I'm looking for a stable and simple opcode-based solution, replacing every macro-call with new opcodes.

IMHO, this approach would be much more stable and useful than codefilters, because "only perl can parse perl" ¹.

I'm wondering if nobody ever tried to implement this, googling and supersearching didn't bring any examples. Do you know any other similar approach?

Cheers Rolf

UPDATES:
(¹) ... but "Even B:: can parse optree" : )

Replies are listed 'Best First'.
Re: Macro system on B-Level?
by moritz (Cardinal) on Nov 26, 2008 at 12:04 UTC
    You could try to plug something like that with Devel::Declare, which for example Method::Signatures uses internally to introduce a new syntax.

    But be warned that it might be non-trivial.

Re: Macro system on B-Level?
by salva (Canon) on Nov 26, 2008 at 11:37 UTC
    I'm wondering if nobody ever tried to implement this

    yes, I did, though I abandoned the idea because it got rather complex, and no other perl5-porters seemed to be interested on the idea :-(

    Search on the perl5-porters mailing list for the details: http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2006-05/msg00455.html.

    Probably, a more advanced version af the patch lays somewhere inside my hard disk.

      (Ola|Bom dia) salva!

      very, very interesting! 8 )

      But as far as I understand, you want to hook into the compilation process. Right?

      I was thinking of modifying the optree *after* compilation, isn't it to complex *during* compilation?

      I think, one can already implement this using B::Generate ... but I'm just a beginner with opcodes, and have no overview about the implementation details

      Cheers Rolf

Re: Macro system on B-Level?
by jdporter (Paladin) on Nov 26, 2008 at 15:48 UTC

    If all you're doing is saving function call overhead, it's not worth the trouble. If performance is that critical, drop down to XS or Inline::C or such like as.

    Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.
      No, no, no, your getting me wrong!

      It's *not* about function call overhead, it's about realising *macros* ¹, therefore helping to extend the syntax much better and reliable than codefilters ever can.

      perl 6 also intends to insert macro-support.

      But maybe you can show me a functional approach to solve this problem. : )

      Cheers Rolf

      UPDATE: (¹) to make it clearer, a macro is executed and operates on the parameters given before returning the new code to be inserted.

      > If all you're doing is saving function call overhead

      I'd rather prefer using some kind of eval like  eval macro() than doing XS.

      Coding in C just for linearising the code *that's* not worth the trouble. : )

      Cheers Rolf