Rakudo Perl 6 and Parrot Jonathan Worthington Linuxwochenende 2008 - - PowerPoint PPT Presentation

rakudo perl 6 and parrot
SMART_READER_LITE
LIVE PREVIEW

Rakudo Perl 6 and Parrot Jonathan Worthington Linuxwochenende 2008 - - PowerPoint PPT Presentation

Rakudo Perl 6 and Parrot Jonathan Worthington Linuxwochenende 2008 Rakudo Perl 6 and Parrot Me From England Rakudo Perl 6 and Parrot Me From England And yes, I do like tea Rakudo Perl 6 and Parrot Me From England And


slide-1
SLIDE 1

Jonathan Worthington Linuxwochenende 2008

Rakudo Perl 6 and Parrot

slide-2
SLIDE 2

Rakudo Perl 6 and Parrot

Me

From England

slide-3
SLIDE 3

Rakudo Perl 6 and Parrot

Me

From England And yes, I do like tea…

slide-4
SLIDE 4

Rakudo Perl 6 and Parrot

Me

From England And yes, I do like tea… …but I prefer it without milk in.

slide-5
SLIDE 5

Rakudo Perl 6 and Parrot

Me

From England And yes, I do like tea… …but I prefer it without milk in. Currently living in Bratislava, Slovakia.

slide-6
SLIDE 6

Rakudo Perl 6 and Parrot

Me

From England And yes, I do like tea… …but I prefer it without milk in. Currently living in Bratislava, Slovakia Just an hour from here Like Vienna, it's beautiful…

slide-7
SLIDE 7

Rakudo Perl 6 and Parrot

Me

From England And yes, I do like tea… …but I prefer it without milk in. Currently living in Bratislava, Slovakia Just an hour from here Like Vienna, it's beautiful… …but beer only costs 1 euro a pint

slide-8
SLIDE 8

Rakudo Perl 6 and Parrot

Me

From England And yes, I do like tea… …but I prefer it without milk in. Currently living in Bratislava, Slovakia Just an hour from here Like Vienna, it's beautiful… …but beer only costs 1 euro a pint Er, half litre

slide-9
SLIDE 9

Rakudo Perl 6 and Parrot

My Talk

An overview of three technologies The Parrot VM – a virtual machine for

dynamic languages

The Parrot Compiler Toolkit (= PCT)

– a tool chain for rapidly developing compilers targeting Parrot

Rakudo, a Perl 6 implementation on

Parrot built using PCT

slide-10
SLIDE 10

Rakudo Perl 6 and Parrot

Parrot

slide-11
SLIDE 11

Rakudo Perl 6 and Parrot

Parrot Is A Virtual Machine

Virtual instruction set Hides away the details of the

underlying hardware

Interface to IO, threading, etc. Hides away the details of the

underlying operating system

"Write once, run anywhere" Or as close as is realistically possible

slide-12
SLIDE 12

Rakudo Perl 6 and Parrot

Register Architecture

.Net CLR and JVM are stack based Parrot is register based Faster to interpret, since no stack

pointer to keep (need to run on many

  • dd platforms; shouldn't rely on JIT)

A little easier to JIT-compile too; "just"

a register allocation problem

Variable sized register frames per sub

slide-13
SLIDE 13

Rakudo Perl 6 and Parrot

HLL Feature Support

Parrot provides support for a range of

high-level language features

By providing support for them at the VM

level…

Compilers for different languages

don't need to re-invent the wheel

Different languages can inter-operate

slide-14
SLIDE 14

Rakudo Perl 6 and Parrot

Examples Of HLL Features In The VM

Common set of calling conventions Multiple dispatch Classes, attributes, methods, objects,

inheritance, introspection (reflection)

Namespaces Continuations, co-routines, closures Lexically scoped variables And more…

slide-15
SLIDE 15

Rakudo Perl 6 and Parrot

But Languages Are Different!

We want to support a load of existing

languages

Python Ruby PHP JavaScript But they all have slightly different ideas

about how certain things work…

slide-16
SLIDE 16

Rakudo Perl 6 and Parrot

PMCs

PMC = Parrot Magic Class Implement some of a fixed set of

methods that perform a range of common operations

Range from simple things, like get an

integer representation of this thing…

…to more complex OO-related things,

such as adding a parent class

slide-17
SLIDE 17

Rakudo Perl 6 and Parrot

Examples Of PMCs

Integer PMC – implements methods

relating to arithmetic

Array PMC – implements methods

relating to keyed access

Class PMC – implements object

  • rientation related methods

Sub PMC – implements invoke method,

and a few others (name, etc.)

slide-18
SLIDE 18

Rakudo Perl 6 and Parrot

Different Semantics, Common Interface

The PMCs all provide the same

interface

Languages can implement this

common interface to provide their own semantics

For example, a Perl array can return

"undefined" on access to an out-of- range element, whereas a Java array could throw an exception

slide-19
SLIDE 19

Rakudo Perl 6 and Parrot

Extensibility

Don't need to have all the PMCs in the

Parrot core; can build them into a dynamically linked library

Can also dynamically load additional

  • pcodes (instructions), to augment the

VM's instruction set

Language distribution = compiler +

(optionally) PMC Library + (optionally) Opcode Library

slide-20
SLIDE 20

Rakudo Perl 6 and Parrot

PIR

Parrot Intermediate Representation Essentially, the Parrot VM's "assembly" However, for some common things (like

method calls), it turns some syntactic sugar into the several real instructions it takes to do it

Also does register allocation for you, so

compiler writers needn't worry about it

slide-21
SLIDE 21

Rakudo Perl 6 and Parrot

Some Simple PIR Examples

"Hello, world!" – of course Compute The Answer

.sub 'main' :main print "Hello, world!\n" .end .sub 'main' :main $I0 = 25 $I1 = 17 $I2 = $I0 + $I1 print $I2 print "\n" .end

slide-22
SLIDE 22

Rakudo Perl 6 and Parrot

Some Simple PIR Examples

Factorial

.sub 'fact' .param int n if n > 1 goto rec .return (1) rec: $I0 = n – 1 $I1 = fact($I0) $I1 *= n .return ($I1) .end

slide-23
SLIDE 23

Rakudo Perl 6 and Parrot

Parrot Compiler Toolkit

slide-24
SLIDE 24

Rakudo Perl 6 and Parrot

Writing Compilers Is Easy…

…if you have the right tools PCT aims to be a Right Tool You write the "front end": Grammar, which specifies syntax Actions, to produce an Abstract

Syntax Tree from the Parse Tree

The backend (from the AST down to

Parrot bytecode) is done for you

slide-25
SLIDE 25

Rakudo Perl 6 and Parrot

Compilation Process

Program Source Parse Tree Abstract Syntax Tree Opcode Syntax Tree PIR (Parrot IL) Parrot Bytecode

slide-26
SLIDE 26

Rakudo Perl 6 and Parrot

Compilation Process

Program Source Parse Tree Abstract Syntax Tree Opcode Syntax Tree PIR (Parrot IL) Parrot Bytecode PGE NQP PCT PCT Parrot

slide-27
SLIDE 27

Rakudo Perl 6 and Parrot

Compilation Process

Program Source Parse Tree Abstract Syntax Tree Opcode Syntax Tree PIR (Parrot IL) Parrot Bytecode PGE NQP PCT PCT Parrot

slide-28
SLIDE 28

Rakudo Perl 6 and Parrot

Compilation Process

Program Source Parse Tree Abstract Syntax Tree Opcode Syntax Tree PIR (Parrot IL) Parrot Bytecode PGE NQP PCT PCT Parrot

slide-29
SLIDE 29

Rakudo Perl 6 and Parrot

PGE = Parrot Grammar Engine

Implementation of Perl 6 rules A bit like regexes, but taken a step

further so we can use them to write a full grammar

Unlike more traditional tools like lex and

yacc, where you write a tokenizer and a grammar, here you just write the parse rules and the tokenizer is generated for you

slide-30
SLIDE 30

Rakudo Perl 6 and Parrot

An Example Rule

You use PGE to write the grammar for

your language

For example, here's how we could

parse an if statement

You put a {*} in place to indicate that

we should run an action

rule if_statement { 'if' <expression> <block> {*} }

slide-31
SLIDE 31

Rakudo Perl 6 and Parrot

Compilation Process

Program Source Parse Tree Abstract Syntax Tree Opcode Syntax Tree PIR (Parrot IL) Parrot Bytecode PGE NQP PCT PCT Parrot

slide-32
SLIDE 32

Rakudo Perl 6 and Parrot

NQP = Not Quite Perl 6

A subset of Perl 6 Contains just enough to allow you to

produce an Abstract Syntax Tree from the parse tree

Variables and literals Binding (but not assignment) Conditionals and loops Object instantiation and method calls

slide-33
SLIDE 33

Rakudo Perl 6 and Parrot

NQP = Not Quite Perl 6

This method is called when the parser

encounters the {*} in the grammar

method if_statement($/) { my $then := $( $<block> ); $then.blocktype('immediate'); my $past := PAST::Op.new( $( $<EXPR> ), $then, :pasttype('if'), :node( $/ ) ); make $past; }

slide-34
SLIDE 34

Rakudo Perl 6 and Parrot

NQP = Not Quite Perl 6

We are passed $/, the match object,

which describes what was parsed

method if_statement($/) { my $then := $( $<block> ); $then.blocktype('immediate'); my $past := PAST::Op.new( $( $<EXPR> ), $then, :pasttype('if'), :node( $/ ) ); make $past; }

slide-35
SLIDE 35

Rakudo Perl 6 and Parrot

NQP = Not Quite Perl 6

Named captures ($<….>) give you the

match object for the sub rules

method if_statement($/) { my $then := $( $<block> ); $then.blocktype('immediate'); my $past := PAST::Op.new( $( $<EXPR> ), $then, :pasttype('if'), :node( $/ ) ); make $past; }

slide-36
SLIDE 36

Rakudo Perl 6 and Parrot

NQP = Not Quite Perl 6

Writing $( $<…> ) gets you the AST for

that match object

method if_statement($/) { my $then := $( $<block> ); $then.blocktype('immediate'); my $past := PAST::Op.new( $( $<EXPR> ), $then, :pasttype('if'), :node( $/ ) ); make $past; }

slide-37
SLIDE 37

Rakudo Perl 6 and Parrot

NQP = Not Quite Perl 6

We instantiate a new AST node of type

Op

method if_statement($/) { my $then := $( $<block> ); $then.blocktype('immediate'); my $past := PAST::Op.new( $( $<EXPR> ), $then, :pasttype('if'), :node( $/ ) ); make $past; }

slide-38
SLIDE 38

Rakudo Perl 6 and Parrot

NQP = Not Quite Perl 6

This node has two children: the

condition and the block to run

method if_statement($/) { my $then := $( $<block> ); $then.blocktype('immediate'); my $past := PAST::Op.new( $( $<EXPR> ), $then, :pasttype('if'), :node( $/ ) ); make $past; }

slide-39
SLIDE 39

Rakudo Perl 6 and Parrot

NQP = Not Quite Perl 6

Also specify the type of operation; PCT

will then generate the appropriate code

method if_statement($/) { my $then := $( $<block> ); $then.blocktype('immediate'); my $past := PAST::Op.new( $( $<EXPR> ), $then, :pasttype('if'), :node( $/ ) ); make $past; }

slide-40
SLIDE 40

Rakudo Perl 6 and Parrot

NQP = Not Quite Perl 6

Also specify the match object that we

made this from, for line numbers etc.

method if_statement($/) { my $then := $( $<block> ); $then.blocktype('immediate'); my $past := PAST::Op.new( $( $<EXPR> ), $then, :pasttype('if'), :node( $/ ) ); make $past; }

slide-41
SLIDE 41

Rakudo Perl 6 and Parrot

NQP = Not Quite Perl 6

The "make" statement specifies the

tree node we have made

method if_statement($/) { my $then := $( $<block> ); $then.blocktype('immediate'); my $past := PAST::Op.new( $( $<EXPR> ), $then, :pasttype('if'), :node( $/ ) ); make $past; }

slide-42
SLIDE 42

Rakudo Perl 6 and Parrot

Compilation Process

Program Source Parse Tree Abstract Syntax Tree Opcode Syntax Tree PIR (Parrot IL) Parrot Bytecode PGE NQP PCT PCT Parrot

slide-43
SLIDE 43

Rakudo Perl 6 and Parrot

PAST to POST

POST is the Parrot Opcode Syntax

Tree

Tree representation of Parrot

assembly program

Often one node = one instruction The PAST compiler, part of PCT,

transforms a PAST node into (usually many) POST nodes

slide-44
SLIDE 44

Rakudo Perl 6 and Parrot

Compilation Process

Program Source Parse Tree Abstract Syntax Tree Opcode Syntax Tree PIR (Parrot IL) Parrot Bytecode PGE NQP PCT PCT Parrot

slide-45
SLIDE 45

Rakudo Perl 6 and Parrot

POST to PIR

PIR = Parrot Intermediate

Representation

Text based rather than tree based The Parrot VM itself understands PIR,

so for now we have to turn the POST tree into PIR

One day, we may be able to go direct

from the tree to the bytecode

slide-46
SLIDE 46

Rakudo Perl 6 and Parrot

Compilation Process

Program Source Parse Tree Abstract Syntax Tree Opcode Syntax Tree PIR (Parrot IL) Parrot Bytecode PGE NQP PCT PCT Parrot

slide-47
SLIDE 47

Rakudo Perl 6 and Parrot

PIR to Parrot Bytecode

The Parrot VM actually executes

bytecode – a binary representation of the program

It contains a compiler that turns PIR

into Parrot Bytecode

We can write the bytecode to disk so

we can load it again in the future => don't need to compile our program every time => performance!

slide-48
SLIDE 48

Rakudo Perl 6 and Parrot

Languages

PCT is being used to build compilers

for a range of languages…

Perl 6 (Rakudo) PHP (Pipp) Python (Pynie) Ruby (Cardinal) SmallTalk (ChitChat) LOLCODE

slide-49
SLIDE 49

Rakudo Perl 6 and Parrot

Yes, LOLCODE

HAI CAN HAS STDIO? I HAS A VAR IM IN YR LOOP UP VAR!!1 VISIBLE VAR IZ VAR BIGGER THAN 10? KTHXBYE IM OUTTA YR LOOP KTHXBYE

slide-50
SLIDE 50

Rakudo Perl 6 and Parrot

Perl 6

slide-51
SLIDE 51

Rakudo Perl 6 and Parrot

What is Perl 6?

Perl 5 has been and continues to be

very widely used

Perl community is still very active Perl is less fashionable than it once

was, but no less useful

Perl 6 is a ground-up re-design and re-

implementation of the language

Not backward compatible

slide-52
SLIDE 52

Rakudo Perl 6 and Parrot

Why start from scratch?

Perl was first released in 1987 It's now more than 20 years later; it'd

be nice to think we learned a few things about languages in that time

Perl 5 internals are difficult to get into

and extend

Breaking backwards compatibility gives

much more design freedom

slide-53
SLIDE 53

Rakudo Perl 6 and Parrot

Migration

As usual with Perl, There's More Than

One Way To Do It

Implementation of a Perl 5 to Perl 6

translator is underway

Will retrain comments and all that a

parser usually throws away

Will be able to use Perl 5 modules from

Perl 6

slide-54
SLIDE 54

Rakudo Perl 6 and Parrot

Isn't it taking a while?

Yes, because… It's a large and complex project Doing some things that haven't been

done in other languages before

Much is done by volunteers, though

there has been and is some funding

Perl 5 is still very powerful and being

actively developed too

slide-55
SLIDE 55

Rakudo Perl 6 and Parrot

So What's New?

Covering all of the new features of Perl

6, along with the changes from Perl 5, would take quite a while

Going to give you a very quick look at

some of the highlights

slide-56
SLIDE 56

Rakudo Perl 6 and Parrot

Chained Comparisons

How often do you write stuff like: In Perl 6 that is just: Note that we can drop the parentheses

  • n the condition too

if ($x >= 0 && $x <= 100) { ... } if 0 <= $x <= 100 { ... }

slide-57
SLIDE 57

Rakudo Perl 6 and Parrot

Junctions

How often do you write stuff like: In Perl 6 that is just: Junction = something that you can use

where you'd use a single value, but acts as all of them simultaneously

if ($drink eq 'Beer' || $drink eq 'Wine') { ... } if $drink eq 'Beer' | 'Wine' { ... }

slide-58
SLIDE 58

Rakudo Perl 6 and Parrot

Powerful New Object Model

Now syntax for declaring classes,

attributes and methods

The method call operator is now .

class Beer is Drink { has $.units; has $.type; method consume($consumer) { $consumer.alcohol += $.units; self.spill() if $consumer.drunk; } }

slide-59
SLIDE 59

Rakudo Perl 6 and Parrot

New Signature Syntax

Rather than just getting an array of

parameters, you can write a signature

sub add($x, $y) { return $x + $y; } say add(37, 5); # 42 sub greet($name, :$greeting = 'Guten Tag') { say "$greeting, $name"; } greet('Hans'); # Guten Tag, Hans greet('Lena', greeting => 'Privet'); # Privet, Lena

slide-60
SLIDE 60

Rakudo Perl 6 and Parrot

New Regex Syntax

Many languages today use Perl

Compatible Regular Expressions

Regex are known for being rather, well,

scary and line-noise-ish

Perl 6 gives them a thorough re-

working

Easy to build reusable bits of regex and

compose them

slide-61
SLIDE 61

Rakudo Perl 6 and Parrot

New Regex Syntax

Whitespace not matched as part of the

regex now – so space stuff out!

[…] is a non-capturing group Put a literal in quotes, like elsewhere Build other regex out of it

regex Decimal { \d+ ['.' \d+]? }; regex Temperature { <Decimal> \s* [C|F] }; regex HighTemp { 'High' \W+ <Temperature> } regex LowTemp { 'Low' \W+ <Temperature> }

slide-62
SLIDE 62

Rakudo Perl 6 and Parrot

Grammars

Can collect a bunch of regex together

into grammars (like classes but with regex instead of methods)

Good for building parsers Unlike traditional regex, has control of

backtracking

Auto-generates a lexer for you Perl 6 will parse itself

slide-63
SLIDE 63

Rakudo Perl 6 and Parrot

Multiple Dispatch

One name, different signatures When you do a call, it dispatches to the

best candidate

All operators are really just multiple

dispatch subs => overloading is just writing a multi-sub

sub win(Paper $x, Stone $y) { True } sub win(Scissors $x, Paper $y) { True } sub win(Stone $x, Scissors $y) { True } sub win(Any $x, Any $y) { False }

slide-64
SLIDE 64

Rakudo Perl 6 and Parrot

Rakudo

slide-65
SLIDE 65

Rakudo Perl 6 and Parrot

Rakudo vs. Perl 6

Perl 6 is the name of the language Rakudo is an implementation of the

Perl 6 language

However, it's not the only one that is in

progress, or that exists

Pugs = Perl 6 in Haskell, currently not

maintained

kp6 and SMOP are two others

slide-66
SLIDE 66

Rakudo Perl 6 and Parrot

Why "Rakudo"?

Suggested by Damian Conway Some years ago, Con Wei Sensei

introduced a new martial art in Japan named "The Way Of The Camel"

In Japanese, this is "Rakuda-do" The name quickly became abbreviated

to "Rakudo", which also happens to mean "paradise" in Japanese

slide-67
SLIDE 67

Rakudo Perl 6 and Parrot

Status

Rakudo is not a complete

implementation of Perl 6 yet

However, all of the code that I showed

in the previous section of the talk will run on Rakudo today…

…and much more. Uses PCT, meaning much of it is in

Perl 6 rules and a subset of Perl 6

slide-68
SLIDE 68

Rakudo Perl 6 and Parrot

What's (Mostly) Implemented?

Variables: scalars, arrays, and hashes Wide range of operators Conditionals and loops Subroutines with signatures Classes, attributes, methods,

inheritance, delegation, roles and composition, runtime mix-ins

Enumerations

slide-69
SLIDE 69

Rakudo Perl 6 and Parrot

What's (Mostly) Implemented?

Regexes and grammars Type constraints on variables,

parameters and attributes

Multiple dispatch (very much a work in

progress, but getting there)

Basic junction support (but much, much

more to do here)

Basic I/O

slide-70
SLIDE 70

Rakudo Perl 6 and Parrot

Compilation To Bytecode

You can compile your programs or

modules down to Parrot bytecode

Means you don't have to run the

compiler every time you want to run the program

Additionally, use mmap when it's

available, so Parrot instances can share the bytecode file

slide-71
SLIDE 71

Rakudo Perl 6 and Parrot

mod_perl6

There is also basic support for writing

Apache handlers in Perl 6 now

mod_parrot provides most of what is

needed, and mod_perl6 is a thin layer (written mostly in Perl 6 itself) on top of that

Easy to make mod_your_language Yes, we do have mod_lolcode

slide-72
SLIDE 72

Rakudo Perl 6 and Parrot

Learning More

slide-73
SLIDE 73

Rakudo Perl 6 and Parrot

Where To Learn More

The Parrot Website

http://www.parrot.org/

The Parrot Blog recently had an 8-part

PCT tutorial posted http://www.parrotblog.org/

The Perl 6 implementation on Parrot

(named Rakudo) has a site here http://www.rakudo.org/

slide-74
SLIDE 74

Rakudo Perl 6 and Parrot

Get Involved!

Join the Parrot and/or Perl 6 compiler

mailing list

Pop onto the IRC channel Get the source and start hacking Partial implementations of many

languages – come and help us get your favorite one running on Parrot

Or if you like C, lots of VM guts work

slide-75
SLIDE 75

Rakudo Perl 6 and Parrot

Come!

There will be a Perl Workshop, one day

in Vienna, one day in Bratislava

7th and 8th of November

http://conferences.yapceurope.org/tcpw2008/

slide-76
SLIDE 76

Rakudo Perl 6 and Parrot

Danke

slide-77
SLIDE 77

Rakudo Perl 6 and Parrot

Questions?