mirror of
https://github.com/google/blockly.git
synced 2026-01-08 17:40:09 +01:00
refactor(generators): Introduce PhpGenerator class, Order enum (#7162)
* refactor(generators): Introduce PhpGenerator class, Order enum * refactor(generators): Use Order.* instead of .ORDER_* * refactor(generators): Don't rename phpGenerator
This commit is contained in:
committed by
GitHub
parent
2f89c0dd09
commit
eeb89194c4
@@ -12,13 +12,13 @@ import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.PHP.math');
|
||||
|
||||
import {NameType} from '../../core/names.js';
|
||||
import {phpGenerator as PHP} from '../php.js';
|
||||
import {phpGenerator, Order} from '../php.js';
|
||||
|
||||
|
||||
PHP.forBlock['math_number'] = function(block) {
|
||||
phpGenerator.forBlock['math_number'] = function(block) {
|
||||
// Numeric value.
|
||||
let code = Number(block.getFieldValue('NUM'));
|
||||
const order = code >= 0 ? PHP.ORDER_ATOMIC : PHP.ORDER_UNARY_NEGATION;
|
||||
const order = code >= 0 ? Order.ATOMIC : Order.UNARY_NEGATION;
|
||||
if (code === Infinity) {
|
||||
code = 'INF';
|
||||
} else if (code === -Infinity) {
|
||||
@@ -27,43 +27,43 @@ PHP.forBlock['math_number'] = function(block) {
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
PHP.forBlock['math_arithmetic'] = function(block) {
|
||||
phpGenerator.forBlock['math_arithmetic'] = function(block) {
|
||||
// Basic arithmetic operators, and power.
|
||||
const OPERATORS = {
|
||||
'ADD': [' + ', PHP.ORDER_ADDITION],
|
||||
'MINUS': [' - ', PHP.ORDER_SUBTRACTION],
|
||||
'MULTIPLY': [' * ', PHP.ORDER_MULTIPLICATION],
|
||||
'DIVIDE': [' / ', PHP.ORDER_DIVISION],
|
||||
'POWER': [' ** ', PHP.ORDER_POWER],
|
||||
'ADD': [' + ', Order.ADDITION],
|
||||
'MINUS': [' - ', Order.SUBTRACTION],
|
||||
'MULTIPLY': [' * ', Order.MULTIPLICATION],
|
||||
'DIVIDE': [' / ', Order.DIVISION],
|
||||
'POWER': [' ** ', Order.POWER],
|
||||
};
|
||||
const tuple = OPERATORS[block.getFieldValue('OP')];
|
||||
const operator = tuple[0];
|
||||
const order = tuple[1];
|
||||
const argument0 = PHP.valueToCode(block, 'A', order) || '0';
|
||||
const argument1 = PHP.valueToCode(block, 'B', order) || '0';
|
||||
const argument0 = phpGenerator.valueToCode(block, 'A', order) || '0';
|
||||
const argument1 = phpGenerator.valueToCode(block, 'B', order) || '0';
|
||||
const code = argument0 + operator + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
PHP.forBlock['math_single'] = function(block) {
|
||||
phpGenerator.forBlock['math_single'] = function(block) {
|
||||
// Math operators with single operand.
|
||||
const operator = block.getFieldValue('OP');
|
||||
let code;
|
||||
let arg;
|
||||
if (operator === 'NEG') {
|
||||
// Negation is a special case given its different operator precedence.
|
||||
arg = PHP.valueToCode(block, 'NUM', PHP.ORDER_UNARY_NEGATION) || '0';
|
||||
arg = phpGenerator.valueToCode(block, 'NUM', Order.UNARY_NEGATION) || '0';
|
||||
if (arg[0] === '-') {
|
||||
// --3 is not legal in JS.
|
||||
arg = ' ' + arg;
|
||||
}
|
||||
code = '-' + arg;
|
||||
return [code, PHP.ORDER_UNARY_NEGATION];
|
||||
return [code, Order.UNARY_NEGATION];
|
||||
}
|
||||
if (operator === 'SIN' || operator === 'COS' || operator === 'TAN') {
|
||||
arg = PHP.valueToCode(block, 'NUM', PHP.ORDER_DIVISION) || '0';
|
||||
arg = phpGenerator.valueToCode(block, 'NUM', Order.DIVISION) || '0';
|
||||
} else {
|
||||
arg = PHP.valueToCode(block, 'NUM', PHP.ORDER_NONE) || '0';
|
||||
arg = phpGenerator.valueToCode(block, 'NUM', Order.NONE) || '0';
|
||||
}
|
||||
// First, handle cases which generate values that don't need parentheses
|
||||
// wrapping the code.
|
||||
@@ -103,7 +103,7 @@ PHP.forBlock['math_single'] = function(block) {
|
||||
break;
|
||||
}
|
||||
if (code) {
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
}
|
||||
// Second, handle cases which generate values that may need parentheses
|
||||
// wrapping the code.
|
||||
@@ -123,43 +123,44 @@ PHP.forBlock['math_single'] = function(block) {
|
||||
default:
|
||||
throw Error('Unknown math operator: ' + operator);
|
||||
}
|
||||
return [code, PHP.ORDER_DIVISION];
|
||||
return [code, Order.DIVISION];
|
||||
};
|
||||
|
||||
PHP.forBlock['math_constant'] = function(block) {
|
||||
phpGenerator.forBlock['math_constant'] = function(block) {
|
||||
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
|
||||
const CONSTANTS = {
|
||||
'PI': ['M_PI', PHP.ORDER_ATOMIC],
|
||||
'E': ['M_E', PHP.ORDER_ATOMIC],
|
||||
'GOLDEN_RATIO': ['(1 + sqrt(5)) / 2', PHP.ORDER_DIVISION],
|
||||
'SQRT2': ['M_SQRT2', PHP.ORDER_ATOMIC],
|
||||
'SQRT1_2': ['M_SQRT1_2', PHP.ORDER_ATOMIC],
|
||||
'INFINITY': ['INF', PHP.ORDER_ATOMIC],
|
||||
'PI': ['M_PI', Order.ATOMIC],
|
||||
'E': ['M_E', Order.ATOMIC],
|
||||
'GOLDEN_RATIO': ['(1 + sqrt(5)) / 2', Order.DIVISION],
|
||||
'SQRT2': ['M_SQRT2', Order.ATOMIC],
|
||||
'SQRT1_2': ['M_SQRT1_2', Order.ATOMIC],
|
||||
'INFINITY': ['INF', Order.ATOMIC],
|
||||
};
|
||||
return CONSTANTS[block.getFieldValue('CONSTANT')];
|
||||
};
|
||||
|
||||
PHP.forBlock['math_number_property'] = function(block) {
|
||||
phpGenerator.forBlock['math_number_property'] = function(block) {
|
||||
// Check if a number is even, odd, prime, whole, positive, or negative
|
||||
// or if it is divisible by certain number. Returns true or false.
|
||||
const PROPERTIES = {
|
||||
'EVEN': ['', ' % 2 == 0', PHP.ORDER_MODULUS, PHP.ORDER_EQUALITY],
|
||||
'ODD': ['', ' % 2 == 1', PHP.ORDER_MODULUS, PHP.ORDER_EQUALITY],
|
||||
'WHOLE': ['is_int(', ')', PHP.ORDER_NONE, PHP.ORDER_FUNCTION_CALL],
|
||||
'POSITIVE': ['', ' > 0', PHP.ORDER_RELATIONAL, PHP.ORDER_RELATIONAL],
|
||||
'NEGATIVE': ['', ' < 0', PHP.ORDER_RELATIONAL, PHP.ORDER_RELATIONAL],
|
||||
'DIVISIBLE_BY': [null, null, PHP.ORDER_MODULUS, PHP.ORDER_EQUALITY],
|
||||
'PRIME': [null, null, PHP.ORDER_NONE, PHP.ORDER_FUNCTION_CALL],
|
||||
'EVEN': ['', ' % 2 == 0', Order.MODULUS, Order.EQUALITY],
|
||||
'ODD': ['', ' % 2 == 1', Order.MODULUS, Order.EQUALITY],
|
||||
'WHOLE': ['is_int(', ')', Order.NONE, Order.FUNCTION_CALL],
|
||||
'POSITIVE': ['', ' > 0', Order.RELATIONAL, Order.RELATIONAL],
|
||||
'NEGATIVE': ['', ' < 0', Order.RELATIONAL, Order.RELATIONAL],
|
||||
'DIVISIBLE_BY': [null, null, Order.MODULUS, Order.EQUALITY],
|
||||
'PRIME': [null, null, Order.NONE, Order.FUNCTION_CALL],
|
||||
};
|
||||
const dropdownProperty = block.getFieldValue('PROPERTY');
|
||||
const [prefix, suffix, inputOrder, outputOrder] = PROPERTIES[dropdownProperty];
|
||||
const numberToCheck = PHP.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
const [prefix, suffix, inputOrder, outputOrder] =
|
||||
PROPERTIES[dropdownProperty];
|
||||
const numberToCheck = phpGenerator.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
inputOrder) || '0';
|
||||
let code;
|
||||
if (dropdownProperty === 'PRIME') {
|
||||
// Prime is a special case as it is not a one-liner test.
|
||||
const functionName = PHP.provideFunction_('math_isPrime', `
|
||||
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($n) {
|
||||
const functionName = phpGenerator.provideFunction_('math_isPrime', `
|
||||
function ${phpGenerator.FUNCTION_NAME_PLACEHOLDER_}($n) {
|
||||
// https://en.wikipedia.org/wiki/Primality_test#Naive_methods
|
||||
if ($n == 2 || $n == 3) {
|
||||
return true;
|
||||
@@ -180,10 +181,10 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($n) {
|
||||
`);
|
||||
code = functionName + '(' + numberToCheck + ')';
|
||||
} else if (dropdownProperty === 'DIVISIBLE_BY') {
|
||||
const divisor = PHP.valueToCode(block, 'DIVISOR',
|
||||
PHP.ORDER_MODULUS) || '0';
|
||||
const divisor = phpGenerator.valueToCode(block, 'DIVISOR',
|
||||
Order.MODULUS) || '0';
|
||||
if (divisor === '0') {
|
||||
return ['false', PHP.ORDER_ATOMIC];
|
||||
return ['false', Order.ATOMIC];
|
||||
|
||||
}
|
||||
code = numberToCheck + ' % ' + divisor + ' == 0';
|
||||
@@ -193,20 +194,22 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($n) {
|
||||
return [code, outputOrder];
|
||||
};
|
||||
|
||||
PHP.forBlock['math_change'] = function(block) {
|
||||
phpGenerator.forBlock['math_change'] = function(block) {
|
||||
// Add to a variable in place.
|
||||
const argument0 = PHP.valueToCode(block, 'DELTA', PHP.ORDER_ADDITION) || '0';
|
||||
const argument0 =
|
||||
phpGenerator.valueToCode(block, 'DELTA', Order.ADDITION) || '0';
|
||||
const varName =
|
||||
PHP.nameDB_.getName(block.getFieldValue('VAR'), NameType.VARIABLE);
|
||||
phpGenerator.nameDB_.getName(
|
||||
block.getFieldValue('VAR'), NameType.VARIABLE);
|
||||
return varName + ' += ' + argument0 + ';\n';
|
||||
};
|
||||
|
||||
// Rounding functions have a single operand.
|
||||
PHP.forBlock['math_round'] = PHP.forBlock['math_single'];
|
||||
phpGenerator.forBlock['math_round'] = phpGenerator.forBlock['math_single'];
|
||||
// Trigonometry functions have a single operand.
|
||||
PHP.forBlock['math_trig'] = PHP.forBlock['math_single'];
|
||||
phpGenerator.forBlock['math_trig'] = phpGenerator.forBlock['math_single'];
|
||||
|
||||
PHP.forBlock['math_on_list'] = function(block) {
|
||||
phpGenerator.forBlock['math_on_list'] = function(block) {
|
||||
// Math functions for lists.
|
||||
const func = block.getFieldValue('OP');
|
||||
let list;
|
||||
@@ -214,38 +217,41 @@ PHP.forBlock['math_on_list'] = function(block) {
|
||||
switch (func) {
|
||||
case 'SUM':
|
||||
list =
|
||||
PHP.valueToCode(block, 'LIST', PHP.ORDER_FUNCTION_CALL) || 'array()';
|
||||
phpGenerator.valueToCode(block, 'LIST', Order.FUNCTION_CALL)
|
||||
|| 'array()';
|
||||
code = 'array_sum(' + list + ')';
|
||||
break;
|
||||
case 'MIN':
|
||||
list =
|
||||
PHP.valueToCode(block, 'LIST', PHP.ORDER_FUNCTION_CALL) || 'array()';
|
||||
phpGenerator.valueToCode(block, 'LIST', Order.FUNCTION_CALL)
|
||||
|| 'array()';
|
||||
code = 'min(' + list + ')';
|
||||
break;
|
||||
case 'MAX':
|
||||
list =
|
||||
PHP.valueToCode(block, 'LIST', PHP.ORDER_FUNCTION_CALL) || 'array()';
|
||||
phpGenerator.valueToCode(block, 'LIST', Order.FUNCTION_CALL)
|
||||
|| 'array()';
|
||||
code = 'max(' + list + ')';
|
||||
break;
|
||||
case 'AVERAGE': {
|
||||
const functionName = PHP.provideFunction_('math_mean', `
|
||||
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($myList) {
|
||||
const functionName = phpGenerator.provideFunction_('math_mean', `
|
||||
function ${phpGenerator.FUNCTION_NAME_PLACEHOLDER_}($myList) {
|
||||
return array_sum($myList) / count($myList);
|
||||
}
|
||||
`);
|
||||
list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || 'array()';
|
||||
list = phpGenerator.valueToCode(block, 'LIST', Order.NONE) || 'array()';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
case 'MEDIAN': {
|
||||
const functionName = PHP.provideFunction_('math_median', `
|
||||
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($arr) {
|
||||
const functionName = phpGenerator.provideFunction_('math_median', `
|
||||
function ${phpGenerator.FUNCTION_NAME_PLACEHOLDER_}($arr) {
|
||||
sort($arr,SORT_NUMERIC);
|
||||
return (count($arr) % 2) ? $arr[floor(count($arr) / 2)] :
|
||||
($arr[floor(count($arr) / 2)] + $arr[floor(count($arr) / 2) - 1]) / 2;
|
||||
}
|
||||
`);
|
||||
list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]';
|
||||
list = phpGenerator.valueToCode(block, 'LIST', Order.NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
@@ -253,8 +259,8 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($arr) {
|
||||
// As a list of numbers can contain more than one mode,
|
||||
// the returned result is provided as an array.
|
||||
// Mode of [3, 'x', 'x', 1, 1, 2, '3'] -> ['x', 1].
|
||||
const functionName = PHP.provideFunction_('math_modes', `
|
||||
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($values) {
|
||||
const functionName = phpGenerator.provideFunction_('math_modes', `
|
||||
function ${phpGenerator.FUNCTION_NAME_PLACEHOLDER_}($values) {
|
||||
if (empty($values)) return array();
|
||||
$counts = array_count_values($values);
|
||||
arsort($counts); // Sort counts in descending order
|
||||
@@ -262,13 +268,14 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($values) {
|
||||
return $modes;
|
||||
}
|
||||
`);
|
||||
list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]';
|
||||
list = phpGenerator.valueToCode(block, 'LIST', Order.NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
case 'STD_DEV': {
|
||||
const functionName = PHP.provideFunction_('math_standard_deviation', `
|
||||
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($numbers) {
|
||||
const functionName =
|
||||
phpGenerator.provideFunction_('math_standard_deviation', `
|
||||
function ${phpGenerator.FUNCTION_NAME_PLACEHOLDER_}($numbers) {
|
||||
$n = count($numbers);
|
||||
if (!$n) return null;
|
||||
$mean = array_sum($numbers) / count($numbers);
|
||||
@@ -276,53 +283,54 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($numbers) {
|
||||
return sqrt(array_sum($devs) / (count($devs) - 1));
|
||||
}
|
||||
`);
|
||||
list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]';
|
||||
list = phpGenerator.valueToCode(block, 'LIST', Order.NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
case 'RANDOM': {
|
||||
const functionName = PHP.provideFunction_('math_random_list', `
|
||||
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($list) {
|
||||
const functionName = phpGenerator.provideFunction_('math_random_list', `
|
||||
function ${phpGenerator.FUNCTION_NAME_PLACEHOLDER_}($list) {
|
||||
$x = rand(0, count($list)-1);
|
||||
return $list[$x];
|
||||
}
|
||||
`);
|
||||
list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]';
|
||||
list = phpGenerator.valueToCode(block, 'LIST', Order.NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw Error('Unknown operator: ' + func);
|
||||
}
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
PHP.forBlock['math_modulo'] = function(block) {
|
||||
phpGenerator.forBlock['math_modulo'] = function(block) {
|
||||
// Remainder computation.
|
||||
const argument0 =
|
||||
PHP.valueToCode(block, 'DIVIDEND', PHP.ORDER_MODULUS) || '0';
|
||||
const argument1 = PHP.valueToCode(block, 'DIVISOR', PHP.ORDER_MODULUS) || '0';
|
||||
phpGenerator.valueToCode(block, 'DIVIDEND', Order.MODULUS) || '0';
|
||||
const argument1 =
|
||||
phpGenerator.valueToCode(block, 'DIVISOR', Order.MODULUS) || '0';
|
||||
const code = argument0 + ' % ' + argument1;
|
||||
return [code, PHP.ORDER_MODULUS];
|
||||
return [code, Order.MODULUS];
|
||||
};
|
||||
|
||||
PHP.forBlock['math_constrain'] = function(block) {
|
||||
phpGenerator.forBlock['math_constrain'] = function(block) {
|
||||
// Constrain a number between two limits.
|
||||
const argument0 = PHP.valueToCode(block, 'VALUE', PHP.ORDER_NONE) || '0';
|
||||
const argument1 = PHP.valueToCode(block, 'LOW', PHP.ORDER_NONE) || '0';
|
||||
const argument0 = phpGenerator.valueToCode(block, 'VALUE', Order.NONE) || '0';
|
||||
const argument1 = phpGenerator.valueToCode(block, 'LOW', Order.NONE) || '0';
|
||||
const argument2 =
|
||||
PHP.valueToCode(block, 'HIGH', PHP.ORDER_NONE) || 'Infinity';
|
||||
phpGenerator.valueToCode(block, 'HIGH', Order.NONE) || 'Infinity';
|
||||
const code =
|
||||
'min(max(' + argument0 + ', ' + argument1 + '), ' + argument2 + ')';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
PHP.forBlock['math_random_int'] = function(block) {
|
||||
phpGenerator.forBlock['math_random_int'] = function(block) {
|
||||
// Random integer between [X] and [Y].
|
||||
const argument0 = PHP.valueToCode(block, 'FROM', PHP.ORDER_NONE) || '0';
|
||||
const argument1 = PHP.valueToCode(block, 'TO', PHP.ORDER_NONE) || '0';
|
||||
const functionName = PHP.provideFunction_('math_random_int', `
|
||||
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($a, $b) {
|
||||
const argument0 = phpGenerator.valueToCode(block, 'FROM', Order.NONE) || '0';
|
||||
const argument1 = phpGenerator.valueToCode(block, 'TO', Order.NONE) || '0';
|
||||
const functionName = phpGenerator.provideFunction_('math_random_int', `
|
||||
function ${phpGenerator.FUNCTION_NAME_PLACEHOLDER_}($a, $b) {
|
||||
if ($a > $b) {
|
||||
return rand($b, $a);
|
||||
}
|
||||
@@ -330,20 +338,20 @@ function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($a, $b) {
|
||||
}
|
||||
`);
|
||||
const code = functionName + '(' + argument0 + ', ' + argument1 + ')';
|
||||
return [code, PHP.ORDER_FUNCTION_CALL];
|
||||
return [code, Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
PHP.forBlock['math_random_float'] = function(block) {
|
||||
phpGenerator.forBlock['math_random_float'] = function(block) {
|
||||
// Random fraction between 0 and 1.
|
||||
return ['(float)rand()/(float)getrandmax()', PHP.ORDER_FUNCTION_CALL];
|
||||
return ['(float)rand()/(float)getrandmax()', Order.FUNCTION_CALL];
|
||||
};
|
||||
|
||||
PHP.forBlock['math_atan2'] = function(block) {
|
||||
phpGenerator.forBlock['math_atan2'] = function(block) {
|
||||
// Arctangent of point (X, Y) in degrees from -180 to 180.
|
||||
const argument0 = PHP.valueToCode(block, 'X', PHP.ORDER_NONE) || '0';
|
||||
const argument1 = PHP.valueToCode(block, 'Y', PHP.ORDER_NONE) || '0';
|
||||
const argument0 = phpGenerator.valueToCode(block, 'X', Order.NONE) || '0';
|
||||
const argument1 = phpGenerator.valueToCode(block, 'Y', Order.NONE) || '0';
|
||||
return [
|
||||
'atan2(' + argument1 + ', ' + argument0 + ') / pi() * 180',
|
||||
PHP.ORDER_DIVISION
|
||||
Order.DIVISION
|
||||
];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user