if.js 791 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. goog.provide('Blockly.Python.if');
  3. goog.require('Blockly.Python');
  4. Blockly.Python['controls_if_better'] = function(block) {
  5. // If/elseif/else condition.
  6. var n = 0;
  7. var code = '', branchCode, conditionCode;
  8. do {
  9. conditionCode = Blockly.Python.valueToCode(block, 'IF' + n,
  10. Blockly.Python.ORDER_NONE) || '___';
  11. branchCode = Blockly.Python.statementToCode(block, 'DO' + n) ||
  12. Blockly.Python.PASS;
  13. code += (n == 0 ? 'if ' : 'elif ' ) + conditionCode + ':\n' + branchCode;
  14. ++n;
  15. } while (block.getInput('IF' + n));
  16. if (block.getInput('ELSE')) {
  17. branchCode = Blockly.Python.statementToCode(block, 'ELSE') ||
  18. Blockly.Python.PASS;
  19. code += 'else:\n' + branchCode;
  20. }
  21. return code;
  22. };