logic.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 Python for logic blocks.
  22. * @author q.neutron@gmail.com (Quynh Neutron)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Python.logic');
  26. goog.require('Blockly.Python');
  27. Blockly.Python['controls_if'] = function(block) {
  28. // If/elseif/else condition.
  29. var n = 0;
  30. var code = '', branchCode, conditionCode;
  31. do {
  32. conditionCode = Blockly.Python.valueToCode(block, 'IF' + n,
  33. Blockly.Python.ORDER_NONE) || '___';
  34. branchCode = Blockly.Python.statementToCode(block, 'DO' + n) ||
  35. Blockly.Python.PASS;
  36. code += (n == 0 ? 'if ' : 'elif ' ) + conditionCode + ':\n' + branchCode;
  37. ++n;
  38. } while (block.getInput('IF' + n));
  39. if (block.getInput('ELSE')) {
  40. branchCode = Blockly.Python.statementToCode(block, 'ELSE') ||
  41. Blockly.Python.PASS;
  42. code += 'else:\n' + branchCode;
  43. }
  44. return code;
  45. };
  46. Blockly.Python['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 order = Blockly.Python.ORDER_RELATIONAL;
  58. var argument0 = Blockly.Python.valueToCode(block, 'A', order) || '___';
  59. var argument1 = Blockly.Python.valueToCode(block, 'B', order) || '___';
  60. var code = argument0 + ' ' + operator + ' ' + argument1;
  61. return [code, order];
  62. };
  63. Blockly.Python['logic_operation'] = function(block) {
  64. // Operations 'and', 'or'.
  65. var operator = (block.getFieldValue('OP') == 'AND') ? 'and' : 'or';
  66. var order = (operator == 'and') ? Blockly.Python.ORDER_LOGICAL_AND :
  67. Blockly.Python.ORDER_LOGICAL_OR;
  68. var argument0 = Blockly.Python.valueToCode(block, 'A', order);
  69. var argument1 = Blockly.Python.valueToCode(block, 'B', order);
  70. if (!argument0 && !argument1) {
  71. // If there are no arguments, then the return value is false.
  72. argument0 = '___';
  73. argument1 = '___';
  74. } else {
  75. // Single missing arguments have no effect on the return value.
  76. var defaultArgument = (operator == 'and') ? '___' : '___';
  77. if (!argument0) {
  78. argument0 = defaultArgument;
  79. }
  80. if (!argument1) {
  81. argument1 = defaultArgument;
  82. }
  83. }
  84. var code = argument0 + ' ' + operator + ' ' + argument1;
  85. return [code, order];
  86. };
  87. Blockly.Python['logic_negate'] = function(block) {
  88. // Negation.
  89. var argument0 = Blockly.Python.valueToCode(block, 'BOOL',
  90. Blockly.Python.ORDER_LOGICAL_NOT) || '___';
  91. var code = 'not ' + argument0;
  92. return [code, Blockly.Python.ORDER_LOGICAL_NOT];
  93. };
  94. Blockly.Python['logic_boolean'] = function(block) {
  95. // Boolean values true and false.
  96. var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'True' : 'False';
  97. return [code, Blockly.Python.ORDER_ATOMIC];
  98. };
  99. Blockly.Python['logic_null'] = function(block) {
  100. // Null data type.
  101. return ['None', Blockly.Python.ORDER_ATOMIC];
  102. };
  103. Blockly.Python['logic_ternary'] = function(block) {
  104. // Ternary operator.
  105. var value_if = Blockly.Python.valueToCode(block, 'IF',
  106. Blockly.Python.ORDER_CONDITIONAL) || '___';
  107. var value_then = Blockly.Python.valueToCode(block, 'THEN',
  108. Blockly.Python.ORDER_CONDITIONAL) || '___';
  109. var value_else = Blockly.Python.valueToCode(block, 'ELSE',
  110. Blockly.Python.ORDER_CONDITIONAL) || '___';
  111. var code = value_then + ' if ' + value_if + ' else ' + value_else;
  112. return [code, Blockly.Python.ORDER_CONDITIONAL];
  113. };
  114. Blockly.Python['logic_isIn'] = function(block) {
  115. // Operations 'in', 'not in'.
  116. var operator = (block.getFieldValue('OP') == 'IN') ? 'in' : 'not in';
  117. var order = Blockly.Python.ORDER_RELATIONAL;
  118. var argument0 = Blockly.Python.valueToCode(block, 'ITEM', order) || '___';
  119. var argument1 = Blockly.Python.valueToCode(block, 'LIST', order) || '___';
  120. var code = argument0 + ' ' + operator + ' ' + argument1;
  121. return [code, order];
  122. };
  123. Blockly.Python['logic_none'] = function(blcok) {
  124. var code = "None";
  125. return [code, Blockly.Python.ORDER_ATOMIC];
  126. }