logic.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 logic blocks.
  22. * @author rodrigoq@google.com (Rodrigo Queiro)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Lua.logic');
  26. goog.require('Blockly.Lua');
  27. Blockly.Lua['controls_if'] = function(block) {
  28. // If/elseif/else condition.
  29. var n = 0;
  30. var argument = Blockly.Lua.valueToCode(block, 'IF' + n,
  31. Blockly.Lua.ORDER_NONE) || 'false';
  32. var branch = Blockly.Lua.statementToCode(block, 'DO' + n);
  33. var code = 'if ' + argument + ' then\n' + branch;
  34. for (n = 1; n <= block.elseifCount_; n++) {
  35. argument = Blockly.Lua.valueToCode(block, 'IF' + n,
  36. Blockly.Lua.ORDER_NONE) || 'false';
  37. branch = Blockly.Lua.statementToCode(block, 'DO' + n);
  38. code += ' elseif ' + argument + ' then\n' + branch;
  39. }
  40. if (block.elseCount_) {
  41. branch = Blockly.Lua.statementToCode(block, 'ELSE');
  42. code += ' else\n' + branch;
  43. }
  44. return code + 'end\n';
  45. };
  46. Blockly.Lua['logic_compare'] = function(block) {
  47. // Comparison operator.
  48. var OPERATORS = {
  49. 'EQ': '==',
  50. 'NEQ': '~=',
  51. 'LT': '<',
  52. 'LTE': '<=',
  53. 'GT': '>',
  54. 'GTE': '>='
  55. };
  56. var operator = OPERATORS[block.getFieldValue('OP')];
  57. var argument0 = Blockly.Lua.valueToCode(block, 'A',
  58. Blockly.Lua.ORDER_RELATIONAL) || '0';
  59. var argument1 = Blockly.Lua.valueToCode(block, 'B',
  60. Blockly.Lua.ORDER_RELATIONAL) || '0';
  61. var code = argument0 + ' ' + operator + ' ' + argument1;
  62. return [code, Blockly.Lua.ORDER_RELATIONAL];
  63. };
  64. Blockly.Lua['logic_operation'] = function(block) {
  65. // Operations 'and', 'or'.
  66. var operator = (block.getFieldValue('OP') == 'AND') ? 'and' : 'or';
  67. var order = (operator == 'and') ? Blockly.Lua.ORDER_AND :
  68. Blockly.Lua.ORDER_OR;
  69. var argument0 = Blockly.Lua.valueToCode(block, 'A', order);
  70. var argument1 = Blockly.Lua.valueToCode(block, 'B', order);
  71. if (!argument0 && !argument1) {
  72. // If there are no arguments, then the return value is false.
  73. argument0 = 'false';
  74. argument1 = 'false';
  75. } else {
  76. // Single missing arguments have no effect on the return value.
  77. var defaultArgument = (operator == 'and') ? 'true' : 'false';
  78. if (!argument0) {
  79. argument0 = defaultArgument;
  80. }
  81. if (!argument1) {
  82. argument1 = defaultArgument;
  83. }
  84. }
  85. var code = argument0 + ' ' + operator + ' ' + argument1;
  86. return [code, order];
  87. };
  88. Blockly.Lua['logic_negate'] = function(block) {
  89. // Negation.
  90. var argument0 = Blockly.Lua.valueToCode(block, 'BOOL',
  91. Blockly.Lua.ORDER_UNARY) || 'true';
  92. var code = 'not ' + argument0;
  93. return [code, Blockly.Lua.ORDER_UNARY];
  94. };
  95. Blockly.Lua['logic_boolean'] = function(block) {
  96. // Boolean values true and false.
  97. var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'true' : 'false';
  98. return [code, Blockly.Lua.ORDER_ATOMIC];
  99. };
  100. Blockly.Lua['logic_null'] = function(block) {
  101. // Null data type.
  102. return ['nil', Blockly.Lua.ORDER_ATOMIC];
  103. };
  104. Blockly.Lua['logic_ternary'] = function(block) {
  105. // Ternary operator.
  106. var value_if = Blockly.Lua.valueToCode(block, 'IF',
  107. Blockly.Lua.ORDER_AND) || 'false';
  108. var value_then = Blockly.Lua.valueToCode(block, 'THEN',
  109. Blockly.Lua.ORDER_AND) || 'nil';
  110. var value_else = Blockly.Lua.valueToCode(block, 'ELSE',
  111. Blockly.Lua.ORDER_OR) || 'nil';
  112. var code = value_if + ' and ' + value_then + ' or ' + value_else;
  113. return [code, Blockly.Lua.ORDER_OR];
  114. };