procedures.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 i = 0; i < block.arguments_.length; i++) {
  62. args[i] = Blockly.PHP.variableDB_.getName(block.arguments_[i],
  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. // Add % so as not to collide with helper functions in definitions list.
  69. Blockly.PHP.definitions_['%' + funcName] = code;
  70. return null;
  71. };
  72. // Defining a procedure without a return value uses the same generator as
  73. // a procedure with a return value.
  74. Blockly.PHP['procedures_defnoreturn'] =
  75. Blockly.PHP['procedures_defreturn'];
  76. Blockly.PHP['procedures_callreturn'] = function(block) {
  77. // Call a procedure with a return value.
  78. var funcName = Blockly.PHP.variableDB_.getName(
  79. block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
  80. var args = [];
  81. for (var i = 0; i < block.arguments_.length; i++) {
  82. args[i] = Blockly.PHP.valueToCode(block, 'ARG' + i,
  83. Blockly.PHP.ORDER_COMMA) || 'null';
  84. }
  85. var code = funcName + '(' + args.join(', ') + ')';
  86. return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
  87. };
  88. Blockly.PHP['procedures_callnoreturn'] = function(block) {
  89. // Call a procedure with no return value.
  90. var funcName = Blockly.PHP.variableDB_.getName(
  91. block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
  92. var args = [];
  93. for (var i = 0; i < block.arguments_.length; i++) {
  94. args[i] = Blockly.PHP.valueToCode(block, 'ARG' + i,
  95. Blockly.PHP.ORDER_COMMA) || 'null';
  96. }
  97. var code = funcName + '(' + args.join(', ') + ');\n';
  98. return code;
  99. };
  100. Blockly.PHP['procedures_ifreturn'] = function(block) {
  101. // Conditionally return value from a procedure.
  102. var condition = Blockly.PHP.valueToCode(block, 'CONDITION',
  103. Blockly.PHP.ORDER_NONE) || 'false';
  104. var code = 'if (' + condition + ') {\n';
  105. if (block.hasReturnValue_) {
  106. var value = Blockly.PHP.valueToCode(block, 'VALUE',
  107. Blockly.PHP.ORDER_NONE) || 'null';
  108. code += ' return ' + value + ';\n';
  109. } else {
  110. code += ' return;\n';
  111. }
  112. code += '}\n';
  113. return code;
  114. };