3 ms·
C translates quite readily to D. I've been able to translate thousands of lines at a time in less than an hour, usually with some global search & replace and th
by WalterBright 4y ago
C translates quite readily to D. I've been able to translate thousands of lines at a time in less than an hour, usually with some global search & replace and then making adjustments after running it through the D compiler. We relied on being able to do this in the D community for quite a while. There also have been three three translators built, with more or less effectiveness. It is nice to get the code into D, and then take advantage of D's safety features.
The fundamental problem with translation, followed by some hand tweaking, is that it only works if the C version is to be abandoned. If the C code is maintained by anyone else, as soon as they make changes, the translation gets out of date. Updating the translation turns out to be impractical because of the hand tweaking necessary.
Then there are some frustrating structural limits. The largest is that C doesn't have modules. The preprocessor puts everything into one file, and every C compilation is for one file. Declarations get duplicated across every translation unit. Somehow, these need to get teased apart into modules. This structural redo gets done by hand, and requires pretty good familiarity with the C code's design.
The preprocessor poses another major problem. The preprocessor language and the core C compiler have no knowledge of each other. They are completely separate languages, with their own syntax, keywords, semantics, etc. The preprocessor, aside from trivial use of it, simply does not translate into other languages. I also have yet to find a C programmer who could resist using the preprocessor as a metaprogramming language, which does a great job at obstructing all efforts at converting to another language.
All this stuff raises a lot of friction for D interacting with C code. Programmers don't like friction, they don't want to deal with C code they are unfamiliar with, they don't want to fold in maintenance changes in C code to the translation, etc. They want it to "just work".
The eventual solution I came up with is obvious, but I'd always dismissed it as impractical. Just fix the D compiler to be able to compile C code directly, and internally make the C declarations and constructs available to D code. This turned out to be fairly easy to do, and is ridiculously effective. It sometimes works even better than C++'s ability to #include C code (C++ doesn't support things like _Generic, old style C declarations, etc.). All you have to do is import .c code just like importing any D module, and the D compiler takes care of all the dirty work for you.
It isn't perfect, for example, C compilers have lots of extensions, and dealing with all of them is hopeless. But we just do the common ones, as it turns out most of them are rarely used.
- bachmeier 4y ago> The preprocessor poses another major problem. The preprocessor language and the core C compiler have no knowledge of each other. They are completely separate languages, with their own syntax, keywords, semantics, etc. "I wrote my program in C." No, you wrote your program in a custom language that only you (at most) understand, and you gave the file a .c extension.
- deleted 4y ago[deleted]
- jeffparsons 4y agoZig also takes this approach, and even exposes its C compiler (which if I recall correctly is basically Clang plus diverse sysroots and other customisation out of the box) as a separate `zig cc`. I do a lot of work in Rust, and cross-compilation can be a pain when you have a lot of C dependencies. Fortunately https://github.com/messense/cargo-zigbuild https://github.com/messense/cargo-zigbuild exists. It sounds crazy, but using Zig's inbuilt C compiler to help build my Rust projects has been the smoothest option I've found. I can't help but wonder if it would be worth it for Rust to follow D and Zig by shipping its own inbuilt C compiler, even if they still want to also support external C toolchains. It should be roughly the same effort as it was for Zig, given that they both use LLVM.
- WalterBright 4y agoD can compile and link C programs with: dmd hello.c C and D code can be mixed with: dmd mars.d pluto.c C code can be imported by D code: import stdio; // looks for stdio.d, stdio.h, stdio.c in that order void main() { printf("using C printf from D!"); } It keys off of the file extension. Amusingly, C code can also import D code: ----- D file ---- int square(int x) { return x * x; } ---- C file ---- __import square; int test() { return square(3); } closing the circle, enabling D libraries to be written and accessed by C.
- WalterBright 4y ago> Zig also takes this approach D took the much more fun way, which is to implement a new C parser from scratch and tweak the D lexer and semantics to handle the differences of C. It's not too bad, about 5000 lines of D: https://github.com/dlang/dmd/blob/master/compiler/src/dmd/cparse.d https://github.com/dlang/dmd/blob/master/compiler/src/dmd/cp...