logic.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * @license Licensed under the Apache License, Version 2.0 (the "License"):
  3. * http://www.apache.org/licenses/LICENSE-2.0
  4. */
  5. /**
  6. * @fileoverview Generating Arduino code for the logic blocks.
  7. */
  8. 'use strict';
  9. goog.provide('Blockly.Python.logic');
  10. goog.require('Blockly.Python');
  11. /**
  12. * Code generator to create if/if else/else statement.
  13. * Arduino code: loop { if (X)/else if ()/else { X } }
  14. * @param {!Blockly.Block} block Block to generate the code from.
  15. * @return {string} Completed code.
  16. */
  17. Blockly.Python['controls_if'] = function(block) {
  18. var n = 0;
  19. var argument = Blockly.Python.valueToCode(block, 'IF' + n,
  20. Blockly.Python.ORDER_NONE) || 'false';
  21. var branch = Blockly.Python.statementToCode(block, 'DO' + n);
  22. var code = 'if (' + argument + ') {\n' + branch + '}';
  23. for (n = 1; n <= block.elseifCount_; n++) {
  24. argument = Blockly.Python.valueToCode(block, 'IF' + n,
  25. Blockly.Python.ORDER_NONE) || 'false';
  26. branch = Blockly.Python.statementToCode(block, 'DO' + n);
  27. code += ' else if (' + argument + ') {\n' + branch + '}';
  28. }
  29. if (block.elseCount_) {
  30. branch = Blockly.Python.statementToCode(block, 'ELSE');
  31. code += ' else {\n' + branch + '}';
  32. }
  33. return code + '\n';
  34. };
  35. Blockly.Python['switch_statement'] = function(block) {
  36. var variable_name = Blockly.Python.variableDB_.getName(block.getFieldValue('switch_var'), Blockly.Variables.NAME_TYPE);
  37. var code = "switch (" + variable_name + ") {\n";
  38. var argument, branch;
  39. for (var n = 0; n <= block.caseCount_; n++) {
  40. argument = Blockly.Python.valueToCode(block, 'CASE' + n,
  41. Blockly.Python.ORDER_NONE) || n;
  42. branch = Blockly.Python.statementToCode(block, 'DO' + n);
  43. code += " case " + argument + ":\n" + branch + " break;\n";
  44. }
  45. if (block.defaultCount_) {
  46. branch = Blockly.Python.statementToCode(block, 'DEFAULT');
  47. code += ' default:\n' + branch + ' break;\n';
  48. }
  49. return code + '}\n';
  50. }
  51. /**
  52. * Code generator for the comparison operator block.
  53. * Arduino code: loop { X operator Y }
  54. * @param {!Blockly.Block} block Block to generate the code from.
  55. * @return {array} Completed code with order of operation.
  56. */
  57. Blockly.Python['logic_compare'] = function(block) {
  58. var OPERATORS = {
  59. 'EQ': '==',
  60. 'NEQ': '!=',
  61. 'LT': '<',
  62. 'LTE': '<=',
  63. 'GT': '>',
  64. 'GTE': '>='
  65. };
  66. var operator = OPERATORS[block.getFieldValue('OP')];
  67. var order = (operator == '==' || operator == '!=') ?
  68. Blockly.Python.ORDER_EQUALITY : Blockly.Python.ORDER_RELATIONAL;
  69. var argument0 = Blockly.Python.valueToCode(block, 'A', order) || '0';
  70. var argument1 = Blockly.Python.valueToCode(block, 'B', order) || '0';
  71. var code = argument0 + ' ' + operator + ' ' + argument1;
  72. return [code, order];
  73. };
  74. /**
  75. * Code generator for the logic operator block.
  76. * Arduino code: loop { X operator Y }
  77. * @param {!Blockly.Block} block Block to generate the code from.
  78. * @return {array} Completed code with order of operation.
  79. */
  80. Blockly.Python['logic_operation'] = function(block) {
  81. var operator = (block.getFieldValue('OP') == 'AND') ? '&&' : '||';
  82. var order = (operator == '&&') ? Blockly.Python.ORDER_LOGICAL_AND :
  83. Blockly.Python.ORDER_LOGICAL_OR;
  84. var argument0 = Blockly.Python.valueToCode(block, 'A', order) || 'false';
  85. var argument1 = Blockly.Python.valueToCode(block, 'B', order) || 'false';
  86. if (!argument0 && !argument1) {
  87. // If there are no arguments, then the return value is false.
  88. argument0 = 'false';
  89. argument1 = 'false';
  90. } else {
  91. // Single missing arguments have no effect on the return value.
  92. var defaultArgument = (operator == '&&') ? 'true' : 'false';
  93. if (!argument0) {
  94. argument0 = defaultArgument;
  95. }
  96. if (!argument1) {
  97. argument1 = defaultArgument;
  98. }
  99. }
  100. var code = argument0 + ' ' + operator + ' ' + argument1;
  101. return [code, order];
  102. };
  103. /**
  104. * Code generator for the logic negate operator.
  105. * Arduino code: loop { !X }
  106. * @param {!Blockly.Block} block Block to generate the code from.
  107. * @return {array} Completed code with order of operation.
  108. */
  109. Blockly.Python['logic_negate'] = function(block) {
  110. var order = Blockly.Python.ORDER_UNARY_PREFIX;
  111. var argument0 = Blockly.Python.valueToCode(block, 'BOOL', order) || 'false';
  112. var code = '!' + argument0;
  113. return [code, order];
  114. };
  115. /**
  116. * Code generator for the boolean values true and false.
  117. * Arduino code: loop { true/false }
  118. * @param {!Blockly.Block} block Block to generate the code from.
  119. * @return {array} Completed code with order of operation.
  120. */
  121. Blockly.Python['logic_boolean'] = function(block) {
  122. var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'true' : 'false';
  123. return [code, Blockly.Python.ORDER_ATOMIC];
  124. };
  125. /**
  126. * Code generator for the null value.
  127. * Arduino code: loop { X ? Y : Z }
  128. * @param {!Blockly.Block} block Block to generate the code from.
  129. * @return {array} Completed code with order of operation.
  130. */
  131. Blockly.Python['logic_null'] = function(block) {
  132. var code = 'NULL';
  133. return [code, Blockly.Python.ORDER_ATOMIC];
  134. };
  135. /**
  136. * Code generator for the ternary operator.
  137. * Arduino code: loop { NULL }
  138. * @param {!Blockly.Block} block Block to generate the code from.
  139. * @return {array} Completed code with order of operation.
  140. *
  141. * TODO: Check types of THEN and ELSE blocks and add warning to this block if
  142. * they are different from each other.
  143. */
  144. Blockly.Python['logic_ternary'] = function(block) {
  145. var valueIf = Blockly.Python.valueToCode(block, 'IF',
  146. Blockly.Python.ORDER_CONDITIONAL) || 'false';
  147. var valueThen = Blockly.Python.valueToCode(block, 'THEN',
  148. Blockly.Python.ORDER_CONDITIONAL) || 'null';
  149. var valueElse = Blockly.Python.valueToCode(block, 'ELSE',
  150. Blockly.Python.ORDER_CONDITIONAL) || 'null';
  151. var code = valueIf + ' ? ' + valueThen + ' : ' + valueElse;
  152. return [code, Blockly.Python.ORDER_CONDITIONAL];
  153. };