mirror of
https://github.com/google/blockly.git
synced 2026-01-07 17:10:11 +01:00
refactor: convert some block generators to goog.module (#5769)
* refactor: convert generators/lua/colour.js to goog.module * refactor: convert generators/lua/colour.js to named requires * chore: run clang-format * refactor: convert generators/lua/lists.js to goog.module * refactor: convert generators/lua/lists.js to named requires * chore: run clang-format * fix: use getListIndex helper function in lua list generators * refactor: convert generators/lua/logic.js to goog.module * refactor: convert generators/lua/logic.js to named requires * chore: run clang-format * refactor: convert generators/lua/loops.js to goog.module * refactor: convert generators/lua/loops.js to named requires * chore: run clang-format * refactor: convert generators/lua/math.js to goog.module * refactor: convert generators/lua/math.js to named requires * chore: run clang-format * refcator: convert generators/lua/procedures.js to goog.module * refactor: convert generators/lua/procedures.js to named requires * chore: run clang-format * chore: rebuild deps.js * refactor: convert generators/lua/text.js to goog.module * refactor: convert generators/lua/text.js to named requires * refactor: convert generators/lua/variables_dynamic.js to goog.module * refactor: convert generators/lua/variables_dynamic.js to named requires * chore: run clang-format on text.js * refactor: convert generators/lua/variables.js to goog.module * refactor: convert generators/lua/variables.js to named requires * chore: run clang-format * chore: make a lua generator function internal * chore: rebuild deps.js
This commit is contained in:
@@ -9,73 +9,64 @@
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Lua.logic');
|
||||
goog.module('Blockly.Lua.logic');
|
||||
|
||||
goog.require('Blockly.Lua');
|
||||
const Lua = goog.require('Blockly.Lua');
|
||||
|
||||
|
||||
Blockly.Lua['controls_if'] = function(block) {
|
||||
Lua['controls_if'] = function(block) {
|
||||
// If/elseif/else condition.
|
||||
let n = 0;
|
||||
let code = '';
|
||||
if (Blockly.Lua.STATEMENT_PREFIX) {
|
||||
if (Lua.STATEMENT_PREFIX) {
|
||||
// Automatic prefix insertion is switched off for this block. Add manually.
|
||||
code += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_PREFIX, block);
|
||||
code += Lua.injectId(Lua.STATEMENT_PREFIX, block);
|
||||
}
|
||||
do {
|
||||
const conditionCode = Blockly.Lua.valueToCode(block, 'IF' + n,
|
||||
Blockly.Lua.ORDER_NONE) || 'false';
|
||||
let branchCode = Blockly.Lua.statementToCode(block, 'DO' + n);
|
||||
if (Blockly.Lua.STATEMENT_SUFFIX) {
|
||||
branchCode = Blockly.Lua.prefixLines(
|
||||
Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block),
|
||||
Blockly.Lua.INDENT) + branchCode;
|
||||
const conditionCode =
|
||||
Lua.valueToCode(block, 'IF' + n, Lua.ORDER_NONE) || 'false';
|
||||
let branchCode = Lua.statementToCode(block, 'DO' + n);
|
||||
if (Lua.STATEMENT_SUFFIX) {
|
||||
branchCode = Lua.prefixLines(
|
||||
Lua.injectId(Lua.STATEMENT_SUFFIX, block), Lua.INDENT) +
|
||||
branchCode;
|
||||
}
|
||||
code += (n > 0 ? 'else' : '') +
|
||||
'if ' + conditionCode + ' then\n' + branchCode;
|
||||
code +=
|
||||
(n > 0 ? 'else' : '') + 'if ' + conditionCode + ' then\n' + branchCode;
|
||||
n++;
|
||||
} while (block.getInput('IF' + n));
|
||||
|
||||
if (block.getInput('ELSE') || Blockly.Lua.STATEMENT_SUFFIX) {
|
||||
let branchCode = Blockly.Lua.statementToCode(block, 'ELSE');
|
||||
if (Blockly.Lua.STATEMENT_SUFFIX) {
|
||||
branchCode = Blockly.Lua.prefixLines(
|
||||
Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block),
|
||||
Blockly.Lua.INDENT) + branchCode;
|
||||
if (block.getInput('ELSE') || Lua.STATEMENT_SUFFIX) {
|
||||
let branchCode = Lua.statementToCode(block, 'ELSE');
|
||||
if (Lua.STATEMENT_SUFFIX) {
|
||||
branchCode = Lua.prefixLines(
|
||||
Lua.injectId(Lua.STATEMENT_SUFFIX, block), Lua.INDENT) +
|
||||
branchCode;
|
||||
}
|
||||
code += 'else\n' + branchCode;
|
||||
}
|
||||
return code + 'end\n';
|
||||
};
|
||||
|
||||
Blockly.Lua['controls_ifelse'] = Blockly.Lua['controls_if'];
|
||||
Lua['controls_ifelse'] = Lua['controls_if'];
|
||||
|
||||
Blockly.Lua['logic_compare'] = function(block) {
|
||||
Lua['logic_compare'] = function(block) {
|
||||
// Comparison operator.
|
||||
const OPERATORS = {
|
||||
'EQ': '==',
|
||||
'NEQ': '~=',
|
||||
'LT': '<',
|
||||
'LTE': '<=',
|
||||
'GT': '>',
|
||||
'GTE': '>='
|
||||
};
|
||||
const OPERATORS =
|
||||
{'EQ': '==', 'NEQ': '~=', 'LT': '<', 'LTE': '<=', 'GT': '>', 'GTE': '>='};
|
||||
const operator = OPERATORS[block.getFieldValue('OP')];
|
||||
const argument0 = Blockly.Lua.valueToCode(block, 'A',
|
||||
Blockly.Lua.ORDER_RELATIONAL) || '0';
|
||||
const argument1 = Blockly.Lua.valueToCode(block, 'B',
|
||||
Blockly.Lua.ORDER_RELATIONAL) || '0';
|
||||
const argument0 = Lua.valueToCode(block, 'A', Lua.ORDER_RELATIONAL) || '0';
|
||||
const argument1 = Lua.valueToCode(block, 'B', Lua.ORDER_RELATIONAL) || '0';
|
||||
const code = argument0 + ' ' + operator + ' ' + argument1;
|
||||
return [code, Blockly.Lua.ORDER_RELATIONAL];
|
||||
return [code, Lua.ORDER_RELATIONAL];
|
||||
};
|
||||
|
||||
Blockly.Lua['logic_operation'] = function(block) {
|
||||
Lua['logic_operation'] = function(block) {
|
||||
// Operations 'and', 'or'.
|
||||
const operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or';
|
||||
const order = (operator === 'and') ? Blockly.Lua.ORDER_AND :
|
||||
Blockly.Lua.ORDER_OR;
|
||||
let argument0 = Blockly.Lua.valueToCode(block, 'A', order);
|
||||
let argument1 = Blockly.Lua.valueToCode(block, 'B', order);
|
||||
const order = (operator === 'and') ? Lua.ORDER_AND : Lua.ORDER_OR;
|
||||
let argument0 = Lua.valueToCode(block, 'A', order);
|
||||
let argument1 = Lua.valueToCode(block, 'B', order);
|
||||
if (!argument0 && !argument1) {
|
||||
// If there are no arguments, then the return value is false.
|
||||
argument0 = 'false';
|
||||
@@ -94,33 +85,29 @@ Blockly.Lua['logic_operation'] = function(block) {
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Lua['logic_negate'] = function(block) {
|
||||
Lua['logic_negate'] = function(block) {
|
||||
// Negation.
|
||||
const argument0 = Blockly.Lua.valueToCode(block, 'BOOL',
|
||||
Blockly.Lua.ORDER_UNARY) || 'true';
|
||||
const argument0 = Lua.valueToCode(block, 'BOOL', Lua.ORDER_UNARY) || 'true';
|
||||
const code = 'not ' + argument0;
|
||||
return [code, Blockly.Lua.ORDER_UNARY];
|
||||
return [code, Lua.ORDER_UNARY];
|
||||
};
|
||||
|
||||
Blockly.Lua['logic_boolean'] = function(block) {
|
||||
Lua['logic_boolean'] = function(block) {
|
||||
// Boolean values true and false.
|
||||
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'true' : 'false';
|
||||
return [code, Blockly.Lua.ORDER_ATOMIC];
|
||||
return [code, Lua.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Lua['logic_null'] = function(block) {
|
||||
Lua['logic_null'] = function(block) {
|
||||
// Null data type.
|
||||
return ['nil', Blockly.Lua.ORDER_ATOMIC];
|
||||
return ['nil', Lua.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Lua['logic_ternary'] = function(block) {
|
||||
Lua['logic_ternary'] = function(block) {
|
||||
// Ternary operator.
|
||||
const value_if = Blockly.Lua.valueToCode(block, 'IF',
|
||||
Blockly.Lua.ORDER_AND) || 'false';
|
||||
const value_then = Blockly.Lua.valueToCode(block, 'THEN',
|
||||
Blockly.Lua.ORDER_AND) || 'nil';
|
||||
const value_else = Blockly.Lua.valueToCode(block, 'ELSE',
|
||||
Blockly.Lua.ORDER_OR) || 'nil';
|
||||
const value_if = Lua.valueToCode(block, 'IF', Lua.ORDER_AND) || 'false';
|
||||
const value_then = Lua.valueToCode(block, 'THEN', Lua.ORDER_AND) || 'nil';
|
||||
const value_else = Lua.valueToCode(block, 'ELSE', Lua.ORDER_OR) || 'nil';
|
||||
const code = value_if + ' and ' + value_then + ' or ' + value_else;
|
||||
return [code, Blockly.Lua.ORDER_OR];
|
||||
return [code, Lua.ORDER_OR];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user