4 ms·
I'm wondering if anyone has tried to use another language - I'm thinking Ruby - as a pre-processor for C++ code. I know people have pre-processed C++ code with
by daemin 9y ago
I'm wondering if anyone has tried to use another language - I'm thinking Ruby - as a pre-processor for C++ code. I know people have pre-processed C++ code with various tools - for example Qt - but I'm thinking of just replacing # pre-processor statements with something like Rails based templates.
- AlbertoGP 9y agoI used Python to generate C++ with a utility I wrote called "PythonPP": http://matracas.org/utiles/index.html.en http://matracas.org/utiles/index.html.en One of the things it did was to generate the function prototypes so that a typical .h file would look like this: #!from base_cc import * #""// Clase: ${project_part_name} #""// Proyecto: ${project_name} #!import time #""// Fecha: ${time.strftime("%A, %d de %B de %Y, %H:%M:%S %Z", time.localtime(time.time()))} #""${cc_copyright_notice("GPL", "2001, 2002, 2003, 2007, 2010")} #""#ifndef _H_${project_part_name} #""#define _H_${project_part_name} #""${cc_function_includes_for_class()} #""${cc_variable_global_declarations_for_class()} #""class ${project_part_name} { #""${cc_variable_declarations_for_class()} #""${cc_function_protos_for_class()} }; #""${cc_operator_protos_for_class()} #endif The .cc file was similar, calling Python functions to get the function definitions from separate files: I built one directory for each class, where I would put each big function or several related small functions in its own file, and they would be found and combined automatically. For each class, there was a vars.cc file which contained member variables, with their declaration line, initialization and destruction in consecutive lines: the declaration went to the .h file, the initialization to the constructor, and the destruction to the destructor. This way I avoided forgetting to release something and had a good overview of all variables and their life cycles. Here is an example of generating constant names from a Python list: #!for token_id in lexer_table: #" " case TOKEN_ID_${token_id[1]}: #' ' cerr << "\n --- [" << index_of_current_character #" " << "] TOKEN_ID_${token_id[1]} '" << buffer << "'" #' ' << endl << flush; #' ' break; The same idea was also used in the Bison/Yacc grammar file: #!%token <markup> T_QMATH_DIRECTIVE %token <markup> T_DIRECTIVE_DEFINITION %token <markup> T_DIRECTIVE_CONTEXT for d in qmath_directives: #" "%token <markup> T_DIRECTIVE_${d.upper()} I've used it also for other languages like Java where it cuts down the boilerplate. For instance, the package names are automatically generated from the file path.
- notalaser 9y agoI've seen it done (on a large-ish codebase), using Perl as a preprocessor for C. It looked good on paper and we were really enthusiastic when we came up with the idea, but it quickly degenerated into the proverbial ad-hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp. In retrospect, we could have done worse, like use C++ :-).
- Keyframe 9y agoM4 was/is quite popular.
- Pete_D 9y agoA while ago, in a fit of defmacro-envy, I wrote a preprocessor that would let you wrap Python snippets in #python/#endpython blocks: http://codepad.org/5B388Wsh http://codepad.org/5B388Wsh. This lets you write silly things like http://codepad.org/eS443WPA http://codepad.org/eS443WPA. I bet the same idea isn't too hard to implement in Ruby, but I don't know enough to try it myself. I never really used it much - generating C code with Python print statements turned out to look uglier than I thought it would, and most of my use cases were better handled with X macros (https://en.wikipedia.org/wiki/X_Macro https://en.wikipedia.org/wiki/X_Macro) or by generating a separate file and #include'ing it. But your description reminded me of it; you might like the idea.