6 ms·
Natalie: An early-stage Ruby implementation that compiles to C++
- turbinerneiter 5y agoI always wonder why you would target another language with your compiler, instead of an IR. What is the benefit of transpiling to C++ over using LLVM? (not meant as a criticism, genuinely curious)
- afranchuk 5y ago"It depends", as always :) Some people may do it because it's more approachable to them (and/or others). Others may have language models/runtimes that align closely with the target language. Or they may want to "stand on the shoulders of giants", benefiting from the higher-level optimizations of the target language. Also, for example, LLVM still doesn't target as many architectures as gcc (though I'm not saying that's necessarily very relevant for most users). That's just what comes to mind. I can't say for certain anything about this particular language though!
- pjmlp 5y agoIt is easier to deal with, going one step further requires dealing with more low level coding and can be demotivating for some. However I would advise to target an IR instead. Doesn't need to be LLVM, if the purpose is only learning about compilers, do as follows: 1 - Create an IR, preferably stack based as they are quite easy to target 2 - Basic IR interpreter for testing the workflow 3 - With a macro assembler, convert the IR into machine code in a dumb way Now you have a workable compiler, even if it won't win any prizes. If still interesting, then proceed to improve the code generation in a proper way.
- sitkack 5y agoRibbit is an R4RS implementation that is extremely compact. The video is only 15 minutes and worth a watch. https://github.com/udem-dlteam/ribbit https://github.com/udem-dlteam/ribbit https://www.iro.umontreal.ca/~feeley/papers/YvonFeeleyVMIL21.pdf https://www.iro.umontreal.ca/~feeley/papers/YvonFeeleyVMIL21... https://www.youtube.com/watch?v=A3r0cYRwrSs https://www.youtube.com/watch?v=A3r0cYRwrSs
- pjmlp 5y agoThanks for sharing.
- diordiderot 5y agoWhat's an IR
- pjmlp 5y agoIntermediate Representation. Kind of high level Assembly like language used across compiler stages to make them more modular and easier to manipulate. Some known examples are LLVM bitcode, GCC GIMPLE, Rust MIR, Swift SIR, Tensorflow MIR,.... Even bytecode formats like MSIL, JVM, WASM, P-Code,.... can be considered some form of IR.
- diordiderot 5y agoOooh okay. Thank you
- sesuximo 5y agoOne reason is that c++ is a stable “api” whereas llvm ir is a somewhat moving target
- rurban 5y agoMost importantly you get immediate startup. No need to parse and compile. Then you'll also need less memory. And third and less important you can add more expensive offline optimizations, which are too heavy at run-time. Like escape analysis, and inlining. Esp for the static parts. The dynamic parts just call into the shared ruby runtime.
- chrisseaton 5y ago> Most importantly you get immediate startup. No need to parse and compile. Compiling to an IR doesn't mean 'and then interpret or JIT it'. Can can use an IR to compile to native. GCC, Clang, Rust etc all an IR, for example.
- boondaburrah 5y agoFor me, it's that it's almost guaranteed a C++ compiler is available for the platform I'm targeting. This could be for various reasons, but it's usually either: 1. The chip isn't supported by LLVM (which most new languages use) 2. The platform owner /requires/ the use of their C++ only SDK and will not approve any other compilers for use on their app store, so C++ becomes the "machine language" of that platform.
- tambre 5y agoautconf in 2021 instead of something more humane like CMake, Meson, Bazel, build2. Why?
- whyareyoul 5y agoWhys it matter? Its solid, exists everywhere and works and contributors dont have to learn $buildsystem of the month
- tambre 5y agoautoconf is nix-only in practice, macro-based, verbose and has a significant legacy burden. I have suffered plenty of pain from trying to build the few projects that still use it. In such cases it's often been faster to write own build scripts for some other newer buildsystem. CMake and Meson I've used across tens of different projects, there's solid online documentation, significant usage, a good underlying language and I already have my toolchain files ready. Using autoconf instead of something more common and modern is likely to turn off people contributing or using your project as they'll be unfamiliar with it and are going to have more trouble solving issues on their own.
- netule 5y agoI'm genuinely curious: why would I use this over something like Crystal, which is pretty much Ruby, but native? https://crystal-lang.org/ https://crystal-lang.org/
- vidarh 5y agoCrystal is superficially similar but it pretty much ends there, and many off the differences feels pretty arbitrary.
- pjmlp 5y agoIdeally, access to the existing Ruby ecosystem. Crystal makes sense in the context of writing stuff from scratch or wanting to contribute to the ecosystem.
- 999900000999 5y agoI sincerely doubt a tool like this will give you access to the full Ruby ecosystem. I definitely love the idea though, I struggle with lower level languages, although I consider myself fairly decent with both Python and JavaScript.
- pjmlp 5y agoThis one in particular probably not, since it is in early stages. RubyMotion on the contrary is quite advanced and an AOT compiler to go to, when one wants to use Ruby for mobile development. http://www.rubymotion.com/ http://www.rubymotion.com/
- _3u10 5y agoRubyMotion is probably as good as it gets for AOT. Ruby depends on dynamic dispatch and thankfully objc is very similar the needs of a Ruby-like language. Truffle might work even better as it’s able to recompile. Any C++ port will likely need to reimplement half of the objc runtime to support all of Ruby. Not sure if clangs/gcc objc support includes the runtime, but I’m imagining it would… so maybe it’s reusable that way.
- adamrt 5y agoAndreas Kling (of SerenityOS) recently did a Natalie contribution video that I found interesting. https://www.youtube.com/watch?v=b4PZgvPYkP4 https://www.youtube.com/watch?v=b4PZgvPYkP4
- akling 5y agoAnd if you'd like to see Tim (original author of Natalie) working on Natalie, he has a YouTube channel with lots of great content here: https://www.youtube.com/channel/UCgWip0vxtqu34rZrFeCpUow https://www.youtube.com/channel/UCgWip0vxtqu34rZrFeCpUow :^)
- ModernMech 5y agoI’m a little confused about this project. I’m trying to build the most complete list of programming languages out there (currently working on it via my favorites, it you want to have a look), and I’m trying to figure out if this qualifies. Usually it’s pretty easy to get on my list: if you call your project a language I add it to the list. But this one gives me a little pause, because it seems like this language is not distinct from Ruby at all. Rather this is a straight up Ruby -> C++. Is it fair to call this a language rather than a compiler? To me, a language is more than syntax and semantics, but includes an library ecosystem, tooling, and community. Does Natalie aim to grow a community, or will it exist fully within the Rudy ecosystem?
- alekq 5y agoIt is Ruby implementation if I got it right, not a Ruby-like language. Not the first and probably not the last one (jRuby, Rubinius, MacRuby etc.).
- pmontra 5y agoIn the JavaScript world this would be called a transpiler. Instead of compiling a newer version of JS into an older one this compiles Ruby in a totally different language, C++. However the Ruby interpreter (MRI) is written in C so C++ is less far away from it than, let's say, Java. I didn't check the code but I wonder if they inlined some code from MRI.
- chrisseaton 5y ago> because it seems like this language is not distinct from Ruby at all I don't think it claims to be - it's a compiler for Ruby.
- ModernMech 5y agoThe domain is Natalie-lang.org and the header on that page is “Natalie Programming Language”, so that’s where I’m a little confused. Maybe the goal isn’t to be a Ruby compiler but to transition into something more, so I was wondering if anyone had any idea.
- racl101 5y agoUgh, these names.
- sitkack 5y agoThis is cool, it looks conceptually similar to Shedskin. A Python to C++ compiler. https://shedskin.github.io/ https://shedskin.github.io/
- kdasme 5y agoI was under impression this can already be achieved via TruffleRuby compilation into a native image? Not that I used it, but thought this is doable, and the process is well tested.
- WalterBright 5y agoCompiling to another language is always going to be very slow, and very frustrating, because one's expressiveness is limited to the expressiveness of the target language. (Yes, I know, because C++ is Turing Complete, anything is expressible, but it's a matter of efficiency.) For example, what if the language needed exceptions to work in a different way than the target does? And these days, one can easily use existing backends like the Digital Mars one (Boost Licensed!), the Gnu one or the LLVM one. Why punish yourself by emitting code in another language? Natalie already requires gcc or clang anyway. I could never have gotten D to work if it emitted C code as output.
- sitkack 5y agoC++, a more complex language in all dimensions managed to produce C code via the first C++ compiler, Cfront. Dlang has its own native code generator? You have amazing chops, I am sure you could have emitted C code. What are your thoughts on producing Wasm instead of C? Maybe Scheme is the perfect compiler target. https://en.wikipedia.org/wiki/Cfront https://en.wikipedia.org/wiki/Cfront
- mhh__ 5y agoThe dmd code generator was/is a C++ compiler backend too, and can trace it's lineage back to the first compilers to make Cfront obsolete. We could emit C code, but it's just worse when you need to get to the cutting edge. Even if you end up going through the same backend via C, you've now (say) lost devirtualization.
- WalterBright 5y agocfront in the 80s was a far simpler C++ compiler than today. And if you ever looked at the code it generated, you'd be horrified. One major problem it had in generating C was the lack of COMDAT sections in the generated object file. This was needed for generating code for the same member function in multiple source files. C compilers would just stick the code into the text segment, resulting in multiple declaration errors from the linker. I am not saying Bjarne was an inept programmer. He's actually brilliant, it was just a very very hard problem he set for himself, and it's amazing cfront worked as well as it did. The cfront on DOS also was very slow (imagine writing the generated source code out to floppies!) and crippled because it didn't have near and far pointers. Zortech C++ was the first native C++ compiler, which bypassed all these problems. ZTC++ simply vaulted over cfront by making C++ simple and fast on DOS. DOS at the time was where 90% of the programming action was, so this was no small thing. My opinion, and that of a few others at the time, was that Zortech saved C++ from oblivion by having a usable implementation on DOS. You can see that by the traffic level in comp.lang.c++ at the time - it took off immediately after ZTC++ was released, and never looked back.
- sanxiyn 5y agoThis heavily reminds me of Nuitka, a similar project for Python.