How to rewrite code in other languages
Consider this simple Java code:
source := 'public class Test { public void foo() { for (int i = 1; i <= 10; i+=1) { bar(i); } } }'.
Here is how to define a rewrite rule that transforms i+=1
into i++
:
rewrite := SmaCCRewriteFile new parserClass: JavaParser; addTransformation: (SmaCCRewrite comment: '' match: (SmaCCRewriteTreeMatch new source: '`i` += 1') transformation: (SmaCCRewriteStringTransformation new string: '`i` ++')).
Once we have the rule, we can apply it, and we get back the rewritten string:
engine := SmaCCRewriteEngine new rewriteRule: rewrite. node := JavaParser parse: source. engine source: (SmaCCAnnotatedString on: source); rewrite: node