3 ms·
If you want to do this at production quality, you probably ought to use the go AST module to parse the Go into an AST and extract that. This sort of technique w
by jerf 5y ago
If you want to do this at production quality, you probably ought to use the go AST module to parse the Go into an AST and extract that. This sort of technique works OK as long as you use the subset between the two target languages where the grammars are compatible, but as you push the level of support for Go up, the odds of the two languages having irreconcilable differences approach 1.
For instance, if there is an order-of-operations disagreement between the two languages, the only solution that works fully is to do a full parse anyhow, because no amount of regex can ever cover all the bases in that case. And that's just an easily-understandable example of irreconcilable grammar differences, they can pop up anywhere (disagreements about reserved words, disagreements about what constitutes legal identifiers, etc.), and when they do you end up backed into doing a full parse of the original language anyhow.
You can then either interpret or compile the resulting AST. That's actually not too hard; what's hard is implementing the more advanced stuff like reflection in Go or dealing with the unsafe package. (If you don't need that, then great! Problem solved.) With the AST in hand you no longer need to worry about those differences.
I don't know ruby well enough to say, but I know Python, a very close neighbor, has everything you need to be able to write a Python module that would allow you to directly import Go modules. With effort it could even compile the Go into Python bytecode directly.