36
JRUBY, NOT JUST FOR HARD- HEADED PRAGMATISTS ANYMORE WHISKEY- CONF 2011 IAN DEES • @UNDEES JRUBY ALSO 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

JRuby, Not Just For Hard-Headed Pragmatists Anymore

Embed Size (px)

DESCRIPTION

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?

Citation preview

Page 1: JRuby, Not Just For Hard-Headed Pragmatists Anymore

JRUBY, NOT JUST FOR HARD-HEADED PRAGMATISTS ANYMORE

WHISKEY-CONF 2011

IAN DEES • @UNDEES

✹JRUBYALSO

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

Page 2: JRuby, Not Just For Hard-Headed Pragmatists Anymore

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.

Page 3: JRuby, Not Just For Hard-Headed Pragmatists Anymore

Time

Abs

trac

tion

compilers

The Three Paths

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

Page 4: JRuby, Not Just For Hard-Headed Pragmatists Anymore

Time

Abs

trac

tion

compilers

e-

xor eax, eax

If, like me, you grew up on the hardware path, a compiler is the next logical step beyond learning programming languages.

Page 5: JRuby, Not Just For Hard-Headed Pragmatists Anymore

Time

Abs

trac

tion

compilers

grammarslogic λx.x

e-

xor eax, eax

If you took the software path and learned the theory of computation first, then compilers are a practical application of those ideas.

Page 6: JRuby, Not Just For Hard-Headed Pragmatists Anymore

Time

Abs

trac

tion

compilers

grammarslogic λx.x

e-

xor eax, eax

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.

Page 7: JRuby, Not Just For Hard-Headed Pragmatists Anymore

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.)

Page 8: JRuby, Not Just For Hard-Headed Pragmatists Anymore

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

Page 9: JRuby, Not Just For Hard-Headed Pragmatists Anymore

The Tools• Parslet

http://kschiess.github.com/parslet

• BiteScripthttps://github.com/headius/bitescript

• Grittyhttps://github.com/undees/gritty

• Graphhttp://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.

Page 10: JRuby, Not Just For Hard-Headed Pragmatists Anymore

The Language

The language we’re going to be writing a compiler for is an extremely simple one.

Page 11: JRuby, Not Just For Hard-Headed Pragmatists Anymore

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.

Page 12: JRuby, Not Just For Hard-Headed Pragmatists Anymore

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.

Page 13: JRuby, Not Just For Hard-Headed Pragmatists Anymore

1. Integers

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

Page 14: JRuby, Not Just For Hard-Headed Pragmatists Anymore

42

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

Page 16: JRuby, Not Just For Hard-Headed Pragmatists Anymore

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.

Page 18: JRuby, Not Just For Hard-Headed Pragmatists Anymore

2. Function Calls

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

Page 19: JRuby, Not Just For Hard-Headed Pragmatists Anymore

foo

We need to be able to parse identifiers; we’ll use these for function and parameter names.

Page 20: JRuby, Not Just For Hard-Headed Pragmatists Anymore

As we did with integers, we want to transform the first hash representation into a more specific Ruby object.

Page 21: JRuby, Not Just For Hard-Headed Pragmatists Anymore

(42, foo)

Next, we’ll parse an argument list, which is a set of parentheses containing integers and parameter names.

Page 22: JRuby, Not Just For Hard-Headed Pragmatists Anymore

We’ll need code to transform that into an array of the Ruby objects we’ve already defined.

Page 23: JRuby, Not Just For Hard-Headed Pragmatists Anymore

baz(42, foo)

Now, we’re finally ready to parse a function call...

Page 24: JRuby, Not Just For Hard-Headed Pragmatists Anymore

...and transform it into a Ruby object that will be able to emit bytecode.

Page 26: JRuby, Not Just For Hard-Headed Pragmatists Anymore

3. Conditionals

The next big piece of our compiler is the conditional expression.

Page 27: JRuby, Not Just For Hard-Headed Pragmatists Anymore

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).

Page 28: JRuby, Not Just For Hard-Headed Pragmatists Anymore

Paradoxically, the more complicated the expression we parse, the more our tree of custom Ruby classes resembles the Ruby hash we started with.

Page 30: JRuby, Not Just For Hard-Headed Pragmatists Anymore

4. Function Definitions

The last piece we need for our factorial Thnad program is the ability to compile function definitions.

Page 31: JRuby, Not Just For Hard-Headed Pragmatists Anymore

function foo(x) { 5}

A function definition is the keyword “function” followed by a list of parameters in parentheses, then a computation between braces.

Page 32: JRuby, Not Just For Hard-Headed Pragmatists Anymore

And here’s the resulting tree of Ruby objects.

Page 34: JRuby, Not Just For Hard-Headed Pragmatists Anymore

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).

Page 35: JRuby, Not Just For Hard-Headed Pragmatists Anymore

Codehttps://github.com/undees/thnad

The full compiler, complete with bytecode generation, is available here.

Page 36: JRuby, Not Just For Hard-Headed Pragmatists Anymore

For more about JRuby (which made this whole exercise possible in half an hour), see our book at http://pragprog.com/titles/jruby.