mirror of
https://github.com/google/blockly.git
synced 2026-01-06 16:40:07 +01:00
chore: update lua block generators to const and let (#5662)
This commit is contained in:
@@ -16,19 +16,19 @@ goog.require('Blockly.Lua');
|
|||||||
|
|
||||||
Blockly.Lua['colour_picker'] = function(block) {
|
Blockly.Lua['colour_picker'] = function(block) {
|
||||||
// Colour picker.
|
// Colour picker.
|
||||||
var code = Blockly.Lua.quote_(block.getFieldValue('COLOUR'));
|
const code = Blockly.Lua.quote_(block.getFieldValue('COLOUR'));
|
||||||
return [code, Blockly.Lua.ORDER_ATOMIC];
|
return [code, Blockly.Lua.ORDER_ATOMIC];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['colour_random'] = function(block) {
|
Blockly.Lua['colour_random'] = function(block) {
|
||||||
// Generate a random colour.
|
// Generate a random colour.
|
||||||
var code = 'string.format("#%06x", math.random(0, 2^24 - 1))';
|
const code = 'string.format("#%06x", math.random(0, 2^24 - 1))';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['colour_rgb'] = function(block) {
|
Blockly.Lua['colour_rgb'] = function(block) {
|
||||||
// Compose a colour from RGB components expressed as percentages.
|
// Compose a colour from RGB components expressed as percentages.
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
const functionName = Blockly.Lua.provideFunction_(
|
||||||
'colour_rgb',
|
'colour_rgb',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b)',
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b)',
|
||||||
' r = math.floor(math.min(100, math.max(0, r)) * 2.55 + .5)',
|
' r = math.floor(math.min(100, math.max(0, r)) * 2.55 + .5)',
|
||||||
@@ -36,19 +36,19 @@ Blockly.Lua['colour_rgb'] = function(block) {
|
|||||||
' b = math.floor(math.min(100, math.max(0, b)) * 2.55 + .5)',
|
' b = math.floor(math.min(100, math.max(0, b)) * 2.55 + .5)',
|
||||||
' return string.format("#%02x%02x%02x", r, g, b)',
|
' return string.format("#%02x%02x%02x", r, g, b)',
|
||||||
'end']);
|
'end']);
|
||||||
var r = Blockly.Lua.valueToCode(block, 'RED',
|
const r = Blockly.Lua.valueToCode(block, 'RED',
|
||||||
Blockly.Lua.ORDER_NONE) || 0;
|
Blockly.Lua.ORDER_NONE) || 0;
|
||||||
var g = Blockly.Lua.valueToCode(block, 'GREEN',
|
const g = Blockly.Lua.valueToCode(block, 'GREEN',
|
||||||
Blockly.Lua.ORDER_NONE) || 0;
|
Blockly.Lua.ORDER_NONE) || 0;
|
||||||
var b = Blockly.Lua.valueToCode(block, 'BLUE',
|
const b = Blockly.Lua.valueToCode(block, 'BLUE',
|
||||||
Blockly.Lua.ORDER_NONE) || 0;
|
Blockly.Lua.ORDER_NONE) || 0;
|
||||||
var code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
|
const code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['colour_blend'] = function(block) {
|
Blockly.Lua['colour_blend'] = function(block) {
|
||||||
// Blend two colours together.
|
// Blend two colours together.
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
const functionName = Blockly.Lua.provideFunction_(
|
||||||
'colour_blend',
|
'colour_blend',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
'(colour1, colour2, ratio)',
|
'(colour1, colour2, ratio)',
|
||||||
@@ -64,12 +64,12 @@ Blockly.Lua['colour_blend'] = function(block) {
|
|||||||
' local b = math.floor(b1 * (1 - ratio) + b2 * ratio + .5)',
|
' local b = math.floor(b1 * (1 - ratio) + b2 * ratio + .5)',
|
||||||
' return string.format("#%02x%02x%02x", r, g, b)',
|
' return string.format("#%02x%02x%02x", r, g, b)',
|
||||||
'end']);
|
'end']);
|
||||||
var colour1 = Blockly.Lua.valueToCode(block, 'COLOUR1',
|
const colour1 = Blockly.Lua.valueToCode(block, 'COLOUR1',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'#000000\'';
|
Blockly.Lua.ORDER_NONE) || '\'#000000\'';
|
||||||
var colour2 = Blockly.Lua.valueToCode(block, 'COLOUR2',
|
const colour2 = Blockly.Lua.valueToCode(block, 'COLOUR2',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'#000000\'';
|
Blockly.Lua.ORDER_NONE) || '\'#000000\'';
|
||||||
var ratio = Blockly.Lua.valueToCode(block, 'RATIO',
|
const ratio = Blockly.Lua.valueToCode(block, 'RATIO',
|
||||||
Blockly.Lua.ORDER_NONE) || 0;
|
Blockly.Lua.ORDER_NONE) || 0;
|
||||||
var code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
|
const code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,18 +21,18 @@ Blockly.Lua['lists_create_empty'] = function(block) {
|
|||||||
|
|
||||||
Blockly.Lua['lists_create_with'] = function(block) {
|
Blockly.Lua['lists_create_with'] = function(block) {
|
||||||
// Create a list with any number of elements of any type.
|
// Create a list with any number of elements of any type.
|
||||||
var elements = new Array(block.itemCount_);
|
const elements = new Array(block.itemCount_);
|
||||||
for (var i = 0; i < block.itemCount_; i++) {
|
for (let i = 0; i < block.itemCount_; i++) {
|
||||||
elements[i] = Blockly.Lua.valueToCode(block, 'ADD' + i,
|
elements[i] = Blockly.Lua.valueToCode(block, 'ADD' + i,
|
||||||
Blockly.Lua.ORDER_NONE) || 'None';
|
Blockly.Lua.ORDER_NONE) || 'None';
|
||||||
}
|
}
|
||||||
var code = '{' + elements.join(', ') + '}';
|
const code = '{' + elements.join(', ') + '}';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['lists_repeat'] = function(block) {
|
Blockly.Lua['lists_repeat'] = function(block) {
|
||||||
// Create a list with one element repeated.
|
// Create a list with one element repeated.
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
const functionName = Blockly.Lua.provideFunction_(
|
||||||
'create_list_repeated',
|
'create_list_repeated',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(item, count)',
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(item, count)',
|
||||||
' local t = {}',
|
' local t = {}',
|
||||||
@@ -41,37 +41,38 @@ Blockly.Lua['lists_repeat'] = function(block) {
|
|||||||
' end',
|
' end',
|
||||||
' return t',
|
' return t',
|
||||||
'end']);
|
'end']);
|
||||||
var element = Blockly.Lua.valueToCode(block, 'ITEM',
|
const element = Blockly.Lua.valueToCode(block, 'ITEM',
|
||||||
Blockly.Lua.ORDER_NONE) || 'None';
|
Blockly.Lua.ORDER_NONE) || 'None';
|
||||||
var repeatCount = Blockly.Lua.valueToCode(block, 'NUM',
|
const repeatCount = Blockly.Lua.valueToCode(block, 'NUM',
|
||||||
Blockly.Lua.ORDER_NONE) || '0';
|
Blockly.Lua.ORDER_NONE) || '0';
|
||||||
var code = functionName + '(' + element + ', ' + repeatCount + ')';
|
const code = functionName + '(' + element + ', ' + repeatCount + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['lists_length'] = function(block) {
|
Blockly.Lua['lists_length'] = function(block) {
|
||||||
// String or array length.
|
// String or array length.
|
||||||
var list = Blockly.Lua.valueToCode(block, 'VALUE',
|
const list = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||||
Blockly.Lua.ORDER_UNARY) || '{}';
|
Blockly.Lua.ORDER_UNARY) || '{}';
|
||||||
return ['#' + list, Blockly.Lua.ORDER_UNARY];
|
return ['#' + list, Blockly.Lua.ORDER_UNARY];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['lists_isEmpty'] = function(block) {
|
Blockly.Lua['lists_isEmpty'] = function(block) {
|
||||||
// Is the string null or array empty?
|
// Is the string null or array empty?
|
||||||
var list = Blockly.Lua.valueToCode(block, 'VALUE',
|
const list = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||||
Blockly.Lua.ORDER_UNARY) || '{}';
|
Blockly.Lua.ORDER_UNARY) || '{}';
|
||||||
var code = '#' + list + ' == 0';
|
const code = '#' + list + ' == 0';
|
||||||
return [code, Blockly.Lua.ORDER_RELATIONAL];
|
return [code, Blockly.Lua.ORDER_RELATIONAL];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['lists_indexOf'] = function(block) {
|
Blockly.Lua['lists_indexOf'] = function(block) {
|
||||||
// Find an item in the list.
|
// Find an item in the list.
|
||||||
var item = Blockly.Lua.valueToCode(block, 'FIND',
|
const item = Blockly.Lua.valueToCode(block, 'FIND',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
var list = Blockly.Lua.valueToCode(block, 'VALUE',
|
const list = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||||
Blockly.Lua.ORDER_NONE) || '{}';
|
Blockly.Lua.ORDER_NONE) || '{}';
|
||||||
|
let functionName;
|
||||||
if (block.getFieldValue('END') === 'FIRST') {
|
if (block.getFieldValue('END') === 'FIRST') {
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
functionName = Blockly.Lua.provideFunction_(
|
||||||
'first_index',
|
'first_index',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t, elem)',
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t, elem)',
|
||||||
' for k, v in ipairs(t) do',
|
' for k, v in ipairs(t) do',
|
||||||
@@ -82,7 +83,7 @@ Blockly.Lua['lists_indexOf'] = function(block) {
|
|||||||
' return 0',
|
' return 0',
|
||||||
'end']);
|
'end']);
|
||||||
} else {
|
} else {
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
functionName = Blockly.Lua.provideFunction_(
|
||||||
'last_index',
|
'last_index',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t, elem)',
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t, elem)',
|
||||||
' for i = #t, 1, -1 do',
|
' for i = #t, 1, -1 do',
|
||||||
@@ -93,7 +94,7 @@ Blockly.Lua['lists_indexOf'] = function(block) {
|
|||||||
' return 0',
|
' return 0',
|
||||||
'end']);
|
'end']);
|
||||||
}
|
}
|
||||||
var code = functionName + '(' + list + ', ' + item + ')';
|
const code = functionName + '(' + list + ', ' + item + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -122,11 +123,11 @@ Blockly.Lua.lists.getIndex_ = function(listName, where, opt_at) {
|
|||||||
Blockly.Lua['lists_getIndex'] = function(block) {
|
Blockly.Lua['lists_getIndex'] = function(block) {
|
||||||
// Get element at index.
|
// Get element at index.
|
||||||
// Note: Until January 2013 this block did not have MODE or WHERE inputs.
|
// Note: Until January 2013 this block did not have MODE or WHERE inputs.
|
||||||
var mode = block.getFieldValue('MODE') || 'GET';
|
const mode = block.getFieldValue('MODE') || 'GET';
|
||||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||||
var list = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.Lua.ORDER_HIGH) ||
|
const list = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.Lua.ORDER_HIGH) ||
|
||||||
'({})';
|
'({})';
|
||||||
var getIndex_ = Blockly.Lua.lists.getIndex_;
|
const getIndex_ = Blockly.Lua.lists.getIndex_;
|
||||||
|
|
||||||
// If `list` would be evaluated more than once (which is the case for LAST,
|
// If `list` would be evaluated more than once (which is the case for LAST,
|
||||||
// FROM_END, and RANDOM) and is non-trivial, make sure to access it only once.
|
// FROM_END, and RANDOM) and is non-trivial, make sure to access it only once.
|
||||||
@@ -135,21 +136,22 @@ Blockly.Lua['lists_getIndex'] = function(block) {
|
|||||||
// `list` is an expression, so we may not evaluate it more than once.
|
// `list` is an expression, so we may not evaluate it more than once.
|
||||||
if (mode === 'REMOVE') {
|
if (mode === 'REMOVE') {
|
||||||
// We can use multiple statements.
|
// We can use multiple statements.
|
||||||
var atOrder = (where === 'FROM_END') ? Blockly.Lua.ORDER_ADDITIVE :
|
const atOrder = (where === 'FROM_END') ? Blockly.Lua.ORDER_ADDITIVE :
|
||||||
Blockly.Lua.ORDER_NONE;
|
Blockly.Lua.ORDER_NONE;
|
||||||
var at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
|
let at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
|
||||||
var listVar = Blockly.Lua.nameDB_.getDistinctName(
|
const listVar = Blockly.Lua.nameDB_.getDistinctName(
|
||||||
'tmp_list', Blockly.VARIABLE_CATEGORY_NAME);
|
'tmp_list', Blockly.VARIABLE_CATEGORY_NAME);
|
||||||
at = getIndex_(listVar, where, at);
|
at = getIndex_(listVar, where, at);
|
||||||
var code = listVar + ' = ' + list + '\n' +
|
const code = listVar + ' = ' + list + '\n' +
|
||||||
'table.remove(' + listVar + ', ' + at + ')\n';
|
'table.remove(' + listVar + ', ' + at + ')\n';
|
||||||
return code;
|
return code;
|
||||||
} else {
|
} else {
|
||||||
// We need to create a procedure to avoid reevaluating values.
|
// We need to create a procedure to avoid reevaluating values.
|
||||||
var at = Blockly.Lua.valueToCode(block, 'AT', Blockly.Lua.ORDER_NONE) ||
|
const at = Blockly.Lua.valueToCode(block, 'AT', Blockly.Lua.ORDER_NONE) ||
|
||||||
'1';
|
'1';
|
||||||
|
let functionName;
|
||||||
if (mode === 'GET') {
|
if (mode === 'GET') {
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
functionName = Blockly.Lua.provideFunction_(
|
||||||
'list_get_' + where.toLowerCase(),
|
'list_get_' + where.toLowerCase(),
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t' +
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t' +
|
||||||
// The value for 'FROM_END' and'FROM_START' depends on `at` so
|
// The value for 'FROM_END' and'FROM_START' depends on `at` so
|
||||||
@@ -159,7 +161,7 @@ Blockly.Lua['lists_getIndex'] = function(block) {
|
|||||||
' return t[' + getIndex_('t', where, 'at') + ']',
|
' return t[' + getIndex_('t', where, 'at') + ']',
|
||||||
'end']);
|
'end']);
|
||||||
} else { // `mode` === 'GET_REMOVE'
|
} else { // `mode` === 'GET_REMOVE'
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
functionName = Blockly.Lua.provideFunction_(
|
||||||
'list_remove_' + where.toLowerCase(),
|
'list_remove_' + where.toLowerCase(),
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t' +
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t' +
|
||||||
// The value for 'FROM_END' and'FROM_START' depends on `at` so
|
// The value for 'FROM_END' and'FROM_START' depends on `at` so
|
||||||
@@ -169,7 +171,7 @@ Blockly.Lua['lists_getIndex'] = function(block) {
|
|||||||
' return table.remove(t, ' + getIndex_('t', where, 'at') + ')',
|
' return table.remove(t, ' + getIndex_('t', where, 'at') + ')',
|
||||||
'end']);
|
'end']);
|
||||||
}
|
}
|
||||||
var code = functionName + '(' + list +
|
const code = functionName + '(' + list +
|
||||||
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
|
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
|
||||||
// pass it.
|
// pass it.
|
||||||
((where === 'FROM_END' || where === 'FROM_START') ? ', ' + at : '') +
|
((where === 'FROM_END' || where === 'FROM_START') ? ', ' + at : '') +
|
||||||
@@ -179,15 +181,15 @@ Blockly.Lua['lists_getIndex'] = function(block) {
|
|||||||
} else {
|
} else {
|
||||||
// Either `list` is a simple variable, or we only need to refer to `list`
|
// Either `list` is a simple variable, or we only need to refer to `list`
|
||||||
// once.
|
// once.
|
||||||
var atOrder = (mode === 'GET' && where === 'FROM_END') ?
|
const atOrder = (mode === 'GET' && where === 'FROM_END') ?
|
||||||
Blockly.Lua.ORDER_ADDITIVE : Blockly.Lua.ORDER_NONE;
|
Blockly.Lua.ORDER_ADDITIVE : Blockly.Lua.ORDER_NONE;
|
||||||
var at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
|
let at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
|
||||||
at = getIndex_(list, where, at);
|
at = getIndex_(list, where, at);
|
||||||
if (mode === 'GET') {
|
if (mode === 'GET') {
|
||||||
var code = list + '[' + at + ']';
|
const code = list + '[' + at + ']';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
} else {
|
} else {
|
||||||
var code = 'table.remove(' + list + ', ' + at + ')';
|
const code = 'table.remove(' + list + ', ' + at + ')';
|
||||||
if (mode === 'GET_REMOVE') {
|
if (mode === 'GET_REMOVE') {
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
} else { // `mode` === 'REMOVE'
|
} else { // `mode` === 'REMOVE'
|
||||||
@@ -200,24 +202,24 @@ Blockly.Lua['lists_getIndex'] = function(block) {
|
|||||||
Blockly.Lua['lists_setIndex'] = function(block) {
|
Blockly.Lua['lists_setIndex'] = function(block) {
|
||||||
// Set element at index.
|
// Set element at index.
|
||||||
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
|
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
|
||||||
var list = Blockly.Lua.valueToCode(block, 'LIST',
|
let list = Blockly.Lua.valueToCode(block, 'LIST',
|
||||||
Blockly.Lua.ORDER_HIGH) || '{}';
|
Blockly.Lua.ORDER_HIGH) || '{}';
|
||||||
var mode = block.getFieldValue('MODE') || 'SET';
|
const mode = block.getFieldValue('MODE') || 'SET';
|
||||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||||
var at = Blockly.Lua.valueToCode(block, 'AT',
|
const at = Blockly.Lua.valueToCode(block, 'AT',
|
||||||
Blockly.Lua.ORDER_ADDITIVE) || '1';
|
Blockly.Lua.ORDER_ADDITIVE) || '1';
|
||||||
var value = Blockly.Lua.valueToCode(block, 'TO',
|
const value = Blockly.Lua.valueToCode(block, 'TO',
|
||||||
Blockly.Lua.ORDER_NONE) || 'None';
|
Blockly.Lua.ORDER_NONE) || 'None';
|
||||||
var getIndex_ = Blockly.Lua.lists.getIndex_;
|
const getIndex_ = Blockly.Lua.lists.getIndex_;
|
||||||
|
|
||||||
var code = '';
|
let code = '';
|
||||||
// If `list` would be evaluated more than once (which is the case for LAST,
|
// If `list` would be evaluated more than once (which is the case for LAST,
|
||||||
// FROM_END, and RANDOM) and is non-trivial, make sure to access it only once.
|
// FROM_END, and RANDOM) and is non-trivial, make sure to access it only once.
|
||||||
if ((where === 'LAST' || where === 'FROM_END' || where === 'RANDOM') &&
|
if ((where === 'LAST' || where === 'FROM_END' || where === 'RANDOM') &&
|
||||||
!list.match(/^\w+$/)) {
|
!list.match(/^\w+$/)) {
|
||||||
// `list` is an expression, so we may not evaluate it more than once.
|
// `list` is an expression, so we may not evaluate it more than once.
|
||||||
// We can use multiple statements.
|
// We can use multiple statements.
|
||||||
var listVar = Blockly.Lua.nameDB_.getDistinctName(
|
const listVar = Blockly.Lua.nameDB_.getDistinctName(
|
||||||
'tmp_list', Blockly.VARIABLE_CATEGORY_NAME);
|
'tmp_list', Blockly.VARIABLE_CATEGORY_NAME);
|
||||||
code = listVar + ' = ' + list + '\n';
|
code = listVar + ' = ' + list + '\n';
|
||||||
list = listVar;
|
list = listVar;
|
||||||
@@ -236,17 +238,17 @@ Blockly.Lua['lists_setIndex'] = function(block) {
|
|||||||
|
|
||||||
Blockly.Lua['lists_getSublist'] = function(block) {
|
Blockly.Lua['lists_getSublist'] = function(block) {
|
||||||
// Get sublist.
|
// Get sublist.
|
||||||
var list = Blockly.Lua.valueToCode(block, 'LIST',
|
const list = Blockly.Lua.valueToCode(block, 'LIST',
|
||||||
Blockly.Lua.ORDER_NONE) || '{}';
|
Blockly.Lua.ORDER_NONE) || '{}';
|
||||||
var where1 = block.getFieldValue('WHERE1');
|
const where1 = block.getFieldValue('WHERE1');
|
||||||
var where2 = block.getFieldValue('WHERE2');
|
const where2 = block.getFieldValue('WHERE2');
|
||||||
var at1 = Blockly.Lua.valueToCode(block, 'AT1',
|
const at1 = Blockly.Lua.valueToCode(block, 'AT1',
|
||||||
Blockly.Lua.ORDER_NONE) || '1';
|
Blockly.Lua.ORDER_NONE) || '1';
|
||||||
var at2 = Blockly.Lua.valueToCode(block, 'AT2',
|
const at2 = Blockly.Lua.valueToCode(block, 'AT2',
|
||||||
Blockly.Lua.ORDER_NONE) || '1';
|
Blockly.Lua.ORDER_NONE) || '1';
|
||||||
var getIndex_ = Blockly.Lua.lists.getIndex_;
|
const getIndex_ = Blockly.Lua.lists.getIndex_;
|
||||||
|
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
const functionName = Blockly.Lua.provideFunction_(
|
||||||
'list_sublist_' + where1.toLowerCase() + '_' + where2.toLowerCase(),
|
'list_sublist_' + where1.toLowerCase() + '_' + where2.toLowerCase(),
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(source' +
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(source' +
|
||||||
// The value for 'FROM_END' and'FROM_START' depends on `at` so
|
// The value for 'FROM_END' and'FROM_START' depends on `at` so
|
||||||
@@ -262,7 +264,7 @@ Blockly.Lua['lists_getSublist'] = function(block) {
|
|||||||
' end',
|
' end',
|
||||||
' return t',
|
' return t',
|
||||||
'end']);
|
'end']);
|
||||||
var code = functionName + '(' + list +
|
const code = functionName + '(' + list +
|
||||||
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
|
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
|
||||||
// pass it.
|
// pass it.
|
||||||
((where1 === 'FROM_END' || where1 === 'FROM_START') ? ', ' + at1 : '') +
|
((where1 === 'FROM_END' || where1 === 'FROM_START') ? ', ' + at1 : '') +
|
||||||
@@ -273,12 +275,12 @@ Blockly.Lua['lists_getSublist'] = function(block) {
|
|||||||
|
|
||||||
Blockly.Lua['lists_sort'] = function(block) {
|
Blockly.Lua['lists_sort'] = function(block) {
|
||||||
// Block for sorting a list.
|
// Block for sorting a list.
|
||||||
var list = Blockly.Lua.valueToCode(
|
const list = Blockly.Lua.valueToCode(
|
||||||
block, 'LIST', Blockly.Lua.ORDER_NONE) || '{}';
|
block, 'LIST', Blockly.Lua.ORDER_NONE) || '{}';
|
||||||
var direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
|
const direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
|
||||||
var type = block.getFieldValue('TYPE');
|
const type = block.getFieldValue('TYPE');
|
||||||
|
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
const functionName = Blockly.Lua.provideFunction_(
|
||||||
'list_sort',
|
'list_sort',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
'(list, typev, direction)',
|
'(list, typev, direction)',
|
||||||
@@ -302,19 +304,19 @@ Blockly.Lua['lists_sort'] = function(block) {
|
|||||||
' return t',
|
' return t',
|
||||||
'end']);
|
'end']);
|
||||||
|
|
||||||
var code = functionName +
|
const code = functionName +
|
||||||
'(' + list + ',"' + type + '", ' + direction + ')';
|
'(' + list + ',"' + type + '", ' + direction + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['lists_split'] = function(block) {
|
Blockly.Lua['lists_split'] = function(block) {
|
||||||
// Block for splitting text into a list, or joining a list into text.
|
// Block for splitting text into a list, or joining a list into text.
|
||||||
var input = Blockly.Lua.valueToCode(block, 'INPUT',
|
let input = Blockly.Lua.valueToCode(block, 'INPUT',
|
||||||
Blockly.Lua.ORDER_NONE);
|
Blockly.Lua.ORDER_NONE);
|
||||||
var delimiter = Blockly.Lua.valueToCode(block, 'DELIM',
|
const delimiter = Blockly.Lua.valueToCode(block, 'DELIM',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
var mode = block.getFieldValue('MODE');
|
const mode = block.getFieldValue('MODE');
|
||||||
var functionName;
|
let functionName;
|
||||||
if (mode === 'SPLIT') {
|
if (mode === 'SPLIT') {
|
||||||
if (!input) {
|
if (!input) {
|
||||||
input = '\'\'';
|
input = '\'\'';
|
||||||
@@ -345,15 +347,15 @@ Blockly.Lua['lists_split'] = function(block) {
|
|||||||
} else {
|
} else {
|
||||||
throw Error('Unknown mode: ' + mode);
|
throw Error('Unknown mode: ' + mode);
|
||||||
}
|
}
|
||||||
var code = functionName + '(' + input + ', ' + delimiter + ')';
|
const code = functionName + '(' + input + ', ' + delimiter + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['lists_reverse'] = function(block) {
|
Blockly.Lua['lists_reverse'] = function(block) {
|
||||||
// Block for reversing a list.
|
// Block for reversing a list.
|
||||||
var list = Blockly.Lua.valueToCode(block, 'LIST',
|
const list = Blockly.Lua.valueToCode(block, 'LIST',
|
||||||
Blockly.Lua.ORDER_NONE) || '{}';
|
Blockly.Lua.ORDER_NONE) || '{}';
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
const functionName = Blockly.Lua.provideFunction_(
|
||||||
'list_reverse',
|
'list_reverse',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(input)',
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(input)',
|
||||||
' local reversed = {}',
|
' local reversed = {}',
|
||||||
@@ -362,6 +364,6 @@ Blockly.Lua['lists_reverse'] = function(block) {
|
|||||||
' end',
|
' end',
|
||||||
' return reversed',
|
' return reversed',
|
||||||
'end']);
|
'end']);
|
||||||
var code = functionName + '(' + list + ')';
|
const code = functionName + '(' + list + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -16,16 +16,16 @@ goog.require('Blockly.Lua');
|
|||||||
|
|
||||||
Blockly.Lua['controls_if'] = function(block) {
|
Blockly.Lua['controls_if'] = function(block) {
|
||||||
// If/elseif/else condition.
|
// If/elseif/else condition.
|
||||||
var n = 0;
|
let n = 0;
|
||||||
var code = '', branchCode, conditionCode;
|
let code = '';
|
||||||
if (Blockly.Lua.STATEMENT_PREFIX) {
|
if (Blockly.Lua.STATEMENT_PREFIX) {
|
||||||
// Automatic prefix insertion is switched off for this block. Add manually.
|
// Automatic prefix insertion is switched off for this block. Add manually.
|
||||||
code += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_PREFIX, block);
|
code += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_PREFIX, block);
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
conditionCode = Blockly.Lua.valueToCode(block, 'IF' + n,
|
const conditionCode = Blockly.Lua.valueToCode(block, 'IF' + n,
|
||||||
Blockly.Lua.ORDER_NONE) || 'false';
|
Blockly.Lua.ORDER_NONE) || 'false';
|
||||||
branchCode = Blockly.Lua.statementToCode(block, 'DO' + n);
|
let branchCode = Blockly.Lua.statementToCode(block, 'DO' + n);
|
||||||
if (Blockly.Lua.STATEMENT_SUFFIX) {
|
if (Blockly.Lua.STATEMENT_SUFFIX) {
|
||||||
branchCode = Blockly.Lua.prefixLines(
|
branchCode = Blockly.Lua.prefixLines(
|
||||||
Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block),
|
Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block),
|
||||||
@@ -37,7 +37,7 @@ Blockly.Lua['controls_if'] = function(block) {
|
|||||||
} while (block.getInput('IF' + n));
|
} while (block.getInput('IF' + n));
|
||||||
|
|
||||||
if (block.getInput('ELSE') || Blockly.Lua.STATEMENT_SUFFIX) {
|
if (block.getInput('ELSE') || Blockly.Lua.STATEMENT_SUFFIX) {
|
||||||
branchCode = Blockly.Lua.statementToCode(block, 'ELSE');
|
let branchCode = Blockly.Lua.statementToCode(block, 'ELSE');
|
||||||
if (Blockly.Lua.STATEMENT_SUFFIX) {
|
if (Blockly.Lua.STATEMENT_SUFFIX) {
|
||||||
branchCode = Blockly.Lua.prefixLines(
|
branchCode = Blockly.Lua.prefixLines(
|
||||||
Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block),
|
Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block),
|
||||||
@@ -52,7 +52,7 @@ Blockly.Lua['controls_ifelse'] = Blockly.Lua['controls_if'];
|
|||||||
|
|
||||||
Blockly.Lua['logic_compare'] = function(block) {
|
Blockly.Lua['logic_compare'] = function(block) {
|
||||||
// Comparison operator.
|
// Comparison operator.
|
||||||
var OPERATORS = {
|
const OPERATORS = {
|
||||||
'EQ': '==',
|
'EQ': '==',
|
||||||
'NEQ': '~=',
|
'NEQ': '~=',
|
||||||
'LT': '<',
|
'LT': '<',
|
||||||
@@ -60,29 +60,29 @@ Blockly.Lua['logic_compare'] = function(block) {
|
|||||||
'GT': '>',
|
'GT': '>',
|
||||||
'GTE': '>='
|
'GTE': '>='
|
||||||
};
|
};
|
||||||
var operator = OPERATORS[block.getFieldValue('OP')];
|
const operator = OPERATORS[block.getFieldValue('OP')];
|
||||||
var argument0 = Blockly.Lua.valueToCode(block, 'A',
|
const argument0 = Blockly.Lua.valueToCode(block, 'A',
|
||||||
Blockly.Lua.ORDER_RELATIONAL) || '0';
|
Blockly.Lua.ORDER_RELATIONAL) || '0';
|
||||||
var argument1 = Blockly.Lua.valueToCode(block, 'B',
|
const argument1 = Blockly.Lua.valueToCode(block, 'B',
|
||||||
Blockly.Lua.ORDER_RELATIONAL) || '0';
|
Blockly.Lua.ORDER_RELATIONAL) || '0';
|
||||||
var code = argument0 + ' ' + operator + ' ' + argument1;
|
const code = argument0 + ' ' + operator + ' ' + argument1;
|
||||||
return [code, Blockly.Lua.ORDER_RELATIONAL];
|
return [code, Blockly.Lua.ORDER_RELATIONAL];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['logic_operation'] = function(block) {
|
Blockly.Lua['logic_operation'] = function(block) {
|
||||||
// Operations 'and', 'or'.
|
// Operations 'and', 'or'.
|
||||||
var operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or';
|
const operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or';
|
||||||
var order = (operator === 'and') ? Blockly.Lua.ORDER_AND :
|
const order = (operator === 'and') ? Blockly.Lua.ORDER_AND :
|
||||||
Blockly.Lua.ORDER_OR;
|
Blockly.Lua.ORDER_OR;
|
||||||
var argument0 = Blockly.Lua.valueToCode(block, 'A', order);
|
let argument0 = Blockly.Lua.valueToCode(block, 'A', order);
|
||||||
var argument1 = Blockly.Lua.valueToCode(block, 'B', order);
|
let argument1 = Blockly.Lua.valueToCode(block, 'B', order);
|
||||||
if (!argument0 && !argument1) {
|
if (!argument0 && !argument1) {
|
||||||
// If there are no arguments, then the return value is false.
|
// If there are no arguments, then the return value is false.
|
||||||
argument0 = 'false';
|
argument0 = 'false';
|
||||||
argument1 = 'false';
|
argument1 = 'false';
|
||||||
} else {
|
} else {
|
||||||
// Single missing arguments have no effect on the return value.
|
// Single missing arguments have no effect on the return value.
|
||||||
var defaultArgument = (operator === 'and') ? 'true' : 'false';
|
const defaultArgument = (operator === 'and') ? 'true' : 'false';
|
||||||
if (!argument0) {
|
if (!argument0) {
|
||||||
argument0 = defaultArgument;
|
argument0 = defaultArgument;
|
||||||
}
|
}
|
||||||
@@ -90,21 +90,21 @@ Blockly.Lua['logic_operation'] = function(block) {
|
|||||||
argument1 = defaultArgument;
|
argument1 = defaultArgument;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var code = argument0 + ' ' + operator + ' ' + argument1;
|
const code = argument0 + ' ' + operator + ' ' + argument1;
|
||||||
return [code, order];
|
return [code, order];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['logic_negate'] = function(block) {
|
Blockly.Lua['logic_negate'] = function(block) {
|
||||||
// Negation.
|
// Negation.
|
||||||
var argument0 = Blockly.Lua.valueToCode(block, 'BOOL',
|
const argument0 = Blockly.Lua.valueToCode(block, 'BOOL',
|
||||||
Blockly.Lua.ORDER_UNARY) || 'true';
|
Blockly.Lua.ORDER_UNARY) || 'true';
|
||||||
var code = 'not ' + argument0;
|
const code = 'not ' + argument0;
|
||||||
return [code, Blockly.Lua.ORDER_UNARY];
|
return [code, Blockly.Lua.ORDER_UNARY];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['logic_boolean'] = function(block) {
|
Blockly.Lua['logic_boolean'] = function(block) {
|
||||||
// Boolean values true and false.
|
// Boolean values true and false.
|
||||||
var code = (block.getFieldValue('BOOL') === 'TRUE') ? 'true' : 'false';
|
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'true' : 'false';
|
||||||
return [code, Blockly.Lua.ORDER_ATOMIC];
|
return [code, Blockly.Lua.ORDER_ATOMIC];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -115,12 +115,12 @@ Blockly.Lua['logic_null'] = function(block) {
|
|||||||
|
|
||||||
Blockly.Lua['logic_ternary'] = function(block) {
|
Blockly.Lua['logic_ternary'] = function(block) {
|
||||||
// Ternary operator.
|
// Ternary operator.
|
||||||
var value_if = Blockly.Lua.valueToCode(block, 'IF',
|
const value_if = Blockly.Lua.valueToCode(block, 'IF',
|
||||||
Blockly.Lua.ORDER_AND) || 'false';
|
Blockly.Lua.ORDER_AND) || 'false';
|
||||||
var value_then = Blockly.Lua.valueToCode(block, 'THEN',
|
const value_then = Blockly.Lua.valueToCode(block, 'THEN',
|
||||||
Blockly.Lua.ORDER_AND) || 'nil';
|
Blockly.Lua.ORDER_AND) || 'nil';
|
||||||
var value_else = Blockly.Lua.valueToCode(block, 'ELSE',
|
const value_else = Blockly.Lua.valueToCode(block, 'ELSE',
|
||||||
Blockly.Lua.ORDER_OR) || 'nil';
|
Blockly.Lua.ORDER_OR) || 'nil';
|
||||||
var code = value_if + ' and ' + value_then + ' or ' + value_else;
|
const code = value_if + ' and ' + value_then + ' or ' + value_else;
|
||||||
return [code, Blockly.Lua.ORDER_OR];
|
return [code, Blockly.Lua.ORDER_OR];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -43,12 +43,13 @@ Blockly.Lua.addContinueLabel_ = function(branch) {
|
|||||||
|
|
||||||
Blockly.Lua['controls_repeat_ext'] = function(block) {
|
Blockly.Lua['controls_repeat_ext'] = function(block) {
|
||||||
// Repeat n times.
|
// Repeat n times.
|
||||||
|
let repeats;
|
||||||
if (block.getField('TIMES')) {
|
if (block.getField('TIMES')) {
|
||||||
// Internal number.
|
// Internal number.
|
||||||
var repeats = String(Number(block.getFieldValue('TIMES')));
|
repeats = String(Number(block.getFieldValue('TIMES')));
|
||||||
} else {
|
} else {
|
||||||
// External number.
|
// External number.
|
||||||
var repeats = Blockly.Lua.valueToCode(block, 'TIMES',
|
repeats = Blockly.Lua.valueToCode(block, 'TIMES',
|
||||||
Blockly.Lua.ORDER_NONE) || '0';
|
Blockly.Lua.ORDER_NONE) || '0';
|
||||||
}
|
}
|
||||||
if (Blockly.isNumber(repeats)) {
|
if (Blockly.isNumber(repeats)) {
|
||||||
@@ -56,12 +57,12 @@ Blockly.Lua['controls_repeat_ext'] = function(block) {
|
|||||||
} else {
|
} else {
|
||||||
repeats = 'math.floor(' + repeats + ')';
|
repeats = 'math.floor(' + repeats + ')';
|
||||||
}
|
}
|
||||||
var branch = Blockly.Lua.statementToCode(block, 'DO');
|
let branch = Blockly.Lua.statementToCode(block, 'DO');
|
||||||
branch = Blockly.Lua.addLoopTrap(branch, block);
|
branch = Blockly.Lua.addLoopTrap(branch, block);
|
||||||
branch = Blockly.Lua.addContinueLabel_(branch);
|
branch = Blockly.Lua.addContinueLabel_(branch);
|
||||||
var loopVar = Blockly.Lua.nameDB_.getDistinctName(
|
const loopVar = Blockly.Lua.nameDB_.getDistinctName(
|
||||||
'count', Blockly.VARIABLE_CATEGORY_NAME);
|
'count', Blockly.VARIABLE_CATEGORY_NAME);
|
||||||
var code = 'for ' + loopVar + ' = 1, ' + repeats + ' do\n' +
|
const code = 'for ' + loopVar + ' = 1, ' + repeats + ' do\n' +
|
||||||
branch + 'end\n';
|
branch + 'end\n';
|
||||||
return code;
|
return code;
|
||||||
};
|
};
|
||||||
@@ -70,11 +71,11 @@ Blockly.Lua['controls_repeat'] = Blockly.Lua['controls_repeat_ext'];
|
|||||||
|
|
||||||
Blockly.Lua['controls_whileUntil'] = function(block) {
|
Blockly.Lua['controls_whileUntil'] = function(block) {
|
||||||
// Do while/until loop.
|
// Do while/until loop.
|
||||||
var until = block.getFieldValue('MODE') === 'UNTIL';
|
const until = block.getFieldValue('MODE') === 'UNTIL';
|
||||||
var argument0 = Blockly.Lua.valueToCode(block, 'BOOL',
|
let argument0 = Blockly.Lua.valueToCode(block, 'BOOL',
|
||||||
until ? Blockly.Lua.ORDER_UNARY :
|
until ? Blockly.Lua.ORDER_UNARY :
|
||||||
Blockly.Lua.ORDER_NONE) || 'false';
|
Blockly.Lua.ORDER_NONE) || 'false';
|
||||||
var branch = Blockly.Lua.statementToCode(block, 'DO');
|
let branch = Blockly.Lua.statementToCode(block, 'DO');
|
||||||
branch = Blockly.Lua.addLoopTrap(branch, block);
|
branch = Blockly.Lua.addLoopTrap(branch, block);
|
||||||
branch = Blockly.Lua.addContinueLabel_(branch);
|
branch = Blockly.Lua.addContinueLabel_(branch);
|
||||||
if (until) {
|
if (until) {
|
||||||
@@ -85,24 +86,24 @@ Blockly.Lua['controls_whileUntil'] = function(block) {
|
|||||||
|
|
||||||
Blockly.Lua['controls_for'] = function(block) {
|
Blockly.Lua['controls_for'] = function(block) {
|
||||||
// For loop.
|
// For loop.
|
||||||
var variable0 = Blockly.Lua.nameDB_.getName(
|
const variable0 = Blockly.Lua.nameDB_.getName(
|
||||||
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
|
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
|
||||||
var startVar = Blockly.Lua.valueToCode(block, 'FROM',
|
const startVar = Blockly.Lua.valueToCode(block, 'FROM',
|
||||||
Blockly.Lua.ORDER_NONE) || '0';
|
Blockly.Lua.ORDER_NONE) || '0';
|
||||||
var endVar = Blockly.Lua.valueToCode(block, 'TO',
|
const endVar = Blockly.Lua.valueToCode(block, 'TO',
|
||||||
Blockly.Lua.ORDER_NONE) || '0';
|
Blockly.Lua.ORDER_NONE) || '0';
|
||||||
var increment = Blockly.Lua.valueToCode(block, 'BY',
|
const increment = Blockly.Lua.valueToCode(block, 'BY',
|
||||||
Blockly.Lua.ORDER_NONE) || '1';
|
Blockly.Lua.ORDER_NONE) || '1';
|
||||||
var branch = Blockly.Lua.statementToCode(block, 'DO');
|
let branch = Blockly.Lua.statementToCode(block, 'DO');
|
||||||
branch = Blockly.Lua.addLoopTrap(branch, block);
|
branch = Blockly.Lua.addLoopTrap(branch, block);
|
||||||
branch = Blockly.Lua.addContinueLabel_(branch);
|
branch = Blockly.Lua.addContinueLabel_(branch);
|
||||||
var code = '';
|
let code = '';
|
||||||
var incValue;
|
let incValue;
|
||||||
if (Blockly.isNumber(startVar) && Blockly.isNumber(endVar) &&
|
if (Blockly.isNumber(startVar) && Blockly.isNumber(endVar) &&
|
||||||
Blockly.isNumber(increment)) {
|
Blockly.isNumber(increment)) {
|
||||||
// All arguments are simple numbers.
|
// All arguments are simple numbers.
|
||||||
var up = Number(startVar) <= Number(endVar);
|
const up = Number(startVar) <= Number(endVar);
|
||||||
var step = Math.abs(Number(increment));
|
const step = Math.abs(Number(increment));
|
||||||
incValue = (up ? '' : '-') + step;
|
incValue = (up ? '' : '-') + step;
|
||||||
} else {
|
} else {
|
||||||
code = '';
|
code = '';
|
||||||
@@ -128,21 +129,21 @@ Blockly.Lua['controls_for'] = function(block) {
|
|||||||
|
|
||||||
Blockly.Lua['controls_forEach'] = function(block) {
|
Blockly.Lua['controls_forEach'] = function(block) {
|
||||||
// For each loop.
|
// For each loop.
|
||||||
var variable0 = Blockly.Lua.nameDB_.getName(
|
const variable0 = Blockly.Lua.nameDB_.getName(
|
||||||
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
|
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
|
||||||
var argument0 = Blockly.Lua.valueToCode(block, 'LIST',
|
const argument0 = Blockly.Lua.valueToCode(block, 'LIST',
|
||||||
Blockly.Lua.ORDER_NONE) || '{}';
|
Blockly.Lua.ORDER_NONE) || '{}';
|
||||||
var branch = Blockly.Lua.statementToCode(block, 'DO');
|
let branch = Blockly.Lua.statementToCode(block, 'DO');
|
||||||
branch = Blockly.Lua.addLoopTrap(branch, block);
|
branch = Blockly.Lua.addLoopTrap(branch, block);
|
||||||
branch = Blockly.Lua.addContinueLabel_(branch);
|
branch = Blockly.Lua.addContinueLabel_(branch);
|
||||||
var code = 'for _, ' + variable0 + ' in ipairs(' + argument0 + ') do \n' +
|
const code = 'for _, ' + variable0 + ' in ipairs(' + argument0 + ') do \n' +
|
||||||
branch + 'end\n';
|
branch + 'end\n';
|
||||||
return code;
|
return code;
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['controls_flow_statements'] = function(block) {
|
Blockly.Lua['controls_flow_statements'] = function(block) {
|
||||||
// Flow statements: continue, break.
|
// Flow statements: continue, break.
|
||||||
var xfix = '';
|
let xfix = '';
|
||||||
if (Blockly.Lua.STATEMENT_PREFIX) {
|
if (Blockly.Lua.STATEMENT_PREFIX) {
|
||||||
// Automatic prefix insertion is switched off for this block. Add manually.
|
// Automatic prefix insertion is switched off for this block. Add manually.
|
||||||
xfix += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_PREFIX, block);
|
xfix += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_PREFIX, block);
|
||||||
@@ -153,7 +154,7 @@ Blockly.Lua['controls_flow_statements'] = function(block) {
|
|||||||
xfix += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block);
|
xfix += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block);
|
||||||
}
|
}
|
||||||
if (Blockly.Lua.STATEMENT_PREFIX) {
|
if (Blockly.Lua.STATEMENT_PREFIX) {
|
||||||
var loop = Blockly.Constants.Loops
|
const loop = Blockly.Constants.Loops
|
||||||
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
|
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
|
||||||
if (loop && !loop.suppressPrefixSuffix) {
|
if (loop && !loop.suppressPrefixSuffix) {
|
||||||
// Inject loop's statement prefix here since the regular one at the end
|
// Inject loop's statement prefix here since the regular one at the end
|
||||||
|
|||||||
@@ -16,35 +16,34 @@ goog.require('Blockly.Lua');
|
|||||||
|
|
||||||
Blockly.Lua['math_number'] = function(block) {
|
Blockly.Lua['math_number'] = function(block) {
|
||||||
// Numeric value.
|
// Numeric value.
|
||||||
var code = Number(block.getFieldValue('NUM'));
|
const code = Number(block.getFieldValue('NUM'));
|
||||||
var order = code < 0 ? Blockly.Lua.ORDER_UNARY :
|
const order = code < 0 ? Blockly.Lua.ORDER_UNARY :
|
||||||
Blockly.Lua.ORDER_ATOMIC;
|
Blockly.Lua.ORDER_ATOMIC;
|
||||||
return [code, order];
|
return [code, order];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['math_arithmetic'] = function(block) {
|
Blockly.Lua['math_arithmetic'] = function(block) {
|
||||||
// Basic arithmetic operators, and power.
|
// Basic arithmetic operators, and power.
|
||||||
var OPERATORS = {
|
const OPERATORS = {
|
||||||
ADD: [' + ', Blockly.Lua.ORDER_ADDITIVE],
|
ADD: [' + ', Blockly.Lua.ORDER_ADDITIVE],
|
||||||
MINUS: [' - ', Blockly.Lua.ORDER_ADDITIVE],
|
MINUS: [' - ', Blockly.Lua.ORDER_ADDITIVE],
|
||||||
MULTIPLY: [' * ', Blockly.Lua.ORDER_MULTIPLICATIVE],
|
MULTIPLY: [' * ', Blockly.Lua.ORDER_MULTIPLICATIVE],
|
||||||
DIVIDE: [' / ', Blockly.Lua.ORDER_MULTIPLICATIVE],
|
DIVIDE: [' / ', Blockly.Lua.ORDER_MULTIPLICATIVE],
|
||||||
POWER: [' ^ ', Blockly.Lua.ORDER_EXPONENTIATION]
|
POWER: [' ^ ', Blockly.Lua.ORDER_EXPONENTIATION]
|
||||||
};
|
};
|
||||||
var tuple = OPERATORS[block.getFieldValue('OP')];
|
const tuple = OPERATORS[block.getFieldValue('OP')];
|
||||||
var operator = tuple[0];
|
const operator = tuple[0];
|
||||||
var order = tuple[1];
|
const order = tuple[1];
|
||||||
var argument0 = Blockly.Lua.valueToCode(block, 'A', order) || '0';
|
const argument0 = Blockly.Lua.valueToCode(block, 'A', order) || '0';
|
||||||
var argument1 = Blockly.Lua.valueToCode(block, 'B', order) || '0';
|
const argument1 = Blockly.Lua.valueToCode(block, 'B', order) || '0';
|
||||||
var code = argument0 + operator + argument1;
|
const code = argument0 + operator + argument1;
|
||||||
return [code, order];
|
return [code, order];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['math_single'] = function(block) {
|
Blockly.Lua['math_single'] = function(block) {
|
||||||
// Math operators with single operand.
|
// Math operators with single operand.
|
||||||
var operator = block.getFieldValue('OP');
|
const operator = block.getFieldValue('OP');
|
||||||
var code;
|
let arg;
|
||||||
var arg;
|
|
||||||
if (operator === 'NEG') {
|
if (operator === 'NEG') {
|
||||||
// Negation is a special case given its different operator precedence.
|
// Negation is a special case given its different operator precedence.
|
||||||
arg = Blockly.Lua.valueToCode(block, 'NUM',
|
arg = Blockly.Lua.valueToCode(block, 'NUM',
|
||||||
@@ -63,6 +62,8 @@ Blockly.Lua['math_single'] = function(block) {
|
|||||||
arg = Blockly.Lua.valueToCode(block, 'NUM',
|
arg = Blockly.Lua.valueToCode(block, 'NUM',
|
||||||
Blockly.Lua.ORDER_NONE) || '0';
|
Blockly.Lua.ORDER_NONE) || '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let code;
|
||||||
switch (operator) {
|
switch (operator) {
|
||||||
case 'ABS':
|
case 'ABS':
|
||||||
code = 'math.abs(' + arg + ')';
|
code = 'math.abs(' + arg + ')';
|
||||||
@@ -115,7 +116,7 @@ Blockly.Lua['math_single'] = function(block) {
|
|||||||
|
|
||||||
Blockly.Lua['math_constant'] = function(block) {
|
Blockly.Lua['math_constant'] = function(block) {
|
||||||
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
|
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
|
||||||
var CONSTANTS = {
|
const CONSTANTS = {
|
||||||
PI: ['math.pi', Blockly.Lua.ORDER_HIGH],
|
PI: ['math.pi', Blockly.Lua.ORDER_HIGH],
|
||||||
E: ['math.exp(1)', Blockly.Lua.ORDER_HIGH],
|
E: ['math.exp(1)', Blockly.Lua.ORDER_HIGH],
|
||||||
GOLDEN_RATIO: ['(1 + math.sqrt(5)) / 2', Blockly.Lua.ORDER_MULTIPLICATIVE],
|
GOLDEN_RATIO: ['(1 + math.sqrt(5)) / 2', Blockly.Lua.ORDER_MULTIPLICATIVE],
|
||||||
@@ -129,13 +130,13 @@ Blockly.Lua['math_constant'] = function(block) {
|
|||||||
Blockly.Lua['math_number_property'] = function(block) {
|
Blockly.Lua['math_number_property'] = function(block) {
|
||||||
// Check if a number is even, odd, prime, whole, positive, or negative
|
// Check if a number is even, odd, prime, whole, positive, or negative
|
||||||
// or if it is divisible by certain number. Returns true or false.
|
// or if it is divisible by certain number. Returns true or false.
|
||||||
var number_to_check = Blockly.Lua.valueToCode(block, 'NUMBER_TO_CHECK',
|
const number_to_check = Blockly.Lua.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||||
Blockly.Lua.ORDER_MULTIPLICATIVE) || '0';
|
Blockly.Lua.ORDER_MULTIPLICATIVE) || '0';
|
||||||
var dropdown_property = block.getFieldValue('PROPERTY');
|
const dropdown_property = block.getFieldValue('PROPERTY');
|
||||||
var code;
|
let code;
|
||||||
if (dropdown_property === 'PRIME') {
|
if (dropdown_property === 'PRIME') {
|
||||||
// Prime is a special case as it is not a one-liner test.
|
// Prime is a special case as it is not a one-liner test.
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
const functionName = Blockly.Lua.provideFunction_(
|
||||||
'math_isPrime',
|
'math_isPrime',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(n)',
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(n)',
|
||||||
' -- https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
' -- https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||||
@@ -174,8 +175,8 @@ Blockly.Lua['math_number_property'] = function(block) {
|
|||||||
case 'NEGATIVE':
|
case 'NEGATIVE':
|
||||||
code = number_to_check + ' < 0';
|
code = number_to_check + ' < 0';
|
||||||
break;
|
break;
|
||||||
case 'DIVISIBLE_BY':
|
case 'DIVISIBLE_BY': {
|
||||||
var divisor = Blockly.Lua.valueToCode(block, 'DIVISOR',
|
const divisor = Blockly.Lua.valueToCode(block, 'DIVISOR',
|
||||||
Blockly.Lua.ORDER_MULTIPLICATIVE);
|
Blockly.Lua.ORDER_MULTIPLICATIVE);
|
||||||
// If 'divisor' is some code that evals to 0, Lua will produce a nan.
|
// If 'divisor' is some code that evals to 0, Lua will produce a nan.
|
||||||
// Let's produce nil if we can determine this at compile-time.
|
// Let's produce nil if we can determine this at compile-time.
|
||||||
@@ -187,15 +188,16 @@ Blockly.Lua['math_number_property'] = function(block) {
|
|||||||
// because nil is false, so allow a runtime failure. :-(
|
// because nil is false, so allow a runtime failure. :-(
|
||||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return [code, Blockly.Lua.ORDER_RELATIONAL];
|
return [code, Blockly.Lua.ORDER_RELATIONAL];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['math_change'] = function(block) {
|
Blockly.Lua['math_change'] = function(block) {
|
||||||
// Add to a variable in place.
|
// Add to a variable in place.
|
||||||
var argument0 = Blockly.Lua.valueToCode(block, 'DELTA',
|
const argument0 = Blockly.Lua.valueToCode(block, 'DELTA',
|
||||||
Blockly.Lua.ORDER_ADDITIVE) || '0';
|
Blockly.Lua.ORDER_ADDITIVE) || '0';
|
||||||
var varName = Blockly.Lua.nameDB_.getName(
|
const varName = Blockly.Lua.nameDB_.getName(
|
||||||
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
|
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
|
||||||
return varName + ' = ' + varName + ' + ' + argument0 + '\n';
|
return varName + ' = ' + varName + ' + ' + argument0 + '\n';
|
||||||
};
|
};
|
||||||
@@ -207,10 +209,10 @@ Blockly.Lua['math_trig'] = Blockly.Lua['math_single'];
|
|||||||
|
|
||||||
Blockly.Lua['math_on_list'] = function(block) {
|
Blockly.Lua['math_on_list'] = function(block) {
|
||||||
// Math functions for lists.
|
// Math functions for lists.
|
||||||
var func = block.getFieldValue('OP');
|
const func = block.getFieldValue('OP');
|
||||||
var list = Blockly.Lua.valueToCode(block, 'LIST',
|
const list = Blockly.Lua.valueToCode(block, 'LIST',
|
||||||
Blockly.Lua.ORDER_NONE) || '{}';
|
Blockly.Lua.ORDER_NONE) || '{}';
|
||||||
var functionName;
|
let functionName;
|
||||||
|
|
||||||
// Functions needed in more than one case.
|
// Functions needed in more than one case.
|
||||||
function provideSum() {
|
function provideSum() {
|
||||||
@@ -375,34 +377,34 @@ Blockly.Lua['math_on_list'] = function(block) {
|
|||||||
|
|
||||||
Blockly.Lua['math_modulo'] = function(block) {
|
Blockly.Lua['math_modulo'] = function(block) {
|
||||||
// Remainder computation.
|
// Remainder computation.
|
||||||
var argument0 = Blockly.Lua.valueToCode(block, 'DIVIDEND',
|
const argument0 = Blockly.Lua.valueToCode(block, 'DIVIDEND',
|
||||||
Blockly.Lua.ORDER_MULTIPLICATIVE) || '0';
|
Blockly.Lua.ORDER_MULTIPLICATIVE) || '0';
|
||||||
var argument1 = Blockly.Lua.valueToCode(block, 'DIVISOR',
|
const argument1 = Blockly.Lua.valueToCode(block, 'DIVISOR',
|
||||||
Blockly.Lua.ORDER_MULTIPLICATIVE) || '0';
|
Blockly.Lua.ORDER_MULTIPLICATIVE) || '0';
|
||||||
var code = argument0 + ' % ' + argument1;
|
const code = argument0 + ' % ' + argument1;
|
||||||
return [code, Blockly.Lua.ORDER_MULTIPLICATIVE];
|
return [code, Blockly.Lua.ORDER_MULTIPLICATIVE];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['math_constrain'] = function(block) {
|
Blockly.Lua['math_constrain'] = function(block) {
|
||||||
// Constrain a number between two limits.
|
// Constrain a number between two limits.
|
||||||
var argument0 = Blockly.Lua.valueToCode(block, 'VALUE',
|
const argument0 = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||||
Blockly.Lua.ORDER_NONE) || '0';
|
Blockly.Lua.ORDER_NONE) || '0';
|
||||||
var argument1 = Blockly.Lua.valueToCode(block, 'LOW',
|
const argument1 = Blockly.Lua.valueToCode(block, 'LOW',
|
||||||
Blockly.Lua.ORDER_NONE) || '-math.huge';
|
Blockly.Lua.ORDER_NONE) || '-math.huge';
|
||||||
var argument2 = Blockly.Lua.valueToCode(block, 'HIGH',
|
const argument2 = Blockly.Lua.valueToCode(block, 'HIGH',
|
||||||
Blockly.Lua.ORDER_NONE) || 'math.huge';
|
Blockly.Lua.ORDER_NONE) || 'math.huge';
|
||||||
var code = 'math.min(math.max(' + argument0 + ', ' + argument1 + '), ' +
|
const code = 'math.min(math.max(' + argument0 + ', ' + argument1 + '), ' +
|
||||||
argument2 + ')';
|
argument2 + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['math_random_int'] = function(block) {
|
Blockly.Lua['math_random_int'] = function(block) {
|
||||||
// Random integer between [X] and [Y].
|
// Random integer between [X] and [Y].
|
||||||
var argument0 = Blockly.Lua.valueToCode(block, 'FROM',
|
const argument0 = Blockly.Lua.valueToCode(block, 'FROM',
|
||||||
Blockly.Lua.ORDER_NONE) || '0';
|
Blockly.Lua.ORDER_NONE) || '0';
|
||||||
var argument1 = Blockly.Lua.valueToCode(block, 'TO',
|
const argument1 = Blockly.Lua.valueToCode(block, 'TO',
|
||||||
Blockly.Lua.ORDER_NONE) || '0';
|
Blockly.Lua.ORDER_NONE) || '0';
|
||||||
var code = 'math.random(' + argument0 + ', ' + argument1 + ')';
|
const code = 'math.random(' + argument0 + ', ' + argument1 + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -413,9 +415,9 @@ Blockly.Lua['math_random_float'] = function(block) {
|
|||||||
|
|
||||||
Blockly.Lua['math_atan2'] = function(block) {
|
Blockly.Lua['math_atan2'] = function(block) {
|
||||||
// Arctangent of point (X, Y) in degrees from -180 to 180.
|
// Arctangent of point (X, Y) in degrees from -180 to 180.
|
||||||
var argument0 = Blockly.Lua.valueToCode(block, 'X',
|
const argument0 = Blockly.Lua.valueToCode(block, 'X',
|
||||||
Blockly.Lua.ORDER_NONE) || '0';
|
Blockly.Lua.ORDER_NONE) || '0';
|
||||||
var argument1 = Blockly.Lua.valueToCode(block, 'Y',
|
const argument1 = Blockly.Lua.valueToCode(block, 'Y',
|
||||||
Blockly.Lua.ORDER_NONE) || '0';
|
Blockly.Lua.ORDER_NONE) || '0';
|
||||||
return ['math.deg(math.atan2(' + argument1 + ', ' + argument0 + '))',
|
return ['math.deg(math.atan2(' + argument1 + ', ' + argument0 + '))',
|
||||||
Blockly.Lua.ORDER_HIGH];
|
Blockly.Lua.ORDER_HIGH];
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ goog.require('Blockly.Lua');
|
|||||||
|
|
||||||
Blockly.Lua['procedures_defreturn'] = function(block) {
|
Blockly.Lua['procedures_defreturn'] = function(block) {
|
||||||
// Define a procedure with a return value.
|
// Define a procedure with a return value.
|
||||||
var funcName = Blockly.Lua.nameDB_.getName(
|
const funcName = Blockly.Lua.nameDB_.getName(
|
||||||
block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
|
block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
|
||||||
var xfix1 = '';
|
let xfix1 = '';
|
||||||
if (Blockly.Lua.STATEMENT_PREFIX) {
|
if (Blockly.Lua.STATEMENT_PREFIX) {
|
||||||
xfix1 += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_PREFIX, block);
|
xfix1 += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_PREFIX, block);
|
||||||
}
|
}
|
||||||
@@ -28,16 +28,16 @@ Blockly.Lua['procedures_defreturn'] = function(block) {
|
|||||||
if (xfix1) {
|
if (xfix1) {
|
||||||
xfix1 = Blockly.Lua.prefixLines(xfix1, Blockly.Lua.INDENT);
|
xfix1 = Blockly.Lua.prefixLines(xfix1, Blockly.Lua.INDENT);
|
||||||
}
|
}
|
||||||
var loopTrap = '';
|
let loopTrap = '';
|
||||||
if (Blockly.Lua.INFINITE_LOOP_TRAP) {
|
if (Blockly.Lua.INFINITE_LOOP_TRAP) {
|
||||||
loopTrap = Blockly.Lua.prefixLines(
|
loopTrap = Blockly.Lua.prefixLines(
|
||||||
Blockly.Lua.injectId(Blockly.Lua.INFINITE_LOOP_TRAP, block),
|
Blockly.Lua.injectId(Blockly.Lua.INFINITE_LOOP_TRAP, block),
|
||||||
Blockly.Lua.INDENT);
|
Blockly.Lua.INDENT);
|
||||||
}
|
}
|
||||||
var branch = Blockly.Lua.statementToCode(block, 'STACK');
|
let branch = Blockly.Lua.statementToCode(block, 'STACK');
|
||||||
var returnValue = Blockly.Lua.valueToCode(block, 'RETURN',
|
let returnValue = Blockly.Lua.valueToCode(block, 'RETURN',
|
||||||
Blockly.Lua.ORDER_NONE) || '';
|
Blockly.Lua.ORDER_NONE) || '';
|
||||||
var xfix2 = '';
|
let xfix2 = '';
|
||||||
if (branch && returnValue) {
|
if (branch && returnValue) {
|
||||||
// After executing the function body, revisit this block for the return.
|
// After executing the function body, revisit this block for the return.
|
||||||
xfix2 = xfix1;
|
xfix2 = xfix1;
|
||||||
@@ -47,13 +47,13 @@ Blockly.Lua['procedures_defreturn'] = function(block) {
|
|||||||
} else if (!branch) {
|
} else if (!branch) {
|
||||||
branch = '';
|
branch = '';
|
||||||
}
|
}
|
||||||
var args = [];
|
const args = [];
|
||||||
var variables = block.getVars();
|
const variables = block.getVars();
|
||||||
for (var i = 0; i < variables.length; i++) {
|
for (let i = 0; i < variables.length; i++) {
|
||||||
args[i] = Blockly.Lua.nameDB_.getName(variables[i],
|
args[i] = Blockly.Lua.nameDB_.getName(variables[i],
|
||||||
Blockly.VARIABLE_CATEGORY_NAME);
|
Blockly.VARIABLE_CATEGORY_NAME);
|
||||||
}
|
}
|
||||||
var code = 'function ' + funcName + '(' + args.join(', ') + ')\n' +
|
let code = 'function ' + funcName + '(' + args.join(', ') + ')\n' +
|
||||||
xfix1 + loopTrap + branch + xfix2 + returnValue + 'end\n';
|
xfix1 + loopTrap + branch + xfix2 + returnValue + 'end\n';
|
||||||
code = Blockly.Lua.scrub_(block, code);
|
code = Blockly.Lua.scrub_(block, code);
|
||||||
// Add % so as not to collide with helper functions in definitions list.
|
// Add % so as not to collide with helper functions in definitions list.
|
||||||
@@ -68,15 +68,15 @@ Blockly.Lua['procedures_defnoreturn'] =
|
|||||||
|
|
||||||
Blockly.Lua['procedures_callreturn'] = function(block) {
|
Blockly.Lua['procedures_callreturn'] = function(block) {
|
||||||
// Call a procedure with a return value.
|
// Call a procedure with a return value.
|
||||||
var funcName = Blockly.Lua.nameDB_.getName(
|
const funcName = Blockly.Lua.nameDB_.getName(
|
||||||
block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
|
block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
|
||||||
var args = [];
|
const args = [];
|
||||||
var variables = block.getVars();
|
const variables = block.getVars();
|
||||||
for (var i = 0; i < variables.length; i++) {
|
for (let i = 0; i < variables.length; i++) {
|
||||||
args[i] = Blockly.Lua.valueToCode(block, 'ARG' + i,
|
args[i] = Blockly.Lua.valueToCode(block, 'ARG' + i,
|
||||||
Blockly.Lua.ORDER_NONE) || 'nil';
|
Blockly.Lua.ORDER_NONE) || 'nil';
|
||||||
}
|
}
|
||||||
var code = funcName + '(' + args.join(', ') + ')';
|
const code = funcName + '(' + args.join(', ') + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -84,15 +84,15 @@ Blockly.Lua['procedures_callnoreturn'] = function(block) {
|
|||||||
// Call a procedure with no return value.
|
// Call a procedure with no return value.
|
||||||
// Generated code is for a function call as a statement is the same as a
|
// Generated code is for a function call as a statement is the same as a
|
||||||
// function call as a value, with the addition of line ending.
|
// function call as a value, with the addition of line ending.
|
||||||
var tuple = Blockly.Lua['procedures_callreturn'](block);
|
const tuple = Blockly.Lua['procedures_callreturn'](block);
|
||||||
return tuple[0] + '\n';
|
return tuple[0] + '\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['procedures_ifreturn'] = function(block) {
|
Blockly.Lua['procedures_ifreturn'] = function(block) {
|
||||||
// Conditionally return value from a procedure.
|
// Conditionally return value from a procedure.
|
||||||
var condition = Blockly.Lua.valueToCode(block, 'CONDITION',
|
const condition = Blockly.Lua.valueToCode(block, 'CONDITION',
|
||||||
Blockly.Lua.ORDER_NONE) || 'false';
|
Blockly.Lua.ORDER_NONE) || 'false';
|
||||||
var code = 'if ' + condition + ' then\n';
|
let code = 'if ' + condition + ' then\n';
|
||||||
if (Blockly.Lua.STATEMENT_SUFFIX) {
|
if (Blockly.Lua.STATEMENT_SUFFIX) {
|
||||||
// Inject any statement suffix here since the regular one at the end
|
// Inject any statement suffix here since the regular one at the end
|
||||||
// will not get executed if the return is triggered.
|
// will not get executed if the return is triggered.
|
||||||
@@ -101,7 +101,7 @@ Blockly.Lua['procedures_ifreturn'] = function(block) {
|
|||||||
Blockly.Lua.INDENT);
|
Blockly.Lua.INDENT);
|
||||||
}
|
}
|
||||||
if (block.hasReturnValue_) {
|
if (block.hasReturnValue_) {
|
||||||
var value = Blockly.Lua.valueToCode(block, 'VALUE',
|
const value = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||||
Blockly.Lua.ORDER_NONE) || 'nil';
|
Blockly.Lua.ORDER_NONE) || 'nil';
|
||||||
code += Blockly.Lua.INDENT + 'return ' + value + '\n';
|
code += Blockly.Lua.INDENT + 'return ' + value + '\n';
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -16,14 +16,14 @@ goog.require('Blockly.Lua');
|
|||||||
|
|
||||||
Blockly.Lua['text'] = function(block) {
|
Blockly.Lua['text'] = function(block) {
|
||||||
// Text value.
|
// Text value.
|
||||||
var code = Blockly.Lua.quote_(block.getFieldValue('TEXT'));
|
const code = Blockly.Lua.quote_(block.getFieldValue('TEXT'));
|
||||||
return [code, Blockly.Lua.ORDER_ATOMIC];
|
return [code, Blockly.Lua.ORDER_ATOMIC];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['text_multiline'] = function(block) {
|
Blockly.Lua['text_multiline'] = function(block) {
|
||||||
// Text value.
|
// Text value.
|
||||||
var code = Blockly.Lua.multiline_quote_(block.getFieldValue('TEXT'));
|
const code = Blockly.Lua.multiline_quote_(block.getFieldValue('TEXT'));
|
||||||
var order = code.indexOf('..') !== -1 ? Blockly.Lua.ORDER_CONCATENATION :
|
const order = code.indexOf('..') !== -1 ? Blockly.Lua.ORDER_CONCATENATION :
|
||||||
Blockly.Lua.ORDER_ATOMIC;
|
Blockly.Lua.ORDER_ATOMIC;
|
||||||
return [code, order];
|
return [code, order];
|
||||||
};
|
};
|
||||||
@@ -33,59 +33,60 @@ Blockly.Lua['text_join'] = function(block) {
|
|||||||
if (block.itemCount_ === 0) {
|
if (block.itemCount_ === 0) {
|
||||||
return ['\'\'', Blockly.Lua.ORDER_ATOMIC];
|
return ['\'\'', Blockly.Lua.ORDER_ATOMIC];
|
||||||
} else if (block.itemCount_ === 1) {
|
} else if (block.itemCount_ === 1) {
|
||||||
var element = Blockly.Lua.valueToCode(block, 'ADD0',
|
const element = Blockly.Lua.valueToCode(block, 'ADD0',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
var code = 'tostring(' + element + ')';
|
const code = 'tostring(' + element + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
} else if (block.itemCount_ === 2) {
|
} else if (block.itemCount_ === 2) {
|
||||||
var element0 = Blockly.Lua.valueToCode(block, 'ADD0',
|
const element0 = Blockly.Lua.valueToCode(block, 'ADD0',
|
||||||
Blockly.Lua.ORDER_CONCATENATION) || '\'\'';
|
Blockly.Lua.ORDER_CONCATENATION) || '\'\'';
|
||||||
var element1 = Blockly.Lua.valueToCode(block, 'ADD1',
|
const element1 = Blockly.Lua.valueToCode(block, 'ADD1',
|
||||||
Blockly.Lua.ORDER_CONCATENATION) || '\'\'';
|
Blockly.Lua.ORDER_CONCATENATION) || '\'\'';
|
||||||
var code = element0 + ' .. ' + element1;
|
const code = element0 + ' .. ' + element1;
|
||||||
return [code, Blockly.Lua.ORDER_CONCATENATION];
|
return [code, Blockly.Lua.ORDER_CONCATENATION];
|
||||||
} else {
|
} else {
|
||||||
var elements = [];
|
const elements = [];
|
||||||
for (var i = 0; i < block.itemCount_; i++) {
|
for (let i = 0; i < block.itemCount_; i++) {
|
||||||
elements[i] = Blockly.Lua.valueToCode(block, 'ADD' + i,
|
elements[i] = Blockly.Lua.valueToCode(block, 'ADD' + i,
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
}
|
}
|
||||||
var code = 'table.concat({' + elements.join(', ') + '})';
|
const code = 'table.concat({' + elements.join(', ') + '})';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['text_append'] = function(block) {
|
Blockly.Lua['text_append'] = function(block) {
|
||||||
// Append to a variable in place.
|
// Append to a variable in place.
|
||||||
var varName = Blockly.Lua.nameDB_.getName(
|
const varName = Blockly.Lua.nameDB_.getName(
|
||||||
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
|
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
|
||||||
var value = Blockly.Lua.valueToCode(block, 'TEXT',
|
const value = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||||
Blockly.Lua.ORDER_CONCATENATION) || '\'\'';
|
Blockly.Lua.ORDER_CONCATENATION) || '\'\'';
|
||||||
return varName + ' = ' + varName + ' .. ' + value + '\n';
|
return varName + ' = ' + varName + ' .. ' + value + '\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['text_length'] = function(block) {
|
Blockly.Lua['text_length'] = function(block) {
|
||||||
// String or array length.
|
// String or array length.
|
||||||
var text = Blockly.Lua.valueToCode(block, 'VALUE',
|
const text = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||||
Blockly.Lua.ORDER_UNARY) || '\'\'';
|
Blockly.Lua.ORDER_UNARY) || '\'\'';
|
||||||
return ['#' + text, Blockly.Lua.ORDER_UNARY];
|
return ['#' + text, Blockly.Lua.ORDER_UNARY];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['text_isEmpty'] = function(block) {
|
Blockly.Lua['text_isEmpty'] = function(block) {
|
||||||
// Is the string null or array empty?
|
// Is the string null or array empty?
|
||||||
var text = Blockly.Lua.valueToCode(block, 'VALUE',
|
const text = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||||
Blockly.Lua.ORDER_UNARY) || '\'\'';
|
Blockly.Lua.ORDER_UNARY) || '\'\'';
|
||||||
return ['#' + text + ' == 0', Blockly.Lua.ORDER_RELATIONAL];
|
return ['#' + text + ' == 0', Blockly.Lua.ORDER_RELATIONAL];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['text_indexOf'] = function(block) {
|
Blockly.Lua['text_indexOf'] = function(block) {
|
||||||
// Search the text for a substring.
|
// Search the text for a substring.
|
||||||
var substring = Blockly.Lua.valueToCode(block, 'FIND',
|
const substring = Blockly.Lua.valueToCode(block, 'FIND',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
var text = Blockly.Lua.valueToCode(block, 'VALUE',
|
const text = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
|
let functionName;
|
||||||
if (block.getFieldValue('END') === 'FIRST') {
|
if (block.getFieldValue('END') === 'FIRST') {
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
functionName = Blockly.Lua.provideFunction_(
|
||||||
'firstIndexOf',
|
'firstIndexOf',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
'(str, substr) ',
|
'(str, substr) ',
|
||||||
@@ -97,7 +98,7 @@ Blockly.Lua['text_indexOf'] = function(block) {
|
|||||||
' end',
|
' end',
|
||||||
'end']);
|
'end']);
|
||||||
} else {
|
} else {
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
functionName = Blockly.Lua.provideFunction_(
|
||||||
'lastIndexOf',
|
'lastIndexOf',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
'(str, substr)',
|
'(str, substr)',
|
||||||
@@ -109,22 +110,22 @@ Blockly.Lua['text_indexOf'] = function(block) {
|
|||||||
' return 0',
|
' return 0',
|
||||||
'end']);
|
'end']);
|
||||||
}
|
}
|
||||||
var code = functionName + '(' + text + ', ' + substring + ')';
|
const code = functionName + '(' + text + ', ' + substring + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['text_charAt'] = function(block) {
|
Blockly.Lua['text_charAt'] = function(block) {
|
||||||
// Get letter at index.
|
// Get letter at index.
|
||||||
// Note: Until January 2013 this block did not have the WHERE input.
|
// Note: Until January 2013 this block did not have the WHERE input.
|
||||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||||
var atOrder = (where === 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
|
const atOrder = (where === 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
|
||||||
Blockly.Lua.ORDER_NONE;
|
Blockly.Lua.ORDER_NONE;
|
||||||
var at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
|
const at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
|
||||||
var text = Blockly.Lua.valueToCode(block, 'VALUE',
|
const text = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
var code;
|
let code;
|
||||||
if (where === 'RANDOM') {
|
if (where === 'RANDOM') {
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
const functionName = Blockly.Lua.provideFunction_(
|
||||||
'text_random_letter',
|
'text_random_letter',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(str)',
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(str)',
|
||||||
' local index = math.random(string.len(str))',
|
' local index = math.random(string.len(str))',
|
||||||
@@ -132,15 +133,16 @@ Blockly.Lua['text_charAt'] = function(block) {
|
|||||||
'end']);
|
'end']);
|
||||||
code = functionName + '(' + text + ')';
|
code = functionName + '(' + text + ')';
|
||||||
} else {
|
} else {
|
||||||
|
let start;
|
||||||
if (where === 'FIRST') {
|
if (where === 'FIRST') {
|
||||||
var start = '1';
|
start = '1';
|
||||||
} else if (where === 'LAST') {
|
} else if (where === 'LAST') {
|
||||||
var start = '-1';
|
start = '-1';
|
||||||
} else {
|
} else {
|
||||||
if (where === 'FROM_START') {
|
if (where === 'FROM_START') {
|
||||||
var start = at;
|
start = at;
|
||||||
} else if (where === 'FROM_END') {
|
} else if (where === 'FROM_END') {
|
||||||
var start = '-' + at;
|
start = '-' + at;
|
||||||
} else {
|
} else {
|
||||||
throw Error('Unhandled option (text_charAt).');
|
throw Error('Unhandled option (text_charAt).');
|
||||||
}
|
}
|
||||||
@@ -149,7 +151,7 @@ Blockly.Lua['text_charAt'] = function(block) {
|
|||||||
code = 'string.sub(' + text + ', ' + start + ', ' + start + ')';
|
code = 'string.sub(' + text + ', ' + start + ', ' + start + ')';
|
||||||
} else {
|
} else {
|
||||||
// use function to avoid reevaluation
|
// use function to avoid reevaluation
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
const functionName = Blockly.Lua.provideFunction_(
|
||||||
'text_char_at',
|
'text_char_at',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
||||||
'(str, index)',
|
'(str, index)',
|
||||||
@@ -163,53 +165,56 @@ Blockly.Lua['text_charAt'] = function(block) {
|
|||||||
|
|
||||||
Blockly.Lua['text_getSubstring'] = function(block) {
|
Blockly.Lua['text_getSubstring'] = function(block) {
|
||||||
// Get substring.
|
// Get substring.
|
||||||
var text = Blockly.Lua.valueToCode(block, 'STRING',
|
const text = Blockly.Lua.valueToCode(block, 'STRING',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
|
|
||||||
// Get start index.
|
// Get start index.
|
||||||
var where1 = block.getFieldValue('WHERE1');
|
const where1 = block.getFieldValue('WHERE1');
|
||||||
var at1Order = (where1 === 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
|
const at1Order = (where1 === 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
|
||||||
Blockly.Lua.ORDER_NONE;
|
Blockly.Lua.ORDER_NONE;
|
||||||
var at1 = Blockly.Lua.valueToCode(block, 'AT1', at1Order) || '1';
|
const at1 = Blockly.Lua.valueToCode(block, 'AT1', at1Order) || '1';
|
||||||
|
let start;
|
||||||
if (where1 === 'FIRST') {
|
if (where1 === 'FIRST') {
|
||||||
var start = 1;
|
start = 1;
|
||||||
} else if (where1 === 'FROM_START') {
|
} else if (where1 === 'FROM_START') {
|
||||||
var start = at1;
|
start = at1;
|
||||||
} else if (where1 === 'FROM_END') {
|
} else if (where1 === 'FROM_END') {
|
||||||
var start = '-' + at1;
|
start = '-' + at1;
|
||||||
} else {
|
} else {
|
||||||
throw Error('Unhandled option (text_getSubstring)');
|
throw Error('Unhandled option (text_getSubstring)');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get end index.
|
// Get end index.
|
||||||
var where2 = block.getFieldValue('WHERE2');
|
const where2 = block.getFieldValue('WHERE2');
|
||||||
var at2Order = (where2 === 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
|
const at2Order = (where2 === 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
|
||||||
Blockly.Lua.ORDER_NONE;
|
Blockly.Lua.ORDER_NONE;
|
||||||
var at2 = Blockly.Lua.valueToCode(block, 'AT2', at2Order) || '1';
|
const at2 = Blockly.Lua.valueToCode(block, 'AT2', at2Order) || '1';
|
||||||
|
let end;
|
||||||
if (where2 === 'LAST') {
|
if (where2 === 'LAST') {
|
||||||
var end = -1;
|
end = -1;
|
||||||
} else if (where2 === 'FROM_START') {
|
} else if (where2 === 'FROM_START') {
|
||||||
var end = at2;
|
end = at2;
|
||||||
} else if (where2 === 'FROM_END') {
|
} else if (where2 === 'FROM_END') {
|
||||||
var end = '-' + at2;
|
end = '-' + at2;
|
||||||
} else {
|
} else {
|
||||||
throw Error('Unhandled option (text_getSubstring)');
|
throw Error('Unhandled option (text_getSubstring)');
|
||||||
}
|
}
|
||||||
var code = 'string.sub(' + text + ', ' + start + ', ' + end + ')';
|
const code = 'string.sub(' + text + ', ' + start + ', ' + end + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['text_changeCase'] = function(block) {
|
Blockly.Lua['text_changeCase'] = function(block) {
|
||||||
// Change capitalization.
|
// Change capitalization.
|
||||||
var operator = block.getFieldValue('CASE');
|
const operator = block.getFieldValue('CASE');
|
||||||
var text = Blockly.Lua.valueToCode(block, 'TEXT',
|
const text = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
|
let functionName;
|
||||||
if (operator === 'UPPERCASE') {
|
if (operator === 'UPPERCASE') {
|
||||||
var functionName = 'string.upper';
|
functionName = 'string.upper';
|
||||||
} else if (operator === 'LOWERCASE') {
|
} else if (operator === 'LOWERCASE') {
|
||||||
var functionName = 'string.lower';
|
functionName = 'string.lower';
|
||||||
} else if (operator === 'TITLECASE') {
|
} else if (operator === 'TITLECASE') {
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
functionName = Blockly.Lua.provideFunction_(
|
||||||
'text_titlecase',
|
'text_titlecase',
|
||||||
// There are shorter versions at
|
// There are shorter versions at
|
||||||
// http://lua-users.org/wiki/SciteTitleCase
|
// http://lua-users.org/wiki/SciteTitleCase
|
||||||
@@ -232,52 +237,53 @@ Blockly.Lua['text_changeCase'] = function(block) {
|
|||||||
' return table.concat(buf)',
|
' return table.concat(buf)',
|
||||||
'end']);
|
'end']);
|
||||||
}
|
}
|
||||||
var code = functionName + '(' + text + ')';
|
const code = functionName + '(' + text + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['text_trim'] = function(block) {
|
Blockly.Lua['text_trim'] = function(block) {
|
||||||
// Trim spaces.
|
// Trim spaces.
|
||||||
var OPERATORS = {
|
const OPERATORS = {
|
||||||
LEFT: '^%s*(,-)',
|
LEFT: '^%s*(,-)',
|
||||||
RIGHT: '(.-)%s*$',
|
RIGHT: '(.-)%s*$',
|
||||||
BOTH: '^%s*(.-)%s*$'
|
BOTH: '^%s*(.-)%s*$'
|
||||||
};
|
};
|
||||||
var operator = OPERATORS[block.getFieldValue('MODE')];
|
const operator = OPERATORS[block.getFieldValue('MODE')];
|
||||||
var text = Blockly.Lua.valueToCode(block, 'TEXT',
|
const text = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
var code = 'string.gsub(' + text + ', "' + operator + '", "%1")';
|
const code = 'string.gsub(' + text + ', "' + operator + '", "%1")';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['text_print'] = function(block) {
|
Blockly.Lua['text_print'] = function(block) {
|
||||||
// Print statement.
|
// Print statement.
|
||||||
var msg = Blockly.Lua.valueToCode(block, 'TEXT',
|
const msg = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
return 'print(' + msg + ')\n';
|
return 'print(' + msg + ')\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['text_prompt_ext'] = function(block) {
|
Blockly.Lua['text_prompt_ext'] = function(block) {
|
||||||
// Prompt function.
|
// Prompt function.
|
||||||
|
let msg;
|
||||||
if (block.getField('TEXT')) {
|
if (block.getField('TEXT')) {
|
||||||
// Internal message.
|
// Internal message.
|
||||||
var msg = Blockly.Lua.quote_(block.getFieldValue('TEXT'));
|
msg = Blockly.Lua.quote_(block.getFieldValue('TEXT'));
|
||||||
} else {
|
} else {
|
||||||
// External message.
|
// External message.
|
||||||
var msg = Blockly.Lua.valueToCode(block, 'TEXT',
|
msg = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
}
|
}
|
||||||
|
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
const functionName = Blockly.Lua.provideFunction_(
|
||||||
'text_prompt',
|
'text_prompt',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(msg)',
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(msg)',
|
||||||
' io.write(msg)',
|
' io.write(msg)',
|
||||||
' io.flush()',
|
' io.flush()',
|
||||||
' return io.read()',
|
' return io.read()',
|
||||||
'end']);
|
'end']);
|
||||||
var code = functionName + '(' + msg + ')';
|
let code = functionName + '(' + msg + ')';
|
||||||
|
|
||||||
var toNumber = block.getFieldValue('TYPE') === 'NUMBER';
|
const toNumber = block.getFieldValue('TYPE') === 'NUMBER';
|
||||||
if (toNumber) {
|
if (toNumber) {
|
||||||
code = 'tonumber(' + code + ', 10)';
|
code = 'tonumber(' + code + ', 10)';
|
||||||
}
|
}
|
||||||
@@ -287,11 +293,11 @@ Blockly.Lua['text_prompt_ext'] = function(block) {
|
|||||||
Blockly.Lua['text_prompt'] = Blockly.Lua['text_prompt_ext'];
|
Blockly.Lua['text_prompt'] = Blockly.Lua['text_prompt_ext'];
|
||||||
|
|
||||||
Blockly.Lua['text_count'] = function(block) {
|
Blockly.Lua['text_count'] = function(block) {
|
||||||
var text = Blockly.Lua.valueToCode(block, 'TEXT',
|
const text = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
var sub = Blockly.Lua.valueToCode(block, 'SUB',
|
const sub = Blockly.Lua.valueToCode(block, 'SUB',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
const functionName = Blockly.Lua.provideFunction_(
|
||||||
'text_count',
|
'text_count',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_
|
||||||
+ '(haystack, needle)',
|
+ '(haystack, needle)',
|
||||||
@@ -311,18 +317,18 @@ Blockly.Lua['text_count'] = function(block) {
|
|||||||
' return count',
|
' return count',
|
||||||
'end',
|
'end',
|
||||||
]);
|
]);
|
||||||
var code = functionName + '(' + text + ', ' + sub + ')';
|
const code = functionName + '(' + text + ', ' + sub + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['text_replace'] = function(block) {
|
Blockly.Lua['text_replace'] = function(block) {
|
||||||
var text = Blockly.Lua.valueToCode(block, 'TEXT',
|
const text = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
var from = Blockly.Lua.valueToCode(block, 'FROM',
|
const from = Blockly.Lua.valueToCode(block, 'FROM',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
var to = Blockly.Lua.valueToCode(block, 'TO',
|
const to = Blockly.Lua.valueToCode(block, 'TO',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
var functionName = Blockly.Lua.provideFunction_(
|
const functionName = Blockly.Lua.provideFunction_(
|
||||||
'text_replace',
|
'text_replace',
|
||||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_
|
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_
|
||||||
+ '(haystack, needle, replacement)',
|
+ '(haystack, needle, replacement)',
|
||||||
@@ -342,13 +348,13 @@ Blockly.Lua['text_replace'] = function(block) {
|
|||||||
' return table.concat(buf)',
|
' return table.concat(buf)',
|
||||||
'end',
|
'end',
|
||||||
]);
|
]);
|
||||||
var code = functionName + '(' + text + ', ' + from + ', ' + to + ')';
|
const code = functionName + '(' + text + ', ' + from + ', ' + to + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['text_reverse'] = function(block) {
|
Blockly.Lua['text_reverse'] = function(block) {
|
||||||
var text = Blockly.Lua.valueToCode(block, 'TEXT',
|
const text = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||||
var code = 'string.reverse(' + text + ')';
|
const code = 'string.reverse(' + text + ')';
|
||||||
return [code, Blockly.Lua.ORDER_HIGH];
|
return [code, Blockly.Lua.ORDER_HIGH];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -16,16 +16,16 @@ goog.require('Blockly.Lua');
|
|||||||
|
|
||||||
Blockly.Lua['variables_get'] = function(block) {
|
Blockly.Lua['variables_get'] = function(block) {
|
||||||
// Variable getter.
|
// Variable getter.
|
||||||
var code = Blockly.Lua.nameDB_.getName(block.getFieldValue('VAR'),
|
const code = Blockly.Lua.nameDB_.getName(block.getFieldValue('VAR'),
|
||||||
Blockly.VARIABLE_CATEGORY_NAME);
|
Blockly.VARIABLE_CATEGORY_NAME);
|
||||||
return [code, Blockly.Lua.ORDER_ATOMIC];
|
return [code, Blockly.Lua.ORDER_ATOMIC];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.Lua['variables_set'] = function(block) {
|
Blockly.Lua['variables_set'] = function(block) {
|
||||||
// Variable setter.
|
// Variable setter.
|
||||||
var argument0 = Blockly.Lua.valueToCode(block, 'VALUE',
|
const argument0 = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||||
Blockly.Lua.ORDER_NONE) || '0';
|
Blockly.Lua.ORDER_NONE) || '0';
|
||||||
var varName = Blockly.Lua.nameDB_.getName(
|
const varName = Blockly.Lua.nameDB_.getName(
|
||||||
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
|
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
|
||||||
return varName + ' = ' + argument0 + '\n';
|
return varName + ' = ' + argument0 + '\n';
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -282,14 +282,14 @@ goog.addDependency('../../generators/javascript/text.js', ['Blockly.JavaScript.t
|
|||||||
goog.addDependency('../../generators/javascript/variables.js', ['Blockly.JavaScript.variables'], ['Blockly.JavaScript'], {'lang': 'es6'});
|
goog.addDependency('../../generators/javascript/variables.js', ['Blockly.JavaScript.variables'], ['Blockly.JavaScript'], {'lang': 'es6'});
|
||||||
goog.addDependency('../../generators/javascript/variables_dynamic.js', ['Blockly.JavaScript.variablesDynamic'], ['Blockly.JavaScript', 'Blockly.JavaScript.variables']);
|
goog.addDependency('../../generators/javascript/variables_dynamic.js', ['Blockly.JavaScript.variablesDynamic'], ['Blockly.JavaScript', 'Blockly.JavaScript.variables']);
|
||||||
goog.addDependency('../../generators/lua.js', ['Blockly.Lua'], ['Blockly.Generator', 'Blockly.inputTypes', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6'});
|
goog.addDependency('../../generators/lua.js', ['Blockly.Lua'], ['Blockly.Generator', 'Blockly.inputTypes', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6'});
|
||||||
goog.addDependency('../../generators/lua/colour.js', ['Blockly.Lua.colour'], ['Blockly.Lua']);
|
goog.addDependency('../../generators/lua/colour.js', ['Blockly.Lua.colour'], ['Blockly.Lua'], {'lang': 'es6'});
|
||||||
goog.addDependency('../../generators/lua/lists.js', ['Blockly.Lua.lists'], ['Blockly.Lua']);
|
goog.addDependency('../../generators/lua/lists.js', ['Blockly.Lua.lists'], ['Blockly.Lua'], {'lang': 'es6'});
|
||||||
goog.addDependency('../../generators/lua/logic.js', ['Blockly.Lua.logic'], ['Blockly.Lua']);
|
goog.addDependency('../../generators/lua/logic.js', ['Blockly.Lua.logic'], ['Blockly.Lua'], {'lang': 'es6'});
|
||||||
goog.addDependency('../../generators/lua/loops.js', ['Blockly.Lua.loops'], ['Blockly.Lua']);
|
goog.addDependency('../../generators/lua/loops.js', ['Blockly.Lua.loops'], ['Blockly.Lua'], {'lang': 'es6'});
|
||||||
goog.addDependency('../../generators/lua/math.js', ['Blockly.Lua.math'], ['Blockly.Lua']);
|
goog.addDependency('../../generators/lua/math.js', ['Blockly.Lua.math'], ['Blockly.Lua'], {'lang': 'es6'});
|
||||||
goog.addDependency('../../generators/lua/procedures.js', ['Blockly.Lua.procedures'], ['Blockly.Lua']);
|
goog.addDependency('../../generators/lua/procedures.js', ['Blockly.Lua.procedures'], ['Blockly.Lua'], {'lang': 'es6'});
|
||||||
goog.addDependency('../../generators/lua/text.js', ['Blockly.Lua.texts'], ['Blockly.Lua'], {'lang': 'es5'});
|
goog.addDependency('../../generators/lua/text.js', ['Blockly.Lua.texts'], ['Blockly.Lua'], {'lang': 'es6'});
|
||||||
goog.addDependency('../../generators/lua/variables.js', ['Blockly.Lua.variables'], ['Blockly.Lua']);
|
goog.addDependency('../../generators/lua/variables.js', ['Blockly.Lua.variables'], ['Blockly.Lua'], {'lang': 'es6'});
|
||||||
goog.addDependency('../../generators/lua/variables_dynamic.js', ['Blockly.Lua.variablesDynamic'], ['Blockly.Lua', 'Blockly.Lua.variables']);
|
goog.addDependency('../../generators/lua/variables_dynamic.js', ['Blockly.Lua.variablesDynamic'], ['Blockly.Lua', 'Blockly.Lua.variables']);
|
||||||
goog.addDependency('../../generators/php.js', ['Blockly.PHP'], ['Blockly.Generator', 'Blockly.inputTypes', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6'});
|
goog.addDependency('../../generators/php.js', ['Blockly.PHP'], ['Blockly.Generator', 'Blockly.inputTypes', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6'});
|
||||||
goog.addDependency('../../generators/php/colour.js', ['Blockly.PHP.colour'], ['Blockly.PHP'], {'lang': 'es6'});
|
goog.addDependency('../../generators/php/colour.js', ['Blockly.PHP.colour'], ['Blockly.PHP'], {'lang': 'es6'});
|
||||||
|
|||||||
Reference in New Issue
Block a user