Writing An Interpreter In 15 Minutes With Ruby
At the last regular London Ruby User Group meetup, James Coglan gave a talk on how to implement a Scheme interpreter in 15 minutes. He recorded a video of the coding in progress beforehand so he could focus on the narration so unfortunately the video (available in a higher resolution here from Vimeo) is without sound. There is, however, a ZIP file of HTML slides available to help you follow along.
Naturally, building an arbitrary interpreter from scratch without having a design in mind in 15 minutes is impossible (except for the most trivial of cases) but James' demo is nonetheless impressive. James' knowledge and examples are built upon his work on Heist, a Ruby-powered Scheme interpreter that has somewhat more functionality than the 15 minute version.
The parsing is taken care of primarily by Treetop, a syntactic analysis / parser creation DSL for Ruby. Without Treetop this would be a three hour demo rather than a 15 minute one, so if you haven't checked out Treetop yet, please do - it deserves a lot more praise than I've seen it get so far.
June 2, 2009 at 9:34 pm
Treetop is indeed awesome. I used it to create an interpreter for a pseudo assembly language called SimpleSem. More details here: http://thinkingdigitally.com/archive/a-simplesem-interpreter/
June 3, 2009 at 6:52 am
Yesterday?
June 3, 2009 at 11:21 am
I second Tom's "yesterday?" next LRUG is on the 8th isn't it? Otherwise I've missed it! (again) :(
June 3, 2009 at 1:00 pm
@Tom the post may be a little late but the talk was fantastic. It should be fairly easy to follow without sound
June 3, 2009 at 1:14 pm
My bad. The post seemed to only become popular yesterday but I now note it was written on May 18th. Thanks for bringing this to my attention.
June 5, 2009 at 5:51 pm
Actually parsing s-expressions without treetop is pretty straightforward. Where treetop shines is for grammars that are more complicated than Scheme's. (Not that this wouldn't be a good demo of its capabilities.) Looks like some good stuff.
June 8, 2009 at 2:56 pm
The hosts for the evening, Skills Matter, also filmed the whole talk, so you can get the audio of James's talk here: http://skillsmatter.com/podcast/ajax-ria/implementing-scheme-in-ruby (including his intro slides that explain exactly what Scheme is and what he's going to implement).