Manually translating code from one language to another

I often write codes in MATLAB / Python to check if my algorithm is possible (and actually works). Then I need to convert all the code to C, and sometimes to FORTRAN90.

What would be a good way to convert mid-size code from one language to another?

I tried:

  • Converting all code from one to another, and then testing it. (Sometimes errors and errors occur that simply will not go away, and finding the source of the error will become a problem)

  • Go along the lines and check the output sequence every few lines. (Too much time)

  • Use transducers such as f2c . (In my experience, they are very terrible. I refer to many libraries that have different function calls for C and Fortran)

Moreover,

  • I am quite familiar with the programming languages ​​that I have to deal with, so I do not need manuals or reference books for my work (i.e. I know the syntax).

  • I do not ask this question specifically about MATLAB and C, but rather as a translation paradigm.

  • Regarding size, codes are less than 100 lines long.

  • I do not want to call the code of one language to another. Please do not offer this.

+6
source share
3 answers

Different languages ​​require different paradigms. You definitely don't write or develop code in the same way, for example. Matlab, Python, C # or C ++. Even hierarchies of objects will vary greatly depending on the language.

However, if your code consists of several interrelated procedures, you can leave with direct line-by-line translation (each language allows you to write two or three interrelated functions, remaining idiomatic). But this applies only to the simplest programs.

Prototyping in a high-level language and then realizing the same idea in a reliable and clean way in a “production” language is very good practice, but it involves two very different things:

  • Prototype in any language you want. Test, experiment and convince yourself that the idea works. Pay attention to the big picture, do not focus on performance, but on high-level ideas. Pay attention to the difficulties that you encounter during implementation, since you will again encounter them in step 2.
  • Bring in an idea from scratch in a production environment in X. This will be faster than if you hadn't completed the prototyping stage, since most of the difficulties were accomplished in the first stage. Use the idiomatic X and focus on correctness. Pay attention to corner cabinets, overall reliability and correct operation. You will notice that approximately half of your code consists of new things that did not appear in 1. (for example, checking errors, handling cases in the corner, I / O, unit testing, etc.).

You can see that line feed line by line is obviously not a good idea, since you are not translating into the same program.

Also, when not prototyping, I find that I discard the first version and create another that I like better, i.e. I find myself a prototype! Implementing the same thing twice is not a waste of time, it is a normal flow of development.

+7
source

You might want to use a language with a higher level domain with multiple backends (e.g. Matlab, C, Fortran), creating clean and idiomatic code for each target language, perhaps with some optimizations. If your problem domain is narrow and each piece of code is more or less typical, it should be pretty trivial to design and implement such a DSL.

+2
source

Divide the source down into psuedo code using input / processing / output, and then write a new code base to match this specification.

+1
source

Source: https://habr.com/ru/post/911960/


All Articles