I have old software (in a language that is not dead, but dead for me ;-)), which implements the basic system of pattern matching and source code handing. I am considering the possibility of resurrecting this code, translating it into a modern language, and opening the project as a refactoring tool. Before I go much further, I want to know if something like this exists (my google-fu is blowing air tonight).
Here's how it works:
- the pattern matching part matches the source code patterns spanning multiple lines of code using a pattern with binding variables,
- the rewriting part of the template uses the template to overwrite the matching code by inserting the contents of the associated variables from the matching template
- matching and rewriting patterns are linked (1: 1) using a simple (unconditional) rewrite rule
the software works with the abstract syntax tree (AST) of the input application and outputs a modified AST, which can then be regenerated into new source code
for example, suppose we find a bunch of while-loops, which really should be for-loops. The following pattern will match the while-loop pattern:
Template oldLoopPtrn int @cnt@ = 0; while (@cnt@ < @max@) { β¦ @body@ ++@cnt@; } End_Template
while the following template will specify an output rewrite template:
Template newLoopPtrn for(int @cnt@ = 0; @cnt@ < @max@; @cnt@++) { @body@ } End_Template
and a simple rule to link them
Rule oldLoopPtrn --> newLoopPtrn
so the code looks like this:
int i=0; while(i<arrlen) { printf("element %d: %f\n",i,arr[i]); ++i; }
automatically rewrites to look like this:
for(int i = 0; i < arrlen; i++) { printf("element %d: %f\n",i,arr[i]); }
The closest I've seen are some of the code refactoring tools, but they seem to be designed to interactively overwrite selected fragments, rather than bulk automatic changes.
I believe that such a tool can overload refactoring and work in several languages ββ(even HTML / CSS). I also believe that converting and polishing the code base will be a huge project that I just can't do it alone at any reasonable time.
So, is there something like this already? If not, then any obvious signs (other than the terms of the rewriting rule) to consider?
EDIT: The only feature of this system that I really like is that the template templates are pretty obvious and easy to read, because they are written in the same language as the target source code, and not in some esoteric mutated regular expressions / Format BNF.