procedures.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 Python for procedure blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Python.procedures');
  26. goog.require('Blockly.Python');
  27. Blockly.Python['procedures_defreturn'] = function(block) {
  28. // Define a procedure with a return value.
  29. // First, add a 'global' statement for every variable that is assigned.
  30. var globals = Blockly.Variables.allVariables(block);
  31. for (var i = globals.length - 1; i >= 0; i--) {
  32. var varName = globals[i];
  33. if (block.arguments_.indexOf(varName) == -1) {
  34. globals[i] = Blockly.Python.variableDB_.getName(varName,
  35. Blockly.Variables.NAME_TYPE);
  36. } else {
  37. // This variable is actually a parameter name. Do not include it in
  38. // the list of globals, thus allowing it be of local scope.
  39. globals.splice(i, 1);
  40. }
  41. }
  42. globals = globals.length ? ' global ' + globals.join(', ') + '\n' : '';
  43. var funcName = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'),
  44. Blockly.Procedures.NAME_TYPE);
  45. var branch = Blockly.Python.statementToCode(block, 'STACK');
  46. if (Blockly.Python.STATEMENT_PREFIX) {
  47. branch = Blockly.Python.prefixLines(
  48. Blockly.Python.STATEMENT_PREFIX.replace(/%1/g,
  49. '\'' + block.id + '\''), Blockly.Python.INDENT) + branch;
  50. }
  51. if (Blockly.Python.INFINITE_LOOP_TRAP) {
  52. branch = Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g,
  53. '"' + block.id + '"') + branch;
  54. }
  55. var returnValue = Blockly.Python.valueToCode(block, 'RETURN',
  56. Blockly.Python.ORDER_NONE) || '';
  57. if (returnValue) {
  58. returnValue = ' return ' + returnValue + '\n';
  59. } else if (!branch) {
  60. branch = Blockly.Python.PASS;
  61. }
  62. var args = [];
  63. for (var i = 0; i < block.arguments_.length; i++) {
  64. args[i] = Blockly.Python.variableDB_.getName(block.arguments_[i],
  65. Blockly.Variables.NAME_TYPE);
  66. }
  67. var code = 'def ' + funcName + '(' + args.join(', ') + '):\n' +
  68. globals + branch + returnValue;
  69. code = Blockly.Python.scrub_(block, code);
  70. // Add % so as not to collide with helper functions in definitions list.
  71. Blockly.Python.definitions_['%' + funcName] = code;
  72. return null;
  73. };
  74. // Defining a procedure without a return value uses the same generator as
  75. // a procedure with a return value.
  76. Blockly.Python['procedures_defnoreturn'] =
  77. Blockly.Python['procedures_defreturn'];
  78. Blockly.Python['procedures_callreturn'] = function(block) {
  79. // Call a procedure with a return value.
  80. var funcName = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'),
  81. Blockly.Procedures.NAME_TYPE);
  82. var args = [];
  83. for (var i = 0; i < block.arguments_.length; i++) {
  84. args[i] = Blockly.Python.valueToCode(block, 'ARG' + i,
  85. Blockly.Python.ORDER_NONE) || 'None';
  86. }
  87. var code = funcName + '(' + args.join(', ') + ')';
  88. return [code, Blockly.Python.ORDER_FUNCTION_CALL];
  89. };
  90. Blockly.Python['procedures_callnoreturn'] = function(block) {
  91. // Call a procedure with no return value.
  92. var funcName = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'),
  93. Blockly.Procedures.NAME_TYPE);
  94. var args = [];
  95. for (var i = 0; i < block.arguments_.length; i++) {
  96. args[i] = Blockly.Python.valueToCode(block, 'ARG' + i,
  97. Blockly.Python.ORDER_NONE) || 'None';
  98. }
  99. var code = funcName + '(' + args.join(', ') + ')\n';
  100. return code;
  101. };
  102. Blockly.Python['procedures_ifreturn'] = function(block) {
  103. // Conditionally return value from a procedure.
  104. var condition = Blockly.Python.valueToCode(block, 'CONDITION',
  105. Blockly.Python.ORDER_NONE) || 'False';
  106. var code = 'if ' + condition + ':\n';
  107. if (block.hasReturnValue_) {
  108. var value = Blockly.Python.valueToCode(block, 'VALUE',
  109. Blockly.Python.ORDER_NONE) || 'None';
  110. code += ' return ' + value + '\n';
  111. } else {
  112. code += ' return\n';
  113. }
  114. return code;
  115. };