mirror of
https://github.com/google/blockly.git
synced 2026-01-04 15:40:08 +01:00
* fix(build): Restore erroneously-deleted filter function This was deleted in PR #7406 as it was mainly being used to filter core/ vs. test/mocha/ deps into separate deps files - but it turns out also to be used for filtering error messages too. Oops. * refactor(tests): Migrate advanced compilation test to ES Modules * refactor(build): Migrate main.js to TypeScript This turns out to be pretty straight forward, even if it would cause crashing if one actually tried to import this module instead of just feeding it to Closure Compiler. * chore(build): Remove goog.declareModuleId calls Replace goog.declareModuleId calls with a comment recording the former module ID for posterity (or at least until we decide how to reformat the renamings file. * chore(tests): Delete closure/goog/* For the moment we still need something to serve as base.js for the benefit of closure-make-deps, so we keep a vestigial base.js around, containing only the @provideGoog declaration. * refactor(build): Remove vestigial base.js By changing slightly the command line arguments to closure-make-deps and closure-calculate-chunks the need to have any base.js is eliminated. * chore: Typo fix for PR #7415
124 lines
3.9 KiB
JavaScript
124 lines
3.9 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2015 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Generating PHP for logic blocks.
|
|
*/
|
|
|
|
// Former goog.module ID: Blockly.PHP.logic
|
|
|
|
import {Order} from './php_generator.js';
|
|
|
|
|
|
export function controls_if(block, generator) {
|
|
// If/elseif/else condition.
|
|
let n = 0;
|
|
let code = '', branchCode, conditionCode;
|
|
if (generator.STATEMENT_PREFIX) {
|
|
// Automatic prefix insertion is switched off for this block. Add manually.
|
|
code += generator.injectId(generator.STATEMENT_PREFIX, block);
|
|
}
|
|
do {
|
|
conditionCode =
|
|
generator.valueToCode(block, 'IF' + n, Order.NONE) || 'false';
|
|
branchCode = generator.statementToCode(block, 'DO' + n);
|
|
if (generator.STATEMENT_SUFFIX) {
|
|
branchCode =
|
|
generator.prefixLines(
|
|
generator.injectId(generator.STATEMENT_SUFFIX, block),
|
|
generator.INDENT) +
|
|
branchCode;
|
|
}
|
|
code += (n > 0 ? ' else ' : '') + 'if (' + conditionCode + ') {\n' +
|
|
branchCode + '}';
|
|
n++;
|
|
} while (block.getInput('IF' + n));
|
|
|
|
if (block.getInput('ELSE') || generator.STATEMENT_SUFFIX) {
|
|
branchCode = generator.statementToCode(block, 'ELSE');
|
|
if (generator.STATEMENT_SUFFIX) {
|
|
branchCode =
|
|
generator.prefixLines(
|
|
generator.injectId(generator.STATEMENT_SUFFIX, block),
|
|
generator.INDENT) +
|
|
branchCode;
|
|
}
|
|
code += ' else {\n' + branchCode + '}';
|
|
}
|
|
return code + '\n';
|
|
};
|
|
|
|
export const controls_ifelse = controls_if;
|
|
|
|
export function logic_compare(block, generator) {
|
|
// Comparison operator.
|
|
const OPERATORS =
|
|
{'EQ': '==', 'NEQ': '!=', 'LT': '<', 'LTE': '<=', 'GT': '>', 'GTE': '>='};
|
|
const operator = OPERATORS[block.getFieldValue('OP')];
|
|
const order = (operator === '==' || operator === '!=') ? Order.EQUALITY :
|
|
Order.RELATIONAL;
|
|
const argument0 = generator.valueToCode(block, 'A', order) || '0';
|
|
const argument1 = generator.valueToCode(block, 'B', order) || '0';
|
|
const code = argument0 + ' ' + operator + ' ' + argument1;
|
|
return [code, order];
|
|
};
|
|
|
|
export function logic_operation(block, generator) {
|
|
// Operations 'and', 'or'.
|
|
const operator = (block.getFieldValue('OP') === 'AND') ? '&&' : '||';
|
|
const order =
|
|
(operator === '&&') ? Order.LOGICAL_AND : Order.LOGICAL_OR;
|
|
let argument0 = generator.valueToCode(block, 'A', order);
|
|
let argument1 = generator.valueToCode(block, 'B', order);
|
|
if (!argument0 && !argument1) {
|
|
// If there are no arguments, then the return value is false.
|
|
argument0 = 'false';
|
|
argument1 = 'false';
|
|
} else {
|
|
// Single missing arguments have no effect on the return value.
|
|
const defaultArgument = (operator === '&&') ? 'true' : 'false';
|
|
if (!argument0) {
|
|
argument0 = defaultArgument;
|
|
}
|
|
if (!argument1) {
|
|
argument1 = defaultArgument;
|
|
}
|
|
}
|
|
const code = argument0 + ' ' + operator + ' ' + argument1;
|
|
return [code, order];
|
|
};
|
|
|
|
export function logic_negate(block, generator) {
|
|
// Negation.
|
|
const order = Order.LOGICAL_NOT;
|
|
const argument0 = generator.valueToCode(block, 'BOOL', order) || 'true';
|
|
const code = '!' + argument0;
|
|
return [code, order];
|
|
};
|
|
|
|
export function logic_boolean(block, generator) {
|
|
// Boolean values true and false.
|
|
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'true' : 'false';
|
|
return [code, Order.ATOMIC];
|
|
};
|
|
|
|
export function logic_null(block, generator) {
|
|
// Null data type.
|
|
return ['null', Order.ATOMIC];
|
|
};
|
|
|
|
export function logic_ternary(block, generator) {
|
|
// Ternary operator.
|
|
const value_if =
|
|
generator.valueToCode(block, 'IF', Order.CONDITIONAL) || 'false';
|
|
const value_then =
|
|
generator.valueToCode(block, 'THEN', Order.CONDITIONAL) || 'null';
|
|
const value_else =
|
|
generator.valueToCode(block, 'ELSE', Order.CONDITIONAL) || 'null';
|
|
const code = value_if + ' ? ' + value_then + ' : ' + value_else;
|
|
return [code, Order.CONDITIONAL];
|
|
};
|