procedures.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 JavaScript for procedure blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.JavaScript.procedures');
  26. goog.require('Blockly.JavaScript');
  27. Blockly.JavaScript['procedures_defreturn'] = function(block) {
  28. // Define a procedure with a return value.
  29. var funcName = Blockly.JavaScript.variableDB_.getName(
  30. block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
  31. var branch = Blockly.JavaScript.statementToCode(block, 'STACK');
  32. if (Blockly.JavaScript.STATEMENT_PREFIX) {
  33. branch = Blockly.JavaScript.prefixLines(
  34. Blockly.JavaScript.STATEMENT_PREFIX.replace(/%1/g,
  35. '\'' + block.id + '\''), Blockly.JavaScript.INDENT) + branch;
  36. }
  37. if (Blockly.JavaScript.INFINITE_LOOP_TRAP) {
  38. branch = Blockly.JavaScript.INFINITE_LOOP_TRAP.replace(/%1/g,
  39. '\'' + block.id + '\'') + branch;
  40. }
  41. var returnValue = Blockly.JavaScript.valueToCode(block, 'RETURN',
  42. Blockly.JavaScript.ORDER_NONE) || '';
  43. if (returnValue) {
  44. returnValue = ' return ' + returnValue + ';\n';
  45. }
  46. var args = [];
  47. for (var x = 0; x < block.arguments_.length; x++) {
  48. args[x] = Blockly.JavaScript.variableDB_.getName(block.arguments_[x],
  49. Blockly.Variables.NAME_TYPE);
  50. }
  51. var code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
  52. branch + returnValue + '}';
  53. code = Blockly.JavaScript.scrub_(block, code);
  54. Blockly.JavaScript.definitions_[funcName] = code;
  55. return null;
  56. };
  57. // Defining a procedure without a return value uses the same generator as
  58. // a procedure with a return value.
  59. Blockly.JavaScript['procedures_defnoreturn'] =
  60. Blockly.JavaScript['procedures_defreturn'];
  61. Blockly.JavaScript['procedures_callreturn'] = function(block) {
  62. // Call a procedure with a return value.
  63. var funcName = Blockly.JavaScript.variableDB_.getName(
  64. block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
  65. var args = [];
  66. for (var x = 0; x < block.arguments_.length; x++) {
  67. args[x] = Blockly.JavaScript.valueToCode(block, 'ARG' + x,
  68. Blockly.JavaScript.ORDER_COMMA) || 'null';
  69. }
  70. var code = funcName + '(' + args.join(', ') + ')';
  71. return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
  72. };
  73. Blockly.JavaScript['procedures_callnoreturn'] = function(block) {
  74. // Call a procedure with no return value.
  75. var funcName = Blockly.JavaScript.variableDB_.getName(
  76. block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
  77. var args = [];
  78. for (var x = 0; x < block.arguments_.length; x++) {
  79. args[x] = Blockly.JavaScript.valueToCode(block, 'ARG' + x,
  80. Blockly.JavaScript.ORDER_COMMA) || 'null';
  81. }
  82. var code = funcName + '(' + args.join(', ') + ');\n';
  83. return code;
  84. };
  85. Blockly.JavaScript['procedures_ifreturn'] = function(block) {
  86. // Conditionally return value from a procedure.
  87. var condition = Blockly.JavaScript.valueToCode(block, 'CONDITION',
  88. Blockly.JavaScript.ORDER_NONE) || 'false';
  89. var code = 'if (' + condition + ') {\n';
  90. if (block.hasReturnValue_) {
  91. var value = Blockly.JavaScript.valueToCode(block, 'VALUE',
  92. Blockly.JavaScript.ORDER_NONE) || 'null';
  93. code += ' return ' + value + ';\n';
  94. } else {
  95. code += ' return;\n';
  96. }
  97. code += '}\n';
  98. return code;
  99. };