Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: convert some block generators to goog.module #5769

Merged
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4f54e62
refactor: convert generators/lua/colour.js to goog.module
rachel-fenichel Dec 1, 2021
a73a8bf
refactor: convert generators/lua/colour.js to named requires
rachel-fenichel Dec 1, 2021
2c1edc1
chore: run clang-format
rachel-fenichel Dec 1, 2021
35427e1
refactor: convert generators/lua/lists.js to goog.module
rachel-fenichel Dec 1, 2021
e0a5d08
refactor: convert generators/lua/lists.js to named requires
rachel-fenichel Dec 1, 2021
5f0a05a
chore: run clang-format
rachel-fenichel Dec 1, 2021
30b2556
fix: use getListIndex helper function in lua list generators
rachel-fenichel Dec 1, 2021
470402e
refactor: convert generators/lua/logic.js to goog.module
rachel-fenichel Dec 1, 2021
7062810
refactor: convert generators/lua/logic.js to named requires
rachel-fenichel Dec 1, 2021
d0c676b
chore: run clang-format
rachel-fenichel Dec 1, 2021
546cba9
refactor: convert generators/lua/loops.js to goog.module
rachel-fenichel Dec 1, 2021
6fe3712
refactor: convert generators/lua/loops.js to named requires
rachel-fenichel Dec 1, 2021
0a2a9e8
chore: run clang-format
rachel-fenichel Dec 1, 2021
f924397
refactor: convert generators/lua/math.js to goog.module
rachel-fenichel Dec 1, 2021
8813787
refactor: convert generators/lua/math.js to named requires
rachel-fenichel Dec 1, 2021
68e4636
chore: run clang-format
rachel-fenichel Dec 1, 2021
ac6b994
refcator: convert generators/lua/procedures.js to goog.module
rachel-fenichel Dec 1, 2021
2a21419
refactor: convert generators/lua/procedures.js to named requires
rachel-fenichel Dec 1, 2021
0b85e9e
chore: run clang-format
rachel-fenichel Dec 1, 2021
8392804
chore: rebuild deps.js
rachel-fenichel Dec 1, 2021
cf16d63
refactor: convert generators/lua/text.js to goog.module
rachel-fenichel Dec 1, 2021
c077bab
refactor: convert generators/lua/text.js to named requires
rachel-fenichel Dec 1, 2021
b9383e9
refactor: convert generators/lua/variables_dynamic.js to goog.module
rachel-fenichel Dec 1, 2021
ee797a1
refactor: convert generators/lua/variables_dynamic.js to named requires
rachel-fenichel Dec 1, 2021
13968ef
chore: run clang-format on text.js
rachel-fenichel Dec 1, 2021
bd29c02
refactor: convert generators/lua/variables.js to goog.module
rachel-fenichel Dec 1, 2021
586ac2e
refactor: convert generators/lua/variables.js to named requires
rachel-fenichel Dec 1, 2021
ca6e376
chore: run clang-format
rachel-fenichel Dec 1, 2021
14c86de
chore: make a lua generator function internal
rachel-fenichel Dec 1, 2021
ee4bea3
chore: rebuild deps.js
rachel-fenichel Dec 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 58 additions & 58 deletions generators/lua/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

/**
* @fileoverview Generating Lua for loop blocks.
* @suppress {missingRequire}
*/
'use strict';

goog.module('Blockly.Lua.loops');

goog.require('Blockly.Lua');
goog.require('Blockly.utils.string');
const Lua = goog.require('Blockly.Lua');
const stringUtils = goog.require('Blockly.utils.string');
const {NameType} = goog.require('Blockly.Names');


/**
Expand All @@ -22,7 +22,7 @@ goog.require('Blockly.utils.string');
* the appropriate label can be put at the end of the loop body.
* @const {string}
*/
Blockly.Lua.CONTINUE_STATEMENT = 'goto continue\n';
const CONTINUE_STATEMENT = 'goto continue\n';

/**
* If the loop body contains a "goto continue" statement, add a continue label
Expand All @@ -34,75 +34,75 @@ Blockly.Lua.CONTINUE_STATEMENT = 'goto continue\n';
* @return {string} Generated label or '' if unnecessary
* @private
*/
Blockly.Lua.addContinueLabel_ = function(branch) {
if (branch.indexOf(Blockly.Lua.CONTINUE_STATEMENT) !== -1) {
Lua.addContinueLabel_ = function(branch) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be converted to a local variable similar to getListIndex, I think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed.

if (branch.indexOf(CONTINUE_STATEMENT) !== -1) {
// False positives are possible (e.g. a string literal), but are harmless.
return branch + Blockly.Lua.INDENT + '::continue::\n';
return branch + Lua.INDENT + '::continue::\n';
} else {
return branch;
}
};

Blockly.Lua['controls_repeat_ext'] = function(block) {
Lua['controls_repeat_ext'] = function(block) {
// Repeat n times.
let repeats;
if (block.getField('TIMES')) {
// Internal number.
repeats = String(Number(block.getFieldValue('TIMES')));
} else {
// External number.
repeats = Blockly.Lua.valueToCode(block, 'TIMES',
Blockly.Lua.ORDER_NONE) || '0';
repeats = Lua.valueToCode(block, 'TIMES',
Lua.ORDER_NONE) || '0';
}
if (Blockly.utils.string.isNumber(repeats)) {
if (stringUtils.isNumber(repeats)) {
repeats = parseInt(repeats, 10);
} else {
repeats = 'math.floor(' + repeats + ')';
}
let branch = Blockly.Lua.statementToCode(block, 'DO');
branch = Blockly.Lua.addLoopTrap(branch, block);
branch = Blockly.Lua.addContinueLabel_(branch);
const loopVar = Blockly.Lua.nameDB_.getDistinctName(
'count', Blockly.VARIABLE_CATEGORY_NAME);
let branch = Lua.statementToCode(block, 'DO');
branch = Lua.addLoopTrap(branch, block);
branch = Lua.addContinueLabel_(branch);
const loopVar = Lua.nameDB_.getDistinctName(
'count', NameType.VARIABLE);
const code = 'for ' + loopVar + ' = 1, ' + repeats + ' do\n' +
branch + 'end\n';
return code;
};

Blockly.Lua['controls_repeat'] = Blockly.Lua['controls_repeat_ext'];
Lua['controls_repeat'] = Lua['controls_repeat_ext'];

Blockly.Lua['controls_whileUntil'] = function(block) {
Lua['controls_whileUntil'] = function(block) {
// Do while/until loop.
const until = block.getFieldValue('MODE') === 'UNTIL';
let argument0 = Blockly.Lua.valueToCode(block, 'BOOL',
until ? Blockly.Lua.ORDER_UNARY :
Blockly.Lua.ORDER_NONE) || 'false';
let branch = Blockly.Lua.statementToCode(block, 'DO');
branch = Blockly.Lua.addLoopTrap(branch, block);
branch = Blockly.Lua.addContinueLabel_(branch);
let argument0 = Lua.valueToCode(block, 'BOOL',
until ? Lua.ORDER_UNARY :
Lua.ORDER_NONE) || 'false';
let branch = Lua.statementToCode(block, 'DO');
branch = Lua.addLoopTrap(branch, block);
branch = Lua.addContinueLabel_(branch);
if (until) {
argument0 = 'not ' + argument0;
}
return 'while ' + argument0 + ' do\n' + branch + 'end\n';
};

Blockly.Lua['controls_for'] = function(block) {
Lua['controls_for'] = function(block) {
// For loop.
const variable0 = Blockly.Lua.nameDB_.getName(
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
const startVar = Blockly.Lua.valueToCode(block, 'FROM',
Blockly.Lua.ORDER_NONE) || '0';
const endVar = Blockly.Lua.valueToCode(block, 'TO',
Blockly.Lua.ORDER_NONE) || '0';
const increment = Blockly.Lua.valueToCode(block, 'BY',
Blockly.Lua.ORDER_NONE) || '1';
let branch = Blockly.Lua.statementToCode(block, 'DO');
branch = Blockly.Lua.addLoopTrap(branch, block);
branch = Blockly.Lua.addContinueLabel_(branch);
const variable0 = Lua.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
const startVar = Lua.valueToCode(block, 'FROM',
Lua.ORDER_NONE) || '0';
const endVar = Lua.valueToCode(block, 'TO',
Lua.ORDER_NONE) || '0';
const increment = Lua.valueToCode(block, 'BY',
Lua.ORDER_NONE) || '1';
let branch = Lua.statementToCode(block, 'DO');
branch = Lua.addLoopTrap(branch, block);
branch = Lua.addContinueLabel_(branch);
let code = '';
let incValue;
if (Blockly.utils.string.isNumber(startVar) && Blockly.utils.string.isNumber(endVar) &&
Blockly.utils.string.isNumber(increment)) {
if (stringUtils.isNumber(startVar) && stringUtils.isNumber(endVar) &&
stringUtils.isNumber(increment)) {
// All arguments are simple numbers.
const up = Number(startVar) <= Number(endVar);
const step = Math.abs(Number(increment));
Expand All @@ -111,16 +111,16 @@ Blockly.Lua['controls_for'] = function(block) {
code = '';
// Determine loop direction at start, in case one of the bounds
// changes during loop execution.
incValue = Blockly.Lua.nameDB_.getDistinctName(
variable0 + '_inc', Blockly.VARIABLE_CATEGORY_NAME);
incValue = Lua.nameDB_.getDistinctName(
variable0 + '_inc', NameType.VARIABLE);
code += incValue + ' = ';
if (Blockly.utils.string.isNumber(increment)) {
if (stringUtils.isNumber(increment)) {
code += Math.abs(increment) + '\n';
} else {
code += 'math.abs(' + increment + ')\n';
}
code += 'if (' + startVar + ') > (' + endVar + ') then\n';
code += Blockly.Lua.INDENT + incValue + ' = -' + incValue + '\n';
code += Lua.INDENT + incValue + ' = -' + incValue + '\n';
code += 'end\n';
}
code += 'for ' + variable0 + ' = ' + startVar + ', ' + endVar +
Expand All @@ -129,46 +129,46 @@ Blockly.Lua['controls_for'] = function(block) {
return code;
};

Blockly.Lua['controls_forEach'] = function(block) {
Lua['controls_forEach'] = function(block) {
// For each loop.
const variable0 = Blockly.Lua.nameDB_.getName(
block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME);
const argument0 = Blockly.Lua.valueToCode(block, 'LIST',
Blockly.Lua.ORDER_NONE) || '{}';
let branch = Blockly.Lua.statementToCode(block, 'DO');
branch = Blockly.Lua.addLoopTrap(branch, block);
branch = Blockly.Lua.addContinueLabel_(branch);
const variable0 = Lua.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
const argument0 = Lua.valueToCode(block, 'LIST',
Lua.ORDER_NONE) || '{}';
let branch = Lua.statementToCode(block, 'DO');
branch = Lua.addLoopTrap(branch, block);
branch = Lua.addContinueLabel_(branch);
const code = 'for _, ' + variable0 + ' in ipairs(' + argument0 + ') do \n' +
branch + 'end\n';
return code;
};

Blockly.Lua['controls_flow_statements'] = function(block) {
Lua['controls_flow_statements'] = function(block) {
// Flow statements: continue, break.
let xfix = '';
if (Blockly.Lua.STATEMENT_PREFIX) {
if (Lua.STATEMENT_PREFIX) {
// Automatic prefix insertion is switched off for this block. Add manually.
xfix += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_PREFIX, block);
xfix += Lua.injectId(Lua.STATEMENT_PREFIX, block);
}
if (Blockly.Lua.STATEMENT_SUFFIX) {
if (Lua.STATEMENT_SUFFIX) {
// Inject any statement suffix here since the regular one at the end
// will not get executed if the break/continue is triggered.
xfix += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block);
xfix += Lua.injectId(Lua.STATEMENT_SUFFIX, block);
}
if (Blockly.Lua.STATEMENT_PREFIX) {
if (Lua.STATEMENT_PREFIX) {
const loop = block.getSurroundLoop();
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
// of the loop will not get executed if 'continue' is triggered.
// In the case of 'break', a prefix is needed due to the loop's suffix.
xfix += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_PREFIX, loop);
xfix += Lua.injectId(Lua.STATEMENT_PREFIX, loop);
}
}
switch (block.getFieldValue('FLOW')) {
case 'BREAK':
return xfix + 'break\n';
case 'CONTINUE':
return xfix + Blockly.Lua.CONTINUE_STATEMENT;
return xfix + CONTINUE_STATEMENT;
}
throw Error('Unknown flow statement.');
};