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. Ive 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, butAfter running the unit test and checking out what it did, I created a new project and started hacking. The built in rewriter doesnt 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
Ive 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. Its 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!