Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
go ahead... be a heretic
 
PerlMonks  

Re: Re: Re: Re: Re: A Macro System for Perl?

by Elian (Parson)
on May 04, 2002 at 18:07 UTC ( #164037=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Re: Re: A Macro System for Perl?
in thread A Macro System for Perl?

You really need to answer the question "What will I do with the macro system" before you have any hope of a design.

Some things, like the compile-time text transformation that C's preprocessor does, is best done with source filters, and at this point is both doable and pretty simple.

Fancier things, like adding new syntax (such as the switch statement or whatever) are best done by actually adding new syntax to the parser and compiler, putting it outside the realm of macros.

You might want to go ahead and mess with tokens--for example changing the definition of + for a lexical scope, but that's best done either by a change to the parser (and thus a change to the underlying language) or a change to the compiler's rules for tokens, both of which are best done in either the parser grammar language or by adding a few regexes and rules to the compiler. (Which, I suppose, could be considered macros of a sort, though I wouldn't)

So what do you want to do with the macros? Until you answer that, there's not much to be done to design the things.


Comment on Re: Re: Re: Re: Re: A Macro System for Perl?
Re: Re: Re: Re: Re: Re: A Macro System for Perl?
by samtregar (Abbot) on May 04, 2002 at 18:49 UTC
    You really need to answer the question "What will I do with the macro system" before you have any hope of a design.

    I was rather hoping we could do it togther. My thoughts on the subject are rather scattered, actually. Perhaps I'm not much of a language designer?

    That said, I think you are essentially correct. It's difficult to motivate a discussion of a feature if you can't even propose some example uses. Perhaps I'll take another pass if I can come up with some.

    -sam

      It's difficult to motivate a discussion of a feature if you can't even propose some example uses.
      I thought "switch" was a good example, even though the syntax was less-than-pleasant. What I'd like to see is something to add rules to the running parser (a la Parse::RecDescent), using the same language as Perl to define the new rules (including Perl itself to express actions). In other words, I could do something like this:
      package Syntax::Stride; syntax { slice: array '[' stride ']' { # implement this. } stride: expr ':' expr ':' expr { # and this. } expr: slice } # end of syntax package main; use Syntax::Stride; @a = (1..100); print @a[1:2:50];
      This would extend the existing grammar by adding to the 'expr' rule and creating new rules 'slice' and 'stride'. It would also define the new semantics, probably returning an AST representing the appropriate Perl code.

      Lisp gets away with this because the syntax is so... simple. In Perl there would probably be an infinity of ways to hurt yourself trying to do this, and lots of people would, so the diagnostics would have to be very good. This might always end up as a "you have to be THIS tall" feature...

      So at least three hard things stand in the way of this: making a stable and extensible API for construct op-trees, writing a comprehensible Perl grammar in top-down format, and creating a good grammar checker. But wouldn't it be loveley

      /s

        The only problem there is that adding rules to the parser is likely to look more like:
        myour => qc( my | our | ), decl => qc( package <package_decl> | module <module_decl> | class <class_decl> | sub <sub_decl> | <@other_declarations> ),
        than perl. Perl's not necessarily the best language to express rules in.

        I'm also not sure we're going top-down with the parser. We'll see what works best.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://164037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-04-18 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls