4 ms·
> generate C code. how is that different from just writing it in another language? End users who need to compile will be able to regardless of the generated C
by chii 10y ago
> generate C code.
how is that different from just writing it in another language? End users who need to compile will be able to regardless of the generated C code, but the end users who need to do a _little_ modification will be given ugly generated C code! Seems stictly worse to me...
- fiedzia 10y agoThe difference is that you don't need compiler for this language. There are many hardware platforms that only come with C compiler.
- tyingq 10y agoFor something like curl, where the library is as popular as the command line tool, preserving the C ABI compatibility is probably the strongest reason.
- simias 10y agoRust could expose a C ABI while keeping safe internals. The interface itself would be unsafe of course. There are a few things that rust doesn't handle natively (like varargs functions IIRC) but other than that you could probably write a Rurl that would be completely backward compatible with Curl.
- tyingq 10y agoYes...the post above me was talking about Ocaml. Similar arguments for not redoing curl in Go.
- steveklabnik 10y agoTo be clear, Rust itself does not have varargs, but can handle them with the C ABI.
- Manishearth 10y agoWell, we can call into vararg functions, but not define them. Since vararg functions have the same ABI as the function with only one of the vararg one idea I've always had is to write a macro that lets you write a one-arg function and have it desugar via asm hax.
- wtetzner 10y agoYou do still need a compiler for the language. It's just that the target language is C, instead of assembly.
- rwmj 10y agoIn the libguestfs generator (first link above) the generated C code is required to be completely readable. It must look like it was written by hand (albeit by a programmer who is impossibly consistent and perfect). So reading the generated C code is fine. Modifying the generated code is of course not fine except for tiny test hacks, but we also include in the generated code comments reflecting where in the generator the code comes from.
- IncRnd 10y agoI've created a number of code generators in my projects. Invariably, developers say exactly what you just wrote, "how do I modify the generated code"? The answer is not to modify the generated code. Modify the input to the code generator to make changes. Even when I output a warning to this effect, that all modifications to target code will get overwritten, not to check the target code into version control, the source code is already checked into version control - invariably developers modify the target code right under the comment that says not to, then they check it into version control. They then wonder why there are bugs, and their modified target code no longer works after the target code gets regenerated after the next build.
- deong 10y agoIt's almost as though you can't solve the problem of programmers making errors by having a different set of programmers whom you tell to not make errors.
- IncRnd 10y agoThe issue had nothing to do with programmers. The impetus wasn't that programmers make errors but to solve the problem of repeatability. Many instances of issues can be solved once. There is no need to recreate the solution a number of times if it is already solved. A code generator allows one to focus on the actual meta-problem, which is often smaller and easier to solve.