3 ms·
> Do you really want to convert the lines one-to-one? Yes, as much as possible! > This seems pretty wasteful. No, it's not! The more 1 to 1 the rewrite is, t
by klibertp 10y ago
> Do you really want to convert the lines one-to-one?
Yes, as much as possible!
> This seems pretty wasteful.
No, it's not! The more 1 to 1 the rewrite is, the more of it is possible to be done automatically.
Assuming your language gives you enough expressive power, it's often faster to write a library of shims for semantics and translate syntax automatically... in the worst case with a bunch of regexes - even that works better than manually writing the code from scratch, even if you read and drew inspiration from another implementation.
As an example, I started porting Python code, the PyParsing library, to Io[1]. I stripped docstrings for the moment, but if you open it side-by-side with the Python code, you'll see that it's almost line-to-line identical, except for the syntax. To do this I first wrote a library emulating some of Python features in Io[2], then passed the original code to a series of around 20 regexes, which resulted in not-working-but-close Io, which I manually corrected.
Now, writing 1.5k (it's about a half of PyParsing for now, not finished) lines of a library (not to mention 3 times as much docstrings, which you can reuse if you keep your translation close to original), even if you had previously a quite good understanding of such a library, wouldn't take two days. Which is exactly how much time it took me using this approach.
Of course, how beneficial this method may be to you in practice depends on the target language and how well it matches features of the original or how easy it is to extend it. Io and Racket are two examples of languages extremely well suited to this approach, but you can do the same with JavaScript, Python, Ruby, PERL and many other high-level, meta-programmable languages, just with a bit more effort.
Anyway, my two cents, as I recently had such an experience :)
[1] https://github.com/piotrklibert/ioparsing/blob/master/src/parsing.io https://github.com/piotrklibert/ioparsing/blob/master/src/pa...
[2] https://github.com/piotrklibert/ioparsing/blob/master/src/syntax.io https://github.com/piotrklibert/ioparsing/blob/master/src/sy... and str.io