logic.js 4.1 KB

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