procedures.js 3.7 KB

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