procedures.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2015 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 PHP for procedure blocks.
  22. * @author daarond@gmail.com (Daaron Dwyer)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.PHP.procedures');
  26. goog.require('Blockly.PHP');
  27. Blockly.PHP['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.PHP.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.PHP.variableDB_.getName(
  44. block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
  45. var branch = Blockly.PHP.statementToCode(block, 'STACK');
  46. if (Blockly.PHP.STATEMENT_PREFIX) {
  47. branch = Blockly.PHP.prefixLines(
  48. Blockly.PHP.STATEMENT_PREFIX.replace(/%1/g,
  49. '\'' + block.id + '\''), Blockly.PHP.INDENT) + branch;
  50. }
  51. if (Blockly.PHP.INFINITE_LOOP_TRAP) {
  52. branch = Blockly.PHP.INFINITE_LOOP_TRAP.replace(/%1/g,
  53. '\'' + block.id + '\'') + branch;
  54. }
  55. var returnValue = Blockly.PHP.valueToCode(block, 'RETURN',
  56. Blockly.PHP.ORDER_NONE) || '';
  57. if (returnValue) {
  58. returnValue = ' return ' + returnValue + ';\n';
  59. }
  60. var args = [];
  61. for (var x = 0; x < block.arguments_.length; x++) {
  62. args[x] = Blockly.PHP.variableDB_.getName(block.arguments_[x],
  63. Blockly.Variables.NAME_TYPE);
  64. }
  65. var code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
  66. globals + branch + returnValue + '}';
  67. code = Blockly.PHP.scrub_(block, code);
  68. Blockly.PHP.definitions_[funcName] = code;
  69. return null;
  70. };
  71. // Defining a procedure without a return value uses the same generator as
  72. // a procedure with a return value.
  73. Blockly.PHP['procedures_defnoreturn'] =
  74. Blockly.PHP['procedures_defreturn'];
  75. Blockly.PHP['procedures_callreturn'] = function(block) {
  76. // Call a procedure with a return value.
  77. var funcName = Blockly.PHP.variableDB_.getName(
  78. block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
  79. var args = [];
  80. for (var x = 0; x < block.arguments_.length; x++) {
  81. args[x] = Blockly.PHP.valueToCode(block, 'ARG' + x,
  82. Blockly.PHP.ORDER_COMMA) || 'null';
  83. }
  84. var code = funcName + '(' + args.join(', ') + ')';
  85. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  86. };
  87. Blockly.PHP['procedures_callnoreturn'] = function(block) {
  88. // Call a procedure with no return value.
  89. var funcName = Blockly.PHP.variableDB_.getName(
  90. block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
  91. var args = [];
  92. for (var x = 0; x < block.arguments_.length; x++) {
  93. args[x] = Blockly.PHP.valueToCode(block, 'ARG' + x,
  94. Blockly.PHP.ORDER_COMMA) || 'null';
  95. }
  96. var code = funcName + '(' + args.join(', ') + ');\n';
  97. return code;
  98. };
  99. Blockly.PHP['procedures_ifreturn'] = function(block) {
  100. // Conditionally return value from a procedure.
  101. var condition = Blockly.PHP.valueToCode(block, 'CONDITION',
  102. Blockly.PHP.ORDER_NONE) || 'false';
  103. var code = 'if (' + condition + ') {\n';
  104. if (block.hasReturnValue_) {
  105. var value = Blockly.PHP.valueToCode(block, 'VALUE',
  106. Blockly.PHP.ORDER_NONE) || 'null';
  107. code += ' return ' + value + ';\n';
  108. } else {
  109. code += ' return;\n';
  110. }
  111. code += '}\n';
  112. return code;
  113. };