Custom Code Generator.

1,575 views
Skip to first unread message

Raghavendr G

unread,
Jan 10, 2019, 6:00:26 AM1/10/19
to Blockly
Does anyone know how to create a custom code generator for c++. The developers page is not helping me at all.

Amber B

unread,
Jan 10, 2019, 9:19:20 AM1/10/19
to Blockly
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:

  1. 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.
  2. 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.
  3. 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.)
  4. At the top of this file, be sure to use goog.require('Generator') and goog.provide('Blockly.CPlusPlus') (or whatever naming convention you use.)
  5. 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.
  6. 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.
  7. 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.

kushal bhattacharya

unread,
Jan 18, 2019, 6:01:42 AM1/18/19
to Blockly
Have a look to this post.Perhaps you will get an idea about how to generate blocks and code in c++.
This guy has done it with C language.

sa...@smartivity.in

unread,
Feb 21, 2019, 1:36:25 AM2/21/19
to Blockly

Hi Amber,
Thanks for your reply.
 
I followed the steps as you mentioned but I am getting error , look at the attachment. This is also happening with C++ and Python.

Screenshot 2019-02-21 at 12.03.38 PM.png

Screenshot 2019-02-21 at 12.03.33 PM.png

Amber B

unread,
Feb 21, 2019, 8:49:59 AM2/21/19
to Blockly
Hrm. I'm afraid I'm not too familiar with C++... is that valid generated code in C++? How did you generate this code? Is this one of the Blockly samples, or?

Coda Highland

unread,
Feb 21, 2019, 12:24:33 PM2/21/19
to blo...@googlegroups.com
The second screenshot is perfectly legitimate Python code. It's completely meaningless C++ code because it's a C-style language that wants parentheses and curly braces.

/s/ Adam

On Thu, Feb 21, 2019 at 7:50 AM Amber B <abla...@citizendeveloper.com> wrote:
Hrm. I'm afraid I'm not too familiar with C++... is that valid generated code in C++? How did you generate this code? Is this one of the Blockly samples, or?

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Amber B

unread,
Feb 21, 2019, 1:09:00 PM2/21/19
to Blockly
Oh, welp. In that case it seems like either it's using the Python generator, or you copied the Python generator and didn't update the C++ generation code appropriately.
Reply all
Reply to author
Forward
0 new messages