text.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 text blocks.
  22. * @author q.neutron@gmail.com (Quynh Neutron)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Pseudo.texts');
  26. goog.require('Blockly.Pseudo');
  27. Blockly.Pseudo['text'] = function(block) {
  28. // Text value.
  29. var code = Blockly.Pseudo.quote_(block.getFieldValue('TEXT'));
  30. return [code, Blockly.Pseudo.ORDER_ATOMIC];
  31. };
  32. Blockly.Pseudo['text_join'] = function(block) {
  33. // Create a string made up of any number of elements of any type.
  34. //Should we allow joining by '-' or ',' or any other characters?
  35. var code;
  36. if (block.itemCount_ == 0) {
  37. return ['\'\'', Blockly.Pseudo.ORDER_ATOMIC];
  38. } else if (block.itemCount_ == 1) {
  39. var argument0 = Blockly.Pseudo.valueToCode(block, 'ADD0',
  40. Blockly.Pseudo.ORDER_NONE) || '___';
  41. code = 'str(' + argument0 + ')';
  42. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  43. } else if (block.itemCount_ == 2) {
  44. var argument0 = Blockly.Pseudo.valueToCode(block, 'ADD0',
  45. Blockly.Pseudo.ORDER_NONE) || '___';
  46. var argument1 = Blockly.Pseudo.valueToCode(block, 'ADD1',
  47. Blockly.Pseudo.ORDER_NONE) || '___';
  48. var code = 'str(' + argument0 + ') + str(' + argument1 + ')';
  49. return [code, Blockly.Pseudo.ORDER_UNARY_SIGN];
  50. } else {
  51. var code = [];
  52. for (var n = 0; n < block.itemCount_; n++) {
  53. code[n] = Blockly.Pseudo.valueToCode(block, 'ADD' + n,
  54. Blockly.Pseudo.ORDER_NONE) || '___';
  55. }
  56. var tempVar = Blockly.Pseudo.variableDB_.getDistinctName('temp_value',
  57. Blockly.Variables.NAME_TYPE);
  58. code = '\'\'.join([str(' + tempVar + ') for ' + tempVar + ' in [' +
  59. code.join(', ') + ']])';
  60. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  61. }
  62. };
  63. Blockly.Pseudo['text_append'] = function(block) {
  64. // Append to a variable in place.
  65. var varName = Blockly.Pseudo.variableDB_.getName(block.getFieldValue('VAR'),
  66. Blockly.Variables.NAME_TYPE);
  67. var argument0 = Blockly.Pseudo.valueToCode(block, 'TEXT',
  68. Blockly.Pseudo.ORDER_NONE) || '___';
  69. return varName + ' = str(' + varName + ') + str(' + argument0 + ')\n';
  70. };
  71. Blockly.Pseudo['text_length'] = function(block) {
  72. // Is the string null or array empty?
  73. var argument0 = Blockly.Pseudo.valueToCode(block, 'VALUE',
  74. Blockly.Pseudo.ORDER_NONE) || '___';
  75. return ['len(' + argument0 + ')', Blockly.Pseudo.ORDER_FUNCTION_CALL];
  76. };
  77. Blockly.Pseudo['text_isEmpty'] = function(block) {
  78. // Is the string null or array empty?
  79. var argument0 = Blockly.Pseudo.valueToCode(block, 'VALUE',
  80. Blockly.Pseudo.ORDER_NONE) || '___';
  81. var code = 'not len(' + argument0 + ')';
  82. return [code, Blockly.Pseudo.ORDER_LOGICAL_NOT];
  83. };
  84. Blockly.Pseudo['text_indexOf'] = function(block) {
  85. // Search the text for a substring.
  86. // Should we allow for non-case sensitive???
  87. var operator = block.getFieldValue('END') == 'FIRST' ? 'find' : 'rfind';
  88. var argument0 = Blockly.Pseudo.valueToCode(block, 'FIND',
  89. Blockly.Pseudo.ORDER_NONE) || '___';
  90. var argument1 = Blockly.Pseudo.valueToCode(block, 'VALUE',
  91. Blockly.Pseudo.ORDER_MEMBER) || '___';
  92. var code = argument1 + '.' + operator + '(' + argument0 + ') + 1';
  93. return [code, Blockly.Pseudo.ORDER_MEMBER];
  94. };
  95. Blockly.Pseudo['text_charAt'] = function(block) {
  96. // Get letter at index.
  97. // Note: Until January 2013 this block did not have the WHERE input.
  98. var where = block.getFieldValue('WHERE') || 'FROM_START';
  99. var at = Blockly.Pseudo.valueToCode(block, 'AT',
  100. Blockly.Pseudo.ORDER_UNARY_SIGN) || '1';
  101. var text = Blockly.Pseudo.valueToCode(block, 'VALUE',
  102. Blockly.Pseudo.ORDER_MEMBER) || '___';
  103. switch (where) {
  104. case 'FIRST':
  105. var code = text + '[0]';
  106. return [code, Blockly.Pseudo.ORDER_MEMBER];
  107. case 'LAST':
  108. var code = text + '[-1]';
  109. return [code, Blockly.Pseudo.ORDER_MEMBER];
  110. case 'FROM_START':
  111. // Blockly uses one-based indicies.
  112. if (Blockly.isNumber(at)) {
  113. // If the index is a naked number, decrement it right now.
  114. at = parseInt(at, 10) - 1;
  115. } else {
  116. // If the index is dynamic, decrement it in code.
  117. at = 'int(' + at + ' - 1)';
  118. }
  119. var code = text + '[' + at + ']';
  120. return [code, Blockly.Pseudo.ORDER_MEMBER];
  121. case 'FROM_END':
  122. var code = text + '[-' + at + ']';
  123. return [code, Blockly.Pseudo.ORDER_MEMBER];
  124. case 'RANDOM':
  125. Blockly.Pseudo.definitions_['import_random'] = 'import random';
  126. var functionName = Blockly.Pseudo.provideFunction_(
  127. 'text_random_letter',
  128. ['def ' + Blockly.Pseudo.FUNCTION_NAME_PLACEHOLDER_ + '(text):',
  129. ' x = int(random.random() * len(text))',
  130. ' return text[x];']);
  131. code = functionName + '(' + text + ')';
  132. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  133. }
  134. throw 'Unhandled option (text_charAt).';
  135. };
  136. Blockly.Pseudo['text_getSubstring'] = function(block) {
  137. // Get substring.
  138. var text = Blockly.Pseudo.valueToCode(block, 'STRING',
  139. Blockly.Pseudo.ORDER_MEMBER) || '___';
  140. var where1 = block.getFieldValue('WHERE1');
  141. var where2 = block.getFieldValue('WHERE2');
  142. var at1 = Blockly.Pseudo.valueToCode(block, 'AT1',
  143. Blockly.Pseudo.ORDER_ADDITIVE) || '1';
  144. var at2 = Blockly.Pseudo.valueToCode(block, 'AT2',
  145. Blockly.Pseudo.ORDER_ADDITIVE) || '1';
  146. if (where1 == 'FIRST' || (where1 == 'FROM_START' && at1 == '1')) {
  147. at1 = '';
  148. } else if (where1 == 'FROM_START') {
  149. // Blockly uses one-based indicies.
  150. if (Blockly.isNumber(at1)) {
  151. // If the index is a naked number, decrement it right now.
  152. at1 = parseInt(at1, 10) - 1;
  153. } else {
  154. // If the index is dynamic, decrement it in code.
  155. at1 = 'int(' + at1 + ' - 1)';
  156. }
  157. } else if (where1 == 'FROM_END') {
  158. if (Blockly.isNumber(at1)) {
  159. at1 = -parseInt(at1, 10);
  160. } else {
  161. at1 = '-int(' + at1 + ')';
  162. }
  163. }
  164. if (where2 == 'LAST' || (where2 == 'FROM_END' && at2 == '1')) {
  165. at2 = '';
  166. } else if (where1 == 'FROM_START') {
  167. if (Blockly.isNumber(at2)) {
  168. at2 = parseInt(at2, 10);
  169. } else {
  170. at2 = 'int(' + at2 + ')';
  171. }
  172. } else if (where1 == 'FROM_END') {
  173. if (Blockly.isNumber(at2)) {
  174. // If the index is a naked number, increment it right now.
  175. at2 = 1 - parseInt(at2, 10);
  176. if (at2 == 0) {
  177. at2 = '';
  178. }
  179. } else {
  180. // If the index is dynamic, increment it in code.
  181. // Add special case for -0.
  182. Blockly.Pseudo.definitions_['import_sys'] = 'import sys';
  183. at2 = 'int(1 - ' + at2 + ') or sys.maxsize';
  184. }
  185. }
  186. var code = text + '[' + at1 + ' : ' + at2 + ']';
  187. return [code, Blockly.Pseudo.ORDER_MEMBER];
  188. };
  189. Blockly.Pseudo['text_changeCase'] = function(block) {
  190. // Change capitalization.
  191. var OPERATORS = {
  192. 'UPPERCASE': '.upper()',
  193. 'LOWERCASE': '.lower()',
  194. 'TITLECASE': '.title()'
  195. };
  196. var operator = OPERATORS[block.getFieldValue('CASE')];
  197. var argument0 = Blockly.Pseudo.valueToCode(block, 'TEXT',
  198. Blockly.Pseudo.ORDER_MEMBER) || '___';
  199. var code = argument0 + operator;
  200. return [code, Blockly.Pseudo.ORDER_MEMBER];
  201. };
  202. Blockly.Pseudo['text_trim'] = function(block) {
  203. // Trim spaces.
  204. var OPERATORS = {
  205. 'LEFT': '.lstrip()',
  206. 'RIGHT': '.rstrip()',
  207. 'BOTH': '.strip()'
  208. };
  209. var operator = OPERATORS[block.getFieldValue('MODE')];
  210. var argument0 = Blockly.Pseudo.valueToCode(block, 'TEXT',
  211. Blockly.Pseudo.ORDER_MEMBER) || '___';
  212. var code = argument0 + operator;
  213. return [code, Blockly.Pseudo.ORDER_MEMBER];
  214. };
  215. Blockly.Pseudo['text_print'] = function(block) {
  216. // Print statement.
  217. var argument0 = Blockly.Pseudo.valueToCode(block, 'TEXT',
  218. Blockly.Pseudo.ORDER_NONE) || '___';
  219. return 'Print ' + argument0 + ' to the printer.\n';
  220. };
  221. Blockly.Pseudo['text_prompt_ext'] = function(block) {
  222. // Prompt function.
  223. var functionName = Blockly.Pseudo.provideFunction_(
  224. 'text_prompt',
  225. ['def ' + Blockly.Pseudo.FUNCTION_NAME_PLACEHOLDER_ + '(msg):',
  226. ' try:',
  227. ' return raw_input(msg)',
  228. ' except NameError:',
  229. ' return input(msg)']);
  230. if (block.getField('TEXT')) {
  231. // Internal message.
  232. var msg = Blockly.Pseudo.quote_(block.getFieldValue('TEXT'));
  233. } else {
  234. // External message.
  235. var msg = Blockly.Pseudo.valueToCode(block, 'TEXT',
  236. Blockly.Pseudo.ORDER_NONE) || '___';
  237. }
  238. var code = functionName + '(' + msg + ')';
  239. var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
  240. if (toNumber) {
  241. code = 'float(' + code + ')';
  242. }
  243. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  244. };
  245. Blockly.Pseudo['text_print_multiple'] = function(block) {
  246. // Create a list with any number of elements of any type.
  247. var inner_prints = new Array(block.itemCount_);
  248. for (var n = 0; n < block.itemCount_; n++) {
  249. inner_prints[n] = Blockly.Pseudo.valueToCode(block, 'PRINT' + n,
  250. Blockly.Pseudo.ORDER_NONE) || '___';
  251. }
  252. var code;
  253. if (block.itemCount_ == 1) {
  254. code = 'Print ' + inner_prints.join(', ') + ' to the printer.\n';
  255. } else {
  256. code = 'Print ' + inner_prints.join(', ') + ' to the printer.\n';
  257. }
  258. return code;
  259. };
  260. Blockly.Pseudo['text_prompt'] = Blockly.Pseudo['text_prompt_ext'];