lists.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Generating Pseudo for list blocks.
  22. * @author q.neutron@gmail.com (Quynh Neutron)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Pseudo.lists');
  26. goog.require('Blockly.Pseudo');
  27. Blockly.Pseudo['lists_create_empty'] = function(block) {
  28. // Create an empty list.
  29. return ['a new empty list', Blockly.Pseudo.ORDER_ATOMIC];
  30. };
  31. Blockly.Pseudo['lists_create_with'] = function(block) {
  32. // Create a list with any number of elements of any type.
  33. var code;
  34. if (block.itemCount_ > 0) {
  35. code = new Array(block.itemCount_);
  36. for (var n = 0; n < block.itemCount_; n++) {
  37. code[n] = Blockly.Pseudo.valueToCode(block, 'ADD' + n,
  38. Blockly.Pseudo.ORDER_NONE) || '___';
  39. }
  40. code = 'a new list of these elements: ' + code.join(', ') + '';
  41. } else {
  42. code = 'a new empty list';
  43. }
  44. return [code, Blockly.Pseudo.ORDER_ATOMIC];
  45. };
  46. Blockly.Pseudo['lists_repeat'] = function(block) {
  47. // Create a list with one element repeated.
  48. var argument0 = Blockly.Pseudo.valueToCode(block, 'ITEM',
  49. Blockly.Pseudo.ORDER_NONE) || '___';
  50. var argument1 = Blockly.Pseudo.valueToCode(block, 'NUM',
  51. Blockly.Pseudo.ORDER_MULTIPLICATIVE) || '___';
  52. var code = '[' + argument0 + '] * ' + argument1;
  53. return [code, Blockly.Pseudo.ORDER_MULTIPLICATIVE];
  54. };
  55. Blockly.Pseudo['lists_length'] = function(block) {
  56. // String or array length.
  57. var argument0 = Blockly.Pseudo.valueToCode(block, 'VALUE',
  58. Blockly.Pseudo.ORDER_NONE) || '___';
  59. return ['the length of ' + argument0, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  60. };
  61. Blockly.Pseudo['lists_isEmpty'] = function(block) {
  62. // Is the string null or array empty?
  63. var argument0 = Blockly.Pseudo.valueToCode(block, 'VALUE',
  64. Blockly.Pseudo.ORDER_NONE) || '___';
  65. var code = 'not len(' + argument0 + ')';
  66. return [code, Blockly.Pseudo.ORDER_LOGICAL_NOT];
  67. };
  68. Blockly.Pseudo['lists_indexOf'] = function(block) {
  69. // Find an item in the list.
  70. var argument0 = Blockly.Pseudo.valueToCode(block, 'FIND',
  71. Blockly.Pseudo.ORDER_NONE) || '___';
  72. var argument1 = Blockly.Pseudo.valueToCode(block, 'VALUE',
  73. Blockly.Pseudo.ORDER_MEMBER) || '___';
  74. var code;
  75. if (block.getFieldValue('END') == 'FIRST') {
  76. var functionName = Blockly.Pseudo.provideFunction_(
  77. 'first_index',
  78. ['def ' + Blockly.Pseudo.FUNCTION_NAME_PLACEHOLDER_ + '(myList, elem):',
  79. ' try: theIndex = myList.index(elem) + 1',
  80. ' except: theIndex = 0',
  81. ' return theIndex']);
  82. code = functionName + '(' + argument1 + ', ' + argument0 + ')';
  83. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  84. } else {
  85. var functionName = Blockly.Pseudo.provideFunction_(
  86. 'last_index',
  87. ['def ' + Blockly.Pseudo.FUNCTION_NAME_PLACEHOLDER_ + '(myList, elem):',
  88. ' try: theIndex = len(myList) - myList[::-1].index(elem)',
  89. ' except: theIndex = 0',
  90. ' return theIndex']);
  91. code = functionName + '(' + argument1 + ', ' + argument0 + ')';
  92. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  93. }
  94. };
  95. Blockly.Pseudo['lists_index'] = function(block) {
  96. var at = Blockly.Pseudo.valueToCode(block, 'ITEM',
  97. Blockly.Pseudo.ORDER_UNARY_SIGN) || '___';
  98. var list = Blockly.Pseudo.valueToCode(block, 'LIST',
  99. Blockly.Pseudo.ORDER_MEMBER) || '___';
  100. var code = list + '[' + at + ']';
  101. return [code, Blockly.Pseudo.ORDER_MEMBER];
  102. }
  103. Blockly.Pseudo['lists_getIndex'] = function(block) {
  104. // Get element at index.
  105. // Note: Until January 2013 this block did not have MODE or WHERE inputs.
  106. var mode = block.getFieldValue('MODE') || 'GET';
  107. var where = block.getFieldValue('WHERE') || 'FROM_START';
  108. var at = Blockly.Pseudo.valueToCode(block, 'AT',
  109. Blockly.Pseudo.ORDER_UNARY_SIGN) || '___';
  110. var list = Blockly.Pseudo.valueToCode(block, 'VALUE',
  111. Blockly.Pseudo.ORDER_MEMBER) || '___';
  112. if (where == 'FIRST') {
  113. if (mode == 'GET') {
  114. var code = list + '[0]';
  115. return [code, Blockly.Pseudo.ORDER_MEMBER];
  116. } else {
  117. var code = list + '.pop(0)';
  118. if (mode == 'GET_REMOVE') {
  119. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  120. } else if (mode == 'REMOVE') {
  121. return code + '\n';
  122. }
  123. }
  124. } else if (where == 'LAST') {
  125. if (mode == 'GET') {
  126. var code = list + '[-1]';
  127. return [code, Blockly.Pseudo.ORDER_MEMBER];
  128. } else {
  129. var code = list + '.pop()';
  130. if (mode == 'GET_REMOVE') {
  131. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  132. } else if (mode == 'REMOVE') {
  133. return code + '\n';
  134. }
  135. }
  136. } else if (where == 'FROM_START') {
  137. // Blockly uses one-based indicies.
  138. if (Blockly.isNumber(at)) {
  139. // If the index is a naked number, decrement it right now.
  140. at = parseInt(at, 10) - 1;
  141. } else {
  142. // If the index is dynamic, decrement it in code.
  143. at = 'int(' + at + ' - 1)';
  144. }
  145. if (mode == 'GET') {
  146. var code = list + '[' + at + ']';
  147. return [code, Blockly.Pseudo.ORDER_MEMBER];
  148. } else {
  149. var code = list + '.pop(' + at + ')';
  150. if (mode == 'GET_REMOVE') {
  151. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  152. } else if (mode == 'REMOVE') {
  153. return code + '\n';
  154. }
  155. }
  156. } else if (where == 'FROM_END') {
  157. if (mode == 'GET') {
  158. var code = list + '[-' + at + ']';
  159. return [code, Blockly.Pseudo.ORDER_MEMBER];
  160. } else {
  161. var code = list + '.pop(-' + at + ')';
  162. if (mode == 'GET_REMOVE') {
  163. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  164. } else if (mode == 'REMOVE') {
  165. return code + '\n';
  166. }
  167. }
  168. } else if (where == 'RANDOM') {
  169. Blockly.Pseudo.definitions_['import_random'] = 'import random';
  170. if (mode == 'GET') {
  171. code = 'random.choice(' + list + ')';
  172. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  173. } else {
  174. var functionName = Blockly.Pseudo.provideFunction_(
  175. 'lists_remove_random_item',
  176. ['def ' + Blockly.Pseudo.FUNCTION_NAME_PLACEHOLDER_ + '(myList):',
  177. ' x = int(random.random() * len(myList))',
  178. ' return myList.pop(x)']);
  179. code = functionName + '(' + list + ')';
  180. if (mode == 'GET_REMOVE') {
  181. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  182. } else if (mode == 'REMOVE') {
  183. return code + '\n';
  184. }
  185. }
  186. }
  187. throw 'Unhandled combination (lists_getIndex).';
  188. };
  189. Blockly.Pseudo['lists_setIndex'] = function(block) {
  190. // Set element at index.
  191. // Note: Until February 2013 this block did not have MODE or WHERE inputs.
  192. var list = Blockly.Pseudo.valueToCode(block, 'LIST',
  193. Blockly.Pseudo.ORDER_MEMBER) || '___';
  194. var mode = block.getFieldValue('MODE') || 'GET';
  195. var where = block.getFieldValue('WHERE') || 'FROM_START';
  196. var at = Blockly.Pseudo.valueToCode(block, 'AT',
  197. Blockly.Pseudo.ORDER_NONE) || '___';
  198. var value = Blockly.Pseudo.valueToCode(block, 'TO',
  199. Blockly.Pseudo.ORDER_NONE) || '___';
  200. // Cache non-trivial values to variables to prevent repeated look-ups.
  201. // Closure, which accesses and modifies 'list'.
  202. function cacheList() {
  203. if (list.match(/^\w+$/)) {
  204. return '';
  205. }
  206. var listVar = Blockly.Pseudo.variableDB_.getDistinctName(
  207. 'tmp_list', Blockly.Variables.NAME_TYPE);
  208. var code = listVar + ' = ' + list + '\n';
  209. list = listVar;
  210. return code;
  211. }
  212. if (where == 'FIRST') {
  213. if (mode == 'SET') {
  214. return list + '[0] = ' + value + '\n';
  215. } else if (mode == 'INSERT') {
  216. return list + '.insert(0, ' + value + ')\n';
  217. }
  218. } else if (where == 'LAST') {
  219. if (mode == 'SET') {
  220. return list + '[-1] = ' + value + '\n';
  221. } else if (mode == 'INSERT') {
  222. return list + '.append(' + value + ')\n';
  223. }
  224. } else if (where == 'FROM_START') {
  225. // Blockly uses one-based indicies.
  226. if (Blockly.isNumber(at)) {
  227. // If the index is a naked number, decrement it right now.
  228. at = parseInt(at, 10) - 1;
  229. } else {
  230. // If the index is dynamic, decrement it in code.
  231. at = 'int(' + at + ' - 1)';
  232. }
  233. if (mode == 'SET') {
  234. return list + '[' + at + '] = ' + value + '\n';
  235. } else if (mode == 'INSERT') {
  236. return list + '.insert(' + at + ', ' + value + ')\n';
  237. }
  238. } else if (where == 'FROM_END') {
  239. if (mode == 'SET') {
  240. return list + '[-' + at + '] = ' + value + '\n';
  241. } else if (mode == 'INSERT') {
  242. return list + '.insert(-' + at + ', ' + value + ')\n';
  243. }
  244. } else if (where == 'RANDOM') {
  245. Blockly.Pseudo.definitions_['import_random'] = 'import random';
  246. var code = cacheList();
  247. var xVar = Blockly.Pseudo.variableDB_.getDistinctName(
  248. 'tmp_x', Blockly.Variables.NAME_TYPE);
  249. code += xVar + ' = int(random.random() * len(' + list + '))\n';
  250. if (mode == 'SET') {
  251. code += list + '[' + xVar + '] = ' + value + '\n';
  252. return code;
  253. } else if (mode == 'INSERT') {
  254. code += list + '.insert(' + xVar + ', ' + value + ')\n';
  255. return code;
  256. }
  257. }
  258. throw 'Unhandled combination (lists_setIndex).';
  259. };
  260. Blockly.Pseudo['lists_getSublist'] = function(block) {
  261. // Get sublist.
  262. var list = Blockly.Pseudo.valueToCode(block, 'LIST',
  263. Blockly.Pseudo.ORDER_MEMBER) || '___';
  264. var where1 = block.getFieldValue('WHERE1');
  265. var where2 = block.getFieldValue('WHERE2');
  266. var at1 = Blockly.Pseudo.valueToCode(block, 'AT1',
  267. Blockly.Pseudo.ORDER_ADDITIVE) || '___';
  268. var at2 = Blockly.Pseudo.valueToCode(block, 'AT2',
  269. Blockly.Pseudo.ORDER_ADDITIVE) || '___';
  270. if (where1 == 'FIRST' || (where1 == 'FROM_START' && at1 == '1')) {
  271. at1 = '';
  272. } else if (where1 == 'FROM_START') {
  273. // Blockly uses one-based indicies.
  274. if (Blockly.isNumber(at1)) {
  275. // If the index is a naked number, decrement it right now.
  276. at1 = parseInt(at1, 10) - 1;
  277. } else {
  278. // If the index is dynamic, decrement it in code.
  279. at1 = 'int(' + at1 + ' - 1)';
  280. }
  281. } else if (where1 == 'FROM_END') {
  282. if (Blockly.isNumber(at1)) {
  283. at1 = -parseInt(at1, 10);
  284. } else {
  285. at1 = '-int(' + at1 + ')';
  286. }
  287. }
  288. if (where2 == 'LAST' || (where2 == 'FROM_END' && at2 == '1')) {
  289. at2 = '';
  290. } else if (where1 == 'FROM_START') {
  291. if (Blockly.isNumber(at2)) {
  292. at2 = parseInt(at2, 10);
  293. } else {
  294. at2 = 'int(' + at2 + ')';
  295. }
  296. } else if (where1 == 'FROM_END') {
  297. if (Blockly.isNumber(at2)) {
  298. // If the index is a naked number, increment it right now.
  299. // Add special case for -0.
  300. at2 = 1 - parseInt(at2, 10);
  301. if (at2 == 0) {
  302. at2 = '';
  303. }
  304. } else {
  305. // If the index is dynamic, increment it in code.
  306. Blockly.Pseudo.definitions_['import_sys'] = 'import sys';
  307. at2 = 'int(1 - ' + at2 + ') or sys.maxsize';
  308. }
  309. }
  310. var code = list + '[' + at1 + ' : ' + at2 + ']';
  311. return [code, Blockly.Pseudo.ORDER_MEMBER];
  312. };
  313. Blockly.Pseudo['lists_split'] = function(block) {
  314. // Block for splitting text into a list, or joining a list into text.
  315. var mode = block.getFieldValue('MODE');
  316. if (mode == 'SPLIT') {
  317. var value_input = Blockly.Pseudo.valueToCode(block, 'INPUT',
  318. Blockly.Pseudo.ORDER_MEMBER) || '___';
  319. var value_delim = Blockly.Pseudo.valueToCode(block, 'DELIM',
  320. Blockly.Pseudo.ORDER_NONE);
  321. var code = value_input + '.split(' + value_delim + ')';
  322. } else if (mode == 'JOIN') {
  323. var value_input = Blockly.Pseudo.valueToCode(block, 'INPUT',
  324. Blockly.Pseudo.ORDER_NONE) || '___';
  325. var value_delim = Blockly.Pseudo.valueToCode(block, 'DELIM',
  326. Blockly.Pseudo.ORDER_MEMBER) || '___';
  327. var code = value_delim + '.join(' + value_input + ')';
  328. } else {
  329. throw 'Unknown mode: ' + mode;
  330. }
  331. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  332. };
  333. Blockly.Pseudo['lists_append'] = function(block) {
  334. // Append
  335. var list = Blockly.Pseudo.valueToCode(block, 'LIST',
  336. Blockly.Pseudo.ORDER_MEMBER) || '___';
  337. var value = Blockly.Pseudo.valueToCode(block, 'ITEM',
  338. Blockly.Pseudo.ORDER_NONE) || '___';
  339. return 'Append ' +value + ' to ' + list + ' (which must be a list).\n';
  340. };