Hacker News new | ask | show | jobs
by diimdeep 1281 days ago
In my understanding, Nim at the moment is really a transpiled language, instead of compiled. Transpiled to C, then tooling uses clang or gcc to do compilation from C to target platforms.

It is like TypeScript to JS in C/C++ world.

Very clever to stand this way on the shoulders of giants, but the amount of moving parts is staggering and horrifying.

5 comments

The distinction between compilation and transpiling is a bit imprecise, but yes. But oddly the basic C that Nim compiles to is fairly resilient and stable. I'd actually expect the Nim compiler to bitrot way less than languages based on LLVM, which have a very short half life.

So Nim's approach has some challenges, but surprisingly has less moving parts than you'd think. I'd be confident I could get the current Nim compiler up and running in 5, or 10 years with minimal effort.

However, there was a lot of effort to get things like pointers and strings in the stdlib to play nicely across the NimVM, JS, and C backends. Theres some gross details there. But in the end its beautiful.

Besides @elcritch's and @pjmlp's excellent points {language "level" is vague & all compilers translate}, C as a backend target has other virtues (though is by no means The Only Way). E.g., TinyC/tcc optimizes for time to compile (it is a single pass over its input) not generated object code qualities like run-time/file-size. This can give Nim code almost interpreter-like edit-compile-debug devel cycles, but even just changing the default gcc -Og to gcc -O0 can be a huge improvement. [1] (`passL:"-lm" --mm:markAndSweep` and recently `--threads:off` became necessary for tcc due to atomics/C11 dependencies, but it is already a kind of "debugging compilation mode".) Similarly, work on space-saving/alternate libc's like musl [2] can be leveraged, [3] although using `config.nims`/NimScript can also lengthen compile-times by 100 milliseconds or so.

[1] https://forum.nim-lang.org/t/8677 [2] https://musl.libc.org/ [3] https://github.com/kaushalmodi/hello_musl/blob/master/config...

It is a compiled language, how the backend is implemented it is an implementation detail, transpiled was a term the JS community came up with, which you won't find in languages like Eiffel that always compiled to native code via C for the last 30 years.

Or C++ and Objective-C in their yearly days, or the P2C Pascal compiler from 40 years ago.

Well, then I have to agree

    the program is nothing more than a sequence of plain characters, stored in a text file. This abstraction is a complete mystery for the computer, which understands only instructions written in machine language. Thus, if we want to execute this program, the first thing we must do is parse the string of characters of which the high-level code is made, uncover its semantics—figure out what the program seeks to do—and then generate low-level code that reexpresses this semantics using the machine language of the target computer. The result of this elaborate translation process, known as compilation, will be an executable sequence of machine language instructions.
    Of course, machine language is also an abstraction—an agreed upon set of binary codes. To make this abstraction concrete, it must be realized by some hardware architecture. And this architecture, in turn, is implemented by a certain set of chips—registers, memory units, adders, and so on. Now, every one of these hardware devices is constructed from lower-level, elementary logic gates. And these gates, in turn, can be built from primitive gates like Nand and Nor. These primitive gates are very low in the hierarchy, but they, too, are made of several switching devices, typically implemented by transistors. And each transistor is made of—Well, we won’t go further than that, because that’s where computer science ends and physics starts.
Excerpt from https://www.nand2tetris.org/book
C seen by some for a long time as a "high-level" assembler :D, and if you choose a safe subset.. and given also e.g. how the LLVM pipeline and other compiling works, and how old and widespread C is, I think it is a clever portability and reuse a lot approach, and the fear of too many parts is exaggerated and not much different from other transpiling&compiling (that difference is also very blurry one and depends on viewpoint, don't think there is one precise definition for that?) pipelines... also not the first to do so.
What isn’t these days?

Even the C code (on many platforms) is going to transport to LLVM IR

> In my understanding, Nim at the moment is really a transpiled language, instead of compiled. Transpiled to C, then tooling uses clang or gcc to do compilation from C to target platforms.

If I understood correctly, like the Vala language: https://vala.dev/ (Note: Vala is strongly integrated with GObject).