javascriptjsonblockly

Custom language generator to Blockly


I know that I can use

Blockly.JavaScript['my_code'] = function() {  ... }

But how can I add another language support like JSON? I tried ..

Blockly.Json['my_code'] = function() {  ... }

It fails when I try to recover

Blockly.Json.workspaceToCode(this.workspace)

workspaceToCode is not a function.

I need to add a new language to Blockly

I will not display this new language (JSON), it will just be used to send instructions to the robots.


I try to

Blockly.Json = new Blockly.Generator('Json');
Blockly.Json['my_code'] = function() {  ... }

But an error occurs in

Blockly.Json.workspaceToCode(this.workspace)

Error ..

Uncaught TypeError: this.init is not a function
    at js.ou3v.module.exports.Blockly.Generator.workspaceToCode

Solution

  • Creating a new language generator for Blockly is a fairly large undertaking, and with that in mind the documentation doesn't seem to offer much in terms of help with creating one.

    From my experience the best way to build a generator is to take a look at the existing ones in generators/, I have used the JavaScript generator as a template for a project of mine where I wanted to generate C++ code.

    It's also worth noting that the Block Factory generates JSON for use in other Blockly applications, however when I took a look at the code for that there isn't an actual generator defined for it.

    I think the error you're getting is caused because Blockly.Json doesn't have an init function. This is something defined in the existing generator files, for example, from the JavaScript generator:

    /**
    * Initialise the database of variable names.
    * @param {!Blockly.Workspace} workspace Workspace to generate code from.
    */
    Blockly.JavaScript.init = function(workspace) {
      ....
    };
    

    With this in mind, your function would look like:

    /**
    * Initialise the database of variable names.
    * @param {!Blockly.Workspace} workspace Workspace to generate code from.
    */
    Blockly.Json.init = function(workspace) {
     .....
    };
    

    There are other functions that the generator file contains, but taking a look at the existing code and adapting it for your needs should help you along the way.