120 lines
3.0 KiB
Markdown
120 lines
3.0 KiB
Markdown
### general:
|
|
```plantuml
|
|
@startuml
|
|
start
|
|
:User creates blocks in Blockly workspace;
|
|
:User triggers code generation;
|
|
:Blockly.JSON.blockToCode(block) is called;
|
|
if (Generator function exists for block type?) then (Yes)
|
|
:Call the generator function for the block type;
|
|
if (Valid JSON object returned?) then (Yes)
|
|
:Proceed to process inputs and statements;
|
|
else (No)
|
|
:Log error about invalid JSON object;
|
|
stop
|
|
endif
|
|
else (No)
|
|
:Log error about missing generator function;
|
|
stop
|
|
endif
|
|
:Process inputs and statements recursively;
|
|
:Handle 'next' blocks recursively;
|
|
:Assemble final JSON representation;
|
|
stop
|
|
@enduml
|
|
|
|
|
|
```
|
|
|
|
### blockToCode:
|
|
```plantuml
|
|
@startuml
|
|
start
|
|
:Input block;
|
|
if (Generator function exists for block.type?) then (Yes)
|
|
:Call generator function to get JSON;
|
|
if (JSON is valid?) then (Yes)
|
|
if (skipAdditionalParsing flag is set?) then (Yes)
|
|
:Return JSON as is;
|
|
else (No)
|
|
:Call _blockToJson for further processing;
|
|
endif
|
|
else (No)
|
|
:Log error about invalid JSON;
|
|
stop
|
|
endif
|
|
else (No)
|
|
:Log error about missing generator function;
|
|
stop
|
|
endif
|
|
stop
|
|
@enduml
|
|
|
|
```
|
|
### _blockToJson:
|
|
```plantuml
|
|
@startuml
|
|
start
|
|
:Check if block is null;
|
|
if (block is null?) then (Yes)
|
|
:Return null;
|
|
stop
|
|
endif
|
|
|
|
:Call _calculateNestingDepth(block, currentDepth);
|
|
if (Depth exceeds maxDepth?) then (Yes)
|
|
:Throw Error "Maximum block nesting depth exceeded.";
|
|
stop
|
|
endif
|
|
|
|
if (Custom JSON generator exists for block.type?) then (Yes)
|
|
:Call custom generator to get json;
|
|
if (json is not valid?) then (Yes)
|
|
:Log error;
|
|
:Return null;
|
|
stop
|
|
endif
|
|
|
|
if (json.skipAdditionalParsing is true?) then (Yes)
|
|
:Remove skipAdditionalParsing from json;
|
|
if (json.type is not defined?) then (Yes)
|
|
:Return null;
|
|
else
|
|
:Return json;
|
|
stop
|
|
endif
|
|
endif
|
|
else (No)
|
|
:Create default json object with type, fields, inputs, statements;
|
|
:Capture all fields in the block;
|
|
endif
|
|
|
|
:Initialize json.inputs and json.statements if not already present;
|
|
:Initialize index = 0;
|
|
while (index < length of block.inputList) is (Yes)
|
|
:input = block.inputList[index];
|
|
if (input.connection and input.connection.targetBlock() exist?) then (Yes)
|
|
:Get targetBlock and targetJson;
|
|
:Call _blockToJson(targetBlock, currentDepth + 1);
|
|
if (input.type == INPUT_VALUE) then (Yes)
|
|
:Add targetJson to json.inputs[input.name];
|
|
else if (input.type == NEXT_STATEMENT) then (Yes)
|
|
:Add targetJson to json.statements[input.name] array;
|
|
endif
|
|
endif
|
|
:Increment index;
|
|
endwhile
|
|
|
|
:Handle 'next' connection;
|
|
if (block has next block?) then (Yes)
|
|
:Call _blockToJson(block.getNextBlock(), currentDepth);
|
|
:Assign result to json.next;
|
|
endif
|
|
|
|
:Return json;
|
|
stop
|
|
@enduml
|
|
|
|
|
|
|
|
``` |