Transpilers : Source-to-Source Compilers
The brute force solution here would be to learn the new language on the go while implementing your logic in it. 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 to get into the nitty gritties and immediately need a working stub of code for your project ?
The Titans Transpilers
A transpiler is a type of compiler that takes the source code of a program written in a programming language as its input and produces the equivalent source code in the same or a different programming language. A source-to-source compiler translates between programming languages that operate at approximately the same level of abstraction.
A transpiler parses the code and extracts the tokens, which are the basic building blocks of the language. Language keywords, variables, literals and operators are examples of tokens. This step is a combination of lexical analysis and syntax analysis. Transpiler knows how to do this because it understands the syntax rules of the input language. Given this understanding, the transpiler builds what is called Abstract Syntax Tree (AST).
The next step is to transform the AST to suit the target language. This is then used to generate code in the target language.
- Transpilers may keep translated code structure as close to the source code as possible. Closeness here refers to the ease with which blocks of code from translated code can be mapped back to the original code. This is essential during development phase where there is a constant need to test and debug the source code.
- Transpilers may change the structure of the original code so much that the translated code does not look like the source code. This use case applies to scenarios where thoroughly tested code just needs to be ported to a new language or the IDE integrated transpiler reports back where error lies in the source code. These transpilers have an opportunity to reduce the size of and perform complex optimisations on the code since there is no need for understandability and readability of the code.
A transpiler may be developed along the lines of one of the above possible use cases depending on the frequency of work where it finds itself in use.
- Migration: Migrate legacy code to a modern language.
- Performance: Initial code was written for quick prototyping or project is moving to a different platform where another language is more suitable. Perhaps the target language has a better compiler that can generate more optimized code. For example, critical parts of a Python codebase can be transpiled to Fortran and then called from Python.
ClojureScript transpiles from Clojure to JS. JSweet transpiles from Java to TypeScript or JS. GWT Web Toolkit, formerly known as Google Web Toolkit, enables Java programmers to use JS frontend for browser-based applications. GWT includes a transpiler from Java to JS. Script# transpiles from C# to JS.C2Rust and Corrode are two alternatives for transpiling C to Rust.
VOC is a transpiler from Python to Java, which means that even Android apps can be built from a Python codebase. Python itself has two incompatible versions: 2 and 3. However, tools are available to convert from one version to the other:
pasteurize. These could be termed as transpilers.
Transpilers, though seldom talked about, are a powerful class of tools in the world of development. I believe the coldness towards transpilers stems from the love to learn and work with new language among developers ( including me 😝 ) and to a certain extent lack of awareness about transpilers, which motivated me to help them get the well deserved attention.
I would love to read your comments on the article below and don’t forget to show your appreciation by applauding it. And remember, keep geeking it out 🤓.