19th October 2021 - Language design , Nim , Programming
This is a question that has come up time and time again in the IRC channel, when talking to people in person, and in the comment section pretty much every time Nim has an article on Hackernews or one of the bigger programming subreddits. It’s also a question that has been answered a lot of times, both with a short and efficient “no”, but also in longer form. This article will go into some detail about why the answer is “no” and can hopefully serve as a reference the next time this question gets asked.
What is a compiler anyways?
But I thought a transpiler just converted between languages?
This seems to be the misconception that leads to people asking the question which titles this article. While it is true that a transpiler converts from one language to another language the distinction lies in levels of abstraction. By compiling C to assembly you’re essentially using information in the C language to make decisions about what assembly code to generate, discard the information in the process. A transpiler on the other hand is a tool that converts between two languages while keeping the same level of abstraction. A theoretical perfect transpilation is therefore a two-way process where you could go back and forth between languages. This is typically not the case however because of structural changes and the assumptions required to transpile between two different languages. Examples of transpilers include JSweet which converts from Java to TypeScript, f2c which transpiles Fortran 77 to C, or J2ObjC which converts from Java to Objective-C, or even the tool c2nim which ships with Nim and compiles from C to Nim. Transpilers are typically used when you want to use code from one language which is on the same level of abstraction and use it in a language of the same or higher abstraction. And even this list is using the term a bit loosely, f2c for example was a part of the chain to compile Fortran to machine code, and is therefore translating Fortran features into C.
So what makes Nim not a transpiler?
So is there nothing which is a true transpiler?
transpiler, n.: Somebody else’s compiler
In summary the transpiler word has been quite overused lately. And while a more relaxed definition than the one I’ve used here can certainly be applied you will need to relax it almost to the point of absurdity in order to categorize Nim as a transpiler. Nim uses C in much the same way that Rust uses the LLVM for example, simply a target for compilation that will optimise well and run on a lot of different targets. After all, standing on the shoulders of giants is a great way to reach a long way without having to do a lot of climbing.