Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling vs Transpiling

The definition you have quoted above is too general for a beginner to understand completely and so let me just simplify it to something we see practically.

Compiler: is an umbrella term to describe a program that takes source code written in one language and produce a (or many) output file in some other language. In practice we mostly use this term to describe a compiler such as gcc which takes in C code as input and produces a binary executable (machine code) as output.

Transpilers are also known as source-to-source compilers. So in essence they are a subset of compilers which take in a source code file and convert it to another source code file in some other language or a different version of the same language. The ouput is generally understandable by a human. This output still has to go through a compiler or interpreter to be able to run on the machine.

Some examples of transpilers:

  1. Emscripten: Transpiles C/C++ to JavaScript
  2. Babel: Transpiles ES6+ code to ES5 (ES6 and ES5 are different versions or generations of the JavaScript language)

Now, what do they mean by "similar level of abstraction": As I said it compiles/transpiles to a source file, one can argue that assembly language is also a source file and thus gcc is also a transpiler. So, this argument is what this similar level of abstraction voids.

The notion of categorizing languages into lower, middle and higher level is based on the level of abstraction they provide from the actual working of the machine/architecture.

Lower level languages like assembly are very close to the processor architecture i.e. have different instructions for different processors. While C/C++/Java/JavaScript, abstract all this away providing more abstraction.

So, a transpiler compiles to a language that is closer to the language you started with in the terms of this abstraction (or is closer to the level of that language in the lower-middle-higher level language ladder).


Here's a sort of descriptive way to answer

If you think of layers of abstraction as this example:

(1) CPU-level (actual logic gates on the CPU)
(2)machine code
(3)assembly code
(4)[C/C++, JVM/bytecode]
(5)[JavaScript, Python]

A compiler goes to a lower level (lower number). A transpiler switches from one language (or version of a language) to another at the same number.


Ex: TypeScript ( a Microsoft superset of JavaScript with type safe checking) transpiles to JavaScript code which can run on different types of browsers.

https://en.wikipedia.org/wiki/Microsoft_TypeScript "Microsoft TypeScript is an open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript, and adds optional static typing to the language.

TypeScript is designed for development of large applications and transcompiles to JavaScript.[5] As TypeScript is a superset of JavaScript, existing JavaScript programs are also valid TypeScript programs. TypeScript may be used to develop JavaScript applications for both client-side and server-side (Node.js) execution."


I mostly agree with tapananand answer, but...


definition

Words are "made", so they serve a purpose. And this also changes with time.

We now tend to use transpiler to specify a compiler that translates code into some other code "more similar" to the source one, that what a compiler might do. And is used to differentiate both mostly when both of them are mentioned in the same context (again mostly implying that a transpile language will have to get compiled at least once more)


examples

So everything is very subjective. At the time of this writing:

  • Coming from the Java world I could call CoffeeScript/TypeScript transpilers to depict that the resulting code is no more efficient than the original one.
  • CoffeScript documentation says it's a compiler, and babel is a transpiler. The want do say that CoffeeScript, although very similar, is not Javascript. At least not a version of it, as that is what babel produces.
  • Babel calls itself a compiler.

fazit

So transpile is at this time very rarely used anymore, and only to tell two compilers apart.

It will probably dissapear as concept, since compilation is much more complicated than that (same/higher/lower language, version, etc), and the word doesn't seem to be useful anymore ("transpilers" are now ubiquitous)