procedures.js 3.8 KB

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