Tales of version five

Hi, this is my first post on my first blog, so please be kind. :)

So, what is this all about? – It is about programming. It is not about about programming in general and not about one single language. It is about programming in 2.5 languages. (I’ll explain that in a few.)

If you have not yet seen the v5 module, you might have a look at its repository. v5 lets you write Perl 5 code in Perl6, call Perl 5 subs from Perl6 and vice-versa and pass variables from/to these two languages.

You know the Inline modules? Well, it is not quite like that. – When using Inline modules you usually need to put the foreign language in a quote, this gets then sort of evaluated and accessors are created that you call from the main language. But since that sounds a bit icky it is not what v5 is for.

The set goals of v5:

  • write Perl 5 code directly in Perl 6 code, usually as a closure
  • allow Perl 6 lexical blocks inside Perl 5 ones
  • make it easy to use variables declared in an outer block (outer means the other language here)
  • provide the behaviour of Perl 5 operators and built-ins for v5 blocks only, nested Perl 6 blocks should not be affected
  • and of course: make subs, packages, regexes, etc available to the other language

And it turns out that all of the above is not just “somehow possible”, it is there*.
* for a certain definition of “there”.

And how does that work?

Most important there is Perl 6’s architecture, which allows one to define slangs (sub languages). And there is the Perl 5 grammar written by Larry Wall. The Perl 6 compiler rakudo is based on a similar Perl 6 grammar (similar in structure), so it wasn’t too hard to get these two pieces together.

After a bit of try&error and help from pmurias making it a module, there was something that allows you to do:

use v6;
{
    use v5;
    print "Hello from Perl 5 on a $^O box!"
}

That was quite nice for a bit more than a week of hacking v5. The next steps were to add thoughts, sanity and some sort of structure.

So at this point the compiler is able to switch to the Perl 5 grammar when it sees the ‘use v5′ token. The AST it builds is pretty similar to the Perl 6 one (in many cases even identical). Every variable you declare in a v5 block is a proper (typed) Perl 6 variable, this makes it easy to pass these variable around. This way you dont need to think about how to call into Perl 5 or Perl 6 land, you just do.

In the early weeks I cared about control structures. This wasn’t too bad since at a first glance the Perl 6 one look pretty much the same, but the devil is in the details: In Perl 5 a closure is a loop. So you can ‘redo’ or ‘last’ it, or wire it to a continue block. After having that the number of passing tests was in the mid 50’s, and I recognized I need a proper test suite. So for one I took the tests of Perlito but since Perlito’s goal is not the same as v5, I had to take the tests of the Perl 5 repository. The problem with that is that the test design is bad, really bad. Perl 6’s test suite is from a language specification view, while Perl 5’s is from an implementations view. It is there for one already existing compiler, with a marture (and huge) test-internal infrastructure that makes it almost not useful for a new implementations. But since there is no other option I set up roast5 repository, and started to write test-helper scripts and started to fudge the 500 test files to be somewhat nice to the parser as it was at that time.

The next important step was to support the Perl 5 special variables, so that more test files are parseable. After that I cared about the operators, these have two interesting facts. For example for ‘ 1 + “42Foo” ‘ there is a different behaviour in Perl 6 than in Perl 5. In Perl 6 it would just die because “42Foo” can’t be transformed to a number, Perl 5 however is fine with just taking the numeric part from the front and ignoring the rest. Perl 6 handled the string->number transformation in method Str.Numeric. Clearly a v5 block needs something else. So I could augment class Str and place a Perl 5-ish Numeric method that wouldn’t die but do The Right Thing, but hold on, then it would pollute the Perl 6 world too. No, that is not what we want. The solution is that the operator ‘+’ is implemented as infix:<P5+>, which calls a method called P5Numeric on its arguments. This operator ‘+’ to infix:<P5+> dispatch is made in the Perl5::Actions, that is the AST-producing step right after parsing the code. And since Perl5::Actions is only in charge for the statements for a v5 block and not for nested Perl 6 blocks, the ‘+’ operator really has two faces, one for Perl 5 and one for Perl 6. (tbh, these are two different operators now.)

Where are we now?

That is a good question, and I love to answer it.
To the date I can proudly say that control structures work, quite a bunch special variables and built-ins are supported. The operators behave more and more Perl 5-ish, and modules/pragmas like Config, overload, warnings, English are more than stubs. Heredocs are working (I really like to send a dung-bomb to its inventor), and indirect object syntax and subroutine signatures are supported to a small but useful degree.

If you want to monitor the progress these is a STATUS file in the repository, right now it says that v5 passes 797 out of 40451 tests, I’m quite happy with that. (Note that pack.t give us 14704 failing tests.)

So, what about the 2.5 languages mentioned at the top?

Ohh yes, almost forgot. I think the ‘2’ is obvious now, it is about Perl 5 and 6. The 0.5 is about NQP – “Not Quite Perl”, that is the language for example the grammar and actions of Perl 6 and v5 is written in. It looks like a subset of Perl 6 and that’s why it only gets 0.5 points, even when it is Quite Awesome(tm).
So if you stay tuned, I’ll report from now and then about the v5 project, and maybe about other (related) programming issues too.


FROGGS

About these ads

2 Comments

Filed under Uncategorized

2 responses to “Tales of version five

  1. > Hi, this is my first post on my first blog, so please be kind.

    You are smart and attractive and so is v5 and your first blog :-D

    > v5 lets you write Perl 5 code in Perl6, call Perl 5 subs from Perl6 and vice-versa and pass variables from/to these two languages.

    You can (or will be able to) *use* (pure perl) modules too, right?

    Once Rakudo is running well on MoarVM (with libperl and XS support), you’ll presumably allow mixing of v6, v5 and 5p5p5/CPAN, right?

    > If you want to monitor the progress these is a STATUS file in the repository, right now it says that v5 passes 797 out of 40451 tests

    STATUS now shows v5 passing 1209 tests, so 400 more in the last week. When did you start at 0?

    > I’ll report from now and then about the v5 project, and maybe about other (related) programming issues too.

    What about posting weekly (Sundays/Mondays)? Even if all you do is a one line post saying what the latest pass count is. As the #perl6 log summarizer I have to add that you could quickly review the #perl6 log summary, searching for FROGGS to remind yourself what you’ve done. :)

    I see a book in your future: “Perl 6 slangs”. :)

  2. > You can (or will be able to) *use* (pure perl) modules too, right?
    If all dependencies are supported* too, then yes. *) supported means that they are either pure perl or they are reimplemented somehow.

    > Once Rakudo is running well on MoarVM (with libperl and XS support), you’ll presumably allow mixing of v6, v5 and 5p5p5/CPAN, right?
    In a perfect world I’d say yes, but I am still a bit sceptical that all problem will vanish at once… We’ll see.

    > STATUS now shows v5 passing 1209 tests, so 400 more in the last week. When did you start at 0?
    That was in late march/early april. So about three months ago.

    > What about posting weekly [...]
    I can try to do that :o)

    > I see a book in your future: “Perl 6 slangs”.
    I’m not the best writer, but I like to share things that I have learned… So I leave that a bit open *g*

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s