procedures.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 not shadowed by
  30. // a local parameter.
  31. // acbart: Actually, skip that, globals are bad news!
  32. /*var globals = []; //Blockly.Variables.allVariables(block);
  33. for (var i = 0, varName; varName = block.workspace.variableList[i]; i++) {
  34. if (block.arguments_.indexOf(varName) == -1) {
  35. globals.push(Blockly.Python.variableDB_.getName(varName,
  36. Blockly.Variables.NAME_TYPE));
  37. }
  38. }
  39. globals = globals.length ? ' global ' + globals.join(', ') + '\n' : '';*/
  40. // Get the function's name
  41. var funcName = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'),
  42. Blockly.Procedures.NAME_TYPE);
  43. // Get the stack of code
  44. var branch = Blockly.Python.statementToCode(block, 'STACK');
  45. // Handle prefixing
  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. // Handle infinite loop trapping
  52. if (Blockly.Python.INFINITE_LOOP_TRAP) {
  53. branch = Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g,
  54. '"' + block.id + '"') + branch;
  55. }
  56. // Handle return value
  57. var returnValue = Blockly.Python.valueToCode(block, 'RETURN',
  58. Blockly.Python.ORDER_NONE) || '';
  59. if (returnValue) {
  60. returnValue = ' return ' + returnValue + '\n';
  61. } else if (!branch) {
  62. branch = Blockly.Python.PASS;
  63. }
  64. var args = [];
  65. for (var i = 0; i < block.arguments_.length; i++) {
  66. args[i] = Blockly.Python.variableDB_.getName(block.arguments_[i],
  67. Blockly.Variables.NAME_TYPE);
  68. }
  69. var code = 'def ' + funcName + '(' + args.join(', ') + '):\n' +
  70. /*globals + */branch + returnValue;
  71. //acbart: I'm not sure why this is used here. It was fine before when
  72. // functions didn't have anything after them, but now it's deadly.
  73. //code = Blockly.Python.scrub_(block, code);
  74. //Blockly.Python.definitions_[funcName] = code;
  75. return code;
  76. };
  77. // Defining a procedure without a return value uses the same generator as
  78. // a procedure with a return value.
  79. Blockly.Python['procedures_defnoreturn'] =
  80. Blockly.Python['procedures_defreturn'];
  81. Blockly.Python['procedures_callreturn'] = function(block) {
  82. // Call a procedure with a return value.
  83. var funcName = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'),
  84. Blockly.Procedures.NAME_TYPE);
  85. var args = [];
  86. for (var i = 0; i < block.arguments_.length; i++) {
  87. args[i] = Blockly.Python.valueToCode(block, 'ARG' + i,
  88. Blockly.Python.ORDER_NONE) || '___';
  89. }
  90. var code = funcName + '(' + args.join(', ') + ')';
  91. if (block.outputConnection) {
  92. return [code, Blockly.Python.ORDER_FUNCTION_CALL];
  93. } else {
  94. return code;
  95. }
  96. };
  97. Blockly.Python['procedures_callnoreturn'] = function(block) {
  98. // Call a procedure with no return value.
  99. var funcName = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'),
  100. Blockly.Procedures.NAME_TYPE);
  101. var args = [];
  102. for (var i = 0; i < block.arguments_.length; i++) {
  103. args[i] = Blockly.Python.valueToCode(block, 'ARG' + i,
  104. Blockly.Python.ORDER_NONE) || '___';
  105. }
  106. var code = funcName + '(' + args.join(', ') + ')\n';
  107. if (block.outputConnection) {
  108. return [code, Blockly.Python.ORDER_FUNCTION_CALL];
  109. } else {
  110. return code;
  111. }
  112. };
  113. Blockly.Python['procedures_ifreturn'] = function(block) {
  114. // Conditionally return value from a procedure.
  115. var condition = Blockly.Python.valueToCode(block, 'CONDITION',
  116. Blockly.Python.ORDER_NONE) || '___';
  117. var code = 'if ' + condition + ':\n';
  118. if (block.hasReturnValue_) {
  119. var value = Blockly.Python.valueToCode(block, 'VALUE',
  120. Blockly.Python.ORDER_NONE) || '___';
  121. code += ' return ' + value + '\n';
  122. } else {
  123. code += ' return\n';
  124. }
  125. return code;
  126. };
  127. Blockly.Python['procedures_return'] = function(block) {
  128. // return value from a procedure.
  129. var code = "return";
  130. if (block.hasReturnValue_) {
  131. var value = Blockly.Python.valueToCode(block, 'VALUE',
  132. Blockly.Python.ORDER_NONE) || '___';
  133. code += ' ' + value + '\n';
  134. } else {
  135. code += '\n';
  136. }
  137. return code;
  138. };
  139. Blockly.Python['procedures_main'] = function(block) {
  140. // return value from a procedure.
  141. var code = "def main():\n";
  142. // Get the stack of code
  143. var branch = Blockly.Python.statementToCode(block, 'STACK');
  144. code += branch + '\nif __name__ == \'__main__\':\n'+' main()\n'
  145. return code;
  146. };