开发者

Can only 'perl6' parse Perl 6?

There's that (relatively) well known Perl axiom, "Only perl can parse Perl." I'm wondering, will that remain true for Perl 6?

Expanding the discussion... I thought of this question given the recent update of PyPy. Does Perl's unique parsability preclude it from similar efforts? Is there much value in a restricted, static view of Perl code (PPI?)? Can Perl 6 have a JIT compiler?*

* I'm not sure开发者_如何转开发 if these concepts are related. Are they?


There is no perl6, and there are many Perl 6 compilers. Perl 6 has a grammar, although it's written in Perl 6, so as long as you can understand that, it tells you everything you need to know.

I just asked Larry this question, since I'm sitting across from him at lunch at the São Paulo Perl Workshop. He says it's now "Only Perl 6 can parse Perl 6", with capital letters on both Perls, which means something different than the original statement.

You don't need a specific program to do that though since Perl 6's goal is one standard and many implementations. There is no "perl6", although Larry had that aliased to his Rakudo for today's talk, even though he used a few different implementations for the examples.


Last I looked, Perl 6 inherits Perl 5's / character, which can mean "beginning of regex" when a term is expected, or "divide" when an operator is expected. Given that, and prototypes, and Perl 6 is at least in the same camp as Perl 5 for static tokenization. The only way to tokenize a Perl 6 program is to have a running Perl 6 interpreter in the Perl 6 compiler. Turtles all the way down, once again.


The axiom "Only perl can parse Perl" where "perl" is the interpreter binary and "Perl" is the language largely stems from the fact that parsing rules can change while the file is being parsed. In Perl 5, this comes from prototyped subroutine declarations, from various pragmas, and from source filters.

In my opinion, this is only going to become more of a problem in Perl 6. In Perl 5, the number of places where parser rules could change are limited, whereas in Perl 6, they are wide and varied. In addition to everything Perl 5 has, Perl 6 allows you to define your own operators, and since this definition is done in Perl code, a Perl interpreter is needed to make sense of it.

As far as I know, no implementation supports it yet, but the Perl 6 spec also includes real language level macros, which can restructure Perl 6 code either textually or by manipulating the AST. Both of these require the existence of a Perl interpreter to perform their magic.

So in conclusion, I have a feeling that Perl 6 will make the axiom stronger than it is for Perl 5. (And will be even more of a nightmare for the authors of syntax highlighters :) ) Of course this is all to increase the expressive power of the language, so I am ok with the concession.

A corollary to the above is that unlike Perl 5, Perl 6 has a formal spec, so the axiom might have to change to "Only an interpreter implementing the Perl 6 spec can parse Perl 6", but that's being a bit pedantic.

Per the update:

I don't think that the above precludes the idea of a JIT compiler for Perl 6, since by definition, such a compiler would also have to contain a Perl 6 interpreter. As far as static compilation goes, it MIGHT be possible, but it would severely restrict the language's runtime power, since any construct involving eval would not work.

PPI is useful in Perl 5 land because the perl interpreter does not provide many rich and easy to use interfaces to its AST. In Perl 6, the level of introspection is FAR greater, so the interpreter itself may provide all of the necessary tools.


Perl 6 is a specification and any program that follows that spec is Perl 6, just as is true for most other languages. There are a number of Perl 6 implementations in the works.


Can Perl6 have a JIT compiler?

You imply that Perl5 can't JITted based on "only perl can parse Perl5", but that's not true. Even though a Perl5 program can compile differently each time it's run, that doesn't prevent it from JITted.

So, if Perl5 —one of the most loosely defined languages— can be JITted, why Perl6 couldn't be too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜