Transpiler
Suppose you've written a program in one language but wish to convert this to another language, then you would invoke what's called a transpiler. The programming language at the input to the transpiler may be called the source language whereas the language at the output may be called the target language. A transpiler is sometimes called a source-to-source compiler.
For example, converting C++ code to C code will involve a transpiler. Converting Python code to Ruby code will involve a transpiler. Let's note that in these example both source and target languages are at the same level of abstraction. But let's say we convert Java code to bytecode, or C code to assembly code, then this is not called transpilation. Transpilers don't change the level of abstraction such as translating from a high-level language to assembly code, bytecode or machine code.
Discussion
-
How is a transpiler different from a compiler? A compiler converts high-level human-readable code to low-level instructions that can run on a computer. For example, a C compiler converts C code to machine executable code. A Java compiler converts Java code to what's called Java bytecode that's interpreted at runtime to execute on the target machine.
A transpiler on the other hand usually works at the abstraction of high-level languages. The output code is still human readable. It cannot be executed directly unless its own compiler or interpreter is invoked. For example, a transpiler can convert Java code to C++ code. Programmer will still need to invoke a C++ compiler before executing the resultant machine code.
However, some folks use these terms interchangeably. For example, VOC is stated to be transpiler from Python source code to Java bytecode when in actual fact these two are at different levels of abstraction. However, it's acceptable to call translations at the same level of abstraction as transpilation, such as from .ASM assembly code to .A86 assembly code.
ISO/IEC/IEEE 24765 defines five generations of languages that can be related to abstractions described above.
-
Why would I want to use a transpiler? Here are a few reasons for using a transpiler:
- 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.
- Tooling: Source language has better IDEs, testing and debugging tools, such as with .NET and Visual Studio.
-
How does a transpiler work internally? 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.
AST Explorer is an online tool to help you view the AST given a piece of code in many popular languages.
-
Could you name some transpilers out there? 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
andpasteurize
. These could be termed as transpilers.
Milestones
Facebook creates HipHop for PHP (HPHPc) for transpiling PHP to C++ for better runtime performance. This is deprecated in 2013 by HipHop Virtual Machine (HHVM) that uses just-in-time compilation. HPHPc did not fully support PHP, its performance flattened out and its deployment across all of Facebook's servers was not trivial.
References
- Ackerman, David. 2016. "WebAssembly: Finally Freed from JavaScript?" Simple Programmer, May 25. Accessed 2018-06-03.
- Bysiek, Mateusz, Aleksandr Drozd, and Satoshi Matsuoka. 2016. "Migrating Legacy Fortran to Python While Retaining Fortran-Level Performance through Transpilation and Type Hints." 6th Workshop on Python for High-Performance and Scientific Computing (PyHPC), via Semantic Scholar. Accessed 2018-06-03.
- ISO/IEC/IEEE. 2017. "Systems and software engineering — Vocabulary." ISO/IEC/IEEE 24765:2017, International Standard, Second edition, September. Accessed 2019-01-24.
- Intel. 1979. "MCS-86, Assembly Language Converter, Operating Instructions for ISIS-II Users." Accessed 2018-06-05.
- Keith-Magee, Russell. 2017. "How to write a Python transpiler." BeeWare, PyCon US 2017, Portland, Oregon, May 17-25.
- Kyle, James. 2015. "Not Born to Die." Babel Blog, February 15. Accessed 2018-06-03.
- Mancini, Alberto. 2015. "The GWT Toolkit: Build Powerful JavaScript Front Ends Using Java." Toptal. Accessed 2018-06-03.
- Pawlak, Renaud. 2016. "Comparing the GWT transpiler with JSweet." JSweet, February 10. Accessed 2018-06-03.
- Samuels, Charles. 2017. "A World of Javascript Transpilers." Site, May 31. Accessed 2018-06-03.
- Schulz, Marius. 2015. "The Era of Transpilers." Blog, May 04. Accessed 2018-06-03.
- Sengstacke, Peleke. 2016. "JavaScript Transpilers: What They Are & Why We Need Them." Scotch, April 25. Accessed 2018-06-03.
- Sharp, James. 2018. "c2rust vs Corrode." non-O(n) musings blog, June 30. Accessed 2018-07-02.
- Wikipedia. 2018a. "Source-to-source compiler." Wikipedia, May 31. Accessed 2018-06-03.
- Wikipedia. 2018b. "Cfront." Wikipedia, April 18. Accessed 2018-06-03.
- Wikipedia. 2018c. "HipHop for PHP." Wikipedia, March 25. Accessed 2018-06-03.
- Zacina, Faris. 2015. "How Babel compares against TypeScript." Ministry of Programming Blog, August 23. Accessed 2018-06-03.
- Zhu, Henry. 2016. "The State of Babel." Babel Blog, December 7. Accessed 2018-06-03.
- nikhilk GitHub. 2013. "scriptsharp." Accessed 2018-06-03.
- pybee GitHub. 2018. "VOC." Accessed 2018-06-03.
Further Reading
- Samuels, Charles. 2017. "A World of Javascript Transpilers." Site, May 31. Accessed 2018-06-03.
- Ewerlöf, Alex. 2017. "You might not need to transpile your JavaScript." freeCodeCamp, May 19. Accessed 2018-06-03.
- Schulz, Marius. 2015. "The Era of Transpilers." Blog, May 04. Accessed 2018-06-03.
- Keith-Magee, Russell. 2017. "How to write a Python transpiler." BeeWare, PyCon US 2017, Portland, Oregon, May 17-25.
Article Stats
Cite As
See Also
- JavaScript Transpilers
- Abstract Syntax Tree
- TypeScript
- Babel (Transpiler)
- Programming Language Generations
- Intermediate Language