loops.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 loop blocks.
  22. * @author q.neutron@gmail.com (Quynh Neutron)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Pseudo.loops');
  26. goog.require('Blockly.Pseudo');
  27. Blockly.Pseudo['controls_repeat_ext'] = function(block) {
  28. // Repeat n times.
  29. if (block.getField('TIMES')) {
  30. // Internal number.
  31. var repeats = String(parseInt(block.getFieldValue('TIMES'), 10));
  32. } else {
  33. // External number.
  34. var repeats = Blockly.Pseudo.valueToCode(block, 'TIMES',
  35. Blockly.Pseudo.ORDER_NONE) || '___';
  36. }
  37. if (Blockly.isNumber(repeats)) {
  38. repeats = parseInt(repeats, 10);
  39. } else {
  40. repeats = 'int(' + repeats + ')';
  41. }
  42. var branch = Blockly.Pseudo.statementToCode(block, 'DO');
  43. branch = Blockly.Pseudo.addLoopTrap(branch, block.id) ||
  44. Blockly.Pseudo.PASS;
  45. var loopVar = Blockly.Pseudo.variableDB_.getDistinctName(
  46. 'count', Blockly.Variables.NAME_TYPE);
  47. var code = 'for ' + loopVar + ' in range(' + repeats + '):\n' + branch;
  48. return code;
  49. };
  50. Blockly.Pseudo['controls_repeat'] = Blockly.Pseudo['controls_repeat_ext'];
  51. Blockly.Pseudo['controls_whileUntil'] = function(block) {
  52. // Do while/until loop.
  53. var until = block.getFieldValue('MODE') == 'UNTIL';
  54. var argument0 = Blockly.Pseudo.valueToCode(block, 'BOOL',
  55. until ? Blockly.Pseudo.ORDER_LOGICAL_NOT :
  56. Blockly.Pseudo.ORDER_NONE) || '___';
  57. var branch = Blockly.Pseudo.statementToCode(block, 'DO');
  58. branch = Blockly.Pseudo.addLoopTrap(branch, block.id) ||
  59. Blockly.Pseudo.PASS;
  60. if (until) {
  61. argument0 = 'not ' + argument0;
  62. }
  63. return 'while ' + argument0 + ':\n' + branch;
  64. };
  65. Blockly.Pseudo['controls_while'] = function(block) {
  66. // Do while/until loop.
  67. var argument0 = Blockly.Pseudo.valueToCode(block, 'BOOL',
  68. Blockly.Pseudo.ORDER_NONE) || '___';
  69. var branch = Blockly.Pseudo.statementToCode(block, 'DO');
  70. branch = Blockly.Pseudo.addLoopTrap(branch, block.id) ||
  71. Blockly.Pseudo.PASS;
  72. return 'while ' + argument0 + ':\n' + branch;
  73. };
  74. Blockly.Pseudo['controls_for'] = function(block) {
  75. // For loop.
  76. var variable0 = Blockly.Pseudo.variableDB_.getName(
  77. block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  78. var argument0 = Blockly.Pseudo.valueToCode(block, 'FROM',
  79. Blockly.Pseudo.ORDER_NONE) || '___';
  80. var argument1 = Blockly.Pseudo.valueToCode(block, 'TO',
  81. Blockly.Pseudo.ORDER_NONE) || '___';
  82. var increment = Blockly.Pseudo.valueToCode(block, 'BY',
  83. Blockly.Pseudo.ORDER_NONE) || '___';
  84. var branch = Blockly.Pseudo.statementToCode(block, 'DO');
  85. branch = Blockly.Pseudo.addLoopTrap(branch, block.id) ||
  86. Blockly.Pseudo.PASS;
  87. var code = '';
  88. var range;
  89. // Helper functions.
  90. var defineUpRange = function() {
  91. return Blockly.Pseudo.provideFunction_(
  92. 'upRange',
  93. ['def ' + Blockly.Pseudo.FUNCTION_NAME_PLACEHOLDER_ +
  94. '(start, stop, step):',
  95. ' while start <= stop:',
  96. ' yield start',
  97. ' start += abs(step)']);
  98. };
  99. var defineDownRange = function() {
  100. return Blockly.Pseudo.provideFunction_(
  101. 'downRange',
  102. ['def ' + Blockly.Pseudo.FUNCTION_NAME_PLACEHOLDER_ +
  103. '(start, stop, step):',
  104. ' while start >= stop:',
  105. ' yield start',
  106. ' start -= abs(step)']);
  107. };
  108. // Arguments are legal Pseudo code (numbers or strings returned by scrub()).
  109. var generateUpDownRange = function(start, end, inc) {
  110. return '(' + start + ' <= ' + end + ') and ' +
  111. defineUpRange() + '(' + start + ', ' + end + ', ' + inc + ') or ' +
  112. defineDownRange() + '(' + start + ', ' + end + ', ' + inc + ')';
  113. };
  114. if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
  115. Blockly.isNumber(increment)) {
  116. // All parameters are simple numbers.
  117. argument0 = parseFloat(argument0);
  118. argument1 = parseFloat(argument1);
  119. increment = Math.abs(parseFloat(increment));
  120. if (argument0 % 1 === 0 && argument1 % 1 === 0 && increment % 1 === 0) {
  121. // All parameters are integers.
  122. if (argument0 <= argument1) {
  123. // Count up.
  124. argument1++;
  125. if (argument0 == 0 && increment == 1) {
  126. // If starting index is 0, omit it.
  127. range = argument1;
  128. } else {
  129. range = argument0 + ', ' + argument1;
  130. }
  131. // If increment isn't 1, it must be explicit.
  132. if (increment != 1) {
  133. range += ', ' + increment;
  134. }
  135. } else {
  136. // Count down.
  137. argument1--;
  138. range = argument0 + ', ' + argument1 + ', -' + increment;
  139. }
  140. range = 'range(' + range + ')';
  141. } else {
  142. // At least one of the parameters is not an integer.
  143. if (argument0 < argument1) {
  144. range = defineUpRange();
  145. } else {
  146. range = defineDownRange();
  147. }
  148. range += '(' + argument0 + ', ' + argument1 + ', ' + increment + ')';
  149. }
  150. } else {
  151. // Cache non-trivial values to variables to prevent repeated look-ups.
  152. var scrub = function(arg, suffix) {
  153. if (Blockly.isNumber(arg)) {
  154. // Simple number.
  155. arg = parseFloat(arg);
  156. } else if (arg.match(/^\w+$/)) {
  157. // Variable.
  158. arg = 'float(' + arg + ')';
  159. } else {
  160. // It's complicated.
  161. var varName = Blockly.Pseudo.variableDB_.getDistinctName(
  162. variable0 + suffix, Blockly.Variables.NAME_TYPE);
  163. code += varName + ' = float(' + arg + ')\n';
  164. arg = varName;
  165. }
  166. return arg;
  167. };
  168. var startVar = scrub(argument0, '_start');
  169. var endVar = scrub(argument1, '_end');
  170. var incVar = scrub(increment, '_inc');
  171. if (typeof startVar == 'number' && typeof endVar == 'number') {
  172. if (startVar < endVar) {
  173. range = defineUpRange(startVar, endVar, increment);
  174. } else {
  175. range = defineDownRange(startVar, endVar, increment);
  176. }
  177. } else {
  178. // We cannot determine direction statically.
  179. range = generateUpDownRange(startVar, endVar, increment);
  180. }
  181. }
  182. code += 'for ' + variable0 + ' in ' + range + ':\n' + branch;
  183. return code;
  184. };
  185. Blockly.Pseudo['controls_forEach'] = function(block) {
  186. // For each loop.
  187. var variable0 = Blockly.Pseudo.variableDB_.getName(
  188. block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  189. var argument0 = Blockly.Pseudo.valueToCode(block, 'LIST',
  190. Blockly.Pseudo.ORDER_RELATIONAL) || '___';
  191. var branch = Blockly.Pseudo.statementToCode(block, 'DO');
  192. branch = Blockly.Pseudo.addLoopTrap(branch, block.id) ||
  193. Blockly.Pseudo.PASS;
  194. var code = 'Create a new property named <u>' + variable0 + '</u>.\nFor every element inside of the list ' + argument0 + ', set ' + variable0 + ' to that element\'s value and execute the following indented commands:\n' + branch;
  195. return code+'\n';
  196. };
  197. Blockly.Pseudo['controls_flow_statements'] = function(block) {
  198. // Flow statements: continue, break.
  199. switch (block.getFieldValue('FLOW')) {
  200. case 'BREAK':
  201. return 'Stop this loop.\n';
  202. case 'CONTINUE':
  203. return 'Continue this loop, using the next value.\n';
  204. }
  205. throw 'Unknown flow statement.';
  206. };
  207. Blockly.Pseudo['controls_pass'] = function(block) {
  208. return 'Do nothing.\n';
  209. };