diff --git a/generators/lua.js b/generators/lua.js index 4825b7598..2b24452f7 100644 --- a/generators/lua.js +++ b/generators/lua.js @@ -11,22 +11,23 @@ */ 'use strict'; -goog.provide('Blockly.Lua'); +goog.module('Blockly.Lua'); +goog.module.declareLegacyNamespace(); -goog.require('Blockly.Generator'); -goog.require('Blockly.Names'); -goog.require('Blockly.inputTypes'); -goog.require('Blockly.utils.object'); -goog.require('Blockly.utils.string'); -goog.requireType('Blockly.Block'); -goog.requireType('Blockly.Workspace'); +const objectUtils = goog.require('Blockly.utils.object'); +const stringUtils = goog.require('Blockly.utils.string'); +const {Block} = goog.requireType('Blockly.Block'); +const {Generator} = goog.require('Blockly.Generator'); +const {inputTypes} = goog.require('Blockly.inputTypes'); +const {Names} = goog.require('Blockly.Names'); +const {Workspace} = goog.requireType('Blockly.Workspace'); /** * Lua code generator. - * @type {!Blockly.Generator} + * @type {!Generator} */ -Blockly.Lua = new Blockly.Generator('Lua'); +const Lua = new Generator('Lua'); /** * List of illegal variable names. @@ -35,7 +36,7 @@ Blockly.Lua = new Blockly.Generator('Lua'); * accidentally clobbering a built-in object or function. * @private */ -Blockly.Lua.addReservedWords( +Lua.addReservedWords( // Special character '_,' + // From theoriginalbit's script: @@ -58,25 +59,24 @@ Blockly.Lua.addReservedWords( 'loadfile,next,pairs,pcall,print,rawequal,rawget,rawlen,rawset,select,' + 'setmetatable,tonumber,tostring,type,_VERSION,xpcall,' + // Modules (http://www.lua.org/manual/5.2/manual.html, section 6.3). - 'require,package,string,table,math,bit32,io,file,os,debug' -); + 'require,package,string,table,math,bit32,io,file,os,debug'); /** * Order of operation ENUMs. * http://www.lua.org/manual/5.3/manual.html#3.4.8 */ -Blockly.Lua.ORDER_ATOMIC = 0; // literals +Lua.ORDER_ATOMIC = 0; // literals // The next level was not explicit in documentation and inferred by Ellen. -Blockly.Lua.ORDER_HIGH = 1; // Function calls, tables[] -Blockly.Lua.ORDER_EXPONENTIATION = 2; // ^ -Blockly.Lua.ORDER_UNARY = 3; // not # - ~ -Blockly.Lua.ORDER_MULTIPLICATIVE = 4; // * / % -Blockly.Lua.ORDER_ADDITIVE = 5; // + - -Blockly.Lua.ORDER_CONCATENATION = 6; // .. -Blockly.Lua.ORDER_RELATIONAL = 7; // < > <= >= ~= == -Blockly.Lua.ORDER_AND = 8; // and -Blockly.Lua.ORDER_OR = 9; // or -Blockly.Lua.ORDER_NONE = 99; +Lua.ORDER_HIGH = 1; // Function calls, tables[] +Lua.ORDER_EXPONENTIATION = 2; // ^ +Lua.ORDER_UNARY = 3; // not # - ~ +Lua.ORDER_MULTIPLICATIVE = 4; // * / % +Lua.ORDER_ADDITIVE = 5; // + - +Lua.ORDER_CONCATENATION = 6; // .. +Lua.ORDER_RELATIONAL = 7; // < > <= >= ~= == +Lua.ORDER_AND = 8; // and +Lua.ORDER_OR = 9; // or +Lua.ORDER_NONE = 99; /** * Note: Lua is not supporting zero-indexing since the language itself is @@ -88,18 +88,18 @@ Blockly.Lua.ORDER_NONE = 99; * Whether the init method has been called. * @type {?boolean} */ -Blockly.Lua.isInitialized = false; +Lua.isInitialized = false; /** * Initialise the database of variable names. - * @param {!Blockly.Workspace} workspace Workspace to generate code from. + * @param {!Workspace} workspace Workspace to generate code from. */ -Blockly.Lua.init = function(workspace) { +Lua.init = function(workspace) { // Call Blockly.Generator's init. Object.getPrototypeOf(this).init.call(this); if (!this.nameDB_) { - this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_); + this.nameDB_ = new Names(this.RESERVED_WORDS_); } else { this.nameDB_.reset(); } @@ -115,9 +115,9 @@ Blockly.Lua.init = function(workspace) { * @param {string} code Generated code. * @return {string} Completed code. */ -Blockly.Lua.finish = function(code) { +Lua.finish = function(code) { // Convert the definitions dictionary into a list. - const definitions = Blockly.utils.object.values(this.definitions_); + const definitions = objectUtils.values(this.definitions_); // Call Blockly.Generator's finish. code = Object.getPrototypeOf(this).finish.call(this, code); this.isInitialized = false; @@ -134,7 +134,7 @@ Blockly.Lua.finish = function(code) { * @param {string} line Line of generated code. * @return {string} Legal line of code. */ -Blockly.Lua.scrubNakedValue = function(line) { +Lua.scrubNakedValue = function(line) { return 'local _ = ' + line + '\n'; }; @@ -145,10 +145,10 @@ Blockly.Lua.scrubNakedValue = function(line) { * @return {string} Lua string. * @protected */ -Blockly.Lua.quote_ = function(string) { +Lua.quote_ = function(string) { string = string.replace(/\\/g, '\\\\') - .replace(/\n/g, '\\\n') - .replace(/'/g, '\\\''); + .replace(/\n/g, '\\\n') + .replace(/'/g, '\\\''); return '\'' + string + '\''; }; @@ -159,7 +159,7 @@ Blockly.Lua.quote_ = function(string) { * @return {string} Lua string. * @protected */ -Blockly.Lua.multiline_quote_ = function(string) { +Lua.multiline_quote_ = function(string) { const lines = string.split(/\n/g).map(this.quote_); // Join with the following, plus a newline: // .. '\n' .. @@ -170,26 +170,26 @@ Blockly.Lua.multiline_quote_ = function(string) { * Common tasks for generating Lua from blocks. * Handles comments for the specified block and any connected value blocks. * Calls any statements following this block. - * @param {!Blockly.Block} block The current block. + * @param {!Block} block The current block. * @param {string} code The Lua code created for this block. * @param {boolean=} opt_thisOnly True to generate code for only this statement. * @return {string} Lua code with comments and subsequent blocks added. * @protected */ -Blockly.Lua.scrub_ = function(block, code, opt_thisOnly) { +Lua.scrub_ = function(block, code, opt_thisOnly) { let commentCode = ''; // Only collect comments for blocks that aren't inline. if (!block.outputConnection || !block.outputConnection.targetConnection) { // Collect comment for this block. let comment = block.getCommentText(); if (comment) { - comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3); + comment = stringUtils.wrap(comment, this.COMMENT_WRAP - 3); commentCode += this.prefixLines(comment, '-- ') + '\n'; } // Collect comments for all value arguments. // Don't collect comments for nested statements. for (let i = 0; i < block.inputList.length; i++) { - if (block.inputList[i].type === Blockly.inputTypes.VALUE) { + if (block.inputList[i].type === inputTypes.VALUE) { const childBlock = block.inputList[i].connection.targetBlock(); if (childBlock) { comment = this.allNestedComments(childBlock); @@ -204,3 +204,5 @@ Blockly.Lua.scrub_ = function(block, code, opt_thisOnly) { const nextCode = opt_thisOnly ? '' : this.blockToCode(nextBlock); return commentCode + code + nextCode; }; + +exports = Lua; diff --git a/generators/php.js b/generators/php.js index 46e756e99..d4e671acc 100644 --- a/generators/php.js +++ b/generators/php.js @@ -10,22 +10,23 @@ */ 'use strict'; -goog.provide('Blockly.PHP'); +goog.module('Blockly.PHP'); +goog.module.declareLegacyNamespace(); -goog.require('Blockly.Generator'); -goog.require('Blockly.Names'); -goog.require('Blockly.inputTypes'); -goog.require('Blockly.utils.object'); -goog.require('Blockly.utils.string'); -goog.requireType('Blockly.Block'); -goog.requireType('Blockly.Workspace'); +const objectUtils = goog.require('Blockly.utils.object'); +const stringUtils = goog.require('Blockly.utils.string'); +const {Block} = goog.requireType('Blockly.Block'); +const {Generator} = goog.require('Blockly.Generator'); +const {inputTypes} = goog.require('Blockly.inputTypes'); +const {Names} = goog.require('Blockly.Names'); +const {Workspace} = goog.requireType('Blockly.Workspace'); /** * PHP code generator. - * @type {!Blockly.Generator} + * @type {!Generator} */ -Blockly.PHP = new Blockly.Generator('PHP'); +const PHP = new Generator('PHP'); /** * List of illegal variable names. @@ -34,8 +35,8 @@ Blockly.PHP = new Blockly.Generator('PHP'); * accidentally clobbering a built-in object or function. * @private */ -Blockly.PHP.addReservedWords( - // http://php.net/manual/en/reserved.keywords.php +PHP.addReservedWords( + // http://php.net/manual/en/reserved.keywords.php '__halt_compiler,abstract,and,array,as,break,callable,case,catch,class,' + 'clone,const,continue,declare,default,die,do,echo,else,elseif,empty,' + 'enddeclare,endfor,endforeach,endif,endswitch,endwhile,eval,exit,extends,' + @@ -43,7 +44,7 @@ Blockly.PHP.addReservedWords( 'include_once,instanceof,insteadof,interface,isset,list,namespace,new,or,' + 'print,private,protected,public,require,require_once,return,static,' + 'switch,throw,trait,try,unset,use,var,while,xor,' + - // http://php.net/manual/en/reserved.constants.php + // http://php.net/manual/en/reserved.constants.php 'PHP_VERSION,PHP_MAJOR_VERSION,PHP_MINOR_VERSION,PHP_RELEASE_VERSION,' + 'PHP_VERSION_ID,PHP_EXTRA_VERSION,PHP_ZTS,PHP_DEBUG,PHP_MAXPATHLEN,' + 'PHP_OS,PHP_SAPI,PHP_EOL,PHP_INT_MAX,PHP_INT_SIZE,DEFAULT_INCLUDE_PATH,' + @@ -54,90 +55,89 @@ Blockly.PHP.addReservedWords( 'E_CORE_WARNING,E_COMPILE_ERROR,E_COMPILE_WARNING,E_USER_ERROR,' + 'E_USER_WARNING,E_USER_NOTICE,E_DEPRECATED,E_USER_DEPRECATED,E_ALL,' + 'E_STRICT,__COMPILER_HALT_OFFSET__,TRUE,FALSE,NULL,__CLASS__,__DIR__,' + - '__FILE__,__FUNCTION__,__LINE__,__METHOD__,__NAMESPACE__,__TRAIT__' -); + '__FILE__,__FUNCTION__,__LINE__,__METHOD__,__NAMESPACE__,__TRAIT__'); /** * Order of operation ENUMs. * http://php.net/manual/en/language.operators.precedence.php */ -Blockly.PHP.ORDER_ATOMIC = 0; // 0 "" ... -Blockly.PHP.ORDER_CLONE = 1; // clone -Blockly.PHP.ORDER_NEW = 1; // new -Blockly.PHP.ORDER_MEMBER = 2.1; // [] -Blockly.PHP.ORDER_FUNCTION_CALL = 2.2; // () -Blockly.PHP.ORDER_POWER = 3; // ** -Blockly.PHP.ORDER_INCREMENT = 4; // ++ -Blockly.PHP.ORDER_DECREMENT = 4; // -- -Blockly.PHP.ORDER_BITWISE_NOT = 4; // ~ -Blockly.PHP.ORDER_CAST = 4; // (int) (float) (string) (array) ... -Blockly.PHP.ORDER_SUPPRESS_ERROR = 4; // @ -Blockly.PHP.ORDER_INSTANCEOF = 5; // instanceof -Blockly.PHP.ORDER_LOGICAL_NOT = 6; // ! -Blockly.PHP.ORDER_UNARY_PLUS = 7.1; // + -Blockly.PHP.ORDER_UNARY_NEGATION = 7.2; // - -Blockly.PHP.ORDER_MULTIPLICATION = 8.1; // * -Blockly.PHP.ORDER_DIVISION = 8.2; // / -Blockly.PHP.ORDER_MODULUS = 8.3; // % -Blockly.PHP.ORDER_ADDITION = 9.1; // + -Blockly.PHP.ORDER_SUBTRACTION = 9.2; // - -Blockly.PHP.ORDER_STRING_CONCAT = 9.3; // . -Blockly.PHP.ORDER_BITWISE_SHIFT = 10; // << >> -Blockly.PHP.ORDER_RELATIONAL = 11; // < <= > >= -Blockly.PHP.ORDER_EQUALITY = 12; // == != === !== <> <=> -Blockly.PHP.ORDER_REFERENCE = 13; // & -Blockly.PHP.ORDER_BITWISE_AND = 13; // & -Blockly.PHP.ORDER_BITWISE_XOR = 14; // ^ -Blockly.PHP.ORDER_BITWISE_OR = 15; // | -Blockly.PHP.ORDER_LOGICAL_AND = 16; // && -Blockly.PHP.ORDER_LOGICAL_OR = 17; // || -Blockly.PHP.ORDER_IF_NULL = 18; // ?? -Blockly.PHP.ORDER_CONDITIONAL = 19; // ?: -Blockly.PHP.ORDER_ASSIGNMENT = 20; // = += -= *= /= %= <<= >>= ... -Blockly.PHP.ORDER_LOGICAL_AND_WEAK = 21; // and -Blockly.PHP.ORDER_LOGICAL_XOR = 22; // xor -Blockly.PHP.ORDER_LOGICAL_OR_WEAK = 23; // or -Blockly.PHP.ORDER_NONE = 99; // (...) +PHP.ORDER_ATOMIC = 0; // 0 "" ... +PHP.ORDER_CLONE = 1; // clone +PHP.ORDER_NEW = 1; // new +PHP.ORDER_MEMBER = 2.1; // [] +PHP.ORDER_FUNCTION_CALL = 2.2; // () +PHP.ORDER_POWER = 3; // ** +PHP.ORDER_INCREMENT = 4; // ++ +PHP.ORDER_DECREMENT = 4; // -- +PHP.ORDER_BITWISE_NOT = 4; // ~ +PHP.ORDER_CAST = 4; // (int) (float) (string) (array) ... +PHP.ORDER_SUPPRESS_ERROR = 4; // @ +PHP.ORDER_INSTANCEOF = 5; // instanceof +PHP.ORDER_LOGICAL_NOT = 6; // ! +PHP.ORDER_UNARY_PLUS = 7.1; // + +PHP.ORDER_UNARY_NEGATION = 7.2; // - +PHP.ORDER_MULTIPLICATION = 8.1; // * +PHP.ORDER_DIVISION = 8.2; // / +PHP.ORDER_MODULUS = 8.3; // % +PHP.ORDER_ADDITION = 9.1; // + +PHP.ORDER_SUBTRACTION = 9.2; // - +PHP.ORDER_STRING_CONCAT = 9.3; // . +PHP.ORDER_BITWISE_SHIFT = 10; // << >> +PHP.ORDER_RELATIONAL = 11; // < <= > >= +PHP.ORDER_EQUALITY = 12; // == != === !== <> <=> +PHP.ORDER_REFERENCE = 13; // & +PHP.ORDER_BITWISE_AND = 13; // & +PHP.ORDER_BITWISE_XOR = 14; // ^ +PHP.ORDER_BITWISE_OR = 15; // | +PHP.ORDER_LOGICAL_AND = 16; // && +PHP.ORDER_LOGICAL_OR = 17; // || +PHP.ORDER_IF_NULL = 18; // ?? +PHP.ORDER_CONDITIONAL = 19; // ?: +PHP.ORDER_ASSIGNMENT = 20; // = += -= *= /= %= <<= >>= ... +PHP.ORDER_LOGICAL_AND_WEAK = 21; // and +PHP.ORDER_LOGICAL_XOR = 22; // xor +PHP.ORDER_LOGICAL_OR_WEAK = 23; // or +PHP.ORDER_NONE = 99; // (...) /** * List of outer-inner pairings that do NOT require parentheses. * @type {!Array>} */ -Blockly.PHP.ORDER_OVERRIDES = [ +PHP.ORDER_OVERRIDES = [ // (foo()).bar() -> foo().bar() // (foo())[0] -> foo()[0] - [Blockly.PHP.ORDER_MEMBER, Blockly.PHP.ORDER_FUNCTION_CALL], + [PHP.ORDER_MEMBER, PHP.ORDER_FUNCTION_CALL], // (foo[0])[1] -> foo[0][1] // (foo.bar).baz -> foo.bar.baz - [Blockly.PHP.ORDER_MEMBER, Blockly.PHP.ORDER_MEMBER], + [PHP.ORDER_MEMBER, PHP.ORDER_MEMBER], // !(!foo) -> !!foo - [Blockly.PHP.ORDER_LOGICAL_NOT, Blockly.PHP.ORDER_LOGICAL_NOT], + [PHP.ORDER_LOGICAL_NOT, PHP.ORDER_LOGICAL_NOT], // a * (b * c) -> a * b * c - [Blockly.PHP.ORDER_MULTIPLICATION, Blockly.PHP.ORDER_MULTIPLICATION], + [PHP.ORDER_MULTIPLICATION, PHP.ORDER_MULTIPLICATION], // a + (b + c) -> a + b + c - [Blockly.PHP.ORDER_ADDITION, Blockly.PHP.ORDER_ADDITION], + [PHP.ORDER_ADDITION, PHP.ORDER_ADDITION], // a && (b && c) -> a && b && c - [Blockly.PHP.ORDER_LOGICAL_AND, Blockly.PHP.ORDER_LOGICAL_AND], + [PHP.ORDER_LOGICAL_AND, PHP.ORDER_LOGICAL_AND], // a || (b || c) -> a || b || c - [Blockly.PHP.ORDER_LOGICAL_OR, Blockly.PHP.ORDER_LOGICAL_OR] + [PHP.ORDER_LOGICAL_OR, PHP.ORDER_LOGICAL_OR] ]; /** * Whether the init method has been called. * @type {?boolean} */ -Blockly.PHP.isInitialized = false; +PHP.isInitialized = false; /** * Initialise the database of variable names. - * @param {!Blockly.Workspace} workspace Workspace to generate code from. + * @param {!Workspace} workspace Workspace to generate code from. */ -Blockly.PHP.init = function(workspace) { +PHP.init = function(workspace) { // Call Blockly.Generator's init. Object.getPrototypeOf(this).init.call(this); if (!this.nameDB_) { - this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_, '$'); + this.nameDB_ = new Names(this.RESERVED_WORDS_, '$'); } else { this.nameDB_.reset(); } @@ -154,9 +154,9 @@ Blockly.PHP.init = function(workspace) { * @param {string} code Generated code. * @return {string} Completed code. */ -Blockly.PHP.finish = function(code) { +PHP.finish = function(code) { // Convert the definitions dictionary into a list. - const definitions = Blockly.utils.object.values(this.definitions_); + const definitions = objectUtils.values(this.definitions_); // Call Blockly.Generator's finish. code = Object.getPrototypeOf(this).finish.call(this, code); this.isInitialized = false; @@ -171,7 +171,7 @@ Blockly.PHP.finish = function(code) { * @param {string} line Line of generated code. * @return {string} Legal line of code. */ -Blockly.PHP.scrubNakedValue = function(line) { +PHP.scrubNakedValue = function(line) { return line + ';\n'; }; @@ -182,10 +182,10 @@ Blockly.PHP.scrubNakedValue = function(line) { * @return {string} PHP string. * @protected */ -Blockly.PHP.quote_ = function(string) { +PHP.quote_ = function(string) { string = string.replace(/\\/g, '\\\\') - .replace(/\n/g, '\\\n') - .replace(/'/g, '\\\''); + .replace(/\n/g, '\\\n') + .replace(/'/g, '\\\''); return '\'' + string + '\''; }; @@ -196,7 +196,7 @@ Blockly.PHP.quote_ = function(string) { * @return {string} PHP string. * @protected */ -Blockly.PHP.multiline_quote_ = function (string) { +PHP.multiline_quote_ = function(string) { const lines = string.split(/\n/g).map(this.quote_); // Join with the following, plus a newline: // . "\n" . @@ -208,26 +208,26 @@ Blockly.PHP.multiline_quote_ = function (string) { * Common tasks for generating PHP from blocks. * Handles comments for the specified block and any connected value blocks. * Calls any statements following this block. - * @param {!Blockly.Block} block The current block. + * @param {!Block} block The current block. * @param {string} code The PHP code created for this block. * @param {boolean=} opt_thisOnly True to generate code for only this statement. * @return {string} PHP code with comments and subsequent blocks added. * @protected */ -Blockly.PHP.scrub_ = function(block, code, opt_thisOnly) { +PHP.scrub_ = function(block, code, opt_thisOnly) { let commentCode = ''; // Only collect comments for blocks that aren't inline. if (!block.outputConnection || !block.outputConnection.targetConnection) { // Collect comment for this block. let comment = block.getCommentText(); if (comment) { - comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3); + comment = stringUtils.wrap(comment, this.COMMENT_WRAP - 3); commentCode += this.prefixLines(comment, '// ') + '\n'; } // Collect comments for all value arguments. // Don't collect comments for nested statements. for (let i = 0; i < block.inputList.length; i++) { - if (block.inputList[i].type === Blockly.inputTypes.VALUE) { + if (block.inputList[i].type === inputTypes.VALUE) { const childBlock = block.inputList[i].connection.targetBlock(); if (childBlock) { comment = this.allNestedComments(childBlock); @@ -245,15 +245,14 @@ Blockly.PHP.scrub_ = function(block, code, opt_thisOnly) { /** * Gets a property and adjusts the value while taking into account indexing. - * @param {!Blockly.Block} block The block. + * @param {!Block} block The block. * @param {string} atId The property ID of the element to get. * @param {number=} opt_delta Value to add. * @param {boolean=} opt_negate Whether to negate the value. * @param {number=} opt_order The highest order acting on this value. * @return {string|number} */ -Blockly.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate, - opt_order) { +PHP.getAdjusted = function(block, atId, opt_delta, opt_negate, opt_order) { let delta = opt_delta || 0; let order = opt_order || this.ORDER_NONE; if (block.workspace.options.oneBasedIndex) { @@ -274,7 +273,7 @@ Blockly.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate, } let at = this.valueToCode(block, atId, outerOrder) || defaultAtIndex; - if (Blockly.utils.string.isNumber(at)) { + if (stringUtils.isNumber(at)) { // If the index is a naked number, adjust it right now. at = Number(at) + delta; if (opt_negate) { @@ -302,3 +301,5 @@ Blockly.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate, } return at; }; + +exports = PHP; diff --git a/generators/php/colour.js b/generators/php/colour.js index 59e55e7f1..d50cbfb63 100644 --- a/generators/php/colour.js +++ b/generators/php/colour.js @@ -9,82 +9,67 @@ */ 'use strict'; -goog.provide('Blockly.PHP.colour'); +goog.module('Blockly.PHP.colour'); -goog.require('Blockly.PHP'); +const PHP = goog.require('Blockly.PHP'); -Blockly.PHP['colour_picker'] = function(block) { +PHP['colour_picker'] = function(block) { // Colour picker. - const code = Blockly.PHP.quote_(block.getFieldValue('COLOUR')); - return [code, Blockly.PHP.ORDER_ATOMIC]; + const code = PHP.quote_(block.getFieldValue('COLOUR')); + return [code, PHP.ORDER_ATOMIC]; }; -Blockly.PHP['colour_random'] = function(block) { +PHP['colour_random'] = function(block) { // Generate a random colour. - const functionName = Blockly.PHP.provideFunction_( - 'colour_random', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '() {', - ' return \'#\' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), ' + - '6, \'0\', STR_PAD_LEFT);', - '}']); + const functionName = PHP.provideFunction_('colour_random', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '() {', + ' return \'#\' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), ' + + '6, \'0\', STR_PAD_LEFT);', + '}' + ]); const code = functionName + '()'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['colour_rgb'] = function(block) { +PHP['colour_rgb'] = function(block) { // Compose a colour from RGB components expressed as percentages. - const red = Blockly.PHP.valueToCode(block, 'RED', - Blockly.PHP.ORDER_NONE) || 0; - const green = Blockly.PHP.valueToCode(block, 'GREEN', - Blockly.PHP.ORDER_NONE) || 0; - const blue = Blockly.PHP.valueToCode(block, 'BLUE', - Blockly.PHP.ORDER_NONE) || 0; - const functionName = Blockly.PHP.provideFunction_( - 'colour_rgb', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($r, $g, $b) {', - ' $r = round(max(min($r, 100), 0) * 2.55);', - ' $g = round(max(min($g, 100), 0) * 2.55);', - ' $b = round(max(min($b, 100), 0) * 2.55);', - ' $hex = \'#\';', - ' $hex .= str_pad(dechex($r), 2, \'0\', STR_PAD_LEFT);', - ' $hex .= str_pad(dechex($g), 2, \'0\', STR_PAD_LEFT);', - ' $hex .= str_pad(dechex($b), 2, \'0\', STR_PAD_LEFT);', - ' return $hex;', - '}']); + const red = PHP.valueToCode(block, 'RED', PHP.ORDER_NONE) || 0; + const green = PHP.valueToCode(block, 'GREEN', PHP.ORDER_NONE) || 0; + const blue = PHP.valueToCode(block, 'BLUE', PHP.ORDER_NONE) || 0; + const functionName = PHP.provideFunction_('colour_rgb', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($r, $g, $b) {', + ' $r = round(max(min($r, 100), 0) * 2.55);', + ' $g = round(max(min($g, 100), 0) * 2.55);', + ' $b = round(max(min($b, 100), 0) * 2.55);', ' $hex = \'#\';', + ' $hex .= str_pad(dechex($r), 2, \'0\', STR_PAD_LEFT);', + ' $hex .= str_pad(dechex($g), 2, \'0\', STR_PAD_LEFT);', + ' $hex .= str_pad(dechex($b), 2, \'0\', STR_PAD_LEFT);', ' return $hex;', + '}' + ]); const code = functionName + '(' + red + ', ' + green + ', ' + blue + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['colour_blend'] = function(block) { +PHP['colour_blend'] = function(block) { // Blend two colours together. - const c1 = Blockly.PHP.valueToCode(block, 'COLOUR1', - Blockly.PHP.ORDER_NONE) || '\'#000000\''; - const c2 = Blockly.PHP.valueToCode(block, 'COLOUR2', - Blockly.PHP.ORDER_NONE) || '\'#000000\''; - const ratio = Blockly.PHP.valueToCode(block, 'RATIO', - Blockly.PHP.ORDER_NONE) || 0.5; - const functionName = Blockly.PHP.provideFunction_( - 'colour_blend', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($c1, $c2, $ratio) {', - ' $ratio = max(min($ratio, 1), 0);', - ' $r1 = hexdec(substr($c1, 1, 2));', - ' $g1 = hexdec(substr($c1, 3, 2));', - ' $b1 = hexdec(substr($c1, 5, 2));', - ' $r2 = hexdec(substr($c2, 1, 2));', - ' $g2 = hexdec(substr($c2, 3, 2));', - ' $b2 = hexdec(substr($c2, 5, 2));', - ' $r = round($r1 * (1 - $ratio) + $r2 * $ratio);', - ' $g = round($g1 * (1 - $ratio) + $g2 * $ratio);', - ' $b = round($b1 * (1 - $ratio) + $b2 * $ratio);', - ' $hex = \'#\';', - ' $hex .= str_pad(dechex($r), 2, \'0\', STR_PAD_LEFT);', - ' $hex .= str_pad(dechex($g), 2, \'0\', STR_PAD_LEFT);', - ' $hex .= str_pad(dechex($b), 2, \'0\', STR_PAD_LEFT);', - ' return $hex;', - '}']); + const c1 = PHP.valueToCode(block, 'COLOUR1', PHP.ORDER_NONE) || '\'#000000\''; + const c2 = PHP.valueToCode(block, 'COLOUR2', PHP.ORDER_NONE) || '\'#000000\''; + const ratio = PHP.valueToCode(block, 'RATIO', PHP.ORDER_NONE) || 0.5; + const functionName = PHP.provideFunction_('colour_blend', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($c1, $c2, $ratio) {', + ' $ratio = max(min($ratio, 1), 0);', ' $r1 = hexdec(substr($c1, 1, 2));', + ' $g1 = hexdec(substr($c1, 3, 2));', ' $b1 = hexdec(substr($c1, 5, 2));', + ' $r2 = hexdec(substr($c2, 1, 2));', ' $g2 = hexdec(substr($c2, 3, 2));', + ' $b2 = hexdec(substr($c2, 5, 2));', + ' $r = round($r1 * (1 - $ratio) + $r2 * $ratio);', + ' $g = round($g1 * (1 - $ratio) + $g2 * $ratio);', + ' $b = round($b1 * (1 - $ratio) + $b2 * $ratio);', ' $hex = \'#\';', + ' $hex .= str_pad(dechex($r), 2, \'0\', STR_PAD_LEFT);', + ' $hex .= str_pad(dechex($g), 2, \'0\', STR_PAD_LEFT);', + ' $hex .= str_pad(dechex($b), 2, \'0\', STR_PAD_LEFT);', ' return $hex;', + '}' + ]); const code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; diff --git a/generators/php/lists.js b/generators/php/lists.js index 420a5c576..efc237887 100644 --- a/generators/php/lists.js +++ b/generators/php/lists.js @@ -6,7 +6,6 @@ /** * @fileoverview Generating PHP for list blocks. - * @suppress {missingRequire} */ /** @@ -21,77 +20,63 @@ */ 'use strict'; -goog.provide('Blockly.PHP.lists'); +goog.module('Blockly.PHP.lists'); -goog.require('Blockly.PHP'); -goog.require('Blockly.utils.string'); +const PHP = goog.require('Blockly.PHP'); +const stringUtils = goog.require('Blockly.utils.string'); +const {NameType} = goog.require('Blockly.Names'); -Blockly.PHP['lists_create_empty'] = function(block) { +PHP['lists_create_empty'] = function(block) { // Create an empty list. - return ['array()', Blockly.PHP.ORDER_FUNCTION_CALL]; + return ['array()', PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['lists_create_with'] = function(block) { +PHP['lists_create_with'] = function(block) { // Create a list with any number of elements of any type. let code = new Array(block.itemCount_); for (let i = 0; i < block.itemCount_; i++) { - code[i] = Blockly.PHP.valueToCode(block, 'ADD' + i, - Blockly.PHP.ORDER_NONE) || 'null'; + code[i] = PHP.valueToCode(block, 'ADD' + i, PHP.ORDER_NONE) || 'null'; } code = 'array(' + code.join(', ') + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['lists_repeat'] = function(block) { +PHP['lists_repeat'] = function(block) { // Create a list with one element repeated. - const functionName = Blockly.PHP.provideFunction_( - 'lists_repeat', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($value, $count) {', - ' $array = array();', - ' for ($index = 0; $index < $count; $index++) {', - ' $array[] = $value;', - ' }', - ' return $array;', - '}']); - const element = Blockly.PHP.valueToCode(block, 'ITEM', - Blockly.PHP.ORDER_NONE) || 'null'; - const repeatCount = Blockly.PHP.valueToCode(block, 'NUM', - Blockly.PHP.ORDER_NONE) || '0'; + const functionName = PHP.provideFunction_('lists_repeat', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($value, $count) {', + ' $array = array();', ' for ($index = 0; $index < $count; $index++) {', + ' $array[] = $value;', ' }', ' return $array;', '}' + ]); + const element = PHP.valueToCode(block, 'ITEM', PHP.ORDER_NONE) || 'null'; + const repeatCount = PHP.valueToCode(block, 'NUM', PHP.ORDER_NONE) || '0'; const code = functionName + '(' + element + ', ' + repeatCount + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['lists_length'] = function(block) { +PHP['lists_length'] = function(block) { // String or array length. - const functionName = Blockly.PHP.provideFunction_( - 'length', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($value) {', - ' if (is_string($value)) {', - ' return strlen($value);', - ' } else {', - ' return count($value);', - ' }', - '}']); - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || '\'\''; - return [functionName + '(' + list + ')', Blockly.PHP.ORDER_FUNCTION_CALL]; + const functionName = PHP.provideFunction_('length', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($value) {', + ' if (is_string($value)) {', ' return strlen($value);', ' } else {', + ' return count($value);', ' }', '}' + ]); + const list = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || '\'\''; + return [functionName + '(' + list + ')', PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['lists_isEmpty'] = function(block) { +PHP['lists_isEmpty'] = function(block) { // Is the string null or array empty? - const argument0 = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()'; - return ['empty(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL]; + const argument0 = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_FUNCTION_CALL) || 'array()'; + return ['empty(' + argument0 + ')', PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['lists_indexOf'] = function(block) { +PHP['lists_indexOf'] = function(block) { // Find an item in the list. - const argument0 = Blockly.PHP.valueToCode(block, 'FIND', - Blockly.PHP.ORDER_NONE) || '\'\''; - const argument1 = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_MEMBER) || '[]'; + const argument0 = PHP.valueToCode(block, 'FIND', PHP.ORDER_NONE) || '\'\''; + const argument1 = PHP.valueToCode(block, 'VALUE', PHP.ORDER_MEMBER) || '[]'; let errorIndex = ' -1'; let indexAdjustment = ''; if (block.workspace.options.oneBasedIndex) { @@ -101,144 +86,131 @@ Blockly.PHP['lists_indexOf'] = function(block) { let functionName; if (block.getFieldValue('END') === 'FIRST') { // indexOf - functionName = Blockly.PHP.provideFunction_( - 'indexOf', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($haystack, $needle) {', - ' for ($index = 0; $index < count($haystack); $index++) {', - ' if ($haystack[$index] == $needle) return $index' + - indexAdjustment + ';', - ' }', - ' return ' + errorIndex + ';', - '}']); + functionName = PHP.provideFunction_('indexOf', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($haystack, $needle) {', + ' for ($index = 0; $index < count($haystack); $index++) {', + ' if ($haystack[$index] == $needle) return $index' + indexAdjustment + + ';', + ' }', ' return ' + errorIndex + ';', '}' + ]); } else { // lastIndexOf - functionName = Blockly.PHP.provideFunction_( - 'lastIndexOf', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($haystack, $needle) {', - ' $last = ' + errorIndex + ';', - ' for ($index = 0; $index < count($haystack); $index++) {', - ' if ($haystack[$index] == $needle) $last = $index' + - indexAdjustment + ';', - ' }', - ' return $last;', - '}']); + functionName = PHP.provideFunction_('lastIndexOf', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($haystack, $needle) {', + ' $last = ' + errorIndex + ';', + ' for ($index = 0; $index < count($haystack); $index++) {', + ' if ($haystack[$index] == $needle) $last = $index' + indexAdjustment + + ';', + ' }', ' return $last;', '}' + ]); } const code = functionName + '(' + argument1 + ', ' + argument0 + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['lists_getIndex'] = function(block) { +PHP['lists_getIndex'] = function(block) { // Get element at index. const mode = block.getFieldValue('MODE') || 'GET'; const where = block.getFieldValue('WHERE') || 'FROM_START'; switch (where) { case 'FIRST': if (mode === 'GET') { - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_MEMBER) || 'array()'; + const list = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_MEMBER) || 'array()'; const code = list + '[0]'; - return [code, Blockly.PHP.ORDER_MEMBER]; + return [code, PHP.ORDER_MEMBER]; } else if (mode === 'GET_REMOVE') { - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || 'array()'; + const list = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'array()'; const code = 'array_shift(' + list + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } else if (mode === 'REMOVE') { - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || 'array()'; + const list = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'array()'; return 'array_shift(' + list + ');\n'; } break; case 'LAST': if (mode === 'GET') { - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || 'array()'; + const list = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'array()'; const code = 'end(' + list + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } else if (mode === 'GET_REMOVE') { - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || 'array()'; + const list = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'array()'; const code = 'array_pop(' + list + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } else if (mode === 'REMOVE') { - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || 'array()'; + const list = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'array()'; return 'array_pop(' + list + ');\n'; } break; case 'FROM_START': { - const at = Blockly.PHP.getAdjusted(block, 'AT'); + const at = PHP.getAdjusted(block, 'AT'); if (mode === 'GET') { - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_MEMBER) || 'array()'; + const list = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_MEMBER) || 'array()'; const code = list + '[' + at + ']'; - return [code, Blockly.PHP.ORDER_MEMBER]; + return [code, PHP.ORDER_MEMBER]; } else if (mode === 'GET_REMOVE') { - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || 'array()'; + const list = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'array()'; const code = 'array_splice(' + list + ', ' + at + ', 1)[0]'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } else if (mode === 'REMOVE') { - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || 'array()'; + const list = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'array()'; return 'array_splice(' + list + ', ' + at + ', 1);\n'; } break; } case 'FROM_END': if (mode === 'GET') { - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || 'array()'; - const at = Blockly.PHP.getAdjusted(block, 'AT', 1, true); + const list = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'array()'; + const at = PHP.getAdjusted(block, 'AT', 1, true); const code = 'array_slice(' + list + ', ' + at + ', 1)[0]'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } else if (mode === 'GET_REMOVE' || mode === 'REMOVE') { - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || 'array()'; - const at = Blockly.PHP.getAdjusted(block, 'AT', 1, false, - Blockly.PHP.ORDER_SUBTRACTION); - const code = 'array_splice(' + list + - ', count(' + list + ') - ' + at + ', 1)[0]'; + const list = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'array()'; + const at = + PHP.getAdjusted(block, 'AT', 1, false, PHP.ORDER_SUBTRACTION); + const code = 'array_splice(' + list + ', count(' + list + ') - ' + at + + ', 1)[0]'; if (mode === 'GET_REMOVE') { - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } else if (mode === 'REMOVE') { return code + ';\n'; } } break; case 'RANDOM': { - const list = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || 'array()'; + const list = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'array()'; if (mode === 'GET') { - const functionName = Blockly.PHP.provideFunction_( - 'lists_get_random_item', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($list) {', - ' return $list[rand(0,count($list)-1)];', - '}']); + const functionName = PHP.provideFunction_('lists_get_random_item', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($list) {', + ' return $list[rand(0,count($list)-1)];', '}' + ]); const code = functionName + '(' + list + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } else if (mode === 'GET_REMOVE') { - const functionName = Blockly.PHP.provideFunction_( - 'lists_get_remove_random_item', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '(&$list) {', - ' $x = rand(0,count($list)-1);', - ' unset($list[$x]);', - ' return array_values($list);', - '}']); + const functionName = + PHP.provideFunction_('lists_get_remove_random_item', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '(&$list) {', + ' $x = rand(0,count($list)-1);', ' unset($list[$x]);', + ' return array_values($list);', '}' + ]); const code = functionName + '(' + list + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } else if (mode === 'REMOVE') { - const functionName = Blockly.PHP.provideFunction_( - 'lists_remove_random_item', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '(&$list) {', - ' unset($list[rand(0,count($list)-1)]);', - '}']); + const functionName = PHP.provideFunction_('lists_remove_random_item', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '(&$list) {', + ' unset($list[rand(0,count($list)-1)]);', '}' + ]); return functionName + '(' + list + ');\n'; } break; @@ -247,13 +219,12 @@ Blockly.PHP['lists_getIndex'] = function(block) { throw Error('Unhandled combination (lists_getIndex).'); }; -Blockly.PHP['lists_setIndex'] = function(block) { +PHP['lists_setIndex'] = function(block) { // Set element at index. // Note: Until February 2013 this block did not have MODE or WHERE inputs. const mode = block.getFieldValue('MODE') || 'GET'; const where = block.getFieldValue('WHERE') || 'FROM_START'; - const value = Blockly.PHP.valueToCode(block, 'TO', - Blockly.PHP.ORDER_ASSIGNMENT) || 'null'; + const value = PHP.valueToCode(block, 'TO', PHP.ORDER_ASSIGNMENT) || 'null'; // Cache non-trivial values to variables to prevent repeated look-ups. // Closure, which accesses and modifies 'list'. let cachedList; @@ -261,8 +232,7 @@ Blockly.PHP['lists_setIndex'] = function(block) { if (cachedList.match(/^\$\w+$/)) { return ''; } - const listVar = Blockly.PHP.nameDB_.getDistinctName( - 'tmp_list', Blockly.VARIABLE_CATEGORY_NAME); + const listVar = PHP.nameDB_.getDistinctName('tmp_list', NameType.VARIABLE); const code = listVar + ' = &' + cachedList + ';\n'; cachedList = listVar; return code; @@ -270,25 +240,22 @@ Blockly.PHP['lists_setIndex'] = function(block) { switch (where) { case 'FIRST': if (mode === 'SET') { - const list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_MEMBER) || 'array()'; + const list = + PHP.valueToCode(block, 'LIST', PHP.ORDER_MEMBER) || 'array()'; return list + '[0] = ' + value + ';\n'; } else if (mode === 'INSERT') { - const list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_NONE) || 'array()'; + const list = + PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || 'array()'; return 'array_unshift(' + list + ', ' + value + ');\n'; } break; case 'LAST': { - const list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_NONE) || 'array()'; + const list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || 'array()'; if (mode === 'SET') { - const functionName = Blockly.PHP.provideFunction_( - 'lists_set_last_item', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '(&$list, $value) {', - ' $list[count($list) - 1] = $value;', - '}']); + const functionName = PHP.provideFunction_('lists_set_last_item', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '(&$list, $value) {', + ' $list[count($list) - 1] = $value;', '}' + ]); return functionName + '(' + list + ', ' + value + ');\n'; } else if (mode === 'INSERT') { return 'array_push(' + list + ', ' + value + ');\n'; @@ -296,55 +263,50 @@ Blockly.PHP['lists_setIndex'] = function(block) { break; } case 'FROM_START': { - const at = Blockly.PHP.getAdjusted(block, 'AT'); + const at = PHP.getAdjusted(block, 'AT'); if (mode === 'SET') { - const list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_MEMBER) || 'array()'; + const list = + PHP.valueToCode(block, 'LIST', PHP.ORDER_MEMBER) || 'array()'; return list + '[' + at + '] = ' + value + ';\n'; } else if (mode === 'INSERT') { - const list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_NONE) || 'array()'; + const list = + PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || 'array()'; return 'array_splice(' + list + ', ' + at + ', 0, ' + value + ');\n'; } break; } case 'FROM_END': { - const list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_NONE) || 'array()'; - const at = Blockly.PHP.getAdjusted(block, 'AT', 1); + const list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || 'array()'; + const at = PHP.getAdjusted(block, 'AT', 1); if (mode === 'SET') { - const functionName = Blockly.PHP.provideFunction_( - 'lists_set_from_end', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '(&$list, $at, $value) {', - ' $list[count($list) - $at] = $value;', - '}']); + const functionName = PHP.provideFunction_('lists_set_from_end', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + + '(&$list, $at, $value) {', + ' $list[count($list) - $at] = $value;', '}' + ]); return functionName + '(' + list + ', ' + at + ', ' + value + ');\n'; } else if (mode === 'INSERT') { - const functionName = Blockly.PHP.provideFunction_( - 'lists_insert_from_end', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '(&$list, $at, $value) {', - ' return array_splice($list, count($list) - $at, 0, $value);', - '}']); + const functionName = PHP.provideFunction_('lists_insert_from_end', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + + '(&$list, $at, $value) {', + ' return array_splice($list, count($list) - $at, 0, $value);', '}' + ]); return functionName + '(' + list + ', ' + at + ', ' + value + ');\n'; } break; } case 'RANDOM': - cachedList = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_REFERENCE) || 'array()'; + cachedList = + PHP.valueToCode(block, 'LIST', PHP.ORDER_REFERENCE) || 'array()'; let code = cacheList(); const list = cachedList; - const xVar = Blockly.PHP.nameDB_.getDistinctName( - 'tmp_x', Blockly.VARIABLE_CATEGORY_NAME); + const xVar = PHP.nameDB_.getDistinctName('tmp_x', NameType.VARIABLE); code += xVar + ' = rand(0, count(' + list + ')-1);\n'; if (mode === 'SET') { code += list + '[' + xVar + '] = ' + value + ';\n'; return code; } else if (mode === 'INSERT') { - code += 'array_splice(' + list + ', ' + xVar + ', 0, ' + value + - ');\n'; + code += 'array_splice(' + list + ', ' + xVar + ', 0, ' + value + ');\n'; return code; } break; @@ -352,27 +314,26 @@ Blockly.PHP['lists_setIndex'] = function(block) { throw Error('Unhandled combination (lists_setIndex).'); }; -Blockly.PHP['lists_getSublist'] = function(block) { +PHP['lists_getSublist'] = function(block) { // Get sublist. - const list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_NONE) || 'array()'; + const list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || 'array()'; const where1 = block.getFieldValue('WHERE1'); const where2 = block.getFieldValue('WHERE2'); let code; if (where1 === 'FIRST' && where2 === 'LAST') { code = list; - } else if (list.match(/^\$\w+$/) || + } else if ( + list.match(/^\$\w+$/) || (where1 !== 'FROM_END' && where2 === 'FROM_START')) { // If the list is a simple value or doesn't require a call for length, don't // generate a helper function. let at1; switch (where1) { case 'FROM_START': - at1 = Blockly.PHP.getAdjusted(block, 'AT1'); + at1 = PHP.getAdjusted(block, 'AT1'); break; case 'FROM_END': - at1 = Blockly.PHP.getAdjusted(block, 'AT1', 1, false, - Blockly.PHP.ORDER_SUBTRACTION); + at1 = PHP.getAdjusted(block, 'AT1', 1, false, PHP.ORDER_SUBTRACTION); at1 = 'count(' + list + ') - ' + at1; break; case 'FIRST': @@ -385,10 +346,10 @@ Blockly.PHP['lists_getSublist'] = function(block) { let length; switch (where2) { case 'FROM_START': - at2 = Blockly.PHP.getAdjusted(block, 'AT2', 0, false, - Blockly.PHP.ORDER_SUBTRACTION); + at2 = PHP.getAdjusted(block, 'AT2', 0, false, PHP.ORDER_SUBTRACTION); length = at2 + ' - '; - if (Blockly.utils.string.isNumber(String(at1)) || String(at1).match(/^\(.+\)$/)) { + if (stringUtils.isNumber(String(at1)) || + String(at1).match(/^\(.+\)$/)) { length += at1; } else { length += '(' + at1 + ')'; @@ -396,10 +357,10 @@ Blockly.PHP['lists_getSublist'] = function(block) { length += ' + 1'; break; case 'FROM_END': - at2 = Blockly.PHP.getAdjusted(block, 'AT2', 0, false, - Blockly.PHP.ORDER_SUBTRACTION); + at2 = PHP.getAdjusted(block, 'AT2', 0, false, PHP.ORDER_SUBTRACTION); length = 'count(' + list + ') - ' + at2 + ' - '; - if (Blockly.utils.string.isNumber(String(at1)) || String(at1).match(/^\(.+\)$/)) { + if (stringUtils.isNumber(String(at1)) || + String(at1).match(/^\(.+\)$/)) { length += at1; } else { length += '(' + at1 + ')'; @@ -407,7 +368,8 @@ Blockly.PHP['lists_getSublist'] = function(block) { break; case 'LAST': length = 'count(' + list + ') - '; - if (Blockly.utils.string.isNumber(String(at1)) || String(at1).match(/^\(.+\)$/)) { + if (stringUtils.isNumber(String(at1)) || + String(at1).match(/^\(.+\)$/)) { length += at1; } else { length += '(' + at1 + ')'; @@ -418,71 +380,61 @@ Blockly.PHP['lists_getSublist'] = function(block) { } code = 'array_slice(' + list + ', ' + at1 + ', ' + length + ')'; } else { - const at1 = Blockly.PHP.getAdjusted(block, 'AT1'); - const at2 = Blockly.PHP.getAdjusted(block, 'AT2'); - const functionName = Blockly.PHP.provideFunction_( - 'lists_get_sublist', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($list, $where1, $at1, $where2, $at2) {', - ' if ($where1 == \'FROM_END\') {', - ' $at1 = count($list) - 1 - $at1;', - ' } else if ($where1 == \'FIRST\') {', - ' $at1 = 0;', - ' } else if ($where1 != \'FROM_START\') {', - ' throw new Exception(\'Unhandled option (lists_get_sublist).\');', - ' }', - ' $length = 0;', - ' if ($where2 == \'FROM_START\') {', - ' $length = $at2 - $at1 + 1;', - ' } else if ($where2 == \'FROM_END\') {', - ' $length = count($list) - $at1 - $at2;', - ' } else if ($where2 == \'LAST\') {', - ' $length = count($list) - $at1;', - ' } else {', - ' throw new Exception(\'Unhandled option (lists_get_sublist).\');', - ' }', - ' return array_slice($list, $at1, $length);', - '}']); - code = functionName + '(' + list + ', \'' + - where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')'; + const at1 = PHP.getAdjusted(block, 'AT1'); + const at2 = PHP.getAdjusted(block, 'AT2'); + const functionName = PHP.provideFunction_('lists_get_sublist', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + + '($list, $where1, $at1, $where2, $at2) {', + ' if ($where1 == \'FROM_END\') {', + ' $at1 = count($list) - 1 - $at1;', + ' } else if ($where1 == \'FIRST\') {', + ' $at1 = 0;', + ' } else if ($where1 != \'FROM_START\') {', + ' throw new Exception(\'Unhandled option (lists_get_sublist).\');', + ' }', + ' $length = 0;', + ' if ($where2 == \'FROM_START\') {', + ' $length = $at2 - $at1 + 1;', + ' } else if ($where2 == \'FROM_END\') {', + ' $length = count($list) - $at1 - $at2;', + ' } else if ($where2 == \'LAST\') {', + ' $length = count($list) - $at1;', + ' } else {', + ' throw new Exception(\'Unhandled option (lists_get_sublist).\');', + ' }', + ' return array_slice($list, $at1, $length);', + '}' + ]); + code = functionName + '(' + list + ', \'' + where1 + '\', ' + at1 + ', \'' + + where2 + '\', ' + at2 + ')'; } - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['lists_sort'] = function(block) { +PHP['lists_sort'] = function(block) { // Block for sorting a list. - const listCode = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_NONE) || 'array()'; + const listCode = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || 'array()'; const direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1; const type = block.getFieldValue('TYPE'); - const functionName = Blockly.PHP.provideFunction_( - 'lists_sort', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($list, $type, $direction) {', - ' $sortCmpFuncs = array(', - ' "NUMERIC" => "strnatcasecmp",', - ' "TEXT" => "strcmp",', - ' "IGNORE_CASE" => "strcasecmp"', - ' );', - ' $sortCmp = $sortCmpFuncs[$type];', - ' $list2 = $list;', // Clone list. - ' usort($list2, $sortCmp);', - ' if ($direction == -1) {', - ' $list2 = array_reverse($list2);', - ' }', - ' return $list2;', - '}']); - const sortCode = functionName + - '(' + listCode + ', "' + type + '", ' + direction + ')'; - return [sortCode, Blockly.PHP.ORDER_FUNCTION_CALL]; + const functionName = PHP.provideFunction_('lists_sort', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + + '($list, $type, $direction) {', + ' $sortCmpFuncs = array(', ' "NUMERIC" => "strnatcasecmp",', + ' "TEXT" => "strcmp",', ' "IGNORE_CASE" => "strcasecmp"', ' );', + ' $sortCmp = $sortCmpFuncs[$type];', + ' $list2 = $list;', // Clone list. + ' usort($list2, $sortCmp);', ' if ($direction == -1) {', + ' $list2 = array_reverse($list2);', ' }', ' return $list2;', '}' + ]); + const sortCode = + functionName + '(' + listCode + ', "' + type + '", ' + direction + ')'; + return [sortCode, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['lists_split'] = function(block) { +PHP['lists_split'] = function(block) { // Block for splitting text into a list, or joining a list into text. - let value_input = Blockly.PHP.valueToCode(block, 'INPUT', - Blockly.PHP.ORDER_NONE); - const value_delim = Blockly.PHP.valueToCode(block, 'DELIM', - Blockly.PHP.ORDER_NONE) || '\'\''; + let value_input = PHP.valueToCode(block, 'INPUT', PHP.ORDER_NONE); + const value_delim = PHP.valueToCode(block, 'DELIM', PHP.ORDER_NONE) || '\'\''; const mode = block.getFieldValue('MODE'); let functionName; if (mode === 'SPLIT') { @@ -499,13 +451,12 @@ Blockly.PHP['lists_split'] = function(block) { throw Error('Unknown mode: ' + mode); } const code = functionName + '(' + value_delim + ', ' + value_input + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['lists_reverse'] = function(block) { +PHP['lists_reverse'] = function(block) { // Block for reversing a list. - const list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_NONE) || '[]'; + const list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]'; const code = 'array_reverse(' + list + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; diff --git a/generators/php/logic.js b/generators/php/logic.js index de632f1fe..a7f6c7062 100644 --- a/generators/php/logic.js +++ b/generators/php/logic.js @@ -9,73 +9,66 @@ */ 'use strict'; -goog.provide('Blockly.PHP.logic'); +goog.module('Blockly.PHP.logic'); -goog.require('Blockly.PHP'); +const PHP = goog.require('Blockly.PHP'); -Blockly.PHP['controls_if'] = function(block) { +PHP['controls_if'] = function(block) { // If/elseif/else condition. let n = 0; let code = '', branchCode, conditionCode; - if (Blockly.PHP.STATEMENT_PREFIX) { + if (PHP.STATEMENT_PREFIX) { // Automatic prefix insertion is switched off for this block. Add manually. - code += Blockly.PHP.injectId(Blockly.PHP.STATEMENT_PREFIX, block); + code += PHP.injectId(PHP.STATEMENT_PREFIX, block); } do { - conditionCode = Blockly.PHP.valueToCode(block, 'IF' + n, - Blockly.PHP.ORDER_NONE) || 'false'; - branchCode = Blockly.PHP.statementToCode(block, 'DO' + n); - if (Blockly.PHP.STATEMENT_SUFFIX) { - branchCode = Blockly.PHP.prefixLines( - Blockly.PHP.injectId(Blockly.PHP.STATEMENT_SUFFIX, block), - Blockly.PHP.INDENT) + branchCode; + conditionCode = PHP.valueToCode(block, 'IF' + n, PHP.ORDER_NONE) || 'false'; + branchCode = PHP.statementToCode(block, 'DO' + n); + if (PHP.STATEMENT_SUFFIX) { + branchCode = PHP.prefixLines( + PHP.injectId(PHP.STATEMENT_SUFFIX, block), PHP.INDENT) + + branchCode; } - code += (n > 0 ? ' else ' : '') + - 'if (' + conditionCode + ') {\n' + branchCode + '}'; + code += (n > 0 ? ' else ' : '') + 'if (' + conditionCode + ') {\n' + + branchCode + '}'; n++; } while (block.getInput('IF' + n)); - if (block.getInput('ELSE') || Blockly.PHP.STATEMENT_SUFFIX) { - branchCode = Blockly.PHP.statementToCode(block, 'ELSE'); - if (Blockly.PHP.STATEMENT_SUFFIX) { - branchCode = Blockly.PHP.prefixLines( - Blockly.PHP.injectId(Blockly.PHP.STATEMENT_SUFFIX, block), - Blockly.PHP.INDENT) + branchCode; + if (block.getInput('ELSE') || PHP.STATEMENT_SUFFIX) { + branchCode = PHP.statementToCode(block, 'ELSE'); + if (PHP.STATEMENT_SUFFIX) { + branchCode = PHP.prefixLines( + PHP.injectId(PHP.STATEMENT_SUFFIX, block), PHP.INDENT) + + branchCode; } code += ' else {\n' + branchCode + '}'; } return code + '\n'; }; -Blockly.PHP['controls_ifelse'] = Blockly.PHP['controls_if']; +PHP['controls_ifelse'] = PHP['controls_if']; -Blockly.PHP['logic_compare'] = function(block) { +PHP['logic_compare'] = function(block) { // Comparison operator. - const OPERATORS = { - 'EQ': '==', - 'NEQ': '!=', - 'LT': '<', - 'LTE': '<=', - 'GT': '>', - 'GTE': '>=' - }; + const OPERATORS = + {'EQ': '==', 'NEQ': '!=', 'LT': '<', 'LTE': '<=', 'GT': '>', 'GTE': '>='}; const operator = OPERATORS[block.getFieldValue('OP')]; - const order = (operator === '==' || operator === '!=') ? - Blockly.PHP.ORDER_EQUALITY : Blockly.PHP.ORDER_RELATIONAL; - const argument0 = Blockly.PHP.valueToCode(block, 'A', order) || '0'; - const argument1 = Blockly.PHP.valueToCode(block, 'B', order) || '0'; + const order = (operator === '==' || operator === '!=') ? PHP.ORDER_EQUALITY : + PHP.ORDER_RELATIONAL; + const argument0 = PHP.valueToCode(block, 'A', order) || '0'; + const argument1 = PHP.valueToCode(block, 'B', order) || '0'; const code = argument0 + ' ' + operator + ' ' + argument1; return [code, order]; }; -Blockly.PHP['logic_operation'] = function(block) { +PHP['logic_operation'] = function(block) { // Operations 'and', 'or'. const operator = (block.getFieldValue('OP') === 'AND') ? '&&' : '||'; - const order = (operator === '&&') ? Blockly.PHP.ORDER_LOGICAL_AND : - Blockly.PHP.ORDER_LOGICAL_OR; - let argument0 = Blockly.PHP.valueToCode(block, 'A', order); - let argument1 = Blockly.PHP.valueToCode(block, 'B', order); + const order = + (operator === '&&') ? PHP.ORDER_LOGICAL_AND : PHP.ORDER_LOGICAL_OR; + let argument0 = PHP.valueToCode(block, 'A', order); + let argument1 = PHP.valueToCode(block, 'B', order); if (!argument0 && !argument1) { // If there are no arguments, then the return value is false. argument0 = 'false'; @@ -94,34 +87,33 @@ Blockly.PHP['logic_operation'] = function(block) { return [code, order]; }; -Blockly.PHP['logic_negate'] = function(block) { +PHP['logic_negate'] = function(block) { // Negation. - const order = Blockly.PHP.ORDER_LOGICAL_NOT; - const argument0 = Blockly.PHP.valueToCode(block, 'BOOL', order) || - 'true'; + const order = PHP.ORDER_LOGICAL_NOT; + const argument0 = PHP.valueToCode(block, 'BOOL', order) || 'true'; const code = '!' + argument0; return [code, order]; }; -Blockly.PHP['logic_boolean'] = function(block) { +PHP['logic_boolean'] = function(block) { // Boolean values true and false. const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'true' : 'false'; - return [code, Blockly.PHP.ORDER_ATOMIC]; + return [code, PHP.ORDER_ATOMIC]; }; -Blockly.PHP['logic_null'] = function(block) { +PHP['logic_null'] = function(block) { // Null data type. - return ['null', Blockly.PHP.ORDER_ATOMIC]; + return ['null', PHP.ORDER_ATOMIC]; }; -Blockly.PHP['logic_ternary'] = function(block) { +PHP['logic_ternary'] = function(block) { // Ternary operator. - const value_if = Blockly.PHP.valueToCode(block, 'IF', - Blockly.PHP.ORDER_CONDITIONAL) || 'false'; - const value_then = Blockly.PHP.valueToCode(block, 'THEN', - Blockly.PHP.ORDER_CONDITIONAL) || 'null'; - const value_else = Blockly.PHP.valueToCode(block, 'ELSE', - Blockly.PHP.ORDER_CONDITIONAL) || 'null'; + const value_if = + PHP.valueToCode(block, 'IF', PHP.ORDER_CONDITIONAL) || 'false'; + const value_then = + PHP.valueToCode(block, 'THEN', PHP.ORDER_CONDITIONAL) || 'null'; + const value_else = + PHP.valueToCode(block, 'ELSE', PHP.ORDER_CONDITIONAL) || 'null'; const code = value_if + ' ? ' + value_then + ' : ' + value_else; - return [code, Blockly.PHP.ORDER_CONDITIONAL]; + return [code, PHP.ORDER_CONDITIONAL]; }; diff --git a/generators/php/loops.js b/generators/php/loops.js index b30c93162..6803c24d6 100644 --- a/generators/php/loops.js +++ b/generators/php/loops.js @@ -6,17 +6,17 @@ /** * @fileoverview Generating PHP for loop blocks. - * @suppress {missingRequire} */ 'use strict'; -goog.provide('Blockly.PHP.loops'); +goog.module('Blockly.PHP.loops'); -goog.require('Blockly.PHP'); -goog.require('Blockly.utils.string'); +const PHP = goog.require('Blockly.PHP'); +const stringUtils = goog.require('Blockly.utils.string'); +const {NameType} = goog.require('Blockly.Names'); -Blockly.PHP['controls_repeat_ext'] = function(block) { +PHP['controls_repeat_ext'] = function(block) { // Repeat n times. let repeats; if (block.getField('TIMES')) { @@ -24,63 +24,55 @@ Blockly.PHP['controls_repeat_ext'] = function(block) { repeats = String(Number(block.getFieldValue('TIMES'))); } else { // External number. - repeats = Blockly.PHP.valueToCode(block, 'TIMES', - Blockly.PHP.ORDER_ASSIGNMENT) || '0'; + repeats = PHP.valueToCode(block, 'TIMES', PHP.ORDER_ASSIGNMENT) || '0'; } - let branch = Blockly.PHP.statementToCode(block, 'DO'); - branch = Blockly.PHP.addLoopTrap(branch, block); + let branch = PHP.statementToCode(block, 'DO'); + branch = PHP.addLoopTrap(branch, block); let code = ''; - const loopVar = Blockly.PHP.nameDB_.getDistinctName( - 'count', Blockly.VARIABLE_CATEGORY_NAME); + const loopVar = PHP.nameDB_.getDistinctName('count', NameType.VARIABLE); let endVar = repeats; - if (!repeats.match(/^\w+$/) && !Blockly.utils.string.isNumber(repeats)) { - endVar = Blockly.PHP.nameDB_.getDistinctName( - 'repeat_end', Blockly.VARIABLE_CATEGORY_NAME); + if (!repeats.match(/^\w+$/) && !stringUtils.isNumber(repeats)) { + endVar = PHP.nameDB_.getDistinctName('repeat_end', NameType.VARIABLE); code += endVar + ' = ' + repeats + ';\n'; } - code += 'for (' + loopVar + ' = 0; ' + - loopVar + ' < ' + endVar + '; ' + - loopVar + '++) {\n' + - branch + '}\n'; + code += 'for (' + loopVar + ' = 0; ' + loopVar + ' < ' + endVar + '; ' + + loopVar + '++) {\n' + branch + '}\n'; return code; }; -Blockly.PHP['controls_repeat'] = Blockly.PHP['controls_repeat_ext']; +PHP['controls_repeat'] = PHP['controls_repeat_ext']; -Blockly.PHP['controls_whileUntil'] = function(block) { +PHP['controls_whileUntil'] = function(block) { // Do while/until loop. const until = block.getFieldValue('MODE') === 'UNTIL'; - let argument0 = Blockly.PHP.valueToCode(block, 'BOOL', - until ? Blockly.PHP.ORDER_LOGICAL_NOT : - Blockly.PHP.ORDER_NONE) || 'false'; - let branch = Blockly.PHP.statementToCode(block, 'DO'); - branch = Blockly.PHP.addLoopTrap(branch, block); + let argument0 = + PHP.valueToCode( + block, 'BOOL', until ? PHP.ORDER_LOGICAL_NOT : PHP.ORDER_NONE) || + 'false'; + let branch = PHP.statementToCode(block, 'DO'); + branch = PHP.addLoopTrap(branch, block); if (until) { argument0 = '!' + argument0; } return 'while (' + argument0 + ') {\n' + branch + '}\n'; }; -Blockly.PHP['controls_for'] = function(block) { +PHP['controls_for'] = function(block) { // For loop. - const variable0 = Blockly.PHP.nameDB_.getName( - block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); - const argument0 = Blockly.PHP.valueToCode(block, 'FROM', - Blockly.PHP.ORDER_ASSIGNMENT) || '0'; - const argument1 = Blockly.PHP.valueToCode(block, 'TO', - Blockly.PHP.ORDER_ASSIGNMENT) || '0'; - const increment = Blockly.PHP.valueToCode(block, 'BY', - Blockly.PHP.ORDER_ASSIGNMENT) || '1'; - let branch = Blockly.PHP.statementToCode(block, 'DO'); - branch = Blockly.PHP.addLoopTrap(branch, block); + const variable0 = + PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE); + const argument0 = PHP.valueToCode(block, 'FROM', PHP.ORDER_ASSIGNMENT) || '0'; + const argument1 = PHP.valueToCode(block, 'TO', PHP.ORDER_ASSIGNMENT) || '0'; + const increment = PHP.valueToCode(block, 'BY', PHP.ORDER_ASSIGNMENT) || '1'; + let branch = PHP.statementToCode(block, 'DO'); + branch = PHP.addLoopTrap(branch, block); let code; - if (Blockly.utils.string.isNumber(argument0) && Blockly.utils.string.isNumber(argument1) && - Blockly.utils.string.isNumber(increment)) { + if (stringUtils.isNumber(argument0) && stringUtils.isNumber(argument1) && + stringUtils.isNumber(increment)) { // All arguments are simple numbers. const up = Number(argument0) <= Number(argument1); - code = 'for (' + variable0 + ' = ' + argument0 + '; ' + - variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' + - variable0; + code = 'for (' + variable0 + ' = ' + argument0 + '; ' + variable0 + + (up ? ' <= ' : ' >= ') + argument1 + '; ' + variable0; const step = Math.abs(Number(increment)); if (step === 1) { code += up ? '++' : '--'; @@ -92,73 +84,71 @@ Blockly.PHP['controls_for'] = function(block) { code = ''; // Cache non-trivial values to variables to prevent repeated look-ups. let startVar = argument0; - if (!argument0.match(/^\w+$/) && !Blockly.utils.string.isNumber(argument0)) { - startVar = Blockly.PHP.nameDB_.getDistinctName( - variable0 + '_start', Blockly.VARIABLE_CATEGORY_NAME); + if (!argument0.match(/^\w+$/) && !stringUtils.isNumber(argument0)) { + startVar = + PHP.nameDB_.getDistinctName(variable0 + '_start', NameType.VARIABLE); code += startVar + ' = ' + argument0 + ';\n'; } let endVar = argument1; - if (!argument1.match(/^\w+$/) && !Blockly.utils.string.isNumber(argument1)) { - endVar = Blockly.PHP.nameDB_.getDistinctName( - variable0 + '_end', Blockly.VARIABLE_CATEGORY_NAME); + if (!argument1.match(/^\w+$/) && !stringUtils.isNumber(argument1)) { + endVar = + PHP.nameDB_.getDistinctName(variable0 + '_end', NameType.VARIABLE); code += endVar + ' = ' + argument1 + ';\n'; } // Determine loop direction at start, in case one of the bounds // changes during loop execution. - const incVar = Blockly.PHP.nameDB_.getDistinctName( - variable0 + '_inc', Blockly.VARIABLE_CATEGORY_NAME); + const incVar = + PHP.nameDB_.getDistinctName(variable0 + '_inc', NameType.VARIABLE); code += incVar + ' = '; - if (Blockly.utils.string.isNumber(increment)) { + if (stringUtils.isNumber(increment)) { code += Math.abs(increment) + ';\n'; } else { code += 'abs(' + increment + ');\n'; } code += 'if (' + startVar + ' > ' + endVar + ') {\n'; - code += Blockly.PHP.INDENT + incVar + ' = -' + incVar + ';\n'; + code += PHP.INDENT + incVar + ' = -' + incVar + ';\n'; code += '}\n'; - code += 'for (' + variable0 + ' = ' + startVar + '; ' + - incVar + ' >= 0 ? ' + - variable0 + ' <= ' + endVar + ' : ' + - variable0 + ' >= ' + endVar + '; ' + - variable0 + ' += ' + incVar + ') {\n' + + code += 'for (' + variable0 + ' = ' + startVar + '; ' + incVar + + ' >= 0 ? ' + variable0 + ' <= ' + endVar + ' : ' + variable0 + + ' >= ' + endVar + '; ' + variable0 + ' += ' + incVar + ') {\n' + branch + '}\n'; } return code; }; -Blockly.PHP['controls_forEach'] = function(block) { +PHP['controls_forEach'] = function(block) { // For each loop. - const variable0 = Blockly.PHP.nameDB_.getName( - block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); - const argument0 = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_ASSIGNMENT) || '[]'; - let branch = Blockly.PHP.statementToCode(block, 'DO'); - branch = Blockly.PHP.addLoopTrap(branch, block); + const variable0 = + PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE); + const argument0 = + PHP.valueToCode(block, 'LIST', PHP.ORDER_ASSIGNMENT) || '[]'; + let branch = PHP.statementToCode(block, 'DO'); + branch = PHP.addLoopTrap(branch, block); let code = ''; - code += 'foreach (' + argument0 + ' as ' + variable0 + - ') {\n' + branch + '}\n'; + code += + 'foreach (' + argument0 + ' as ' + variable0 + ') {\n' + branch + '}\n'; return code; }; -Blockly.PHP['controls_flow_statements'] = function(block) { +PHP['controls_flow_statements'] = function(block) { // Flow statements: continue, break. let xfix = ''; - if (Blockly.PHP.STATEMENT_PREFIX) { + if (PHP.STATEMENT_PREFIX) { // Automatic prefix insertion is switched off for this block. Add manually. - xfix += Blockly.PHP.injectId(Blockly.PHP.STATEMENT_PREFIX, block); + xfix += PHP.injectId(PHP.STATEMENT_PREFIX, block); } - if (Blockly.PHP.STATEMENT_SUFFIX) { + if (PHP.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.PHP.injectId(Blockly.PHP.STATEMENT_SUFFIX, block); + xfix += PHP.injectId(PHP.STATEMENT_SUFFIX, block); } - if (Blockly.PHP.STATEMENT_PREFIX) { + if (PHP.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.PHP.injectId(Blockly.PHP.STATEMENT_PREFIX, loop); + xfix += PHP.injectId(PHP.STATEMENT_PREFIX, loop); } } switch (block.getFieldValue('FLOW')) { diff --git a/generators/php/math.js b/generators/php/math.js index fe7c68dc0..d77f47806 100644 --- a/generators/php/math.js +++ b/generators/php/math.js @@ -6,20 +6,19 @@ /** * @fileoverview Generating PHP for math blocks. - * @suppress {missingRequire} */ 'use strict'; -goog.provide('Blockly.PHP.math'); +goog.module('Blockly.PHP.math'); -goog.require('Blockly.PHP'); +const PHP = goog.require('Blockly.PHP'); +const {NameType} = goog.require('Blockly.Names'); -Blockly.PHP['math_number'] = function(block) { +PHP['math_number'] = function(block) { // Numeric value. let code = Number(block.getFieldValue('NUM')); - const order = code >= 0 ? Blockly.PHP.ORDER_ATOMIC : - Blockly.PHP.ORDER_UNARY_NEGATION; + const order = code >= 0 ? PHP.ORDER_ATOMIC : PHP.ORDER_UNARY_NEGATION; if (code === Infinity) { code = 'INF'; } else if (code === -Infinity) { @@ -28,46 +27,43 @@ Blockly.PHP['math_number'] = function(block) { return [code, order]; }; -Blockly.PHP['math_arithmetic'] = function(block) { +PHP['math_arithmetic'] = function(block) { // Basic arithmetic operators, and power. const OPERATORS = { - 'ADD': [' + ', Blockly.PHP.ORDER_ADDITION], - 'MINUS': [' - ', Blockly.PHP.ORDER_SUBTRACTION], - 'MULTIPLY': [' * ', Blockly.PHP.ORDER_MULTIPLICATION], - 'DIVIDE': [' / ', Blockly.PHP.ORDER_DIVISION], - 'POWER': [' ** ', Blockly.PHP.ORDER_POWER] + 'ADD': [' + ', PHP.ORDER_ADDITION], + 'MINUS': [' - ', PHP.ORDER_SUBTRACTION], + 'MULTIPLY': [' * ', PHP.ORDER_MULTIPLICATION], + 'DIVIDE': [' / ', PHP.ORDER_DIVISION], + 'POWER': [' ** ', PHP.ORDER_POWER] }; const tuple = OPERATORS[block.getFieldValue('OP')]; const operator = tuple[0]; const order = tuple[1]; - const argument0 = Blockly.PHP.valueToCode(block, 'A', order) || '0'; - const argument1 = Blockly.PHP.valueToCode(block, 'B', order) || '0'; + const argument0 = PHP.valueToCode(block, 'A', order) || '0'; + const argument1 = PHP.valueToCode(block, 'B', order) || '0'; const code = argument0 + operator + argument1; return [code, order]; }; -Blockly.PHP['math_single'] = function(block) { +PHP['math_single'] = function(block) { // Math operators with single operand. const operator = block.getFieldValue('OP'); let code; let arg; if (operator === 'NEG') { // Negation is a special case given its different operator precedence. - arg = Blockly.PHP.valueToCode(block, 'NUM', - Blockly.PHP.ORDER_UNARY_NEGATION) || '0'; + arg = PHP.valueToCode(block, 'NUM', PHP.ORDER_UNARY_NEGATION) || '0'; if (arg[0] === '-') { // --3 is not legal in JS. arg = ' ' + arg; } code = '-' + arg; - return [code, Blockly.PHP.ORDER_UNARY_NEGATION]; + return [code, PHP.ORDER_UNARY_NEGATION]; } if (operator === 'SIN' || operator === 'COS' || operator === 'TAN') { - arg = Blockly.PHP.valueToCode(block, 'NUM', - Blockly.PHP.ORDER_DIVISION) || '0'; + arg = PHP.valueToCode(block, 'NUM', PHP.ORDER_DIVISION) || '0'; } else { - arg = Blockly.PHP.valueToCode(block, 'NUM', - Blockly.PHP.ORDER_NONE) || '0'; + arg = PHP.valueToCode(block, 'NUM', PHP.ORDER_NONE) || '0'; } // First, handle cases which generate values that don't need parentheses // wrapping the code. @@ -107,7 +103,7 @@ Blockly.PHP['math_single'] = function(block) { break; } if (code) { - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } // Second, handle cases which generate values that may need parentheses // wrapping the code. @@ -127,54 +123,47 @@ Blockly.PHP['math_single'] = function(block) { default: throw Error('Unknown math operator: ' + operator); } - return [code, Blockly.PHP.ORDER_DIVISION]; + return [code, PHP.ORDER_DIVISION]; }; -Blockly.PHP['math_constant'] = function(block) { +PHP['math_constant'] = function(block) { // Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY. const CONSTANTS = { - 'PI': ['M_PI', Blockly.PHP.ORDER_ATOMIC], - 'E': ['M_E', Blockly.PHP.ORDER_ATOMIC], - 'GOLDEN_RATIO': ['(1 + sqrt(5)) / 2', Blockly.PHP.ORDER_DIVISION], - 'SQRT2': ['M_SQRT2', Blockly.PHP.ORDER_ATOMIC], - 'SQRT1_2': ['M_SQRT1_2', Blockly.PHP.ORDER_ATOMIC], - 'INFINITY': ['INF', Blockly.PHP.ORDER_ATOMIC] + 'PI': ['M_PI', PHP.ORDER_ATOMIC], + 'E': ['M_E', PHP.ORDER_ATOMIC], + 'GOLDEN_RATIO': ['(1 + sqrt(5)) / 2', PHP.ORDER_DIVISION], + 'SQRT2': ['M_SQRT2', PHP.ORDER_ATOMIC], + 'SQRT1_2': ['M_SQRT1_2', PHP.ORDER_ATOMIC], + 'INFINITY': ['INF', PHP.ORDER_ATOMIC] }; return CONSTANTS[block.getFieldValue('CONSTANT')]; }; -Blockly.PHP['math_number_property'] = function(block) { +PHP['math_number_property'] = function(block) { // Check if a number is even, odd, prime, whole, positive, or negative // or if it is divisible by certain number. Returns true or false. - const number_to_check = Blockly.PHP.valueToCode(block, 'NUMBER_TO_CHECK', - Blockly.PHP.ORDER_MODULUS) || '0'; + const number_to_check = + PHP.valueToCode(block, 'NUMBER_TO_CHECK', PHP.ORDER_MODULUS) || '0'; const dropdown_property = block.getFieldValue('PROPERTY'); let code; if (dropdown_property === 'PRIME') { // Prime is a special case as it is not a one-liner test. - const functionName = Blockly.PHP.provideFunction_( - 'math_isPrime', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($n) {', - ' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods', - ' if ($n == 2 || $n == 3) {', - ' return true;', - ' }', - ' // False if n is NaN, negative, is 1, or not whole.', - ' // And false if n is divisible by 2 or 3.', - ' if (!is_numeric($n) || $n <= 1 || $n % 1 != 0 || $n % 2 == 0 ||' + - ' $n % 3 == 0) {', - ' return false;', - ' }', - ' // Check all the numbers of form 6k +/- 1, up to sqrt(n).', - ' for ($x = 6; $x <= sqrt($n) + 1; $x += 6) {', - ' if ($n % ($x - 1) == 0 || $n % ($x + 1) == 0) {', - ' return false;', - ' }', - ' }', - ' return true;', - '}']); + const functionName = PHP.provideFunction_('math_isPrime', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($n) {', + ' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods', + ' if ($n == 2 || $n == 3) {', ' return true;', ' }', + ' // False if n is NaN, negative, is 1, or not whole.', + ' // And false if n is divisible by 2 or 3.', + ' if (!is_numeric($n) || $n <= 1 || $n % 1 != 0 || $n % 2 == 0 ||' + + ' $n % 3 == 0) {', + ' return false;', ' }', + ' // Check all the numbers of form 6k +/- 1, up to sqrt(n).', + ' for ($x = 6; $x <= sqrt($n) + 1; $x += 6) {', + ' if ($n % ($x - 1) == 0 || $n % ($x + 1) == 0) {', + ' return false;', ' }', ' }', ' return true;', '}' + ]); code = functionName + '(' + number_to_check + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } switch (dropdown_property) { case 'EVEN': @@ -193,74 +182,68 @@ Blockly.PHP['math_number_property'] = function(block) { code = number_to_check + ' < 0'; break; case 'DIVISIBLE_BY': { - const divisor = Blockly.PHP.valueToCode(block, 'DIVISOR', - Blockly.PHP.ORDER_MODULUS) || '0'; + const divisor = + PHP.valueToCode(block, 'DIVISOR', PHP.ORDER_MODULUS) || '0'; code = number_to_check + ' % ' + divisor + ' == 0'; break; } } - return [code, Blockly.PHP.ORDER_EQUALITY]; + return [code, PHP.ORDER_EQUALITY]; }; -Blockly.PHP['math_change'] = function(block) { +PHP['math_change'] = function(block) { // Add to a variable in place. - const argument0 = Blockly.PHP.valueToCode(block, 'DELTA', - Blockly.PHP.ORDER_ADDITION) || '0'; - const varName = Blockly.PHP.nameDB_.getName( - block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); + const argument0 = PHP.valueToCode(block, 'DELTA', PHP.ORDER_ADDITION) || '0'; + const varName = + PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE); return varName + ' += ' + argument0 + ';\n'; }; // Rounding functions have a single operand. -Blockly.PHP['math_round'] = Blockly.PHP['math_single']; +PHP['math_round'] = PHP['math_single']; // Trigonometry functions have a single operand. -Blockly.PHP['math_trig'] = Blockly.PHP['math_single']; +PHP['math_trig'] = PHP['math_single']; -Blockly.PHP['math_on_list'] = function(block) { +PHP['math_on_list'] = function(block) { // Math functions for lists. const func = block.getFieldValue('OP'); let list; let code; switch (func) { case 'SUM': - list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()'; + list = + PHP.valueToCode(block, 'LIST', PHP.ORDER_FUNCTION_CALL) || 'array()'; code = 'array_sum(' + list + ')'; break; case 'MIN': - list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()'; + list = + PHP.valueToCode(block, 'LIST', PHP.ORDER_FUNCTION_CALL) || 'array()'; code = 'min(' + list + ')'; break; case 'MAX': - list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()'; + list = + PHP.valueToCode(block, 'LIST', PHP.ORDER_FUNCTION_CALL) || 'array()'; code = 'max(' + list + ')'; break; case 'AVERAGE': { - const functionName = Blockly.PHP.provideFunction_( - 'math_mean', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($myList) {', - ' return array_sum($myList) / count($myList);', - '}']); - list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_NONE) || 'array()'; + const functionName = PHP.provideFunction_('math_mean', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($myList) {', + ' return array_sum($myList) / count($myList);', '}' + ]); + list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || 'array()'; code = functionName + '(' + list + ')'; break; } case 'MEDIAN': { - const functionName = Blockly.PHP.provideFunction_( - 'math_median', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($arr) {', - ' sort($arr,SORT_NUMERIC);', - ' return (count($arr) % 2) ? $arr[floor(count($arr)/2)] : ', - ' ($arr[floor(count($arr)/2)] + $arr[floor(count($arr)/2)' + - ' - 1]) / 2;', - '}']); - list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_NONE) || '[]'; + const functionName = PHP.provideFunction_('math_median', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($arr) {', + ' sort($arr,SORT_NUMERIC);', + ' return (count($arr) % 2) ? $arr[floor(count($arr)/2)] : ', + ' ($arr[floor(count($arr)/2)] + $arr[floor(count($arr)/2)' + + ' - 1]) / 2;', + '}' + ]); + list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]'; code = functionName + '(' + list + ')'; break; } @@ -268,110 +251,90 @@ Blockly.PHP['math_on_list'] = function(block) { // As a list of numbers can contain more than one mode, // the returned result is provided as an array. // Mode of [3, 'x', 'x', 1, 1, 2, '3'] -> ['x', 1]. - const functionName = Blockly.PHP.provideFunction_( - 'math_modes', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($values) {', - ' if (empty($values)) return array();', - ' $counts = array_count_values($values);', - ' arsort($counts); // Sort counts in descending order', - ' $modes = array_keys($counts, current($counts), true);', - ' return $modes;', - '}']); - list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_NONE) || '[]'; + const functionName = PHP.provideFunction_('math_modes', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($values) {', + ' if (empty($values)) return array();', + ' $counts = array_count_values($values);', + ' arsort($counts); // Sort counts in descending order', + ' $modes = array_keys($counts, current($counts), true);', + ' return $modes;', '}' + ]); + list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]'; code = functionName + '(' + list + ')'; break; } case 'STD_DEV': { - const functionName = Blockly.PHP.provideFunction_( - 'math_standard_deviation', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($numbers) {', - ' $n = count($numbers);', - ' if (!$n) return null;', - ' $mean = array_sum($numbers) / count($numbers);', - ' foreach($numbers as $key => $num) $devs[$key] = ' + - 'pow($num - $mean, 2);', - ' return sqrt(array_sum($devs) / (count($devs) - 1));', - '}']); - list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_NONE) || '[]'; + const functionName = PHP.provideFunction_('math_standard_deviation', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($numbers) {', + ' $n = count($numbers);', ' if (!$n) return null;', + ' $mean = array_sum($numbers) / count($numbers);', + ' foreach($numbers as $key => $num) $devs[$key] = ' + + 'pow($num - $mean, 2);', + ' return sqrt(array_sum($devs) / (count($devs) - 1));', '}' + ]); + list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]'; code = functionName + '(' + list + ')'; break; } case 'RANDOM': { - const functionName = Blockly.PHP.provideFunction_( - 'math_random_list', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($list) {', - ' $x = rand(0, count($list)-1);', - ' return $list[$x];', - '}']); - list = Blockly.PHP.valueToCode(block, 'LIST', - Blockly.PHP.ORDER_NONE) || '[]'; + const functionName = PHP.provideFunction_('math_random_list', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($list) {', + ' $x = rand(0, count($list)-1);', ' return $list[$x];', '}' + ]); + list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]'; code = functionName + '(' + list + ')'; break; } default: throw Error('Unknown operator: ' + func); } - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['math_modulo'] = function(block) { +PHP['math_modulo'] = function(block) { // Remainder computation. - const argument0 = Blockly.PHP.valueToCode(block, 'DIVIDEND', - Blockly.PHP.ORDER_MODULUS) || '0'; - const argument1 = Blockly.PHP.valueToCode(block, 'DIVISOR', - Blockly.PHP.ORDER_MODULUS) || '0'; + const argument0 = + PHP.valueToCode(block, 'DIVIDEND', PHP.ORDER_MODULUS) || '0'; + const argument1 = PHP.valueToCode(block, 'DIVISOR', PHP.ORDER_MODULUS) || '0'; const code = argument0 + ' % ' + argument1; - return [code, Blockly.PHP.ORDER_MODULUS]; + return [code, PHP.ORDER_MODULUS]; }; -Blockly.PHP['math_constrain'] = function(block) { +PHP['math_constrain'] = function(block) { // Constrain a number between two limits. - const argument0 = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || '0'; - const argument1 = Blockly.PHP.valueToCode(block, 'LOW', - Blockly.PHP.ORDER_NONE) || '0'; - const argument2 = Blockly.PHP.valueToCode(block, 'HIGH', - Blockly.PHP.ORDER_NONE) || 'Infinity'; - const code = 'min(max(' + argument0 + ', ' + argument1 + '), ' + - argument2 + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + const argument0 = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || '0'; + const argument1 = PHP.valueToCode(block, 'LOW', PHP.ORDER_NONE) || '0'; + const argument2 = + PHP.valueToCode(block, 'HIGH', PHP.ORDER_NONE) || 'Infinity'; + const code = + 'min(max(' + argument0 + ', ' + argument1 + '), ' + argument2 + ')'; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['math_random_int'] = function(block) { +PHP['math_random_int'] = function(block) { // Random integer between [X] and [Y]. - const argument0 = Blockly.PHP.valueToCode(block, 'FROM', - Blockly.PHP.ORDER_NONE) || '0'; - const argument1 = Blockly.PHP.valueToCode(block, 'TO', - Blockly.PHP.ORDER_NONE) || '0'; - const functionName = Blockly.PHP.provideFunction_( - 'math_random_int', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($a, $b) {', - ' if ($a > $b) {', - ' return rand($b, $a);', - ' }', - ' return rand($a, $b);', - '}']); + const argument0 = PHP.valueToCode(block, 'FROM', PHP.ORDER_NONE) || '0'; + const argument1 = PHP.valueToCode(block, 'TO', PHP.ORDER_NONE) || '0'; + const functionName = PHP.provideFunction_('math_random_int', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($a, $b) {', + ' if ($a > $b) {', ' return rand($b, $a);', ' }', + ' return rand($a, $b);', '}' + ]); const code = functionName + '(' + argument0 + ', ' + argument1 + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['math_random_float'] = function(block) { +PHP['math_random_float'] = function(block) { // Random fraction between 0 and 1. - return ['(float)rand()/(float)getrandmax()', Blockly.PHP.ORDER_FUNCTION_CALL]; + return ['(float)rand()/(float)getrandmax()', PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['math_atan2'] = function(block) { +PHP['math_atan2'] = function(block) { // Arctangent of point (X, Y) in degrees from -180 to 180. - const argument0 = Blockly.PHP.valueToCode(block, 'X', - Blockly.PHP.ORDER_NONE) || '0'; - const argument1 = Blockly.PHP.valueToCode(block, 'Y', - Blockly.PHP.ORDER_NONE) || '0'; - return ['atan2(' + argument1 + ', ' + argument0 + ') / pi() * 180', - Blockly.PHP.ORDER_DIVISION]; + const argument0 = PHP.valueToCode(block, 'X', PHP.ORDER_NONE) || '0'; + const argument1 = PHP.valueToCode(block, 'Y', PHP.ORDER_NONE) || '0'; + return [ + 'atan2(' + argument1 + ', ' + argument0 + ') / pi() * 180', + PHP.ORDER_DIVISION + ]; }; diff --git a/generators/php/procedures.js b/generators/php/procedures.js index 94303ff78..812e97053 100644 --- a/generators/php/procedures.js +++ b/generators/php/procedures.js @@ -6,128 +6,119 @@ /** * @fileoverview Generating PHP for procedure blocks. - * @suppress {missingRequire} */ 'use strict'; -goog.provide('Blockly.PHP.procedures'); +goog.module('Blockly.PHP.procedures'); -goog.require('Blockly.PHP'); -goog.require('Blockly.Names'); -goog.require('Blockly.Variables'); +const PHP = goog.require('Blockly.PHP'); +const Variables = goog.require('Blockly.Variables'); +const {NameType} = goog.require('Blockly.Names'); -Blockly.PHP['procedures_defreturn'] = function(block) { +PHP['procedures_defreturn'] = function(block) { // Define a procedure with a return value. // First, add a 'global' statement for every variable that is not shadowed by // a local parameter. const globals = []; const workspace = block.workspace; - const usedVariables = Blockly.Variables.allUsedVarModels(workspace) || []; + const usedVariables = Variables.allUsedVarModels(workspace) || []; for (let i = 0, variable; variable = usedVariables[i]; i++) { const varName = variable.name; if (block.getVars().indexOf(varName) === -1) { - globals.push(Blockly.PHP.nameDB_.getName(varName, - Blockly.VARIABLE_CATEGORY_NAME)); + globals.push(PHP.nameDB_.getName(varName, NameType.VARIABLE)); } } // Add developer variables. - const devVarList = Blockly.Variables.allDeveloperVariables(workspace); + const devVarList = Variables.allDeveloperVariables(workspace); for (let i = 0; i < devVarList.length; i++) { - globals.push(Blockly.PHP.nameDB_.getName(devVarList[i], - Blockly.Names.DEVELOPER_VARIABLE_TYPE)); + globals.push( + PHP.nameDB_.getName(devVarList[i], NameType.DEVELOPER_VARIABLE)); } - const globalStr = globals.length ? - Blockly.PHP.INDENT + 'global ' + globals.join(', ') + ';\n' : ''; + const globalStr = + globals.length ? PHP.INDENT + 'global ' + globals.join(', ') + ';\n' : ''; - const funcName = Blockly.PHP.nameDB_.getName( - block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME); + const funcName = + PHP.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE); let xfix1 = ''; - if (Blockly.PHP.STATEMENT_PREFIX) { - xfix1 += Blockly.PHP.injectId(Blockly.PHP.STATEMENT_PREFIX, block); + if (PHP.STATEMENT_PREFIX) { + xfix1 += PHP.injectId(PHP.STATEMENT_PREFIX, block); } - if (Blockly.PHP.STATEMENT_SUFFIX) { - xfix1 += Blockly.PHP.injectId(Blockly.PHP.STATEMENT_SUFFIX, block); + if (PHP.STATEMENT_SUFFIX) { + xfix1 += PHP.injectId(PHP.STATEMENT_SUFFIX, block); } if (xfix1) { - xfix1 = Blockly.PHP.prefixLines(xfix1, Blockly.PHP.INDENT); + xfix1 = PHP.prefixLines(xfix1, PHP.INDENT); } let loopTrap = ''; - if (Blockly.PHP.INFINITE_LOOP_TRAP) { - loopTrap = Blockly.PHP.prefixLines( - Blockly.PHP.injectId(Blockly.PHP.INFINITE_LOOP_TRAP, block), - Blockly.PHP.INDENT); + if (PHP.INFINITE_LOOP_TRAP) { + loopTrap = PHP.prefixLines( + PHP.injectId(PHP.INFINITE_LOOP_TRAP, block), PHP.INDENT); } - const branch = Blockly.PHP.statementToCode(block, 'STACK'); - let returnValue = Blockly.PHP.valueToCode(block, 'RETURN', - Blockly.PHP.ORDER_NONE) || ''; + const branch = PHP.statementToCode(block, 'STACK'); + let returnValue = PHP.valueToCode(block, 'RETURN', PHP.ORDER_NONE) || ''; let xfix2 = ''; if (branch && returnValue) { // After executing the function body, revisit this block for the return. xfix2 = xfix1; } if (returnValue) { - returnValue = Blockly.PHP.INDENT + 'return ' + returnValue + ';\n'; + returnValue = PHP.INDENT + 'return ' + returnValue + ';\n'; } const args = []; const variables = block.getVars(); for (let i = 0; i < variables.length; i++) { - args[i] = Blockly.PHP.nameDB_.getName(variables[i], - Blockly.VARIABLE_CATEGORY_NAME); + args[i] = PHP.nameDB_.getName(variables[i], NameType.VARIABLE); } let code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' + globalStr + xfix1 + loopTrap + branch + xfix2 + returnValue + '}'; - code = Blockly.PHP.scrub_(block, code); + code = PHP.scrub_(block, code); // Add % so as not to collide with helper functions in definitions list. - Blockly.PHP.definitions_['%' + funcName] = code; + PHP.definitions_['%' + funcName] = code; return null; }; // Defining a procedure without a return value uses the same generator as // a procedure with a return value. -Blockly.PHP['procedures_defnoreturn'] = - Blockly.PHP['procedures_defreturn']; +PHP['procedures_defnoreturn'] = PHP['procedures_defreturn']; -Blockly.PHP['procedures_callreturn'] = function(block) { +PHP['procedures_callreturn'] = function(block) { // Call a procedure with a return value. - const funcName = Blockly.PHP.nameDB_.getName( - block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME); + const funcName = + PHP.nameDB_.getName(block.getFieldValue('NAME'), NameType.PROCEDURE); const args = []; const variables = block.getVars(); for (let i = 0; i < variables.length; i++) { - args[i] = Blockly.PHP.valueToCode(block, 'ARG' + i, - Blockly.PHP.ORDER_NONE) || 'null'; + args[i] = PHP.valueToCode(block, 'ARG' + i, PHP.ORDER_NONE) || 'null'; } const code = funcName + '(' + args.join(', ') + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['procedures_callnoreturn'] = function(block) { +PHP['procedures_callnoreturn'] = function(block) { // Call a procedure with no return value. // 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. - const tuple = Blockly.PHP['procedures_callreturn'](block); + const tuple = PHP['procedures_callreturn'](block); return tuple[0] + ';\n'; }; -Blockly.PHP['procedures_ifreturn'] = function(block) { +PHP['procedures_ifreturn'] = function(block) { // Conditionally return value from a procedure. - const condition = Blockly.PHP.valueToCode(block, 'CONDITION', - Blockly.PHP.ORDER_NONE) || 'false'; + const condition = + PHP.valueToCode(block, 'CONDITION', PHP.ORDER_NONE) || 'false'; let code = 'if (' + condition + ') {\n'; - if (Blockly.PHP.STATEMENT_SUFFIX) { + if (PHP.STATEMENT_SUFFIX) { // Inject any statement suffix here since the regular one at the end // will not get executed if the return is triggered. - code += Blockly.PHP.prefixLines( - Blockly.PHP.injectId(Blockly.PHP.STATEMENT_SUFFIX, block), - Blockly.PHP.INDENT); + code += + PHP.prefixLines(PHP.injectId(PHP.STATEMENT_SUFFIX, block), PHP.INDENT); } if (block.hasReturnValue_) { - const value = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || 'null'; - code += Blockly.PHP.INDENT + 'return ' + value + ';\n'; + const value = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || 'null'; + code += PHP.INDENT + 'return ' + value + ';\n'; } else { - code += Blockly.PHP.INDENT + 'return;\n'; + code += PHP.INDENT + 'return;\n'; } code += '}\n'; return code; diff --git a/generators/php/text.js b/generators/php/text.js index 26fc5cc7d..eadf4cf5b 100644 --- a/generators/php/text.js +++ b/generators/php/text.js @@ -6,198 +6,184 @@ /** * @fileoverview Generating PHP for text blocks. - * @suppress {missingRequire} */ 'use strict'; -goog.provide('Blockly.PHP.texts'); +goog.module('Blockly.PHP.texts'); -goog.require('Blockly.PHP'); +const PHP = goog.require('Blockly.PHP'); +const {NameType} = goog.require('Blockly.Names'); -Blockly.PHP['text'] = function(block) { +PHP['text'] = function(block) { // Text value. - const code = Blockly.PHP.quote_(block.getFieldValue('TEXT')); - return [code, Blockly.PHP.ORDER_ATOMIC]; + const code = PHP.quote_(block.getFieldValue('TEXT')); + return [code, PHP.ORDER_ATOMIC]; }; -Blockly.PHP['text_multiline'] = function(block) { +PHP['text_multiline'] = function(block) { // Text value. - const code = Blockly.PHP.multiline_quote_(block.getFieldValue('TEXT')); - const order = code.indexOf('.') !== -1 ? Blockly.PHP.ORDER_STRING_CONCAT : - Blockly.PHP.ORDER_ATOMIC; + const code = PHP.multiline_quote_(block.getFieldValue('TEXT')); + const order = + code.indexOf('.') !== -1 ? PHP.ORDER_STRING_CONCAT : PHP.ORDER_ATOMIC; return [code, order]; }; -Blockly.PHP['text_join'] = function(block) { +PHP['text_join'] = function(block) { // Create a string made up of any number of elements of any type. if (block.itemCount_ === 0) { - return ['\'\'', Blockly.PHP.ORDER_ATOMIC]; + return ['\'\'', PHP.ORDER_ATOMIC]; } else if (block.itemCount_ === 1) { - const element = Blockly.PHP.valueToCode(block, 'ADD0', - Blockly.PHP.ORDER_NONE) || '\'\''; + const element = PHP.valueToCode(block, 'ADD0', PHP.ORDER_NONE) || '\'\''; const code = element; - return [code, Blockly.PHP.ORDER_NONE]; + return [code, PHP.ORDER_NONE]; } else if (block.itemCount_ === 2) { - const element0 = Blockly.PHP.valueToCode(block, 'ADD0', - Blockly.PHP.ORDER_STRING_CONCAT) || '\'\''; - const element1 = Blockly.PHP.valueToCode(block, 'ADD1', - Blockly.PHP.ORDER_STRING_CONCAT) || '\'\''; + const element0 = + PHP.valueToCode(block, 'ADD0', PHP.ORDER_STRING_CONCAT) || '\'\''; + const element1 = + PHP.valueToCode(block, 'ADD1', PHP.ORDER_STRING_CONCAT) || '\'\''; const code = element0 + ' . ' + element1; - return [code, Blockly.PHP.ORDER_STRING_CONCAT]; + return [code, PHP.ORDER_STRING_CONCAT]; } else { const elements = new Array(block.itemCount_); for (let i = 0; i < block.itemCount_; i++) { - elements[i] = Blockly.PHP.valueToCode(block, 'ADD' + i, - Blockly.PHP.ORDER_NONE) || '\'\''; + elements[i] = PHP.valueToCode(block, 'ADD' + i, PHP.ORDER_NONE) || '\'\''; } const code = 'implode(\'\', array(' + elements.join(',') + '))'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } }; -Blockly.PHP['text_append'] = function(block) { +PHP['text_append'] = function(block) { // Append to a variable in place. - const varName = Blockly.PHP.nameDB_.getName( - block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); - const value = Blockly.PHP.valueToCode(block, 'TEXT', - Blockly.PHP.ORDER_ASSIGNMENT) || '\'\''; + const varName = + PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE); + const value = PHP.valueToCode(block, 'TEXT', PHP.ORDER_ASSIGNMENT) || '\'\''; return varName + ' .= ' + value + ';\n'; }; -Blockly.PHP['text_length'] = function(block) { +PHP['text_length'] = function(block) { // String or array length. - const functionName = Blockly.PHP.provideFunction_( - 'length', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($value) {', - ' if (is_string($value)) {', - ' return strlen($value);', - ' } else {', - ' return count($value);', - ' }', - '}']); - const text = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || '\'\''; - return [functionName + '(' + text + ')', Blockly.PHP.ORDER_FUNCTION_CALL]; + const functionName = PHP.provideFunction_('length', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($value) {', + ' if (is_string($value)) {', ' return strlen($value);', ' } else {', + ' return count($value);', ' }', '}' + ]); + const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || '\'\''; + return [functionName + '(' + text + ')', PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['text_isEmpty'] = function(block) { +PHP['text_isEmpty'] = function(block) { // Is the string null or array empty? - const text = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || '\'\''; - return ['empty(' + text + ')', Blockly.PHP.ORDER_FUNCTION_CALL]; + const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || '\'\''; + return ['empty(' + text + ')', PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['text_indexOf'] = function(block) { +PHP['text_indexOf'] = function(block) { // Search the text for a substring. - const operator = block.getFieldValue('END') === 'FIRST' ? - 'strpos' : 'strrpos'; - const substring = Blockly.PHP.valueToCode(block, 'FIND', - Blockly.PHP.ORDER_NONE) || '\'\''; - const text = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_NONE) || '\'\''; + const operator = + block.getFieldValue('END') === 'FIRST' ? 'strpos' : 'strrpos'; + const substring = PHP.valueToCode(block, 'FIND', PHP.ORDER_NONE) || '\'\''; + const text = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || '\'\''; let errorIndex = ' -1'; let indexAdjustment = ''; if (block.workspace.options.oneBasedIndex) { errorIndex = ' 0'; indexAdjustment = ' + 1'; } - const functionName = Blockly.PHP.provideFunction_( - block.getFieldValue('END') === 'FIRST' ? - 'text_indexOf' : 'text_lastIndexOf', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($text, $search) {', - ' $pos = ' + operator + '($text, $search);', - ' return $pos === false ? ' + errorIndex + ' : $pos' + - indexAdjustment + ';', - '}']); + const functionName = PHP.provideFunction_( + block.getFieldValue('END') === 'FIRST' ? 'text_indexOf' : + 'text_lastIndexOf', + [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($text, $search) {', + ' $pos = ' + operator + '($text, $search);', + ' return $pos === false ? ' + errorIndex + ' : $pos' + + indexAdjustment + ';', + '}' + ]); const code = functionName + '(' + text + ', ' + substring + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['text_charAt'] = function(block) { +PHP['text_charAt'] = function(block) { // Get letter at index. const where = block.getFieldValue('WHERE') || 'FROM_START'; - const textOrder = (where === 'RANDOM') ? Blockly.PHP.ORDER_NONE : - Blockly.PHP.ORDER_NONE; - const text = Blockly.PHP.valueToCode(block, 'VALUE', textOrder) || '\'\''; + const textOrder = (where === 'RANDOM') ? PHP.ORDER_NONE : PHP.ORDER_NONE; + const text = PHP.valueToCode(block, 'VALUE', textOrder) || '\'\''; switch (where) { case 'FIRST': { const code = 'substr(' + text + ', 0, 1)'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } case 'LAST': { const code = 'substr(' + text + ', -1)'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } case 'FROM_START': { - const at = Blockly.PHP.getAdjusted(block, 'AT'); + const at = PHP.getAdjusted(block, 'AT'); const code = 'substr(' + text + ', ' + at + ', 1)'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } case 'FROM_END': { - const at = Blockly.PHP.getAdjusted(block, 'AT', 1, true); + const at = PHP.getAdjusted(block, 'AT', 1, true); const code = 'substr(' + text + ', ' + at + ', 1)'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } case 'RANDOM': { - const functionName = Blockly.PHP.provideFunction_( - 'text_random_letter', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($text) {', - ' return $text[rand(0, strlen($text) - 1)];', - '}']); + const functionName = PHP.provideFunction_('text_random_letter', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($text) {', + ' return $text[rand(0, strlen($text) - 1)];', '}' + ]); const code = functionName + '(' + text + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; } } throw Error('Unhandled option (text_charAt).'); }; -Blockly.PHP['text_getSubstring'] = function(block) { +PHP['text_getSubstring'] = function(block) { // Get substring. const where1 = block.getFieldValue('WHERE1'); const where2 = block.getFieldValue('WHERE2'); - const text = Blockly.PHP.valueToCode(block, 'STRING', - Blockly.PHP.ORDER_NONE) || '\'\''; + const text = PHP.valueToCode(block, 'STRING', PHP.ORDER_NONE) || '\'\''; if (where1 === 'FIRST' && where2 === 'LAST') { const code = text; - return [code, Blockly.PHP.ORDER_NONE]; + return [code, PHP.ORDER_NONE]; } else { - const at1 = Blockly.PHP.getAdjusted(block, 'AT1'); - const at2 = Blockly.PHP.getAdjusted(block, 'AT2'); - const functionName = Blockly.PHP.provideFunction_( - 'text_get_substring', - ['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + - '($text, $where1, $at1, $where2, $at2) {', - ' if ($where1 == \'FROM_END\') {', - ' $at1 = strlen($text) - 1 - $at1;', - ' } else if ($where1 == \'FIRST\') {', - ' $at1 = 0;', - ' } else if ($where1 != \'FROM_START\') {', - ' throw new Exception(\'Unhandled option (text_get_substring).\');', - ' }', - ' $length = 0;', - ' if ($where2 == \'FROM_START\') {', - ' $length = $at2 - $at1 + 1;', - ' } else if ($where2 == \'FROM_END\') {', - ' $length = strlen($text) - $at1 - $at2;', - ' } else if ($where2 == \'LAST\') {', - ' $length = strlen($text) - $at1;', - ' } else {', - ' throw new Exception(\'Unhandled option (text_get_substring).\');', - ' }', - ' return substr($text, $at1, $length);', - '}']); - const code = functionName + '(' + text + ', \'' + - where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + const at1 = PHP.getAdjusted(block, 'AT1'); + const at2 = PHP.getAdjusted(block, 'AT2'); + const functionName = PHP.provideFunction_('text_get_substring', [ + 'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + + '($text, $where1, $at1, $where2, $at2) {', + ' if ($where1 == \'FROM_END\') {', + ' $at1 = strlen($text) - 1 - $at1;', + ' } else if ($where1 == \'FIRST\') {', + ' $at1 = 0;', + ' } else if ($where1 != \'FROM_START\') {', + ' throw new Exception(\'Unhandled option (text_get_substring).\');', + ' }', + ' $length = 0;', + ' if ($where2 == \'FROM_START\') {', + ' $length = $at2 - $at1 + 1;', + ' } else if ($where2 == \'FROM_END\') {', + ' $length = strlen($text) - $at1 - $at2;', + ' } else if ($where2 == \'LAST\') {', + ' $length = strlen($text) - $at1;', + ' } else {', + ' throw new Exception(\'Unhandled option (text_get_substring).\');', + ' }', + ' return substr($text, $at1, $length);', + '}' + ]); + const code = functionName + '(' + text + ', \'' + where1 + '\', ' + at1 + + ', \'' + where2 + '\', ' + at2 + ')'; + return [code, PHP.ORDER_FUNCTION_CALL]; } }; -Blockly.PHP['text_changeCase'] = function(block) { +PHP['text_changeCase'] = function(block) { // Change capitalization. - const text = Blockly.PHP.valueToCode(block, 'TEXT', - Blockly.PHP.ORDER_NONE) || '\'\''; + const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\''; let code; if (block.getFieldValue('CASE') === 'UPPERCASE') { code = 'strtoupper(' + text + ')'; @@ -206,75 +192,62 @@ Blockly.PHP['text_changeCase'] = function(block) { } else if (block.getFieldValue('CASE') === 'TITLECASE') { code = 'ucwords(strtolower(' + text + '))'; } - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['text_trim'] = function(block) { +PHP['text_trim'] = function(block) { // Trim spaces. - const OPERATORS = { - 'LEFT': 'ltrim', - 'RIGHT': 'rtrim', - 'BOTH': 'trim' - }; + const OPERATORS = {'LEFT': 'ltrim', 'RIGHT': 'rtrim', 'BOTH': 'trim'}; const operator = OPERATORS[block.getFieldValue('MODE')]; - const text = Blockly.PHP.valueToCode(block, 'TEXT', - Blockly.PHP.ORDER_NONE) || '\'\''; - return [operator + '(' + text + ')', Blockly.PHP.ORDER_FUNCTION_CALL]; + const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\''; + return [operator + '(' + text + ')', PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['text_print'] = function(block) { +PHP['text_print'] = function(block) { // Print statement. - const msg = Blockly.PHP.valueToCode(block, 'TEXT', - Blockly.PHP.ORDER_NONE) || '\'\''; + const msg = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\''; return 'print(' + msg + ');\n'; }; -Blockly.PHP['text_prompt_ext'] = function(block) { +PHP['text_prompt_ext'] = function(block) { // Prompt function. let msg; if (block.getField('TEXT')) { // Internal message. - msg = Blockly.PHP.quote_(block.getFieldValue('TEXT')); + msg = PHP.quote_(block.getFieldValue('TEXT')); } else { // External message. - msg = Blockly.PHP.valueToCode(block, 'TEXT', - Blockly.PHP.ORDER_NONE) || '\'\''; + msg = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\''; } let code = 'readline(' + msg + ')'; const toNumber = block.getFieldValue('TYPE') === 'NUMBER'; if (toNumber) { code = 'floatval(' + code + ')'; } - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['text_prompt'] = Blockly.PHP['text_prompt_ext']; +PHP['text_prompt'] = PHP['text_prompt_ext']; -Blockly.PHP['text_count'] = function(block) { - const text = Blockly.PHP.valueToCode(block, 'TEXT', - Blockly.PHP.ORDER_NONE) || '\'\''; - const sub = Blockly.PHP.valueToCode(block, 'SUB', - Blockly.PHP.ORDER_NONE) || '\'\''; - const code = 'strlen(' + sub + ') === 0' - + ' ? strlen(' + text + ') + 1' - + ' : substr_count(' + text + ', ' + sub + ')'; - return [code, Blockly.PHP.ORDER_CONDITIONAL]; +PHP['text_count'] = function(block) { + const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\''; + const sub = PHP.valueToCode(block, 'SUB', PHP.ORDER_NONE) || '\'\''; + const code = 'strlen(' + sub + ') === 0' + + ' ? strlen(' + text + ') + 1' + + ' : substr_count(' + text + ', ' + sub + ')'; + return [code, PHP.ORDER_CONDITIONAL]; }; -Blockly.PHP['text_replace'] = function(block) { - const text = Blockly.PHP.valueToCode(block, 'TEXT', - Blockly.PHP.ORDER_NONE) || '\'\''; - const from = Blockly.PHP.valueToCode(block, 'FROM', - Blockly.PHP.ORDER_NONE) || '\'\''; - const to = Blockly.PHP.valueToCode(block, 'TO', - Blockly.PHP.ORDER_NONE) || '\'\''; +PHP['text_replace'] = function(block) { + const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\''; + const from = PHP.valueToCode(block, 'FROM', PHP.ORDER_NONE) || '\'\''; + const to = PHP.valueToCode(block, 'TO', PHP.ORDER_NONE) || '\'\''; const code = 'str_replace(' + from + ', ' + to + ', ' + text + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; -Blockly.PHP['text_reverse'] = function(block) { - const text = Blockly.PHP.valueToCode(block, 'TEXT', - Blockly.PHP.ORDER_NONE) || '\'\''; +PHP['text_reverse'] = function(block) { + const text = PHP.valueToCode(block, 'TEXT', PHP.ORDER_NONE) || '\'\''; const code = 'strrev(' + text + ')'; - return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; + return [code, PHP.ORDER_FUNCTION_CALL]; }; diff --git a/generators/php/variables.js b/generators/php/variables.js index 13ae0d64f..1a9da02e9 100644 --- a/generators/php/variables.js +++ b/generators/php/variables.js @@ -6,27 +6,27 @@ /** * @fileoverview Generating PHP for variable blocks. - * @suppress {missingRequire} */ 'use strict'; -goog.provide('Blockly.PHP.variables'); +goog.module('Blockly.PHP.variables'); -goog.require('Blockly.PHP'); +const PHP = goog.require('Blockly.PHP'); +const {NameType} = goog.require('Blockly.Names'); -Blockly.PHP['variables_get'] = function(block) { - // Variable getter. - const code = Blockly.PHP.nameDB_.getName(block.getFieldValue('VAR'), - Blockly.VARIABLE_CATEGORY_NAME); - return [code, Blockly.PHP.ORDER_ATOMIC]; +PHP['variables_get'] = function(block) { + // Variable getter. + const code = + PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE); + return [code, PHP.ORDER_ATOMIC]; }; -Blockly.PHP['variables_set'] = function(block) { - // Variable setter. - const argument0 = Blockly.PHP.valueToCode(block, 'VALUE', - Blockly.PHP.ORDER_ASSIGNMENT) || '0'; - const varName = Blockly.PHP.nameDB_.getName( - block.getFieldValue('VAR'), Blockly.VARIABLE_CATEGORY_NAME); - return varName + ' = ' + argument0 + ';\n'; +PHP['variables_set'] = function(block) { + // Variable setter. + const argument0 = + PHP.valueToCode(block, 'VALUE', PHP.ORDER_ASSIGNMENT) || '0'; + const varName = + PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE); + return varName + ' = ' + argument0 + ';\n'; }; diff --git a/generators/php/variables_dynamic.js b/generators/php/variables_dynamic.js index ab2dc714f..889052360 100644 --- a/generators/php/variables_dynamic.js +++ b/generators/php/variables_dynamic.js @@ -6,16 +6,16 @@ /** * @fileoverview Generating PHP for dynamic variable blocks. - * @suppress {extraRequire} */ 'use strict'; -goog.provide('Blockly.PHP.variablesDynamic'); +goog.module('Blockly.PHP.variablesDynamic'); -goog.require('Blockly.PHP'); +const PHP = goog.require('Blockly.PHP'); +/** @suppress {extraRequire} */ goog.require('Blockly.PHP.variables'); // PHP is dynamically typed. -Blockly.PHP['variables_get_dynamic'] = Blockly.PHP['variables_get']; -Blockly.PHP['variables_set_dynamic'] = Blockly.PHP['variables_set']; +PHP['variables_get_dynamic'] = PHP['variables_get']; +PHP['variables_set_dynamic'] = PHP['variables_set']; diff --git a/tests/deps.js b/tests/deps.js index d746bd740..98f5b25c6 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -287,7 +287,7 @@ goog.addDependency('../../generators/javascript/procedures.js', ['Blockly.JavaSc goog.addDependency('../../generators/javascript/text.js', ['Blockly.JavaScript.texts'], ['Blockly.JavaScript', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/javascript/variables.js', ['Blockly.JavaScript.variables'], ['Blockly.JavaScript', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/javascript/variables_dynamic.js', ['Blockly.JavaScript.variablesDynamic'], ['Blockly.JavaScript', 'Blockly.JavaScript.variables'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../generators/lua.js', ['Blockly.Lua'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.inputTypes', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6'}); +goog.addDependency('../../generators/lua.js', ['Blockly.Lua'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.inputTypes', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/lua/all.js', ['Blockly.Lua.all'], ['Blockly.Lua.colour', 'Blockly.Lua.lists', 'Blockly.Lua.logic', 'Blockly.Lua.loops', 'Blockly.Lua.math', 'Blockly.Lua.procedures', 'Blockly.Lua.texts', 'Blockly.Lua.variables', 'Blockly.Lua.variablesDynamic'], {'module': 'goog'}); goog.addDependency('../../generators/lua/colour.js', ['Blockly.Lua.colour'], ['Blockly.Lua'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/lua/lists.js', ['Blockly.Lua.lists'], ['Blockly.Lua', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'}); @@ -298,17 +298,17 @@ goog.addDependency('../../generators/lua/procedures.js', ['Blockly.Lua.procedure goog.addDependency('../../generators/lua/text.js', ['Blockly.Lua.texts'], ['Blockly.Lua', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/lua/variables.js', ['Blockly.Lua.variables'], ['Blockly.Lua', 'Blockly.Names'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/lua/variables_dynamic.js', ['Blockly.Lua.variablesDynamic'], ['Blockly.Lua', 'Blockly.Lua.variables'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../generators/php.js', ['Blockly.PHP'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.inputTypes', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6'}); +goog.addDependency('../../generators/php.js', ['Blockly.PHP'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.inputTypes', 'Blockly.utils.object', 'Blockly.utils.string'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/php/all.js', ['Blockly.PHP.all'], ['Blockly.PHP.colour', 'Blockly.PHP.lists', 'Blockly.PHP.logic', 'Blockly.PHP.loops', 'Blockly.PHP.math', 'Blockly.PHP.procedures', 'Blockly.PHP.texts', 'Blockly.PHP.variables', 'Blockly.PHP.variablesDynamic'], {'module': 'goog'}); -goog.addDependency('../../generators/php/colour.js', ['Blockly.PHP.colour'], ['Blockly.PHP'], {'lang': 'es6'}); -goog.addDependency('../../generators/php/lists.js', ['Blockly.PHP.lists'], ['Blockly.PHP', 'Blockly.utils.string'], {'lang': 'es6'}); -goog.addDependency('../../generators/php/logic.js', ['Blockly.PHP.logic'], ['Blockly.PHP'], {'lang': 'es6'}); -goog.addDependency('../../generators/php/loops.js', ['Blockly.PHP.loops'], ['Blockly.PHP', 'Blockly.utils.string'], {'lang': 'es6'}); -goog.addDependency('../../generators/php/math.js', ['Blockly.PHP.math'], ['Blockly.PHP'], {'lang': 'es6'}); -goog.addDependency('../../generators/php/procedures.js', ['Blockly.PHP.procedures'], ['Blockly.Names', 'Blockly.PHP', 'Blockly.Variables'], {'lang': 'es6'}); -goog.addDependency('../../generators/php/text.js', ['Blockly.PHP.texts'], ['Blockly.PHP'], {'lang': 'es6'}); -goog.addDependency('../../generators/php/variables.js', ['Blockly.PHP.variables'], ['Blockly.PHP'], {'lang': 'es6'}); -goog.addDependency('../../generators/php/variables_dynamic.js', ['Blockly.PHP.variablesDynamic'], ['Blockly.PHP', 'Blockly.PHP.variables']); +goog.addDependency('../../generators/php/colour.js', ['Blockly.PHP.colour'], ['Blockly.PHP'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/php/lists.js', ['Blockly.PHP.lists'], ['Blockly.Names', 'Blockly.PHP', 'Blockly.utils.string'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/php/logic.js', ['Blockly.PHP.logic'], ['Blockly.PHP'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/php/loops.js', ['Blockly.PHP.loops'], ['Blockly.Names', 'Blockly.PHP', 'Blockly.utils.string'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/php/math.js', ['Blockly.PHP.math'], ['Blockly.Names', 'Blockly.PHP'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/php/procedures.js', ['Blockly.PHP.procedures'], ['Blockly.Names', 'Blockly.PHP', 'Blockly.Variables'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/php/text.js', ['Blockly.PHP.texts'], ['Blockly.Names', 'Blockly.PHP'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/php/variables.js', ['Blockly.PHP.variables'], ['Blockly.Names', 'Blockly.PHP'], {'lang': 'es6', 'module': 'goog'}); +goog.addDependency('../../generators/php/variables_dynamic.js', ['Blockly.PHP.variablesDynamic'], ['Blockly.PHP', 'Blockly.PHP.variables'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../generators/python.js', ['Blockly.Python'], ['Blockly.Generator', 'Blockly.Names', 'Blockly.Variables', 'Blockly.inputTypes', 'Blockly.utils.string'], {'lang': 'es6'}); goog.addDependency('../../generators/python/all.js', ['Blockly.Python.all'], ['Blockly.Python.colour', 'Blockly.Python.lists', 'Blockly.Python.logic', 'Blockly.Python.loops', 'Blockly.Python.math', 'Blockly.Python.procedures', 'Blockly.Python.texts', 'Blockly.Python.variables', 'Blockly.Python.variablesDynamic'], {'module': 'goog'}); goog.addDependency('../../generators/python/colour.js', ['Blockly.Python.colour'], ['Blockly.Python'], {'lang': 'es6'});