building a compiler in jruby
DESCRIPTION
Building a Compiler in JRuby - Chris Umbel at Jan 24th 2012 PittJUG meeting showing how you can create a language using JRuby tools to parse the language an emit JVM bytecode.TRANSCRIPT
![Page 1: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/1.jpg)
Building a Miserably Fun Compiler in
JRuby.Chris Umbel
@chrisumbel
http://www.chrisumbel.com
Sunday, January 29, 12
![Page 2: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/2.jpg)
Agenda
Ruby/JRuby introduction
Talk background
Building a compiler
Sunday, January 29, 12
![Page 3: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/3.jpg)
RubyDynamic, but strongly typed.
Object-oriented
metaprogrammable
Has multiple implementations (MRI, IronRuby, Rubinius, JRuby)
Has functional features (Higher-order functions, blocks)
Sunday, January 29, 12
![Page 4: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/4.jpg)
Ruby Examples
OO
Data Structures
Sunday, January 29, 12
![Page 5: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/5.jpg)
JRuby
Highly compatible Ruby implementation
Written in Java/runs on the JVM
Can consume arbitrary Java libraries
Favorable concurrency characteristics to CRuby (no GIL)
Sunday, January 29, 12
![Page 6: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/6.jpg)
JRuby Examples
Java types
Using .jars
conventions
Sunday, January 29, 12
![Page 7: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/7.jpg)
Inspiration
Ian Dees @undees
JRubyConf 2011
JRuby, Not Just for Hard-Headed Pragmatists Anymore
Author: Using JRuby: Bringing Ruby to Java
Sunday, January 29, 12
![Page 8: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/8.jpg)
JRubyConf
Practical problem solving
Customers, Customers, Customers!
ROI
Sunday, January 29, 12
![Page 9: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/9.jpg)
ThnadJust enough of a fictional programming language to show how to write a compiler
https://github.com/undees/thnad
Sort of JavaScript-ey
Allusion to the greatest of classical literature
Sunday, January 29, 12
![Page 10: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/10.jpg)
What We’ll Build
Lisp-style S-expressions
Pure JRuby
Compiles to Java bytecode
Sunday, January 29, 12
![Page 11: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/11.jpg)
What We’ll UseJRuby
Parslet
Kaspar Shiess
Parser in pure Ruby
Bitescript
Charles Nutter
JVM bytecode emitter for JRubySunday, January 29, 12
![Page 12: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/12.jpg)
What it wont’ do
Conditionals
Non-parametric locally scoped variables
OO
Provide Java library support, standard or otherwise.
Sunday, January 29, 12
![Page 13: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/13.jpg)
What It’ll Do
Basic math
Functions
Standard output
Suck!!!
Sunday, January 29, 12
![Page 14: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/14.jpg)
sucklang
Sunday, January 29, 12
![Page 15: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/15.jpg)
Language Components
Simple Atoms (identifiers; literal strings and integers)
Lists
Function calls
Sunday, January 29, 12
![Page 16: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/16.jpg)
(defun foo [x y] (+ x y))(println "result is " (foo 6 9))
sucklang Code Sample
Sunday, January 29, 12
![Page 17: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/17.jpg)
Parts
Parser
Transmogrifier
Compiler
Sunday, January 29, 12
![Page 18: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/18.jpg)
Step 1
Parser
Numbers
Basic list
Whitespace handling
Sunday, January 29, 12
![Page 19: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/19.jpg)
Step 2
Parser
Expressions
Sunday, January 29, 12
![Page 20: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/20.jpg)
Step 3
Parser
Identifiers
Atoms
Lists in lists
Operators are Identifiers
Sunday, January 29, 12
![Page 21: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/21.jpg)
Step 4
Parser
Strings
Sunday, January 29, 12
![Page 22: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/22.jpg)
Step 5
Parser
Vectors
Sunday, January 29, 12
![Page 23: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/23.jpg)
Step 6
Transmogrifier
AST
Sunday, January 29, 12
![Page 24: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/24.jpg)
Step 7: STOP!
javap - Java disassembler
Sunday, January 29, 12
![Page 25: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/25.jpg)
Step 8
bitescript example
Sunday, January 29, 12
![Page 26: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/26.jpg)
Step 9
compiler
java requires/imports
emitting of simple types
Sunday, January 29, 12
![Page 27: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/27.jpg)
Step 10
emitting of functions
+
println
defun
calls
Sunday, January 29, 12
![Page 28: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/28.jpg)
What It Did
Parsed S-expressions into an AST
Compiled AST to JVM bytecode
Sucked (a lot of assumptions)
Sunday, January 29, 12
![Page 29: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/29.jpg)
What We’ve Learned
Parslet
Bitescript
JVM innards
Sunday, January 29, 12
![Page 30: Building a compiler in JRuby](https://reader034.vdocuments.site/reader034/viewer/2022051412/54bd160b4a795932448b45c7/html5/thumbnails/30.jpg)
Thank you!
Sunday, January 29, 12