4 ms·
Taking a step back: what you're trying to accomplish is code generation -- a program that generates code. From that perspective, why would you choose to write
by evmar 11y ago
Taking a step back: what you're trying to accomplish is code generation -- a program that generates code. From that perspective, why would you choose to write that program using the C preprocessor, when there are so many other nice languages around?
- anon4 11y agoThis is what I was going to suggest. C is even pretty easy to parse - you can just take the ready ANTLR grammar and use it to generate a parser in whatever language you want. Of course, he might go even one step further and specify the structs in some other language, then generate C source files from them, which would also allow you to easily generate consumers for that data format in other languages and now you're reinventing Cap'n Proto.
- DannyBee 11y agoErrr, he doesn't even need to use ANTLR, he can use clang, as a library. It can be linked into an app, has a nice AST, and matching abilities: http://clang.llvm.org/docs/LibASTMatchersReference.html http://clang.llvm.org/docs/LibASTMatchersReference.html Now he has the actual compiler doing the work for him.
- comex 11y agoSure, and now everyone who wants to compile your program has to install clang, which might take forever to compile for someone on a source-based distro, or require PITA manual work such as on Windows. Not to mention that you have to figure out how to configure your build system to (a) depend on clang and (b) build a helper program (including overriding the normal target architecture choice) - the difficulty of which depends on your setup, but if you follow the common practice of maintaining separate Visual Studio and CMake/etc. project files, in order to cater to IDE users while still being cross-platform, you have to do it twice! I hate build systems.
- ncraun 11y agoIn actual production use, I think it could be better to write a separate program that will generate the code for you instead of using the C preprocessor (or use a language with this kind of introspection built in). However, using this hack lets you depend only on the facilities of the language, and that can be useful in simplifying the build process. Plus it was fun to create, too!
- uxcn 11y agoIt can be a bit tricky if it isn't integrated with the build process.
- Kristine1975 11y agoYou can use e.g. cog which puts the generated code into the file itself: http://nedbatchelder.com/code/cog/ http://nedbatchelder.com/code/cog/ Then all you need to do is to remember to run the preprocessor after you've made a change to the generator code.
- uxcn 11y agoPython is definitely a better syntax to write meta-programs than the pre-processor, but you still have the issue of the additional step/tool. Anything that depends on the generated code depends on the step which, if it's missed, can lead to non-good things.
- Kristine1975 11y agoSure. But you do test your changes, don't you? ;-) So if you forget to run cog after making a change to the meta-program, you should notice right away. But it still is a cludge. It would be better if C/C++ had a better preprocessor.
- uxcn 11y agoIt isn't a question of testing changes. The more people working on the codebase, the more people it impacts. Adding manual steps is tempting the fates.