Oof, this isn't a small undertaking here. From my experience working with, investigating and hacking the JavaScript generator, I think you'd need to do the following:
- Under the generators folder, create a cplusplus.js (or whatever naming convention you want to use; I suggest using something that's going to be a valid variable name in JavaScript just for niceness) file.
- Referencing the generator.js file and another generator (I suggest javascript.js), look at any of the functions in generator.js which you will need to override for C++-specific use cases. (For example, the JavaScript generator's scrubNakedValue function always adds a semicolon and line break at the end of them.) Make sure you understand what each function does, and ask here if you're not sure.
- Also referencing javascript.js and generator.js, make sure to add the appropriate reserved words for C++ and appropriate order constants according to the operator precedence of C++. (Check out line 73 in javascript.js to see what I mean.)
- At the top of this file, be sure to use goog.require('Generator') and goog.provide('Blockly.CPlusPlus') (or whatever naming convention you use.)
- Make a new cplusplus (or again, the naming convention you've chosen) folder under generators and make files corresponding to the files for the Blockly-provided block groups. (For reference, reference the folders in generators/javascript). You'll want each of these files to include the C++ generators for each block contained in your reference file. So, for example, generators/javascript/text.js provides Blockly.JavaScript['text'], etc. You'll want to do Blockly.CPlusPlus['text'] = function(block) {} and then do your stuff.
- Also in each of these files, be sure to use the goog.provide and goog.require appropriately. So, for example, in your generators/cplusplus/text.js file, you'd want to put goog.provide('Blockly.CPlusPlus.texts') and goog.require('Blockly.JavaScript'), etc.
- Once you have all that, you'll need to update build.py to also generate a cplusplus_compressed.js file. I'll be honest, I tend not to do much with this file, but I did have to update it once, so I have a few guesses about what you'd need to do. I believe you'd need to add self.gen_generator("cplusplus") after self.gen_generator("dart"). I'd also update the comments just to be safe by adding # cplusplus_compressed.js: The compressed C++ generator after # dart_compressed.js: The compressed Dart generator.
And I
believe that all of that is sufficient to get your generator working. Then you'd just have to be sure to include the
cplusplus_compressed.js file and use the generator appropriately when you generate your code.