Last week I downloaded the source to JRuby and began poking around its source. My hope is that eventually I can convince it that converting Ruby source to Python is a better use of its time than Java class files. I’ve had to decipher large Java projects before and I was dreading the prospect of untangling inter-class dependencies, but as it turns out, JRuby is a great example of how to modularize. I think its also an example of how unit tests can show developers new to a project how to use it. The packages in JRuby are mostly dedicated to interpreting Abstract Syntax Trees (AST) into Java, but the lexer and parser packages are tantalizingly close to the root. After a quick survey of the classes contained therein, I took a look at what sort of unit tests were available specifically for the parser.
One of the things I found during my package perusal was something called an AST node visitor. Better than that, I found a specific package for node rewriting. Better still, there was a unit test for it! Presumably this is for code reformatting of Ruby itself, but After running the unit test and checking out what it did, I created a new project and started hacking. The built in rewriter doesn’t do anything interesting – it just regurgitates what its fed without comments – so my first job was to try a little tweak that rewrites a Ruby subclass from “subclass < parentclass” to Python’s “subclass(parentclass):”. Since JRuby already told me how to test this thing, it was simplicity itself to verify that my change had its desired affect.
I’ve only begun to pick at the syntax differences between Ruby and Python, but going through each compilation unit has the advantage of providing a road map. It’s also started me thinking about one layer above base syntax to simple code refactoring. Another interesting possibility is the use of AST graphs as a tool for code review. I guess I should reexamine the joys of Metaprogramming!