4 ms·
Show HN: Loop macros, label break, slices in C with the Cedro preprocessor
- AlbertoGP 4y agoHi, when I presented the first release here 8 months ago (https://news.ycombinator.com/item?id=28166125 https://news.ycombinator.com/item?id=28166125) it got interesting comments and today I’ve made a new release with additional features: - Break out of nested loops: break label; (https://sentido-labs.com/en/library/cedro/202106171400/#label-break https://sentido-labs.com/en/library/cedro/202106171400/#labe...) - Notation for array slices: array[start..end] (https://sentido-labs.com/en/library/cedro/202106171400/#slice-notation https://sentido-labs.com/en/library/cedro/202106171400/#slic...) - Loop macros: #foreach { ... #foreach } (https://sentido-labs.com/en/library/cedro/202106171400/#loop-macros https://sentido-labs.com/en/library/cedro/202106171400/#loop...) The possibility of loop macros was discussed in this thread: https://news.ycombinator.com/item?id=28166698 https://news.ycombinator.com/item?id=28166698 At the time I hadn’t figured out how to make it useful without type information, but now it works. The same for the array slice notation: it took me a while to figure out how to make it do something useful without reflection, with a purely syntantical transformation. Loop macros are useful for the kind of things for which you would use x-macros (https://en.wikipedia.org/wiki/X_Macro https://en.wikipedia.org/wiki/X_Macro), with the advantage that it does not pollute the global namespace with macro names and the body of the macro stays more similar to the final result, which makes big macros easier to read. Since it runs before the standard C preprocessor it can do things like building up `_Generic` macros as shown in the loop macro vec example: https://sentido-labs.com/en/library/cedro/202106171400/#loop-macros-vec-example https://sentido-labs.com/en/library/cedro/202106171400/#loop... Source code (Apache 2.0 license): https://sentido-labs.com/en/library/ https://sentido-labs.com/en/library/ or https://github.com/Sentido-Labs/cedro https://github.com/Sentido-Labs/cedro