Recently in Parrot Category

On Saturday, I wrote up a possible API for Parrot compilers to support loading libraries written in other languages and discussed some of the details with Jonathan++ and Allison++. It’s not perfect, and is missing a few parts, but should be extensible enough to support whatever else we need in the future. I still need to formalize it a bit and add it to the Parrot docs and the example language shell.

On Sunday, I implemented it on Rakudo (Perl 6) and Cardinal (Ruby; very incomplete).

This morning, after confirming the spec with pmicahud++, I merged the changes into Rakudo trunk.

The syntax for specifying the source language for Perl 6 is:

use Foo:lang<cardinal>;

I couldn’t quite figure out what an appropriate way to do this in Ruby would be, so I just added a function to cardinal:

foreign_load('perl6','Foo/Bar')

If you have a better suggestion for what it should look like in Ruby, please let me know! I don’t actually know much Ruby at all, so my Ruby compiler is fairly limited.

I’ll be adding support for this to pynie (Python) soon, and other languages after that.

Here’s a simple example of using a Perl library from Ruby:

[sweeks@kweh ~]$ cat Foo.pm
module Foo {
    sub greet($name) is export {
        say "Hello, $name!"
    }
}
[sweeks@kweh ~]$ cat perl6.rb
foreign_load 'perl6', 'Foo'
['Ruby', 'Perl', 'World'].each { |name| greet name }
[sweeks@kweh ~]$ cardinal perl6.rb
Hello, Ruby!
Hello, Perl!
Hello, World!

Here’s a similar example of using a Ruby library from Perl:

[sweeks@kweh ~]$ cat Foo.rb
module Foo
    def greet(name)
        puts "hello, " + name
    end
    def apply_people(cb)
        people = ['Dave', 'Bryan', 'Stuart', 'Dax']
        people.each { |name| cb(name) }
    end
end
[sweeks@kweh ~]$ cat ruby.pl
use Foo:lang<cardinal>;
greet("person $_") for 1..5;
apply_people( { say "hello from perl, $^name" } )
[sweeks@kweh ~]$ perl6 ruby.pl
hello, person 1
hello, person 2
hello, person 3
hello, person 4
hello, person 5
hello from perl, Dave
hello from perl, Bryan
hello from perl, Stuart
hello from perl, Dax

Thanks go to my employer (Guru Labs) for their support in my work on Rakudo and Parrot.

Rakudo is just starting to get support for adding custom operators to the grammar from user-level code. You can’t specify the precedence yet, but you can run the traditional examples:

multi sub infix:<±>(Int $a, Int $b) { return $a + $b | $a - $b }
multi sub postfix:<!>(Int $a where { $_ > 0 }) { return [*] 1..$a }

my $x = 5! ± 2;
say "hi dood" if $x > 121;
say "hello again" if $x < 119;

I was playing around today with defining operators for mathematical set operations (∩ ∪ ∖ ⊂ ⊃ ⊆ ⊇ etc.) and then decided that I wanted a fancy syntax for defining sets, so I added support to rakudo for circumfix operator definition, and i now have this running on rakudo:

say "subset" if ⦃ 1, 3, 5 ⦄ ⊆ ⦃ 1, 2, 3, 4, 5 ⦄;

I recently finished digging through the cleanups necessary to allow Parrot languages to exist in their own separate namespaces. Before this when you tried to run code from, say, Ruby and Perl 6 in the same interpreter, they would both try to define a Hash class, for example, and step on each other's toes in a variety of ways. Now you can load as many languages as you want into the same interpreter.

I also added a hackish implementation of the :lang parameter to Perl 6's eval that loads up the appropriate compiler to use instead of the Perl 6 compiler. This means that Rakudo can now do things like this:

eval(q<VISIBLE "O HAI GUYZ">, :lang<lolcode>);
my $x = eval(q<10×5÷2>, :lang<APL>);
my $rubysub = eval(q<do |i| puts "ruby got " + i; return i + 10 end>, :lang<cardinal>);
my $schemesub = eval(q<(lambda (msg) (write "scheme got " msg "\n"))>, :lang<pheme>);
$schemesub($rubysub($x));

The output of that is:

O HAI GUYZ
25
ruby got 25
scheme got 35

The extra '25' is there because the APL spec says that any non-assignment results in a print as a side-effect.

I haven't added support for this to any of the other Parrot languages yet because I don't know what the API should look like for any other language.

I haven't added support for loading foreign libraries to Perl 6 yet because there are a couple of awkward semantic issues to work out, and I haven't added support for loading foreign libraries to any other language yet because I don't know what the API should look like.

This is where you come in, my opinionated Internet friends. If you have suggestions for the API for evaluating foreign code or loading foreign libraries in any of Ruby, Python, LOLCODE, PHP, or any other Parrot language, please speak up here or on the social news site of your choice.

Thanks go to my employer for sponsoring my work on Parrot. Most of what I've done wouldn't be possible without their support.

Perl 6 - Given/When

| 235 Comments | No TrackBacks

Today I fixed the majority of the issues with Rakudo's 'when' blocks. When blocks do smartmatching against the current topic. For example, this:

when 'foo' { ... }

could also be written approximately like this:

if $_ ~~ 'foo' { ... }

The biggest difference is that 'when' blocks automatically break out to the innermost scope that has $_ as one of its formal parameters (either explicit or implicit), so:

for @numbers {
    when 1..5 { say 'low' }
    when 6..10 { say 'high' }
}

Could also be written as:

for @numbers {
    if $_ ~~ 1..5 { say 'low'; break }
    if $_ ~~ 6..10 { say 'high'; break }
}

(Which currently doesn't work in Rakudo, as we don't have control exceptions on loops handled properly yet.)

this lets us use the 'given' block, which just sets the topic for the block, to implement an idiomatic switch statement in Perl 6:

given $foo {
    when 'A' { say 'got A' }
    when 'B' { say 'got B' }
    when 'C' { say 'got C' }
    when '1' { say 'got 1' }
    when '2' { say 'got 2' }
    when '3' { say 'got 3' }
    when /foo/ { say 'matched a regex' }
    when 1..100 { say 'matched a range' }
    when 'etc' { say 'you know... like this.' }
    default { say 'When all else fails...' }
}

There's a handler for CONTINUE exceptions around every 'when' block to continue on through the block, so if you want to fall through, you just say:

given $num {
    when '3' { say 'got 3'; continue }
    when 1..100 { say 'got from 1 to 100' }
}

Non-fatal exceptions

| 153 Comments | No TrackBacks

Parrot's exceptions have a 'severity' attribute to indicate, of course, how severe the exception is. The current severities are: (taken from runtime/parrot/include/except_severity.pasm

EXCEPT_NORMAL   
EXCEPT_WARNING  
EXCEPT_ERROR    
EXCEPT_SEVERE   
EXCEPT_FATAL    
EXCEPT_DOOMED   
EXCEPT_EXIT

For a long time now, the spec has said that non-fatal exceptions shouldn't cause termination of the program, but should result in the message being printed and normal execution resuming. This hasn't been the case until today. It was a pretty simple change, but should make some things much nicer in the future.

Here's a simple program demonstrating this behaviour:

.include 'include/except_severity.pasm'
.sub main :main
    say 'before the warning'
    $P0 = new 'Exception'
    $P0['severity'] = .EXCEPT_WARNING
    $P0['message'] = "\tOMG something is kinda wrong"
    throw $P0
    say 'after the warning'
.end

And here's the output:

[sweeks@kweh parrot]$ ./parrot et.pir
before the warning
    OMG something is kinda wrong
after the warning

I've also updated Perl 6's warn() to use a non-fatal exception, meaning that these can now be caught.

Currying in Perl 6

| 139 Comments | No TrackBacks

There is still quite a lot of low-hanging fruit in Rakudo.

I checked on IRC today during a break in the class I'm teaching to see jhorwitz asking for currying in Perl 6, which is spelled like:

my $curried = &function.assuming(...);

A few minutes later, I committed an implementation of it. Here is the currying patch for Rakudo. It's about the same size as the last patch I mentioned here. Here are the tests we're now passing:

sub tester(:$a, :$b, :$c) {
"a$a b$b c$c";
}

my $u = &tester.assuming(b => 'x');
is $u(a => 'w', c => 'y'), 'aw bx cy', 'currying one named param';

my $w = &tester.assuming(b => 'b');
my $v = $w.assuming(c => 'c');
is $v(a => 'x'), 'ax bb cc', 'can curry on an already curried sub';
is $w(a => 'x', c => 'd'), 'ax bb cd', '... and the old one still works';

I just added support to the Parrot Compiler Toolkit for generating exception handlers. The only language I've patched to use this so far is Perl 6.

For example, this works:

do {
    say "foo";
    die "here is a failure";
    say "baz";
    CATCH {
        say "bar" if $_<message> ~~ /failure/;
        $_<resume>();
    }
}

And produces the output:

[sweeks@kweh]$ perl6 ehtest.pl
foo
bar
baz
[sweeks@kewh]$

Any Parrot language can now add support for exception handlers by adding a PAST::Control node to the 'handlers' attribute of a PAST::Stmts or PAST::Block node.

Here's the patch to Rakudo. Pretty small.

After another minor supporting change to Parrot, we'll have CONTROL blocks, and soon after we'll start supporting all of the control exceptions (next/last/redo) in all the appropriate loops.

Contact me on IRC if you want help or need anything else to support this in your language.

Thanks to my employer, Guru Labs for supporting all my work on Parrot.

Exceptions in Parrot

| 92 Comments | No TrackBacks

So, let’s talk about exceptions in Parrot.

All of the code in this blog entry is in PIR (Parrot Intermediate Representation, Parrot’s equivalent of ASM).

Throwing an exception from Parrot is pretty straightforward:

.sub main :main
    $P0 = new 'Exception'
    throw $P0
.end

If you want a message on the exception, a severity, etc, it’s like this:

.sub main :main
    $P0 = new 'Exception'
    $P0 = "I couldn't foo. :("
    $P0['severity'] = .EXCEPT_ERROR
    throw $P0
.end

Catching an exception isn’t much harder:

.sub main :main
    push_eh handler
    function_that_could_fail()
    pop_eh
    .return (1)
  handler:
    .get_results ($P0)
    say "Couldn't foo because some function failed"
    .return (0)
.end

That function just returns failure if an exception was thrown between ‘push_eh’ and ‘pop_eh’. Pretty straightforward.

If you wanted to actually recover from the exception and resume continuation, we can do this too. Parrot adds a continuation to the exception object that you can invoke to resume execution at the next opcode after you threw the exception:

.sub main :main
    push_eh handler
    maybe_fail()
    pop_eh
    .return (1)
  handler:
    .local pmc exception, continuation
    .get_results (exception)
    cleanup_and_recover()
    continuation = exception['resume']
    continuation()
.end

There’s a potential problem here with what happens when something in the exception handler, such as the ‘cleanupandrecover’ function we imagined here, throws an exception. When this exceptions system was first implemented, the handler would just catch the exception again, the handler would misbehave again, infinite loop. Bad.

The solution that has been in place for quite a while is to mark the exception handler as “already used” and then to have exception handlers refuse to handle anything if they had already been used. This doesn’t play nicely with resumable exceptions. For example, this code:

.sub main :main
    .local pmc e
    push_eh handler
    e = new 'Exception'
    e = "First Exception"
    throw e
    e = new 'Exception'
    e = "Second Exception"
    throw e
    pop_eh
    say "Everything was fine."
    .return ()
  handler:
    .local pmc ex, co
    .get_results (ex)
    print "Caught exception with message: "
    say ex
    co = ex['resume']
    co()
.end

Currently produces this output:

[sweeks@kweh parrot]$ ./parrot exception-demo.pir
Caught exception with message: First Exception
Second Exception
current instr.: 'main' pc 16 (exception-demo.pir:9)

Parrot can’t find a valid handler for the exception, so it prints the message and the file/line-number it was thrown on and dies.

The currently-used workaround for this is to make an actual ExceptionHandler object and manually mark the exception handler as unused before invoking the resume continuation. This is ugly.

.sub main :main
    .local pmc eh, e
    eh = new 'ExceptionHandler'
    set_addr eh, handler
    push_eh eh
    e = new 'Exception'
    e = "First Exception"
    throw e
    e = new 'Exception'
    e = "Second Exception"
    throw e
    say "Everything was fine."
    pop_eh
    .return ()
  handler:
    .local pmc ex, co
    .get_results (ex)
    print "Caught exception with message: "
    say ex
    eh = 0
    co = ex['resume']
    co()
.end

This works, though:

[sweeks@kweh parrot]$ ./parrot exception-demo.pir
Caught exception with message: First Exception
Caught exception with message: Second Exception
Everything was fine.

To deal with this ugliness, I added the ability to set a filter for exception types or severities on exception handlers a while back. That works like the following:

.include 'include/except_severity.pasm'
.sub main :main
    .local pmc eh, e
    eh = new 'ExceptionHandler'
    set_addr eh, handler
    eh.'min_severity'(.EXCEPT_NORMAL)
    eh.'max_severity'(.EXCEPT_WARNING)
    push_eh eh
    e = new 'Exception'
    e = "First Exception"
    e['severity'] = .EXCEPT_NORMAL
    throw e
    e = new 'Exception'
    e = "Second Exception"
    e['severity'] = .EXCEPT_WARNING
    throw e
    say "Everything was fine."
    pop_eh
    .return ()
  handler:
    .local pmc ex, co
    .get_results (ex)
    print "Caught a warning: "
    say ex
    co = ex['resume']
    co()
.end

That example will only work on parrot r32783 or newer, due to a small bug I just fixed.

So, this ‘disable exception handler’ behavior is very simple to remove. We’ve left it in so long because there are still a few tests that rely on the old behavior. We’re looking to remove it soon, though.

That’s about it for the basics of using exceptions in Parrot. Soon I’ll post about the bug in Parrot that made resuming to another subroutine not work.

About this Archive

This page is an archive of recent entries in the Parrot category.

Grant is the previous category.

Perl6 is the next category.

Find recent content on the main index or look in the archives to find all content.

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.32-en