Implementing Perl 6 Jonathan Worthington Dutch Perl Workshop 2008 - - PowerPoint PPT Presentation

implementing perl 6
SMART_READER_LITE
LIVE PREVIEW

Implementing Perl 6 Jonathan Worthington Dutch Perl Workshop 2008 - - PowerPoint PPT Presentation

Implementing Perl 6 Jonathan Worthington Dutch Perl Workshop 2008 Implementing Perl 6 I didnt know I was giving this talk until yesterday. Implementing Perl 6 I could have written my slides last night, but Implementing Perl 6


slide-1
SLIDE 1

Implementing Perl 6

Jonathan Worthington Dutch Perl Workshop 2008

slide-2
SLIDE 2

Implementing Perl 6

I didn’t know I was giving this talk until yesterday.

slide-3
SLIDE 3

Implementing Perl 6

I could have written my slides last night, but…

slide-4
SLIDE 4

Implementing Perl 6

slide-5
SLIDE 5

Implementing Perl 6

Guess what will be released at Christmas?*

slide-6
SLIDE 6

Implementing Perl 6

Guess what will be released at Christmas?*

* Which Christmas not specified.

slide-7
SLIDE 7

Implementing Perl 6

Perl 6!

slide-8
SLIDE 8

Implementing Perl 6

Introducing Rakudo

Name of the Perl 6 compiler targeting

the Parrot Virtual Machine

Parts written in Perl 6 Parser written using Perl 6 regexes

(now known as rules)

Parser actions (more later) written in

subset of Perl 6 called NQP

Other bits in Parrot Intermediate Repr.

slide-9
SLIDE 9

Implementing Perl 6

Compiler Architecture

slide-10
SLIDE 10

Implementing Perl 6

Parrot Compiler Tools

PCT is a tool chain for building

compilers

You write the "front end": Grammar, which specifies syntax Actions, which produce an Abstract

Syntax Tree from the Parse Tree

The backend (from the AST down to

Parrot bytecode) is done for you

slide-11
SLIDE 11

Implementing Perl 6

Compilation Process

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

slide-12
SLIDE 12

Implementing Perl 6

Compilation Process

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

slide-13
SLIDE 13

Implementing Perl 6

Compilation Process

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

slide-14
SLIDE 14

Implementing Perl 6

Compilation Process

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

slide-15
SLIDE 15

Implementing Perl 6

PGE = Parrot Grammar Engine

Implementation of Perl 6 regexes Can name regexes and call them from

each other (recursively too)

regex Year { \d**4 }; regex Place { Ukrainian | Dutch | German }; regex Workshop { <Place> \s Perl \s Workshop \s <Year> }; regex YAPC { 'YAPC::' ['EU'|'NA'|'Asia'] \s <Year> }; regex PerlEvent { <Workshop> | <YAPC> };

slide-16
SLIDE 16

Implementing Perl 6

PGE = Parrot Grammar Engine

You use PGE to write the grammar for

your language

You put a {*} in place to indicate that

we should run an action

rule unless_statement { 'unless' <EXPR> <block> {*} }

slide-17
SLIDE 17

Implementing Perl 6

Compilation Process

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

slide-18
SLIDE 18

Implementing Perl 6

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-19
SLIDE 19

Implementing Perl 6

NQP = Not Quite Perl 6

This method is called when the parser

encounters the {*} in the grammar

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

slide-20
SLIDE 20

Implementing Perl 6

NQP = Not Quite Perl 6

We are passed $/, the match object,

which describes what was parsed

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

slide-21
SLIDE 21

Implementing Perl 6

NQP = Not Quite Perl 6

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

match object for the sub rules

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

slide-22
SLIDE 22

Implementing Perl 6

NQP = Not Quite Perl 6

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

that match object

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

slide-23
SLIDE 23

Implementing Perl 6

NQP = Not Quite Perl 6

We instantiate a new AST node of type

Op

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

slide-24
SLIDE 24

Implementing Perl 6

NQP = Not Quite Perl 6

This node has two children: the

condition and the block to run

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

slide-25
SLIDE 25

Implementing Perl 6

NQP = Not Quite Perl 6

Also specify the type of operation; PCT

will then generate the appropriate code

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

slide-26
SLIDE 26

Implementing Perl 6

NQP = Not Quite Perl 6

Also specify the match object that we

made this from, for line numbers etc.

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

slide-27
SLIDE 27

Implementing Perl 6

NQP = Not Quite Perl 6

The "make" statement specifies the

tree node we have made

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

slide-28
SLIDE 28

Implementing Perl 6

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

Implementing Perl 6

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-30
SLIDE 30

Implementing Perl 6

Compilation Process

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

slide-31
SLIDE 31

Implementing Perl 6

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-32
SLIDE 32

Implementing Perl 6

Compilation Process

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

slide-33
SLIDE 33

Implementing Perl 6

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-34
SLIDE 34

Implementing Perl 6

The Perl 6 Grammar

slide-35
SLIDE 35

Implementing Perl 6

STD.pm

STD.pm is the standard Perl 6

grammar, written in Perl 6 rules

Mostly complete, though we find

missing things occasionally

PGE doesn't support all of the syntax it

uses yet, so we don't use it as is; instead, import it bit by bit and tweak it

End goal is that they will converge

slide-36
SLIDE 36

Implementing Perl 6

Two Parsers In One

Use bottom-up parsing for expressions

and top-down parsing for the rest

Have to call between them When top-down parser needs an

expression, uses <EXPR> to call into bottom-up parser to get one

If it needs a term, uses <term> to call

into the top-down parser to get one

slide-37
SLIDE 37

Implementing Perl 6

Top-down Parser

Defined using token, rule and regex sigspace means replace any

whitespace in the pattern with <.ws>, which is the current language's whitespace rule

yes no

  • no

no

  • no

yes

  • Sigspace

Backtracking

slide-38
SLIDE 38

Implementing Perl 6

Bottom-up Parser

We specify the operators in the

expression grammar, for bottom up parsing

## multiplicative operators proto infix:<*> is precedence('u=') { ... } proto infix:</> is equiv(infix:<*>) { ... } proto infix:<%> is equiv(infix:<*>) { ... } ## additive operators proto infix:<+> is precedence('t=') { ... } proto infix:<-> is equiv(infix:<+>) { ... }

slide-39
SLIDE 39

Implementing Perl 6

Implementing Built-ins

slide-40
SLIDE 40

Implementing Perl 6

Implementing Operators

In Perl 6, an operator is just a (multi-

dispatch) sub called with special syntax

Operator implemented in PIR

.sub 'infix:+' :multi(_,_) .param pmc a .param pmc b $P0 = n_add a, b .return ($P0) .end

slide-41
SLIDE 41

Implementing Perl 6

Random Aside: Operator Overloading

Note that because they are just multi-

dispatch subs, operator overloading is just an extra sub.

This is one of the overloads for

junctions

.sub 'infix:+' :multi('Junction',_) .param pmc x .param pmc j $P0 = find_global 'infix:+' .return infix_junc_helper($P0, j, x, 1) .end

slide-42
SLIDE 42

Implementing Perl 6

Implementing Built-ins

For now, writing a lot of these in PIR

too, because quite a few of them map to Parrot opcodes

Here is the built-in to compute the co-

tangent

.sub 'cotan' .param num a $N0 = tan a $N0 = 1 / $N0 .return ($N0) .end

slide-43
SLIDE 43

Implementing Perl 6

Implementing Built-ins

Recently someone submitted a patch to

allow writing of built-ins in Perl 6

Has needed a few tweaks, but folks are

working on that and it will be applied probably within a week or so

Will write what we can in Perl 6 rather

than PIR, but some things will always just be easier to do in PIR

slide-44
SLIDE 44

Implementing Perl 6

What's Implemented

slide-45
SLIDE 45

Implementing Perl 6

Never do live demos…

Because it WILL go wrong Because somebody will probably have

checked in something that broke what you are about to demonstrate

Because when things don't work

everyone will think…

I didn't learn Perl 6 yet That Rakudo sucks, not me

slide-46
SLIDE 46

Implementing Perl 6

How To Play And Help

slide-47
SLIDE 47

Implementing Perl 6

How To Build Rakudo

Check out the source from SVN

https://svn.perl.org/parrot/trunk/

Build it: Run it on the command line, with a

script or in interactive mode

perl Configure.pl make perl6 perl6 –e "say 'Hello, world!'" perl6 script.p6 perl6

slide-48
SLIDE 48

Implementing Perl 6

How To Explore The Source

Go into the rakudo directory In here you should run the PBC file, not

the executable

Most exciting stuff in the src directory,

especially under classes, builtins and parser

cd languages/perl6 ../../parrot perl6.pbc

slide-49
SLIDE 49

Implementing Perl 6

Ways To Help

Try to use it and report problems that

you encounter

Contribute to the test suite Write a built-in (some fairly easy stuff

here; anyone up for implementing

  • ?)

Contribute to the grammar and actions

slide-50
SLIDE 50

Implementing Perl 6

rakudo.org parrotcode.org dev.perl.org/perl6/

slide-51
SLIDE 51

Implementing Perl 6

Thank You

slide-52
SLIDE 52

Implementing Perl 6

Questions?