procedures.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 procedure blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Pseudo.procedures');
  26. goog.require('Blockly.Pseudo');
  27. Blockly.Pseudo['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. // acbart: Actually, skip that, globals are bad news!
  31. var globals = []; //Blockly.Variables.allVariables(block);
  32. for (var i = globals.length - 1; i >= 0; i--) {
  33. var varName = globals[i];
  34. if (block.arguments_.indexOf(varName) == -1) {
  35. globals[i] = Blockly.Pseudo.variableDB_.getName(varName,
  36. Blockly.Variables.NAME_TYPE);
  37. } else {
  38. // This variable is actually a parameter name. Do not include it in
  39. // the list of globals, thus allowing it be of local scope.
  40. globals.splice(i, 1);
  41. }
  42. }
  43. globals = globals.length ? ' global ' + globals.join(', ') + '\n' : '';
  44. // Get the function's name
  45. var funcName = Blockly.Pseudo.variableDB_.getName(block.getFieldValue('NAME'),
  46. Blockly.Procedures.NAME_TYPE);
  47. // Get the stack of code
  48. var branch = Blockly.Pseudo.statementToCode(block, 'STACK');
  49. // Handle prefixing
  50. if (Blockly.Pseudo.STATEMENT_PREFIX) {
  51. branch = Blockly.Pseudo.prefixLines(
  52. Blockly.Pseudo.STATEMENT_PREFIX.replace(/%1/g,
  53. '\'' + block.id + '\''), Blockly.Pseudo.INDENT) + branch;
  54. }
  55. // Handle infinite loop trapping
  56. if (Blockly.Pseudo.INFINITE_LOOP_TRAP) {
  57. branch = Blockly.Pseudo.INFINITE_LOOP_TRAP.replace(/%1/g,
  58. '"' + block.id + '"') + branch;
  59. }
  60. // Handle return value
  61. var returnValue = Blockly.Pseudo.valueToCode(block, 'RETURN',
  62. Blockly.Pseudo.ORDER_NONE) || '';
  63. if (returnValue) {
  64. returnValue = 'Now the function is done, so it returns ' + returnValue + '.\n';
  65. } else if (!branch) {
  66. branch = Blockly.Pseudo.PASS;
  67. }
  68. var args = [];
  69. for (var x = 0; x < block.arguments_.length; x++) {
  70. args[x] = '<u>'+Blockly.Pseudo.variableDB_.getName(block.arguments_[x],
  71. Blockly.Variables.NAME_TYPE)+'</u>';
  72. }
  73. var funcHeader;
  74. if (args.length > 0) {
  75. funcHeader = 'The function can take the following parameters: ' + args.join(', ');
  76. } else {
  77. funcHeader = 'The function takes no parameters';
  78. }
  79. var code = 'Define a new function named <u>' + funcName + '</u>.\n' + funcHeader+ '.\nWhen called, it will do the following:\n' +
  80. globals + branch + returnValue;
  81. //acbart: I'm not sure why this is used here. It was fine before when
  82. // functions didn't have anything after them, but now it's deadly.
  83. //code = Blockly.Pseudo.scrub_(block, code);
  84. //Blockly.Pseudo.definitions_[funcName] = code;
  85. return code+'\n';
  86. };
  87. // Defining a procedure without a return value uses the same generator as
  88. // a procedure with a return value.
  89. Blockly.Pseudo['procedures_defnoreturn'] =
  90. Blockly.Pseudo['procedures_defreturn'];
  91. Blockly.Pseudo['procedures_callreturn'] = function(block) {
  92. // Call a procedure with a return value.
  93. var funcName = Blockly.Pseudo.variableDB_.getName(block.getFieldValue('NAME'),
  94. Blockly.Procedures.NAME_TYPE);
  95. var args = [];
  96. for (var x = 0; x < block.arguments_.length; x++) {
  97. args[x] = Blockly.Pseudo.valueToCode(block, 'ARG' + x,
  98. Blockly.Pseudo.ORDER_NONE) || '___';
  99. }
  100. var code = 'the result of calling the function '+funcName + ', with the following arguments: ' + args.join(', ') + '';
  101. return [code, Blockly.Pseudo.ORDER_FUNCTION_CALL];
  102. };
  103. Blockly.Pseudo['procedures_callnoreturn'] = function(block) {
  104. // Call a procedure with no return value.
  105. var funcName = Blockly.Pseudo.variableDB_.getName(block.getFieldValue('NAME'),
  106. Blockly.Procedures.NAME_TYPE);
  107. var args = [];
  108. for (var x = 0; x < block.arguments_.length; x++) {
  109. args[x] = Blockly.Pseudo.valueToCode(block, 'ARG' + x,
  110. Blockly.Pseudo.ORDER_NONE) || '___';
  111. }
  112. var code = 'Call the function '+funcName + ', with the following arguments: ' + args.join(', ') + '';
  113. return code;
  114. };
  115. Blockly.Pseudo['procedures_ifreturn'] = function(block) {
  116. // Conditionally return value from a procedure.
  117. var condition = Blockly.Pseudo.valueToCode(block, 'CONDITION',
  118. Blockly.Pseudo.ORDER_NONE) || '___';
  119. var code = 'if ' + condition + ':\n';
  120. if (block.hasReturnValue_) {
  121. var value = Blockly.Pseudo.valueToCode(block, 'VALUE',
  122. Blockly.Pseudo.ORDER_NONE) || '___';
  123. code += ' return ' + value + '\n';
  124. } else {
  125. code += ' return\n';
  126. }
  127. return code;
  128. };
  129. Blockly.Pseudo['procedures_return'] = function(block) {
  130. // return value from a procedure.
  131. var code = "Return";
  132. if (block.hasReturnValue_) {
  133. var value = Blockly.Pseudo.valueToCode(block, 'VALUE',
  134. Blockly.Pseudo.ORDER_NONE) || '___';
  135. code += ' ' + value + '.\n';
  136. } else {
  137. code += '.\n';
  138. }
  139. return code;
  140. };