Set the order of a statement block

223 views
Skip to first unread message

windows- ubuntu

unread,
Oct 16, 2022, 2:55:01 PM10/16/22
to Blockly
Hi! I wanna know how I can set the order of a statement block, like function blocks.
They can be at the bottom of the workspace but in the code they are awlays on top.

Neil Fraser

unread,
Oct 16, 2022, 3:06:02 PM10/16/22
to blo...@googlegroups.com
The output of the code generators is in two parts:
* First (at the top) there are the definitions.  These include function definitions, variable declarations, and helper functions.
* After the definitions, there's the general code.  These are in the order the blocks appear on screen, top to bottom (with a slight LTR or RTL angle).

Here's the code responsible for putting these two parts together:

For JavaScript the function definitions could be anywhere in the program and it will still work.  That's called variable hoisting.  Thus this is valid JavaScript code:
foo();
function foo(){}

But for other languages (such as Python), the calls to a function must be after the definition of that function.  Thus they need to be generated like this:
function foo(){}
foo();

In the interests of consistency, Blockly places all definitions at the start of the program.  You could write your own generator (just copy and modify an existing one) to output function definitions inline instead of in a definitions section.  But it's not an option that the existing generators support.

Am So., 16. Okt. 2022 um 20:55 Uhr schrieb windows- ubuntu <guido.f...@gmail.com>:
Hi! I wanna know how I can set the order of a statement block, like function blocks.
They can be at the bottom of the workspace but in the code they are awlays on top.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/610b4cad-92e0-484d-829a-5558e0ee9c37n%40googlegroups.com.


--

windows- ubuntu

unread,
Oct 16, 2022, 3:25:40 PM10/16/22
to Blockly
Thanks!

fu6...@gmail.com

unread,
Oct 16, 2022, 9:51:56 PM10/16/22
to Blockly
Hi, guido.f...

Hope this helps.

JavaScript.init = function(workspace) {
    //etc...
    this.functions_=Object.create(null);
    //etc...
};

JavaScript.finish = function(code) {
    const definitions = Object.values(this.definitions_);
    const functions = Object.values( this.functions_);
    //etc...
    return definitions.join('\n\n') + '\n\n\n' + code + '\n\n\n' + functions.join('\n\n');
};

Blockly.JavaScript['test'] = function (block) {
    Blockly.JavaScript.definitions_['test'] = 'var a;';
    Blockly.JavaScript.functions_['test'] = 'function test() {}';
    return '';
};


Besh wishes.
Fu6...


guido.f...@gmail.com 在 2022年10月17日 星期一凌晨3:25:40 [UTC+8] 的信中寫道:
Reply all
Reply to author
Forward
0 new messages