Transpiler : A compiler for Source to Source compilation.
The simplest solution would be to learn new language from scratch. But is it feasible? This would be feasible only if you see yourself using the language frequently in future. But what if it’s a one-time requirement or you don’t have enough time? This is where transpilers come in picture!
What is a Transpiler?
A source-to-source translator or transpiler or transcompiler converts between programming languages that operate at approximately the same level of abstraction i.e. same level of complexity. Basically, what a transpiler does is, it takes one programming language as input which is the source language and , and gives an output as another programming language of same level which is the target code.
Transpiler Vs Compiler
The working of compiler is pretty state forward, it take high level code as input , scans it as whole translate and gives an output as lower level equivalent code, while transpiler just convert langauges from one to anotehr on same compelxity level . Following diagram lists where some of our more popular programming languages fall in this high-level vs. low-level spectrum explaining compilation and transpilation.
Working of Tranpiler
The transformation is not performed directly on the code but on the a representaion of the code which is the abstarct syntax tree(AST)
1. Parsing stage: obtain a AST using a parser from source code done by using parser generators like ANTLR
2. Transformation stage: we will transform in one or more steps the AST of the original language into the corresponding AST of the target language. The number of steps depend on how similar the source language and target language are. More similarity lesser steps
3. Generation stage: generating target code from transformed AST using code generators
A transpiler either can keep the structure of target code as close as possible to the source code , or do completely opposite of this and the target code would not look like source code depending on the frequency of work where it finds itself in use
· Migration: Migrate legacy code to a modern language.
· Compatibility: Generate a code of newer version from older version for better features. Eg: Python 3 from python 2. Or a code conforming older version while from a newer version.
Performance: the target language has a better compiler that can generate more optimized code.
Examples of Transpilers:
HipHop takes your PHP source code and converts it to a C++ equivalent, then compiles the generated C++ code and runs it within an embedded HTTP server. Similarly,
• Cfront: C++ to c source code.
• J2ObjC: Java to objective-C. etc
There are many more transpilers which are very useful and are used oftenly.
Transpilers can be a powerful class of tools in the world of development. Many organizations and developers are not really aware that it is possible to write transpilers. Writing a good transpiler is just as much work as writing a good compiler, because they basically do the same thing. Most of those who do know that this is possible have no idea how to get started. In this blog, we tried to explain what are they, when should they have used, and how should they be designed.