SoFunction
Updated on 2024-11-18

Scratch3.0 secondary development with blocks to generate python code

Before we begin, let's review the previously written article "Thescratch3.0 secondary development of blocks to generate code ideas", in the article probably wrote a few steps to generate code, the reader may not understand, this is based on my development experience. Now specifically write the realization of the method.

I. Introducing a file to generate code

For example, to generate arduino code, you have to introduce the file that generates arduino code, and to generate python code, you have to introduce the python file.

This generator file is imported from Blockly, on which scratch-blocks is based. The current scratch-blocks does not have a generators directory, which can be copied from blockly.
Analyze blockly's code specifically:

 = new ('Python');

An instance of python is that will call methods in generator;

(...);

What this method does is add keywords to python, and blockly's python file already has many python keywords set up in it.

.ORDER_ATOMIC = 0;            // 0 "" ...
.ORDER_COLLECTION = 1;        // tuples, lists, dictionaries
.ORDER_STRING_CONVERSION = 1; // `expression...`
.ORDER_MEMBER = 2.1;          // . []
.ORDER_FUNCTION_CALL = 2.2;   // ()
.ORDER_EXPONENTIATION = 3;    // **
.ORDER_UNARY_SIGN = 4;        // + -
.ORDER_BITWISE_NOT = 4;       // ~
.ORDER_MULTIPLICATIVE = 5;    // * / // %
.ORDER_ADDITIVE = 6;          // + -
.ORDER_BITWISE_SHIFT = 7;     // << >>
.ORDER_BITWISE_AND = 8;       // &
.ORDER_BITWISE_XOR = 9;       // ^
.ORDER_BITWISE_OR = 10;       // |
.ORDER_RELATIONAL = 11;       // in, not in, is, is not,
                                            //     <, <=, >, >=, <>, !=, ==
.ORDER_LOGICAL_NOT = 12;      // not
.ORDER_LOGICAL_AND = 13;      // and
.ORDER_LOGICAL_OR = 14;       // or
.ORDER_CONDITIONAL = 15;      // if else
.ORDER_LAMBDA = 16;           // lambda
.ORDER_NONE = 99;             // (...)

The above code sets the priority.
See blockly's python file for other methods.

II. Define the generated python code

The generated code needs to be defined for each block.

1. Get the type of the block

Blocks are distinguished by type, and the type of each block is unique. scratch-blocks' basic blocks are defined in the scratch-blocks\blocks_vertical directory.
For details, check out the previous blog, "TheScratch3.0 secondary development of scratch-blocks in the blocks of the type, definition and use of methods

The following is the definition of a block of type "motion_movesteps".

在这里插入图片描述

['motion_movesteps'] = {
  /**
   * Block to move steps.
   * @this 
   */
  init: function() {
    ({
      "message0": .MOTION_MOVESTEPS,
      "args0": [
        {
          "type": "input_value",
          "name": "STEPS"
        }
      ],
      "category": ,
      "extensions": ["colours_motion", "shape_statement"]
    });
  }
};

Defines the generated python statements:

 ['motion_movesteps'] = function (block) {
        var steps = (block, "STEPS", .ORDER_NONE);
        return 'move ' + steps + ' steps\n';
    };

2、Get the value of the parameter

Depending on the type of the parameter, choose, or to get the value.

type methodologies
input_value
input_statement
field_*

3, from xml to parse the composition of a block structure

This is the xml structure of the motion_movesteps block, it consists of two blocks, "motion_movesteps" and "math_number", and the shadow is also a block.

<block type="motion_movesteps">
            <value name="STEPS">
                <shadow type="math_number">
                    <field name="NUM">10</field>
                </shadow>
            </value>
        </block>

So in addition to the "motion_movesteps" generated python statement defined above, you also need to define the math_number generated statement.

['math_number'] = function(block) {
  // Numeric value.
  var code = parseFloat(('NUM'));
  var order;
  if (code == Infinity) {
    code = 'float("inf")';
    order = .ORDER_FUNCTION_CALL;
  } else if (code == -Infinity) {
    code = '-float("inf")';
    order = .ORDER_UNARY_SIGN;
  } else {
    order = code < 0 ? .ORDER_UNARY_SIGN :
            .ORDER_ATOMIC;
  }
  return [code, order];
}

4. According to the shape of the block to set the data type of return

geometry Type of return
string (computer science)
string (computer science)
[code, order] array
[code, order] array

Introduce the module: .definitions_[module name] = "import xxxx";
Declare the function: .definitions_[module name] = "def xxx ...";

在这里插入图片描述

III. Generating python code

In the scratch-gui/src/containers/ file, add a listen to the workspace

 ();

IV. Conclusion

This article mainly introduces the blocks to generate python code methods, due to limited space, write is not very comprehensive, but probably generate these methods. If you encounter problems in the development process, you can private message me to leave me a message, I try to help you solve the problem!

to this article on scratch3.0 secondary development with blocks to generate python code is introduced to this article, more related to scratch blocks to generate python code content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!