Reputation: 6808
Is there a way to convert code from one high-level representation to another? For example, in Javascript, a popular way of maintaining state is with a third-party library called Redux. VueJS on the other hand, has its own version of Redux, which is slightly similar but different. Is there a way to automagically convert between the two representations?
I suspect there might be a way using ASTs; however, at least in JS, ASTs seem to be good at low-level transforms, ex: const x = 2*4
to const x = 8
. However (and again, at least for JS), making higher-level transformations like the one I described above seem painful.
If there are no existing solutions, what is the state of the art (i.e., current research papers, etc)?
Upvotes: 1
Views: 116
Reputation: 95392
You can use a program transformation systemn (PTS).
These are tools that parse source code, build ASTs, and allow one to apply transformations to the ASTs to produce a different program; when transformation on the AST is complete, the PTS regenerates source text from the AST.
There are only a few tools out there (all on that Wikipedia page) that can handle arbitrary languages (parsing C++ is damn hard) or are robust enough to let one implement arbitrarily complicated transforms which is what you really need if you go from one language to another.
You can try to do this by just getting a parse tree and attempting to implement the rest of it yourself. See Life After Parsing for why that simply doesn't work in practice. These are hard tools to build in practice, which is why there aren't very many of them.
Even if you use on of the better tools, building a serious code translation engine is expensive. I do this for a living; it takes us 12-24 man-months to build such a tool on top of our particular PTS, which we've spent 20 years tuning for this kind of task.
Upvotes: 1