functions

This commit is contained in:
sussy layers dev 2023-11-16 21:46:45 +00:00 committed by GitHub
commit 371b7e2fdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 102 additions and 0 deletions

View file

@ -78,6 +78,13 @@
<block type="blocks_get" />
<block type="blocks_return" />
</category>
<category name="Functions" colour="#5531D6">
<block type="functions_create" />
<block type="functions_return" />
<sep gap="48"></sep>
<block type="functions_call" />
<block type="functions_callreporter" />
</category>
<category name="Debug" colour="#666666">
<block type="debug_log" />
<block type="debug_warn" />

View file

@ -0,0 +1,95 @@
import javascriptGenerator from '../javascriptGenerator';
import registerBlock from '../register';
const categoryPrefix = 'functions_';
const categoryColor = '#5531D6';
function register() {
// function
registerBlock(`${categoryPrefix}create`, {
message0: 'function %1 %2 %3',
args0: [
{
"type": "field_input",
"name": "ID",
"text": "id",
"spellcheck": false
},
{
"type": "input_dummy"
},
{
"type": "input_statement",
"name": "FUNC"
}
],
nextStatement: null,
inputsInline: true,
colour: categoryColor,
}, (block) => {
const ID = block.getFieldValue('ID')
const FUNC = javascriptGenerator.statementToCode(block, 'FUNC');
const code = `function ${ID}() { ${FUNC} }`;
return `${code}\n`;
})
// return
registerBlock(`${categoryPrefix}return`, {
message0: 'return %1',
args0: [
{
"type": "input_value",
"name": "VALUE",
}
],
previousStatement: null,
inputsInline: true,
colour: categoryColor,
}, (block) => {
const VALUE = javascriptGenerator.valueToCode(block, 'VALUE', javascriptGenerator.ORDER_ATOMIC);
const code = `return ${VALUE || ''}`;
return `${code}\n`;
})
// call
registerBlock(`${categoryPrefix}call`, {
message0: 'call %1',
args0: [
{
"type": "field_input",
"name": "ID",
"text": "id",
"spellcheck": false
},
],
previousStatement: null,
nextStatement: null,
inputsInline: true,
colour: categoryColor,
}, (block) => {
const ID = block.getFieldValue('ID')
const code = `${ID}()`;
return `${code}\n`;
})
// call
registerBlock(`${categoryPrefix}callreporter`, {
message0: 'call %1',
args0: [
{
"type": "field_input",
"name": "ID",
"text": "id",
"spellcheck": false
},
],
output: null,
inputsInline: true,
colour: categoryColor,
}, (block) => {
const ID = block.getFieldValue('ID')
return [`${ID}()\n`, javascriptGenerator.ORDER_ATOMIC];
})
}
export default register;