Results tagged “Parrot”

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.

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.

New Toys in Perl 6: custom ops.

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 ⦄;

Parrot Speaks Your Language

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

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

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

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';

Exception Handlers in HLLs

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.

1