From 46316c7cea9a5b819ba65ee2b5ed140209c9f304 Mon Sep 17 00:00:00 2001 From: Tim Dawborn Date: Sun, 22 Jan 2017 05:48:42 +1100 Subject: [PATCH] Add a block to reverse a list (#844) --- blocks/lists.js | 24 ++++++ generators/dart/lists.js | 9 ++ generators/javascript/lists.js | 8 ++ generators/lua/lists.js | 17 ++++ generators/php/lists.js | 8 ++ generators/python/lists.js | 8 ++ msg/json/en.json | 5 +- msg/json/qqq.json | 3 + msg/messages.js | 7 ++ tests/generators/index.html | 1 + tests/generators/lists.xml | 150 +++++++++++++++++++++++++++++++++ tests/playground.html | 1 + 12 files changed, 240 insertions(+), 1 deletion(-) diff --git a/blocks/lists.js b/blocks/lists.js index eea532a7a..20d0aa2cc 100644 --- a/blocks/lists.js +++ b/blocks/lists.js @@ -841,3 +841,27 @@ Blockly.Blocks['lists_split'] = { this.updateType_(xmlElement.getAttribute('mode')); } }; + +Blockly.Blocks['lists_reverse'] = { + /** + * Block for reversing a list. + * @this Blockly.Block + **/ + init: function() { + this.jsonInit({ + "message0": Blockly.Msg.LISTS_REVERSE_MESSAGE0, + "args0": [ + { + "type": "input_value", + "name": "LIST", + "check": "Array" + } + ], + "output": "Array", + "inputsInline": true, + "colour": Blockly.Blocks.lists.HUE, + "tooltip": Blockly.Msg.LISTS_REVERSE_TOOLTIP, + "helpUrl": Blockly.Msg.LISTS_REVERSE_HELPURL + }); + } +}; diff --git a/generators/dart/lists.js b/generators/dart/lists.js index b1b575c87..42552713c 100644 --- a/generators/dart/lists.js +++ b/generators/dart/lists.js @@ -450,3 +450,12 @@ Blockly.Dart['lists_split'] = function(block) { var code = input + '.' + functionName + '(' + delimiter + ')'; return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; }; + +Blockly.Dart['lists_reverse'] = function(block) { + // Block for reversing a list. + var list = Blockly.Dart.valueToCode(block, 'LIST', + Blockly.Dart.ORDER_NONE) || '[]'; + // XXX What should the operator precedence be for a `new`? + var code = 'new List.from(' + list + '.reversed)'; + return [code, Blockly.Dart.ORDER_UNARY_POSTFIX]; +}; diff --git a/generators/javascript/lists.js b/generators/javascript/lists.js index 8dd5d880d..d9075953f 100644 --- a/generators/javascript/lists.js +++ b/generators/javascript/lists.js @@ -392,3 +392,11 @@ Blockly.JavaScript['lists_split'] = function(block) { var code = input + '.' + functionName + '(' + delimiter + ')'; return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; }; + +Blockly.JavaScript['lists_reverse'] = function(block) { + // Block for reversing a list. + var list = Blockly.JavaScript.valueToCode(block, 'LIST', + Blockly.JavaScript.ORDER_FUNCTION_CALL) || '[]'; + var code = list + '.slice().reverse()'; + return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL]; +}; diff --git a/generators/lua/lists.js b/generators/lua/lists.js index 5b7711dbb..6b9fe006e 100644 --- a/generators/lua/lists.js +++ b/generators/lua/lists.js @@ -363,3 +363,20 @@ Blockly.Lua['lists_split'] = function(block) { var code = functionName + '(' + input + ', ' + delimiter + ')'; return [code, Blockly.Lua.ORDER_HIGH]; }; + +Blockly.Lua['lists_reverse'] = function(block) { + // Block for reversing a list. + var list = Blockly.Lua.valueToCode(block, 'LIST', + Blockly.Lua.ORDER_NONE) || '{}'; + var functionName = Blockly.Lua.provideFunction_( + 'list_reverse', + ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(input)', + ' local reversed = {}', + ' for i = #input, 1, -1 do', + ' table.insert(reversed, input[i])', + ' end', + ' return reversed', + 'end']); + var code = 'list_reverse(' + list + ')'; + return [code, Blockly.Lua.ORDER_HIGH]; +}; diff --git a/generators/php/lists.js b/generators/php/lists.js index 0ffe7078b..3fd261695 100644 --- a/generators/php/lists.js +++ b/generators/php/lists.js @@ -502,3 +502,11 @@ Blockly.PHP['lists_split'] = function(block) { var code = functionName + '(' + value_delim + ', ' + value_input + ')'; return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; }; + +Blockly.PHP['lists_reverse'] = function(block) { + // Block for reversing a list. + var list = Blockly.PHP.valueToCode(block, 'LIST', + Blockly.PHP.ORDER_COMMA) || '[]'; + var code = 'array_reverse(' + list + ')'; + return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; +}; diff --git a/generators/python/lists.js b/generators/python/lists.js index 84618f8cc..3d29acf22 100644 --- a/generators/python/lists.js +++ b/generators/python/lists.js @@ -353,3 +353,11 @@ Blockly.Python['lists_split'] = function(block) { } return [code, Blockly.Python.ORDER_FUNCTION_CALL]; }; + +Blockly.Python['lists_reverse'] = function(block) { + // Block for reversing a list. + var list = Blockly.Python.valueToCode(block, 'LIST', + Blockly.Python.ORDER_NONE) || '[]'; + var code = 'list(reversed(' + list + '))'; + return [code, Blockly.Python.ORDER_FUNCTION_CALL]; +}; diff --git a/msg/json/en.json b/msg/json/en.json index 486819e64..eb0718833 100644 --- a/msg/json/en.json +++ b/msg/json/en.json @@ -1,7 +1,7 @@ { "@metadata": { "author": "Ellen Spertus ", - "lastupdated": "2017-01-18 10:58:30.631169", + "lastupdated": "2017-01-20 09:40:15.080443", "locale": "en", "messagedocumentation" : "qqq" }, @@ -343,6 +343,9 @@ "LISTS_SPLIT_WITH_DELIMITER": "with delimiter", "LISTS_SPLIT_TOOLTIP_SPLIT": "Split text into a list of texts, breaking at each delimiter.", "LISTS_SPLIT_TOOLTIP_JOIN": "Join a list of texts into one text, separated by a delimiter.", + "LISTS_REVERSE_HELPURL": "", + "LISTS_REVERSE_MESSAGE0": "reverse %1", + "LISTS_REVERSE_TOOLTIP": "Reverse a copy of a list.", "ORDINAL_NUMBER_SUFFIX": "", "VARIABLES_GET_HELPURL": "https://github.com/google/blockly/wiki/Variables#get", "VARIABLES_GET_TOOLTIP": "Returns the value of this variable.", diff --git a/msg/json/qqq.json b/msg/json/qqq.json index d37228ff9..6f72bb595 100644 --- a/msg/json/qqq.json +++ b/msg/json/qqq.json @@ -337,6 +337,9 @@ "LISTS_SPLIT_WITH_DELIMITER": "block text - Prompts for a letter to be used as a separator when splitting or joining text.", "LISTS_SPLIT_TOOLTIP_SPLIT": "tooltip - See [https://github.com/google/blockly/wiki/Lists#make-list-from-text https://github.com/google/blockly/wiki/Lists#make-list-from-text] for more information.", "LISTS_SPLIT_TOOLTIP_JOIN": "tooltip - See [https://github.com/google/blockly/wiki/Lists#make-text-from-list https://github.com/google/blockly/wiki/Lists#make-text-from-list] for more information.", + "LISTS_REVERSE_HELPURL": "url - Information describing reversing a list.", + "LISTS_REVERSE_MESSAGE0": "Reverse a list of items %1.", + "LISTS_REVERSE_TOOLTIP": "tooltip - See [https://github.com/google/blockly/wiki/Lists#reversing-a-list].", "ORDINAL_NUMBER_SUFFIX": "grammar - Text that follows an ordinal number (a number that indicates position relative to other numbers). In most languages, such text appears before the number, so this should be blank. An exception is Hungarian. See [[Translating:Blockly#Ordinal_numbers]] for more information.", "VARIABLES_GET_HELPURL": "url - Information about ''variables'' in computer programming. Consider using your language's translation of [https://en.wikipedia.org/wiki/Variable_(computer_science) https://en.wikipedia.org/wiki/Variable_(computer_science)], if it exists.", "VARIABLES_GET_TOOLTIP": "tooltip - This gets the value of the named variable without modifying it.", diff --git a/msg/messages.js b/msg/messages.js index a11c62d6c..f9456b093 100644 --- a/msg/messages.js +++ b/msg/messages.js @@ -1039,6 +1039,13 @@ Blockly.Msg.LISTS_SPLIT_TOOLTIP_SPLIT = 'Split text into a list of texts, breaki /// https://github.com/google/blockly/wiki/Lists#make-text-from-list] for more information. Blockly.Msg.LISTS_SPLIT_TOOLTIP_JOIN = 'Join a list of texts into one text, separated by a delimiter.'; +/// url - Information describing reversing a list. +Blockly.Msg.LISTS_REVERSE_HELPURL = 'https://github.com/google/blockly/wiki/Lists#reversing-a-list'; +/// block text - Title of block that returns a copy of a list (%1) with the order of items reversed. +Blockly.Msg.LISTS_REVERSE_MESSAGE0 = 'reverse %1'; +/// tooltip - Short description for a block that reverses a copy of a list. +Blockly.Msg.LISTS_REVERSE_TOOLTIP = 'Reverse a copy of a list.'; + /// grammar - Text that follows an ordinal number (a number that indicates /// position relative to other numbers). In most languages, such text appears /// before the number, so this should be blank. An exception is Hungarian. diff --git a/tests/generators/index.html b/tests/generators/index.html index af802f2a9..a4d39ae97 100644 --- a/tests/generators/index.html +++ b/tests/generators/index.html @@ -296,6 +296,7 @@ h1 { + diff --git a/tests/generators/lists.xml b/tests/generators/lists.xml index 88e4946b5..aae25dfe6 100644 --- a/tests/generators/lists.xml +++ b/tests/generators/lists.xml @@ -54,6 +54,11 @@ + + + + + @@ -8131,4 +8136,149 @@ + + test reverse + Tests the "list reverse" block. + + + list + + + + + + 8 + + + + + 18 + + + + + -1 + + + + + 64 + + + + + + + + + reverse a copy + + + + + + + list + + + + + + + + + + 64 + + + + + -1 + + + + + 18 + + + + + 8 + + + + + + + + + reverse a copy original + + + + + list + + + + + + + + 8 + + + + + 18 + + + + + -1 + + + + + 64 + + + + + + + list + + + + + + + + empty list + + + + + + + list + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/playground.html b/tests/playground.html index c05c25b81..8c87b6383 100644 --- a/tests/playground.html +++ b/tests/playground.html @@ -681,6 +681,7 @@ h1 { +