logic.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2015 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 PHP for logic blocks.
  22. * @author daarond@gmail.com (Daaron Dwyer)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.PHP.logic');
  26. goog.require('Blockly.PHP');
  27. Blockly.PHP['controls_if'] = function(block) {
  28. // If/elseif/else condition.
  29. var n = 0;
  30. var argument = Blockly.PHP.valueToCode(block, 'IF' + n,
  31. Blockly.PHP.ORDER_NONE) || 'false';
  32. var branch = Blockly.PHP.statementToCode(block, 'DO' + n);
  33. var code = 'if (' + argument + ') {\n' + branch + '}';
  34. for (n = 1; n <= block.elseifCount_; n++) {
  35. argument = Blockly.PHP.valueToCode(block, 'IF' + n,
  36. Blockly.PHP.ORDER_NONE) || 'false';
  37. branch = Blockly.PHP.statementToCode(block, 'DO' + n);
  38. code += ' else if (' + argument + ') {\n' + branch + '}';
  39. }
  40. if (block.elseCount_) {
  41. branch = Blockly.PHP.statementToCode(block, 'ELSE');
  42. code += ' else {\n' + branch + '}';
  43. }
  44. return code + '\n';
  45. };
  46. Blockly.PHP['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 = (operator == '==' || operator == '!=') ?
  58. Blockly.PHP.ORDER_EQUALITY : Blockly.PHP.ORDER_RELATIONAL;
  59. var argument0 = Blockly.PHP.valueToCode(block, 'A', order) || '0';
  60. var argument1 = Blockly.PHP.valueToCode(block, 'B', order) || '0';
  61. var code = argument0 + ' ' + operator + ' ' + argument1;
  62. return [code, order];
  63. };
  64. Blockly.PHP['logic_operation'] = function(block) {
  65. // Operations 'and', 'or'.
  66. var operator = (block.getFieldValue('OP') == 'AND') ? '&&' : '||';
  67. var order = (operator == '&&') ? Blockly.PHP.ORDER_LOGICAL_AND :
  68. Blockly.PHP.ORDER_LOGICAL_OR;
  69. var argument0 = Blockly.PHP.valueToCode(block, 'A', order);
  70. var argument1 = Blockly.PHP.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 == '&&') ? '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.PHP['logic_negate'] = function(block) {
  89. // Negation.
  90. var order = Blockly.PHP.ORDER_LOGICAL_NOT;
  91. var argument0 = Blockly.PHP.valueToCode(block, 'BOOL', order) ||
  92. 'true';
  93. var code = '!' + argument0;
  94. return [code, order];
  95. };
  96. Blockly.PHP['logic_boolean'] = function(block) {
  97. // Boolean values true and false.
  98. var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'true' : 'false';
  99. return [code, Blockly.PHP.ORDER_ATOMIC];
  100. };
  101. Blockly.PHP['logic_null'] = function(block) {
  102. // Null data type.
  103. return ['null', Blockly.PHP.ORDER_ATOMIC];
  104. };
  105. Blockly.PHP['logic_ternary'] = function(block) {
  106. // Ternary operator.
  107. var value_if = Blockly.PHP.valueToCode(block, 'IF',
  108. Blockly.PHP.ORDER_CONDITIONAL) || 'false';
  109. var value_then = Blockly.PHP.valueToCode(block, 'THEN',
  110. Blockly.PHP.ORDER_CONDITIONAL) || 'null';
  111. var value_else = Blockly.PHP.valueToCode(block, 'ELSE',
  112. Blockly.PHP.ORDER_CONDITIONAL) || 'null';
  113. var code = value_if + ' ? ' + value_then + ' : ' + value_else;
  114. return [code, Blockly.PHP.ORDER_CONDITIONAL];
  115. };