rakudo perl 6 and parrot
play

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


  1. Rakudo Perl 6 and Parrot Jonathan Worthington Linuxwochenende 2008

  2. Rakudo Perl 6 and Parrot Me � From England

  3. Rakudo Perl 6 and Parrot Me � From England � And yes, I do like tea…

  4. Rakudo Perl 6 and Parrot Me � From England � And yes, I do like tea… � …but I prefer it without milk in.

  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.

  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…

  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 �

  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

  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

  10. Rakudo Perl 6 and Parrot Parrot

  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

  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 odd platforms; shouldn't rely on JIT) � A little easier to JIT-compile too; "just" a register allocation problem � Variable sized register frames per sub

  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

  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…

  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…

  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

  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 orientation related methods � Sub PMC – implements invoke method, and a few others (name, etc.)

  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

  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 opcodes (instructions), to augment the VM's instruction set � Language distribution = compiler + (optionally) PMC Library + (optionally) Opcode Library

  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

  21. Rakudo Perl 6 and Parrot Some Simple PIR Examples � "Hello, world!" – of course .sub 'main' :main print "Hello, world!\n" .end � Compute The Answer .sub 'main' :main $I0 = 25 $I1 = 17 $I2 = $I0 + $I1 print $I2 print "\n" .end

  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

  23. Rakudo Perl 6 and Parrot Parrot Compiler Toolkit

  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

  25. Rakudo Perl 6 and Parrot Compilation Process Program Source Parse Tree Abstract Syntax Tree Opcode Syntax Tree PIR (Parrot IL) Parrot Bytecode

  26. Rakudo Perl 6 and Parrot Compilation Process Program Source PGE Parse Tree NQP Abstract Syntax Tree PCT Opcode Syntax Tree PCT PIR (Parrot IL) Parrot Parrot Bytecode

  27. Rakudo Perl 6 and Parrot Compilation Process Program Source PGE Parse Tree NQP Abstract Syntax Tree PCT Opcode Syntax Tree PCT PIR (Parrot IL) Parrot Parrot Bytecode

  28. Rakudo Perl 6 and Parrot Compilation Process Program Source PGE Parse Tree NQP Abstract Syntax Tree PCT Opcode Syntax Tree PCT PIR (Parrot IL) Parrot Parrot Bytecode

  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

  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 rule if_statement { 'if' <expression> <block> {*} } � You put a {*} in place to indicate that we should run an action

  31. Rakudo Perl 6 and Parrot Compilation Process Program Source PGE Parse Tree NQP Abstract Syntax Tree PCT Opcode Syntax Tree PCT PIR (Parrot IL) Parrot Parrot Bytecode

  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

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

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

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

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

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

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend