logic.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2012 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 Pseudo for logic blocks.
  22. * @author q.neutron@gmail.com (Quynh Neutron)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Pseudo.logic');
  26. goog.require('Blockly.Pseudo');
  27. Blockly.Pseudo['controls_if'] = function(block) {
  28. // If/elseif/else condition.
  29. var n = 0;
  30. var argument = Blockly.Pseudo.valueToCode(block, 'IF' + n,
  31. Blockly.Pseudo.ORDER_NONE) || '___';
  32. var branch = Blockly.Pseudo.statementToCode(block, 'DO' + n) ||
  33. Blockly.Pseudo.PASS;
  34. var code = 'if ' + argument + ':\n' + branch;
  35. for (n = 1; n <= block.elseifCount_; n++) {
  36. argument = Blockly.Pseudo.valueToCode(block, 'IF' + n,
  37. Blockly.Pseudo.ORDER_NONE) || '___';
  38. branch = Blockly.Pseudo.statementToCode(block, 'DO' + n) ||
  39. Blockly.Pseudo.PASS;
  40. code += 'otherwise if ' + argument + ':\n' + branch;
  41. }
  42. if (block.elseCount_) {
  43. branch = Blockly.Pseudo.statementToCode(block, 'ELSE') ||
  44. Blockly.Pseudo.PASS;
  45. code += 'otherwise:\n' + branch;
  46. }
  47. return code;
  48. };
  49. Blockly.Pseudo['logic_compare'] = function(block) {
  50. // Comparison operator.
  51. var OPERATORS = {
  52. 'EQ': ' is equal to ',
  53. 'NEQ': ' is not equal to ',
  54. 'LT': ' is less than ',
  55. 'LTE': ' is less than or equal to ',
  56. 'GT': ' is greater than ',
  57. 'GTE': ' is greater than or equal to '
  58. };
  59. var operator = OPERATORS[block.getFieldValue('OP')];
  60. var order = Blockly.Pseudo.ORDER_RELATIONAL;
  61. var argument0 = Blockly.Pseudo.valueToCode(block, 'A', order) || '___';
  62. var argument1 = Blockly.Pseudo.valueToCode(block, 'B', order) || '___';
  63. var code = argument0 + ' ' + operator + ' ' + argument1;
  64. return [code, order];
  65. };
  66. Blockly.Pseudo['logic_operation'] = function(block) {
  67. // Operations 'and', 'or'.
  68. var operator = (block.getFieldValue('OP') == 'AND') ? 'and' : 'or';
  69. var order = (operator == 'and') ? Blockly.Pseudo.ORDER_LOGICAL_AND :
  70. Blockly.Pseudo.ORDER_LOGICAL_OR;
  71. var argument0 = Blockly.Pseudo.valueToCode(block, 'A', order);
  72. var argument1 = Blockly.Pseudo.valueToCode(block, 'B', order);
  73. if (!argument0 && !argument1) {
  74. // If there are no arguments, then the return value is false.
  75. argument0 = '___';
  76. argument1 = '___';
  77. } else {
  78. // Single missing arguments have no effect on the return value.
  79. var defaultArgument = (operator == 'and') ? '___' : '___';
  80. if (!argument0) {
  81. argument0 = defaultArgument;
  82. }
  83. if (!argument1) {
  84. argument1 = defaultArgument;
  85. }
  86. }
  87. var code = argument0 + ' ' + operator + ' ' + argument1;
  88. return [code, order];
  89. };
  90. Blockly.Pseudo['logic_negate'] = function(block) {
  91. // Negation.
  92. var argument0 = Blockly.Pseudo.valueToCode(block, 'BOOL',
  93. Blockly.Pseudo.ORDER_LOGICAL_NOT) || '___';
  94. var code = 'not ' + argument0;
  95. return [code, Blockly.Pseudo.ORDER_LOGICAL_NOT];
  96. };
  97. Blockly.Pseudo['logic_boolean'] = function(block) {
  98. // Boolean values true and false.
  99. var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'True' : 'False';
  100. return [code, Blockly.Pseudo.ORDER_ATOMIC];
  101. };
  102. Blockly.Pseudo['logic_null'] = function(block) {
  103. // Null data type.
  104. return ['None', Blockly.Pseudo.ORDER_ATOMIC];
  105. };
  106. Blockly.Pseudo['logic_ternary'] = function(block) {
  107. // Ternary operator.
  108. var value_if = Blockly.Pseudo.valueToCode(block, 'IF',
  109. Blockly.Pseudo.ORDER_CONDITIONAL) || '___';
  110. var value_then = Blockly.Pseudo.valueToCode(block, 'THEN',
  111. Blockly.Pseudo.ORDER_CONDITIONAL) || '___';
  112. var value_else = Blockly.Pseudo.valueToCode(block, 'ELSE',
  113. Blockly.Pseudo.ORDER_CONDITIONAL) || '___';
  114. var code = value_then + ' if ' + value_if + ' else ' + value_else;
  115. return [code, Blockly.Pseudo.ORDER_CONDITIONAL];
  116. };
  117. Blockly.Pseudo['logic_isIn'] = function(block) {
  118. // Operations 'in', 'not in'.
  119. var operator = (block.getFieldValue('OP') == 'IN') ? 'in' : 'not in';
  120. var order = Blockly.Pseudo.ORDER_RELATIONAL;
  121. var argument0 = Blockly.Pseudo.valueToCode(block, 'ITEM', order) || '___';
  122. var argument1 = Blockly.Pseudo.valueToCode(block, 'LIST', order) || '___';
  123. var code = argument0 + ' ' + operator + ' ' + argument1;
  124. return [code, order];
  125. };