Transpilers : Source-to-Source Compilers

How often does a need arise when, despite being an expert in a programming language, you are forced to write code in another one ? Maybe you’re fluent in C++ while your group is working on a project in JavaScript or you need to deploy a web service quick so you pick Flask and hence need python equivalent of your code.

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 ?

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.

  1. 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.
  2. 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.
  • Compatibility: Generate code conforming to an older version while developers benefit from features of a newer version, such as with browsers not updated to latest JavaScript standards.
  • Coding Skills: Codebase can be transpiled to a language in which the skills are readily available. Or it might be a matter of preference. For example, those who are from OOP background prefer TypeScript. Python coders prefer CoffeeScript instead. In both cases, code is transpiled to JavaScript.
  • 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.

JavaScript is a language that’s been evolving at a regular pace but there are also lots of browsers out on the web using older versions of the language. Transpilers are therefore used commonly in the JS world to transpile code to ES5, a version that’s supported by most browsers. There are lots of transpilers for JS but the popular ones are Babel, TypeScript and CoffeeScript.

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: 2to3, modernize, futurize and 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 🤓.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aarnav Jindal

Avid programmer chasing developments in the dynamic and invigorating world of technology 🤓