From 4dc17c9eb33b0f5c76fd1b4345437dc898ea3ae8 Mon Sep 17 00:00:00 2001 From: Tim Dawborn Date: Fri, 20 Jan 2017 09:53:44 +1100 Subject: [PATCH 1/4] Add a list reverse block and associated codegen. --- blocks/lists.js | 24 ++++++++++++++++++++++++ generators/dart/lists.js | 9 +++++++++ generators/javascript/lists.js | 8 ++++++++ generators/lua/lists.js | 8 ++++++++ generators/php/lists.js | 8 ++++++++ generators/python/lists.js | 8 ++++++++ msg/json/en.json | 5 ++++- msg/json/qqq.json | 10 +++------- msg/messages.js | 7 +++++++ tests/generators/index.html | 1 + tests/playground.html | 1 + 11 files changed, 81 insertions(+), 8 deletions(-) diff --git a/blocks/lists.js b/blocks/lists.js index eea532a7a79..20d0aa2ccca 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 b1b575c8748..42552713c11 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 8dd5d880d57..d9075953fbb 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 5b7711dbb50..993a82aabf9 100644 --- a/generators/lua/lists.js +++ b/generators/lua/lists.js @@ -363,3 +363,11 @@ 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 code = 'table.Reverse(' + list + ')'; + return [code, Blockly.Lua.ORDER_HIGH]; +}; diff --git a/generators/php/lists.js b/generators/php/lists.js index 0ffe7078b46..3fd26169541 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 84618f8cc90..3600516e062 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 = 'reversed(' + list + ')'; + return [code, Blockly.Python.ORDER_FUNCTION_CALL]; +}; diff --git a/msg/json/en.json b/msg/json/en.json index 1f5625dd634..c0f282c4bad 100644 --- a/msg/json/en.json +++ b/msg/json/en.json @@ -1,7 +1,7 @@ { "@metadata": { "author": "Ellen Spertus ", - "lastupdated": "2017-01-12 13:53:04.889198", + "lastupdated": "2017-01-20 09:40:15.080443", "locale": "en", "messagedocumentation" : "qqq" }, @@ -334,6 +334,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 dbb69c7cb05..9890dd07e12 100644 --- a/msg/json/qqq.json +++ b/msg/json/qqq.json @@ -1,11 +1,4 @@ { - "@metadata": { - "authors": [ - "Espertus", - "Liuxinyu970226", - "Shirayuki" - ] - }, "VARIABLES_DEFAULT_NAME": "default name - A simple, general default name for a variable, preferably short. For more context, see [[Translating:Blockly#infrequent_message_types]].\n{{Identical|Item}}", "TODAY": "button text - Button that sets a calendar to today's date.\n{{Identical|Today}}", "DUPLICATE_BLOCK": "context menu - Make a copy of the selected block (and any blocks it contains).\n{{Identical|Duplicate}}", @@ -335,6 +328,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 24fc1fa14bb..ac9df905903 100644 --- a/msg/messages.js +++ b/msg/messages.js @@ -1015,6 +1015,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 = ''; +/// Reverse a list of items %1. +Blockly.Msg.LISTS_REVERSE_MESSAGE0 = 'reverse %1'; +/// tooltip - See [https://github.com/google/blockly/wiki/Lists#reversing-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 3f905b69827..be0339de213 100644 --- a/tests/generators/index.html +++ b/tests/generators/index.html @@ -293,6 +293,7 @@ + diff --git a/tests/playground.html b/tests/playground.html index c728879e7b0..2f1e28cda4f 100644 --- a/tests/playground.html +++ b/tests/playground.html @@ -657,6 +657,7 @@

Blockly Playground

+
From 3d0e85f97dcfcdfe0e340738cff7398e43e38e09 Mon Sep 17 00:00:00 2001 From: Tim Dawborn Date: Fri, 20 Jan 2017 10:15:01 +1100 Subject: [PATCH 2/4] Added unit tests for list reverse block. Corrected bugs in codegen. --- generators/lua/lists.js | 11 ++- generators/python/lists.js | 2 +- tests/generators/lists.xml | 150 +++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+), 2 deletions(-) diff --git a/generators/lua/lists.js b/generators/lua/lists.js index 993a82aabf9..837f3a2e381 100644 --- a/generators/lua/lists.js +++ b/generators/lua/lists.js @@ -368,6 +368,15 @@ Blockly.Lua['lists_reverse'] = function(block) { // Block for reversing a list. var list = Blockly.Lua.valueToCode(block, 'LIST', Blockly.Lua.ORDER_NONE) || '{}'; - var code = 'table.Reverse(' + list + ')'; + var functionName = Blockly.Lua.provideFunction_( + 'list_reverse', + ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(l)', + ' local t = {}', + ' for i = #l, 1, -1 do', + ' table.insert(t, l[i])', + ' end', + ' return t', + 'end']); + var code = 'list_reverse(' + list + ')'; return [code, Blockly.Lua.ORDER_HIGH]; }; diff --git a/generators/python/lists.js b/generators/python/lists.js index 3600516e062..3d29acf22bb 100644 --- a/generators/python/lists.js +++ b/generators/python/lists.js @@ -358,6 +358,6 @@ Blockly.Python['lists_reverse'] = function(block) { // Block for reversing a list. var list = Blockly.Python.valueToCode(block, 'LIST', Blockly.Python.ORDER_NONE) || '[]'; - var code = 'reversed(' + list + ')'; + var code = 'list(reversed(' + list + '))'; return [code, Blockly.Python.ORDER_FUNCTION_CALL]; }; diff --git a/tests/generators/lists.xml b/tests/generators/lists.xml index 88e4946b54f..aae25dfe66a 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 From 5b3b643eaf8c389371c7a3ae5062c11103304da5 Mon Sep 17 00:00:00 2001 From: Tim Dawborn Date: Sat, 21 Jan 2017 10:03:52 +1100 Subject: [PATCH 3/4] Renamed variables in Lua `list_reverse` function to be more meaningful. --- generators/lua/lists.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/generators/lua/lists.js b/generators/lua/lists.js index 837f3a2e381..6b9fe006e49 100644 --- a/generators/lua/lists.js +++ b/generators/lua/lists.js @@ -370,12 +370,12 @@ Blockly.Lua['lists_reverse'] = function(block) { Blockly.Lua.ORDER_NONE) || '{}'; var functionName = Blockly.Lua.provideFunction_( 'list_reverse', - ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(l)', - ' local t = {}', - ' for i = #l, 1, -1 do', - ' table.insert(t, l[i])', + ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(input)', + ' local reversed = {}', + ' for i = #input, 1, -1 do', + ' table.insert(reversed, input[i])', ' end', - ' return t', + ' return reversed', 'end']); var code = 'list_reverse(' + list + ')'; return [code, Blockly.Lua.ORDER_HIGH]; From 5d41270bcb16338c22b433713241a8fa510b8285 Mon Sep 17 00:00:00 2001 From: Tim Dawborn Date: Sat, 21 Jan 2017 21:37:59 +1100 Subject: [PATCH 4/4] Updated block strings as per code review. --- msg/messages.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/msg/messages.js b/msg/messages.js index ac9df905903..88d5beaed13 100644 --- a/msg/messages.js +++ b/msg/messages.js @@ -1016,10 +1016,10 @@ Blockly.Msg.LISTS_SPLIT_TOOLTIP_SPLIT = 'Split text into a list of texts, breaki 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 = ''; -/// Reverse a list of items %1. +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 - See [https://github.com/google/blockly/wiki/Lists#reversing-a-list]. +/// 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