SlideShare a Scribd company logo
JRUBY, NOT JUST FOR HARD-
    HEADED PRAGMATISTS ANYMORE
                                                        IAN DEES • @UNDEES


                                                        WHISKEY-
                                                        CONF 2011
                                                                               ✹       ALSO
                                                                                       JR UBY




Hi, I’m Ian. Hope everyone’s having a great WhiskyConf. I hear we’ll have some JRuby material at
this conference as well. ;-)

http://www.flickr.com/photos/etnobofin/3786071796
Write a Compiler



Most JRuby talks are pragmatic: either a case study of how JRuby helped a project, or a specific
library that developers can use. This talk will be more theoretical. We’ll use JRuby as a vehicle to
explore an area of computer science; namely, writing a compiler.
The Three Paths
  Abstraction




                                                                             compilers




                                              Time
Depending on your path through computer science, you may have encountered the topic already.
Abstraction




                                                                                  compilers


                                                                    xor eax, eax




                       e-
                                                 Time
If, like me, you grew up on the hardware path, a compiler is the next logical step beyond learning
programming languages.
logic
                                              λx.x                 grammars
  Abstraction




                                                                                 compilers


                                                                   xor eax, eax




                       e-
                                                Time
If you took the software path and learned the theory of computation first, then compilers are a
practical application of those ideas.
logic
                                             λx.x                grammars
  Abstraction




                                                                               compilers


                                                                 xor eax, eax




                      e-
                                               Time
If you took the self-made path, you may or may not have encountered compilers by this point on
your journey. Either way, I hope the tools we talk about today pique your interest.
The Plan

           ✓Stay up late drinking whisky
           ✓Wear the T-shirt of the band that’s playing
           ✓Choose the wrong topic
           ✓Go at a pace that’s awkward for everyone
           ✓Code live on stage

The plan for this talk is to show up tired, wear a JRubyConf t-shirt (this is like showing up at a
concert wearing the t-shirt of the band that’s playing), choose a topic and pace different from all
the other talks, and introduce an element of uncertainty by coding on stage. (If you’re reading this,
I’ll show you GitHub commits instead.)
This might be a train wreck, but train wrecks can be entertaining as long as they’re toy trains.

http://www.flickr.com/photos/cianginty/3148870954
The Tools
           • Parslet
                http://kschiess.github.com/parslet

           • BiteScript
                https://github.com/headius/bitescript

           • Gritty
                https://github.com/undees/gritty

           • Graph
                http://rubygems.org/gems/graph

           • JRuby!
The first two tools are the ones we’ll use to write the compiler. The next two are the ones I used to
draw the pictures on the subsequent slides.
The Language



The language we’re going to be writing a compiler for is an extremely simple one.
Thnad
                                    (thank you, Dr. Seuss!)




Most of the real letters of the alphabet are already used for programming languages (C, D, J, K, R,
etc.). So we’ll use the fictional letter “Thnad,” which comes to us from Dr. Seuss.
function factorial(n) {
         if (eq(n, 1)) {
           1
         } else {
           times(n, factorial(minus(n, 1)))
         }
       }

       print(factorial(4))



Here’s the Thnad program we’d eventually like to compile. It has integers, function calls,
conditionals, and function definitions—and that’s about it.
1. Integers



One big step is to write a compiler top to bottom that parses an integer and outputs a working
Java .class file.
42




First, we’re going to write code that parses the text “42” into a Ruby hash representing the tree on
the right.
Following Along


            • Test → test_parser.rb
            • Code → parser.rb



Slides like this one will contain a link to the tests, code, or full examples for the steps we’ve just
been over.
Next, we’re going to transform that Ruby hash (which isn’t as useful by itself) into a custom Ruby
class that will eventually be able to output Java bytecode.
• Test → test_transform.rb
• Code → transform.rb
• Full Example
2. Function Calls



Now, let’s add the ability to compile function calls.
foo




We need to be able to parse identifiers; we’ll use these for function and parameter names.
As we did with integers, we want to transform the first hash representation into a more specific
Ruby object.
(42, foo)




Next, we’ll parse an argument list, which is a set of parentheses containing integers and parameter
names.
We’ll need code to transform that into an array of the Ruby objects we’ve already defined.
baz(42, foo)




Now, we’re finally ready to parse a function call...
...and transform it into a Ruby object that will be able to emit bytecode.
• Full Example
3. Conditionals



The next big piece of our compiler is the conditional expression.
if (0) {
                42
              } else {
                667
              }




Here’s the kind of conditional statement we’d like to be able to parse. The “else” clause is
mandatory (I hope Zed Shaw will approve).
Paradoxically, the more complicated the expression we parse, the more our tree of custom Ruby
classes resembles the Ruby hash we started with.
• Full Example
4. Function Definitions



The last piece we need for our factorial Thnad program is the ability to compile function definitions.
function foo(x) {
       5
     }




A function definition is the keyword “function” followed by a list of parameters in parentheses, then
a computation between braces.
And here’s the resulting tree of Ruby objects.
• Full Example
Marc-André Cournoyer

Marc-André Cournoyer’s book on creating your own programming language was helpful during this
process as a way of “checking my answers” (just like checking your answers to today’s crossword
puzzle by looking in tomorrow’s paper).
Code
                           https://github.com/undees/thnad




The full compiler, complete with bytecode generation, is available here.
For more about JRuby (which made this whole exercise possible in half an hour), see our book at
http://pragprog.com/titles/jruby.

More Related Content

What's hot

ppt9
ppt9ppt9
ppt9
callroom
 
ppt18
ppt18ppt18
ppt18
callroom
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.ppt
callroom
 
Inside Python [OSCON 2012]
Inside Python [OSCON 2012]Inside Python [OSCON 2012]
Inside Python [OSCON 2012]
Tom Lee
 
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
Tom Lee
 
Embed--Basic PERL XS
Embed--Basic PERL XSEmbed--Basic PERL XS
Embed--Basic PERL XS
byterock
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
Python Compiler Internals Presentation Slides
Python Compiler Internals Presentation SlidesPython Compiler Internals Presentation Slides
Python Compiler Internals Presentation Slides
Tom Lee
 
Variables: names, bindings, type, scope
Variables: names, bindings, type, scopeVariables: names, bindings, type, scope
Variables: names, bindings, type, scope
suthi
 
Ti1220 Lecture 2: Names, Bindings, and Scopes
Ti1220 Lecture 2: Names, Bindings, and ScopesTi1220 Lecture 2: Names, Bindings, and Scopes
Ti1220 Lecture 2: Names, Bindings, and Scopes
Eelco Visser
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
Rasan Samarasinghe
 
Python ppt
Python pptPython ppt
Python ppt
Mohita Pandey
 
Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
melbournepatterns
 

What's hot (13)

ppt9
ppt9ppt9
ppt9
 
ppt18
ppt18ppt18
ppt18
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.ppt
 
Inside Python [OSCON 2012]
Inside Python [OSCON 2012]Inside Python [OSCON 2012]
Inside Python [OSCON 2012]
 
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
 
Embed--Basic PERL XS
Embed--Basic PERL XSEmbed--Basic PERL XS
Embed--Basic PERL XS
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
 
Python Compiler Internals Presentation Slides
Python Compiler Internals Presentation SlidesPython Compiler Internals Presentation Slides
Python Compiler Internals Presentation Slides
 
Variables: names, bindings, type, scope
Variables: names, bindings, type, scopeVariables: names, bindings, type, scope
Variables: names, bindings, type, scope
 
Ti1220 Lecture 2: Names, Bindings, and Scopes
Ti1220 Lecture 2: Names, Bindings, and ScopesTi1220 Lecture 2: Names, Bindings, and Scopes
Ti1220 Lecture 2: Names, Bindings, and Scopes
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Python ppt
Python pptPython ppt
Python ppt
 
Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
 

Similar to JRuby, Not Just For Hard-Headed Pragmatists Anymore

Os Keysholistic
Os KeysholisticOs Keysholistic
Os Keysholistic
oscon2007
 
The Holistic Programmer
The Holistic ProgrammerThe Holistic Programmer
The Holistic Programmer
Adam Keys
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
AboutYouGmbH
 
Ruby tutorial
Ruby tutorialRuby tutorial
Ruby tutorial
knoppix
 
Lpi Part 2 Basic Administration
Lpi Part 2 Basic AdministrationLpi Part 2 Basic Administration
Lpi Part 2 Basic Administration
YemenLinux
 
Exploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with ArcheometerExploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with Archeometer
Agustin Ramos
 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: Introduction
Eelco Visser
 
Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1
Mark Menard
 
Sugar Presentation - YULHackers March 2009
Sugar Presentation - YULHackers March 2009Sugar Presentation - YULHackers March 2009
Sugar Presentation - YULHackers March 2009
spierre
 
Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex
Espen Brækken
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
Devnology
 
Rust presentation convergeconf
Rust presentation convergeconfRust presentation convergeconf
Rust presentation convergeconf
Krishna Kumar Thokala
 
Plc part 1
Plc part 1Plc part 1
Plc part 1
Taymoor Nazmy
 
2016 bioinformatics i_python_part_1_wim_vancriekinge
2016 bioinformatics i_python_part_1_wim_vancriekinge2016 bioinformatics i_python_part_1_wim_vancriekinge
2016 bioinformatics i_python_part_1_wim_vancriekinge
Prof. Wim Van Criekinge
 
P1 2017 python
P1 2017 pythonP1 2017 python
P1 2017 python
Prof. Wim Van Criekinge
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
Iván Montes
 
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming EssentialsRaleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Sean McCarthy
 
C++0x
C++0xC++0x
Why Ruby?
Why Ruby? Why Ruby?
Why Ruby?
IT Weekend
 
Rustbridge
RustbridgeRustbridge
Rustbridge
kent marete
 

Similar to JRuby, Not Just For Hard-Headed Pragmatists Anymore (20)

Os Keysholistic
Os KeysholisticOs Keysholistic
Os Keysholistic
 
The Holistic Programmer
The Holistic ProgrammerThe Holistic Programmer
The Holistic Programmer
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
 
Ruby tutorial
Ruby tutorialRuby tutorial
Ruby tutorial
 
Lpi Part 2 Basic Administration
Lpi Part 2 Basic AdministrationLpi Part 2 Basic Administration
Lpi Part 2 Basic Administration
 
Exploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with ArcheometerExploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with Archeometer
 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: Introduction
 
Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1
 
Sugar Presentation - YULHackers March 2009
Sugar Presentation - YULHackers March 2009Sugar Presentation - YULHackers March 2009
Sugar Presentation - YULHackers March 2009
 
Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
 
Rust presentation convergeconf
Rust presentation convergeconfRust presentation convergeconf
Rust presentation convergeconf
 
Plc part 1
Plc part 1Plc part 1
Plc part 1
 
2016 bioinformatics i_python_part_1_wim_vancriekinge
2016 bioinformatics i_python_part_1_wim_vancriekinge2016 bioinformatics i_python_part_1_wim_vancriekinge
2016 bioinformatics i_python_part_1_wim_vancriekinge
 
P1 2017 python
P1 2017 pythonP1 2017 python
P1 2017 python
 
Programming Languages #devcon2013
Programming Languages #devcon2013Programming Languages #devcon2013
Programming Languages #devcon2013
 
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming EssentialsRaleigh Code Camp 2103 - .Net Metaprogramming Essentials
Raleigh Code Camp 2103 - .Net Metaprogramming Essentials
 
C++0x
C++0xC++0x
C++0x
 
Why Ruby?
Why Ruby? Why Ruby?
Why Ruby?
 
Rustbridge
RustbridgeRustbridge
Rustbridge
 

More from Erin Dees

Logic Lessons That Last Generations
Logic Lessons That Last GenerationsLogic Lessons That Last Generations
Logic Lessons That Last Generations
Erin Dees
 
How 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 book
How 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 bookHow 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 book
How 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 book
Erin Dees
 
How 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 book
How 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 bookHow 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 book
How 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 book
Erin Dees
 
A jar-nORM-ous Task
A jar-nORM-ous TaskA jar-nORM-ous Task
A jar-nORM-ous Task
Erin Dees
 
Cucumber meets iPhone
Cucumber meets iPhoneCucumber meets iPhone
Cucumber meets iPhone
Erin Dees
 
Yes, But
Yes, ButYes, But
Yes, But
Erin Dees
 

More from Erin Dees (6)

Logic Lessons That Last Generations
Logic Lessons That Last GenerationsLogic Lessons That Last Generations
Logic Lessons That Last Generations
 
How 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 book
How 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 bookHow 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 book
How 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 book
 
How 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 book
How 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 bookHow 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 book
How 5 people with 4 day jobs in 3 time zones enjoyed 2 years writing 1 book
 
A jar-nORM-ous Task
A jar-nORM-ous TaskA jar-nORM-ous Task
A jar-nORM-ous Task
 
Cucumber meets iPhone
Cucumber meets iPhoneCucumber meets iPhone
Cucumber meets iPhone
 
Yes, But
Yes, ButYes, But
Yes, But
 

Recently uploaded

How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 

Recently uploaded (20)

How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 

JRuby, Not Just For Hard-Headed Pragmatists Anymore

  • 1. JRUBY, NOT JUST FOR HARD- HEADED PRAGMATISTS ANYMORE IAN DEES • @UNDEES WHISKEY- CONF 2011 ✹ ALSO JR UBY Hi, I’m Ian. Hope everyone’s having a great WhiskyConf. I hear we’ll have some JRuby material at this conference as well. ;-) http://www.flickr.com/photos/etnobofin/3786071796
  • 2. Write a Compiler Most JRuby talks are pragmatic: either a case study of how JRuby helped a project, or a specific library that developers can use. This talk will be more theoretical. We’ll use JRuby as a vehicle to explore an area of computer science; namely, writing a compiler.
  • 3. The Three Paths Abstraction compilers Time Depending on your path through computer science, you may have encountered the topic already.
  • 4. Abstraction compilers xor eax, eax e- Time If, like me, you grew up on the hardware path, a compiler is the next logical step beyond learning programming languages.
  • 5. logic λx.x grammars Abstraction compilers xor eax, eax e- Time If you took the software path and learned the theory of computation first, then compilers are a practical application of those ideas.
  • 6. logic λx.x grammars Abstraction compilers xor eax, eax e- Time If you took the self-made path, you may or may not have encountered compilers by this point on your journey. Either way, I hope the tools we talk about today pique your interest.
  • 7. The Plan ✓Stay up late drinking whisky ✓Wear the T-shirt of the band that’s playing ✓Choose the wrong topic ✓Go at a pace that’s awkward for everyone ✓Code live on stage The plan for this talk is to show up tired, wear a JRubyConf t-shirt (this is like showing up at a concert wearing the t-shirt of the band that’s playing), choose a topic and pace different from all the other talks, and introduce an element of uncertainty by coding on stage. (If you’re reading this, I’ll show you GitHub commits instead.)
  • 8. This might be a train wreck, but train wrecks can be entertaining as long as they’re toy trains. http://www.flickr.com/photos/cianginty/3148870954
  • 9. The Tools • Parslet http://kschiess.github.com/parslet • BiteScript https://github.com/headius/bitescript • Gritty https://github.com/undees/gritty • Graph http://rubygems.org/gems/graph • JRuby! The first two tools are the ones we’ll use to write the compiler. The next two are the ones I used to draw the pictures on the subsequent slides.
  • 10. The Language The language we’re going to be writing a compiler for is an extremely simple one.
  • 11. Thnad (thank you, Dr. Seuss!) Most of the real letters of the alphabet are already used for programming languages (C, D, J, K, R, etc.). So we’ll use the fictional letter “Thnad,” which comes to us from Dr. Seuss.
  • 12. function factorial(n) { if (eq(n, 1)) { 1 } else { times(n, factorial(minus(n, 1))) } } print(factorial(4)) Here’s the Thnad program we’d eventually like to compile. It has integers, function calls, conditionals, and function definitions—and that’s about it.
  • 13. 1. Integers One big step is to write a compiler top to bottom that parses an integer and outputs a working Java .class file.
  • 14. 42 First, we’re going to write code that parses the text “42” into a Ruby hash representing the tree on the right.
  • 15. Following Along • Test → test_parser.rb • Code → parser.rb Slides like this one will contain a link to the tests, code, or full examples for the steps we’ve just been over.
  • 16. Next, we’re going to transform that Ruby hash (which isn’t as useful by itself) into a custom Ruby class that will eventually be able to output Java bytecode.
  • 17. • Test → test_transform.rb • Code → transform.rb • Full Example
  • 18. 2. Function Calls Now, let’s add the ability to compile function calls.
  • 19. foo We need to be able to parse identifiers; we’ll use these for function and parameter names.
  • 20. As we did with integers, we want to transform the first hash representation into a more specific Ruby object.
  • 21. (42, foo) Next, we’ll parse an argument list, which is a set of parentheses containing integers and parameter names.
  • 22. We’ll need code to transform that into an array of the Ruby objects we’ve already defined.
  • 23. baz(42, foo) Now, we’re finally ready to parse a function call...
  • 24. ...and transform it into a Ruby object that will be able to emit bytecode.
  • 26. 3. Conditionals The next big piece of our compiler is the conditional expression.
  • 27. if (0) { 42 } else { 667 } Here’s the kind of conditional statement we’d like to be able to parse. The “else” clause is mandatory (I hope Zed Shaw will approve).
  • 28. Paradoxically, the more complicated the expression we parse, the more our tree of custom Ruby classes resembles the Ruby hash we started with.
  • 30. 4. Function Definitions The last piece we need for our factorial Thnad program is the ability to compile function definitions.
  • 31. function foo(x) { 5 } A function definition is the keyword “function” followed by a list of parameters in parentheses, then a computation between braces.
  • 32. And here’s the resulting tree of Ruby objects.
  • 34. Marc-André Cournoyer Marc-André Cournoyer’s book on creating your own programming language was helpful during this process as a way of “checking my answers” (just like checking your answers to today’s crossword puzzle by looking in tomorrow’s paper).
  • 35. Code https://github.com/undees/thnad The full compiler, complete with bytecode generation, is available here.
  • 36. For more about JRuby (which made this whole exercise possible in half an hour), see our book at http://pragprog.com/titles/jruby.