JRUBY, NOT JUST FOR HARD-    HEADED PRAGMATISTS ANYMORE                                                        IAN DEES • ...
Write a CompilerMost JRuby talks are pragmatic: either a case study of how JRuby helped a project, or a specificlibrary tha...
The Three Paths  Abstraction                                                                             compilers        ...
Abstraction                                                                                  compilers                    ...
logic                                              λx.x                 grammars  Abstraction                             ...
logic                                             λx.x                grammars  Abstraction                               ...
The Plan           ✓Stay up late drinking whisky           ✓Wear the T-shirt of the band that’s playing           ✓Choose ...
This might be a train wreck, but train wrecks can be entertaining as long as they’re toy trains.http://www.flickr.com/photo...
The Tools           • Parslet                http://kschiess.github.com/parslet           • BiteScript                http...
The LanguageThe 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 ...
function factorial(n) {         if (eq(n, 1)) {           1         } else {           times(n, factorial(minus(n, 1)))   ...
1. IntegersOne big step is to write a compiler top to bottom that parses an integer and outputs a workingJava .class file.
42First, we’re going to write code that parses the text “42” into a Ruby hash representing the tree onthe right.
Following Along            • Test → test_parser.rb            • Code → parser.rbSlides like this one will contain a link t...
Next, we’re going to transform that Ruby hash (which isn’t as useful by itself) into a custom Rubyclass that will eventual...
• Test → test_transform.rb• Code → transform.rb• Full Example
2. Function CallsNow, let’s add the ability to compile function calls.
fooWe 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 specificRuby object.
(42, foo)Next, we’ll parse an argument list, which is a set of parentheses containing integers and parameternames.
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. ConditionalsThe next big piece of our compiler is the conditional expression.
if (0) {                42              } else {                667              }Here’s the kind of conditional statement...
Paradoxically, the more complicated the expression we parse, the more our tree of custom Rubyclasses resembles the Ruby ha...
• Full Example
4. Function DefinitionsThe 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 parenthe...
And here’s the resulting tree of Ruby objects.
• Full Example
Marc-André CournoyerMarc-André Cournoyer’s book on creating your own programming language was helpful during thisprocess a...
Code                           https://github.com/undees/thnadThe full compiler, complete with bytecode generation, is ava...
For more about JRuby (which made this whole exercise possible in half an hour), see our book athttp://pragprog.com/titles/...
Upcoming SlideShare
Loading in …5
×

JRuby, Not Just For Hard-Headed Pragmatists Anymore

1,033 views
932 views

Published on

JRuby bills itself as the pragmatic Ruby, the go-to implementation when you need to fit into the Java universe or support a ton of platforms.

Who knew it was also a tool for having fun exploring the realms of computer science?

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,033
On SlideShare
0
From Embeds
0
Number of Embeds
32
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

JRuby, Not Just For Hard-Headed Pragmatists Anymore

  1. 1. JRUBY, NOT JUST FOR HARD- HEADED PRAGMATISTS ANYMORE IAN DEES • @UNDEES WHISKEY- CONF 2011 ✹ ALSO JR UBYHi, I’m Ian. Hope everyone’s having a great WhiskyConf. I hear we’ll have some JRuby material atthis conference as well. ;-)http://www.flickr.com/photos/etnobofin/3786071796
  2. 2. Write a CompilerMost JRuby talks are pragmatic: either a case study of how JRuby helped a project, or a specificlibrary that developers can use. This talk will be more theoretical. We’ll use JRuby as a vehicle toexplore an area of computer science; namely, writing a compiler.
  3. 3. The Three Paths Abstraction compilers TimeDepending on your path through computer science, you may have encountered the topic already.
  4. 4. Abstraction compilers xor eax, eax e- TimeIf, like me, you grew up on the hardware path, a compiler is the next logical step beyond learningprogramming languages.
  5. 5. logic λx.x grammars Abstraction compilers xor eax, eax e- TimeIf you took the software path and learned the theory of computation first, then compilers are apractical application of those ideas.
  6. 6. logic λx.x grammars Abstraction compilers xor eax, eax e- TimeIf you took the self-made path, you may or may not have encountered compilers by this point onyour journey. Either way, I hope the tools we talk about today pique your interest.
  7. 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 stageThe plan for this talk is to show up tired, wear a JRubyConf t-shirt (this is like showing up at aconcert wearing the t-shirt of the band that’s playing), choose a topic and pace different from allthe 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. 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. 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 todraw the pictures on the subsequent slides.
  10. 10. The LanguageThe language we’re going to be writing a compiler for is an extremely simple one.
  11. 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. 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. 13. 1. IntegersOne big step is to write a compiler top to bottom that parses an integer and outputs a workingJava .class file.
  14. 14. 42First, we’re going to write code that parses the text “42” into a Ruby hash representing the tree onthe right.
  15. 15. Following Along • Test → test_parser.rb • Code → parser.rbSlides like this one will contain a link to the tests, code, or full examples for the steps we’ve justbeen over.
  16. 16. Next, we’re going to transform that Ruby hash (which isn’t as useful by itself) into a custom Rubyclass that will eventually be able to output Java bytecode.
  17. 17. • Test → test_transform.rb• Code → transform.rb• Full Example
  18. 18. 2. Function CallsNow, let’s add the ability to compile function calls.
  19. 19. fooWe need to be able to parse identifiers; we’ll use these for function and parameter names.
  20. 20. As we did with integers, we want to transform the first hash representation into a more specificRuby object.
  21. 21. (42, foo)Next, we’ll parse an argument list, which is a set of parentheses containing integers and parameternames.
  22. 22. We’ll need code to transform that into an array of the Ruby objects we’ve already defined.
  23. 23. baz(42, foo)Now, we’re finally ready to parse a function call...
  24. 24. ...and transform it into a Ruby object that will be able to emit bytecode.
  25. 25. • Full Example
  26. 26. 3. ConditionalsThe next big piece of our compiler is the conditional expression.
  27. 27. if (0) { 42 } else { 667 }Here’s the kind of conditional statement we’d like to be able to parse. The “else” clause ismandatory (I hope Zed Shaw will approve).
  28. 28. Paradoxically, the more complicated the expression we parse, the more our tree of custom Rubyclasses resembles the Ruby hash we started with.
  29. 29. • Full Example
  30. 30. 4. Function DefinitionsThe last piece we need for our factorial Thnad program is the ability to compile function definitions.
  31. 31. function foo(x) { 5 }A function definition is the keyword “function” followed by a list of parameters in parentheses, thena computation between braces.
  32. 32. And here’s the resulting tree of Ruby objects.
  33. 33. • Full Example
  34. 34. Marc-André CournoyerMarc-André Cournoyer’s book on creating your own programming language was helpful during thisprocess as a way of “checking my answers” (just like checking your answers to today’s crosswordpuzzle by looking in tomorrow’s paper).
  35. 35. Code https://github.com/undees/thnadThe full compiler, complete with bytecode generation, is available here.
  36. 36. For more about JRuby (which made this whole exercise possible in half an hour), see our book athttp://pragprog.com/titles/jruby.

×