The biggest loss is that a lot of error and debug info will be relative to the C rather than your language. This means the user will be exposed to the internals.
Probably the biggest win, is that it’s almost trivial to integrate C, and potentially C++ libraries into your language. This has been hugely beneficial to Nim, for example. They have written a full front end though so they don’t have the error issue above.
Like most things it’s a trade off.
> The biggest loss is that a lot of error and debug info will be relative to the C rather than your language. This means the user will be exposed to the internals.
In other words, sometimes the end user has to debug a bunch of code they didn't write. I wouldn't underestimate how much a pain this can be.
> Probably the biggest win, is that it’s almost trivial to integrate C, and potentially C++ libraries into your language.
Again, subject to adhering to threading models, etc. If you're writing a language as a hobby, this may not be a big concern, but at scale, someone will be using memcpy or memory barriers or thread-local memory somewhere. It is certainly possible to support those use cases, but I'd be careful about overstating how trivial that is.
You also can't create JIT compiled REPL using C as easily.
BTW this isn't a criticism of LLVM (far from it). It's expected to be used as a backend so any invalid IR is, pretty much by definition, a programmer error.
At the quick and dirty level, IMO C wins here as you can just splat the C you're building to a file and inspect it. With LLVM you need to get through the hurdle of building valid IR before you can serialise it.
In the longer term, I would argue that LLVM is better as you have way more control over what you're generating. Once your lowering works then the issue above is irrelevant.
LLVM does have a verifier pass which should catch every invalid IR. However, optimization passes aren't necessarily robust against weird types (I don't expect <13 x i3> to go over very well), and the backends are liable to flip out if the IR isn't in the subset they expect (e.g., weird types).
Instead of serializing it, directly write as text and process the file with the LLVM IR assembler.
Granted, it is still harder as just using C, but a step closer to LLVM and easier to debug.
Essentially you generate a C-ast and then pretty print it. It has some limitations though.
I'm kind of surprised that there's not a library for emitting C code, though maybe my Google-fu is failing.
But for anything more complex you are better off implementing complete parser with your own error handling and reporting.
Edit: one of the clearest signs of language implemented in this way is use of '@' character as part of syntax extensions as it is the only printable ascii character that has no syntactic meaning in C (the other such "unused character" is '$', but its meaning is implementation-defined, for example gcc has option that causes it to be accepted as part of identifiers, which is quite obviously the default on VMS)
If you only play around, using C is fine.
1 - https://nim-lang.org/blog/2017/10/02/documenting-profiling-a...
I can give you an example from C compilers. Consider a simple loop:
int a[] = ...;
for (int i=0; i<max; i++) {
print(a[i]);
}
Something fishy is printed, so I run it in my debugger, break at some point, and print "i". Gdb can usually do that. Now think about the optimizations a compiler probably did: While the index is incremented by 1, we actually step 4 bytes further due to the int array, so the compiler does i+=4 instead to avoid the multiply by 4 instruction. While i starts at 0, we can instead use the "a" pointer and avoid to add an offset at every loop iteration. The code might actually look like this: int *a = ...
int end = a + max*4;
for (char* x=a; x < end; x+=4) {
print(x);
}
How can gdb print "i"? The trick is DWARF [0], basically the compiler inserts the information: If you want to print i here, then compute it by i=(x-a)/4.tldr: #line directives are not enough
Still, my simple opinion is: If you are serious, use LLVM. It is not that much harder than outputting C and has clear advantages like debug information.
I believe it's based on #pragmas that tell the C compiler (and/or GDB, presumably) which line of which file to blame.
Serious work has been done in Vala. It's not fair to dismiss the language as a quaint toy.
The solution is to disable optimisation for debug builds, not to avoid C.
From the gnome.org page:
> The -g command line option tells the Vala compiler to include Vala source code line information in the compiled binary
I've only dabbled in Vala, but apparently its "-g" flag lets you debug with GDB. https://wiki.gnome.org/Projects/Vala/Tutorial#Debugging
In the long run you always want to do it specifically for your language, because otherwise giving usable error messages and debugging information becomes impossible.
If my language is close to c (functions, scope, variable, types), can I take advantage of it so it's less work in my compiler to let the c compiler catch errors, or must I rewrite a full parser?
All I want to do is add pythonic indenting, range loops, maps, and geometric types with their operators (a little similar to shader languages).