3 ms·
What is this? It claims to be a (GNU99) C library, but I don't see how this can be the case, considering all the non-C constructs in the sample code ($(), try/c
by ambrop7 13y ago
What is this? It claims to be a (GNU99) C library, but I don't see how this can be the case, considering all the non-C constructs in the sample code ($(), try/catch, foreach). So it this just a language of its own that is compiled into C?
- tehwalrus 13y agocheck out the Prelude.h file on their github - it's essentially all implemented as preprocessor nameswaps and macros. It's quite clever actually!
- koenigdavidmj 13y ago$ is a valid function name according to gcc. try/catch/foreach are #define'd as part of it. As an example, foreach is defined to a for loop over an iterator, which is any Cello class with a couple of functions defined.
- maaku 13y agoI'm guessing that the GNU99 qualification is important. GCC supports most if not all of the claimed features, or at least the low-level constructs required to create them.
- mcpherrinm 13y ago#define foreach(x, xs) \ for(var x = iter_start(xs); x = iter_next(xs); x != iter_end(xs)) It's all C: http://libcello.org/documentation/hacking http://libcello.org/documentation/hacking
- kragen 13y agoImagine what happens when someone says foreach(x, get_next(queue)) f(x); To solve this problem, with GCC, you have the option of defining foreach as { typeof (xs) _xs = xs; \ for (var x = iter_start(_xs); x = iter_next(_xs); x != iter_end(_xs)) but then you need a corresponding endforeach to apply the matching }. If you just leave off the opening {, then you end up with a mysterious compile error on if (ready(queue)) foreach(x, get_next(queue)) f(x); since the declaration of _xs is not a statement. And this kind of thing is why a "macro system" means something very different in C and in Lisp, and why Brad Cox wrote a compiler in 1983 instead of a macro library.
- dfbrown 13y agoYou could do #define foreach(x, xs) \ for (var _xs = xs, *x = iter_start(_xs); x = iter_next(_xs); x != iter_end(_xs)) Because var is just void* and anything you can iterate over will also be a void*.
- kragen 13y agoOh, that's excellent! I don't know why the C9x for-declaration didn't occur to me as a possible solution, particularly since the macro was already using it. You should submit this as a fix!
- Demiurge 13y agoYou can do pretty much anything if you rely on preprocessor. The $ is a define: http://libcello.org/documentation/hacking http://libcello.org/documentation/hacking