---
created_at: '2016-02-07T18:53:56.000Z'
title: Writing a compiler in Ruby, bottom up (2008)
url: http://hokstad.com/compiler
author: rspivak
points: 89
story_text: 
comment_text: 
num_comments: 4
story_id: 
story_title: 
story_url: 
parent_id: 
created_at_i: 1454871236
_tags:
- story
- author_rspivak
- story_11054159
objectID: '11054159'

---
[Source](http://hokstad.com/compiler "Permalink to Writing a compiler in Ruby, bottom up")

# Writing a compiler in Ruby, bottom up

[Vidar Hokstad][1] __

* [Home ][1]
* [Blog][2]
* [Compiler][3]
* [Docker][4]
* [Consulting][5]
* [Home][1]
* [Blog][2]
* [Compiler][3]
* [Docker][4]
* [Consulting][5]

# Writing a compiler in Ruby, bottom up

(To follow my compiler related posts, either subscribe to my main RSS feed, or [the compiler specific one][6] )

Back in March 2008 I started publishing a series on how to write a compiler in Ruby, bottom up, that is, starting with the code generator and working my way up instead of the more traditional approach of writing the parser first. Here are the parts I've published on my blog so far.

(The bits labeled "interlude" are sort-of related articles in that the cover issues with writing/developing compilers, or issues related to compilation, but are not directly tied into the actual article series)

* [Step 1][7] \- Creating a simple prolog / epilog for the main function
* [Step 2][8] \- Function calls / Hello World
* [Step 3][9] \- Chaining expressions ; Adding sub-expressions
* [Step 4][10] \- Defining functions ; Adding support for a runtime
* [Step 5][11] \- Number literals ; if .. then .. else
* [Step 6][12] \- Anonymous functions: lamba / call
* [Step 7][13] \- Making use of lambda / call ; Function arguments
* [Step 8][14] \- Assignment, arithmetic and comparisons
* [Step 9][15] \- A builtin "while"
* [Step 10][16] \- Testing the compiler: A primitive "parser"
* [Step 11][17] \- A separate emitter class to output assembler
* [Step 12][18] \- Adding support for arrays
* [Interlude - A simple operator precedence parser][19]
* [Step 13][20] \- Local variables
* [Step 14][21] \- Variable length arguments
* [Step 15][22] \- Starting on the real parser
* [Step 16][23] \- Extending the parser
* [Step 17][24] \- Inferring "let"'s
* [Step 18][25] \- Plugging in an operator precedence parser
* [Interlude - The Ruby Object Model][26] \- useful overview as the next steps is to start fleshing out an object model
* [Step 19][27] \- The Object Model
* [Interlude - The problem with compiling Ruby][28] \- Overview of some of the problems with compiling Ruby
* [Step 20][29] \- Taking stock of where we are; detail walkthrough of the shunting yard parse component
* [Step 21][30] \- Basic support for the Symbol class; starting to work `on attr_reader`/`attr_writer`/`attr_accessor`
* [Step 22][31] \- A diversion into method_missing
* [Step 23][32] \- Real Strings: Turning strings into objects
* [Interlude - How to implement closures][33]
* [Step 24][34] \- The Outer Scope: Cleaning up and adding the "main" object
* [Step 25][35] \- Towards define_method via closures
* [Step 26][36] \- Taking a stab at STABS support (debugging/gdb)
* [Step 27][37] \- The Operators - Part 1: Parsing; The start of the removal of 'runtime.c'
* [Step 28][38] \- The Operators - Part 2: Turning numbers into Numbers (or small enough integers into Fixnum's at least)
* [Step 29][39] \- The Operators - Part 3: Operating on our newly minted Fixnums
* [Step 30][40] \- The Operators - Part 4: Comparisons
* [Step 31][41] \- Stringing things together; basic string output and string interpolation
* [Step 32][42] \- Testing code generation
* [Step 33][43] \- Register Allocation
* [Step 34][44] \- Putting self in a register
* [Step 35][45] \- Towards Self Hosting
* [Step 36][46] \- Scanning for problems
* [Step 37][47] \- Default Arguments
* [Step 38][48] \- Super
* [Step 39][49] \- To be or not to be nil
* [Step 40][50] \- Untangling "expect"
* [Step 41][51] \- Messy further steps towards self-hosting
* [Step 42][52] \- Re-opening class definitions
* [Step 43][53] \- Eigenclasses
* [Step 44][54] \- Mostly wrapping up self-hosting of the tokenization code
* [Step 45][55] \- '#send' and method lookup (provisionally April '15)

All the code can now be found in [this GitHub repository][56]

## Related resources

A recent (as of 2014) article series on ["reconstructing" Ruby][57] (in C)

[Someone commenting on this series][58] on Hackernews pointed to [Nicklaus Wirth][59]'s excellent book on compiler construction (PDF available on his homepage) as a good starting point from a traditional, top down point of view. It's a good and very approachable book, and well worth a read.

Another great series with a more traditional approach is a famous series by Jack Crenshaw: [Let's build a compiler][60]

[This page at Stackoverflow][61] has a great list of other compiler writing resources.

  
  

Please enable JavaScript to view the [comments powered by Disqus.][62] [blog comments powered by Disqus][63]

![][64]

I'm a Norwegian technologist who has been living in London since 2000. I have a son, Tristan, born in 2009.   
  
I've got development and management experience from a number of startups as well as more established companies, after co-founding my first company at age 19. I currently split my time between being the Technical Director of [Nudge CRM Ltd][65], and running my [consulting business][66], primarily related to development and devops. ([More...][67]) 

##### Contact me

__   [ [email protected] ][68]   
  
[ __ ][69]  [ __ ][70]  [ __ ][71] 

##### Tags

[assembler (5)][72] [blog (4)][2] [blogging (14)][73] [cairo (3)][74] [compiler (56)][3] [compiler-in-Ruby-bottom-up (48)][75] [compilers (4)][76] [debugging (3)][77] [devops (5)][78] [diagrams (5)][79] [dot (4)][80] [family (6)][81] [frameworks (3)][82] [git (4)][83] [github (4)][84] [grammar (3)][85] [graphics (7)][86] [graphviz (8)][87] [howto (8)][88] [humor (10)][89] [interesting-sites (32)][90] [internet (14)][91] [law (8)][92] [linux (7)][93] [management (5)][94] [microsoft (4)][95] [mvc (3)][96] [my-life (25)][97] [ooxml (4)][98] [openvz (6)][99] [operators (8)][100] [opinion (5)][101] [parser (10)][102] [parser-assembler (11)][103] [politics (25)][104] [programming (52)][105] [programming-languages (10)][106] [rack (7)][107] [random-ideas (5)][108] [ruby (81)][109] [semantic-web (26)][110] [sequel (4)][111] [software-design (26)][112] [software-patents (15)][113] [strace (3)][114] [tutorial (26)][115] [virtualization (5)][116] [visualization (3)][117] [vm (5)][118] [xsl (4)][119] [xslt (3)][120]

© 2015 Copyright Vidar Hokstad - All rights reserved 

[1]: http://hokstad.com/
[2]: http://hokstad.com/blog
[3]: http://hokstad.com/compiler
[4]: http://hokstad.com/docker
[5]: http://hokstadconsulting.com/
[6]: http://www.hokstad.com/tag/compiler/feed
[7]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-1
[8]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-2
[9]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-3
[10]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-4
[11]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-5
[12]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-6
[13]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-7
[14]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-8
[15]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-9
[16]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-10
[17]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-11
[18]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-12
[19]: http://hokstad.com/operator-precedence-parser.html
[20]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-13
[21]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-14
[22]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-15
[23]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-16
[24]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-17
[25]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-18
[26]: http://hokstad.com/ruby-object-model
[27]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-19
[28]: http://hokstad.com/the-problem-with-compiling-ruby
[29]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-20
[30]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-21
[31]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-22
[32]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-23
[33]: http://hokstad.com/how-to-implement-closures.html
[34]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-24
[35]: http://hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-25
[36]: http://hokstad.com/compiler/26-stabs
[37]: http://hokstad.com/compiler/27-the-operators
[38]: http://hokstad.com/compiler/28-the-operators-2
[39]: http://hokstad.com/compiler/29-the-operators-3
[40]: http://hokstad.com/compiler/30-the-operators-4
[41]: http://hokstad.com/compiler/31-stringing-things-together
[42]: http://hokstad.com/compiler/32-testing-code-generation
[43]: http://hokstad.com/compiler/33-register-allocation
[44]: http://hokstad.com/compiler/34-self-in-a-register
[45]: http://hokstad.com/compiler/35-towards-self-hosting
[46]: http://hokstad.com/compiler/36-scanning-for-problems
[47]: http://hokstad.com/compiler/37-default-arguments
[48]: http://hokstad.com/compiler/38-super
[49]: http://hokstad.com/compiler/39-to-be-or-not-to-be-nil
[50]: http://hokstad.com/compiler/40-untangling-expect
[51]: http://hokstad.com/compiler/41-further-steps-towards-self-hosting
[52]: http://hokstad.com/compiler/42-reopening-classes
[53]: http://hokstad.com/compiler/43-eigenclasses
[54]: http://hokstad.com/compiler/44
[55]: http://hokstad.com/compiler/45-send
[56]: https://github.com/vidarh/writing-a-compiler-in-ruby/tree/master
[57]: http://www.halogenandtoast.com/reconstructing-ruby-our-first-lexer/
[58]: http://news.ycombinator.com/item?id=319337
[59]: http://www.inf.ethz.ch/personal/wirth/
[60]: http://lambda-the-ultimate.org/classic/message6192.html
[61]: http://stackoverflow.com/questions/1669/learning-to-write-a-compiler
[62]: http://disqus.com/?ref_noscript
[63]: http://disqus.com
[64]: http://hokstad.com/images/face3.jpg
[65]: http://nudgecrm.com
[66]: http://hokstadconsulting.com
[67]: http://hokstad.com/about
[68]: http://hokstad.com#
[69]: http://www.twitter.com/vhokstad
[70]: http://www.linkedin.com/in/vhokstad
[71]: https://github.com/vidarh
[72]: http://hokstad.com/assembler
[73]: http://hokstad.com/blogging
[74]: http://hokstad.com/cairo
[75]: http://hokstad.com/compiler-in-Ruby-bottom-up
[76]: http://hokstad.com/compilers
[77]: http://hokstad.com/debugging
[78]: http://hokstad.com/devops
[79]: http://hokstad.com/diagrams
[80]: http://hokstad.com/dot
[81]: http://hokstad.com/family
[82]: http://hokstad.com/frameworks
[83]: http://hokstad.com/git
[84]: http://hokstad.com/github
[85]: http://hokstad.com/grammar
[86]: http://hokstad.com/graphics
[87]: http://hokstad.com/graphviz
[88]: http://hokstad.com/howto
[89]: http://hokstad.com/humor
[90]: http://hokstad.com/interesting-sites
[91]: http://hokstad.com/internet
[92]: http://hokstad.com/law
[93]: http://hokstad.com/linux
[94]: http://hokstad.com/management
[95]: http://hokstad.com/microsoft
[96]: http://hokstad.com/mvc
[97]: http://hokstad.com/my-life
[98]: http://hokstad.com/ooxml
[99]: http://hokstad.com/openvz
[100]: http://hokstad.com/operators
[101]: http://hokstad.com/opinion
[102]: http://hokstad.com/parser
[103]: http://hokstad.com/parser-assembler
[104]: http://hokstad.com/politics
[105]: http://hokstad.com/programming
[106]: http://hokstad.com/programming-languages
[107]: http://hokstad.com/rack
[108]: http://hokstad.com/random-ideas
[109]: http://hokstad.com/ruby
[110]: http://hokstad.com/semantic-web
[111]: http://hokstad.com/sequel
[112]: http://hokstad.com/software-design
[113]: http://hokstad.com/software-patents
[114]: http://hokstad.com/strace
[115]: http://hokstad.com/tutorial
[116]: http://hokstad.com/virtualization
[117]: http://hokstad.com/visualization
[118]: http://hokstad.com/vm
[119]: http://hokstad.com/xsl
[120]: http://hokstad.com/xslt

