mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-10 21:18:53 -04:00
fixing some items
This commit is contained in:
parent
f0c837bb2d
commit
da2e0710bf
5 changed files with 46 additions and 42 deletions
generators
|
@ -105,17 +105,22 @@ Blockly.PHP.init = function(workspace) {
|
||||||
} else {
|
} else {
|
||||||
Blockly.PHP.variableDB_.reset();
|
Blockly.PHP.variableDB_.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
var defvars = [];
|
|
||||||
var variables = Blockly.Variables.allVariables(workspace);
|
|
||||||
for (var x = 0; x < variables.length; x++) {
|
|
||||||
defvars[x] = 'var ' +
|
|
||||||
Blockly.PHP.variableDB_.getName(variables[x],
|
|
||||||
Blockly.Variables.NAME_TYPE) + ';';
|
|
||||||
}
|
|
||||||
Blockly.PHP.definitions_['variables'] = defvars.join('\n');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Blockly.PHP.getDistinctName = function(name, type) {
|
||||||
|
var safeName = this.variableDB_.safeName_(name);
|
||||||
|
var i = '';
|
||||||
|
while (this.variableDB_.dbReverse_[safeName + i] ||
|
||||||
|
(safeName + i) in this.variableDB_.reservedDict_) {
|
||||||
|
// Collision with existing name. Create a unique name.
|
||||||
|
i = i ? i + 1 : 2;
|
||||||
|
}
|
||||||
|
safeName += i;
|
||||||
|
this.variableDB_.dbReverse_[safeName] = true;
|
||||||
|
return '$' + safeName;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepend the generated code with the variable definitions.
|
* Prepend the generated code with the variable definitions.
|
||||||
* @param {string} code Generated code.
|
* @param {string} code Generated code.
|
||||||
|
|
|
@ -61,7 +61,7 @@ Blockly.PHP['lists_repeat'] = function(block) {
|
||||||
Blockly.PHP.ORDER_COMMA) || 'null';
|
Blockly.PHP.ORDER_COMMA) || 'null';
|
||||||
var argument1 = Blockly.PHP.valueToCode(block, 'NUM',
|
var argument1 = Blockly.PHP.valueToCode(block, 'NUM',
|
||||||
Blockly.PHP.ORDER_COMMA) || '0';
|
Blockly.PHP.ORDER_COMMA) || '0';
|
||||||
var code = functionName + '($' + argument0 + ', $' + argument1 + ')';
|
var code = functionName + '(' + argument0 + ', ' + argument1 + ')';
|
||||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -197,7 +197,7 @@ Blockly.PHP['lists_setIndex'] = function(block) {
|
||||||
if (list.match(/^\w+$/)) {
|
if (list.match(/^\w+$/)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
var listVar = Blockly.PHP.variableDB_.getDistinctName(
|
var listVar = Blockly.PHP.getDistinctName(
|
||||||
'tmp_list', Blockly.Variables.NAME_TYPE);
|
'tmp_list', Blockly.Variables.NAME_TYPE);
|
||||||
var code = 'var ' + listVar + ' = ' + list + ';\n';
|
var code = 'var ' + listVar + ' = ' + list + ';\n';
|
||||||
list = listVar;
|
list = listVar;
|
||||||
|
@ -243,7 +243,7 @@ Blockly.PHP['lists_setIndex'] = function(block) {
|
||||||
}
|
}
|
||||||
} else if (where == 'RANDOM') {
|
} else if (where == 'RANDOM') {
|
||||||
var code = cacheList();
|
var code = cacheList();
|
||||||
var xVar = Blockly.PHP.variableDB_.getDistinctName(
|
var xVar = Blockly.PHP.getDistinctName(
|
||||||
'tmp_x', Blockly.Variables.NAME_TYPE);
|
'tmp_x', Blockly.Variables.NAME_TYPE);
|
||||||
code += 'var ' + xVar + ' = Math.floor(Math.random() * ' + list +
|
code += 'var ' + xVar + ' = Math.floor(Math.random() * ' + list +
|
||||||
'.length);\n';
|
'.length);\n';
|
||||||
|
|
|
@ -34,10 +34,10 @@ Blockly.PHP['controls_repeat'] = function(block) {
|
||||||
var repeats = Number(block.getFieldValue('TIMES'));
|
var repeats = Number(block.getFieldValue('TIMES'));
|
||||||
var branch = Blockly.PHP.statementToCode(block, 'DO');
|
var branch = Blockly.PHP.statementToCode(block, 'DO');
|
||||||
branch = Blockly.PHP.addLoopTrap(branch, block.id);
|
branch = Blockly.PHP.addLoopTrap(branch, block.id);
|
||||||
var loopVar = Blockly.PHP.variableDB_.getDistinctName(
|
var loopVar = Blockly.PHP.getDistinctName(
|
||||||
'count', Blockly.Variables.NAME_TYPE);
|
'count', Blockly.Variables.NAME_TYPE);
|
||||||
var code = 'for ($' + loopVar + ' = 0; $' +
|
var code = 'for (' + loopVar + ' = 0; ' +
|
||||||
loopVar + ' < ' + repeats + '; $' +
|
loopVar + ' < ' + repeats + '; ' +
|
||||||
loopVar + '++) {\n' +
|
loopVar + '++) {\n' +
|
||||||
branch + '}\n';
|
branch + '}\n';
|
||||||
return code;
|
return code;
|
||||||
|
@ -50,16 +50,16 @@ Blockly.PHP['controls_repeat_ext'] = function(block) {
|
||||||
var branch = Blockly.PHP.statementToCode(block, 'DO');
|
var branch = Blockly.PHP.statementToCode(block, 'DO');
|
||||||
branch = Blockly.PHP.addLoopTrap(branch, block.id);
|
branch = Blockly.PHP.addLoopTrap(branch, block.id);
|
||||||
var code = '';
|
var code = '';
|
||||||
var loopVar = Blockly.PHP.variableDB_.getDistinctName(
|
var loopVar = Blockly.PHP.getDistinctName(
|
||||||
'count', Blockly.Variables.NAME_TYPE);
|
'count', Blockly.Variables.NAME_TYPE);
|
||||||
var endVar = repeats;
|
var endVar = repeats;
|
||||||
if (!repeats.match(/^\w+$/) && !Blockly.isNumber(repeats)) {
|
if (!repeats.match(/^\w+$/) && !Blockly.isNumber(repeats)) {
|
||||||
var endVar = Blockly.PHP.variableDB_.getDistinctName(
|
var endVar = Blockly.PHP.getDistinctName(
|
||||||
'repeat_end', Blockly.Variables.NAME_TYPE);
|
'repeat_end', Blockly.Variables.NAME_TYPE);
|
||||||
code += 'var ' + endVar + ' = ' + repeats + ';\n';
|
code += endVar + ' = ' + repeats + ';\n';
|
||||||
}
|
}
|
||||||
code += 'for ($' + loopVar + ' = 0; $' +
|
code += 'for (' + loopVar + ' = 0; ' +
|
||||||
loopVar + ' < $' + endVar + '; $' +
|
loopVar + ' < ' + endVar + '; ' +
|
||||||
loopVar + '++) {\n' +
|
loopVar + '++) {\n' +
|
||||||
branch + '}\n';
|
branch + '}\n';
|
||||||
return code;
|
return code;
|
||||||
|
@ -96,8 +96,8 @@ Blockly.PHP['controls_for'] = function(block) {
|
||||||
Blockly.isNumber(increment)) {
|
Blockly.isNumber(increment)) {
|
||||||
// All arguments are simple numbers.
|
// All arguments are simple numbers.
|
||||||
var up = parseFloat(argument0) <= parseFloat(argument1);
|
var up = parseFloat(argument0) <= parseFloat(argument1);
|
||||||
code = 'for ($' + variable0 + ' = ' + argument0 + '; $' +
|
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
|
||||||
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; $' +
|
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
|
||||||
variable0;
|
variable0;
|
||||||
var step = Math.abs(parseFloat(increment));
|
var step = Math.abs(parseFloat(increment));
|
||||||
if (step == 1) {
|
if (step == 1) {
|
||||||
|
@ -111,34 +111,34 @@ Blockly.PHP['controls_for'] = function(block) {
|
||||||
// Cache non-trivial values to variables to prevent repeated look-ups.
|
// Cache non-trivial values to variables to prevent repeated look-ups.
|
||||||
var startVar = argument0;
|
var startVar = argument0;
|
||||||
if (!argument0.match(/^\w+$/) && !Blockly.isNumber(argument0)) {
|
if (!argument0.match(/^\w+$/) && !Blockly.isNumber(argument0)) {
|
||||||
startVar = Blockly.PHP.variableDB_.getDistinctName(
|
startVar = Blockly.PHP.getDistinctName(
|
||||||
variable0 + '_start', Blockly.Variables.NAME_TYPE);
|
variable0 + '_start', Blockly.Variables.NAME_TYPE);
|
||||||
code += '$' + startVar + ' = ' + argument0 + ';\n';
|
code += startVar + ' = ' + argument0 + ';\n';
|
||||||
}
|
}
|
||||||
var endVar = argument1;
|
var endVar = argument1;
|
||||||
if (!argument1.match(/^\w+$/) && !Blockly.isNumber(argument1)) {
|
if (!argument1.match(/^\w+$/) && !Blockly.isNumber(argument1)) {
|
||||||
var endVar = Blockly.PHP.variableDB_.getDistinctName(
|
var endVar = Blockly.PHP.getDistinctName(
|
||||||
variable0 + '_end', Blockly.Variables.NAME_TYPE);
|
variable0 + '_end', Blockly.Variables.NAME_TYPE);
|
||||||
code += '$' + endVar + ' = ' + argument1 + ';\n';
|
code += endVar + ' = ' + argument1 + ';\n';
|
||||||
}
|
}
|
||||||
// Determine loop direction at start, in case one of the bounds
|
// Determine loop direction at start, in case one of the bounds
|
||||||
// changes during loop execution.
|
// changes during loop execution.
|
||||||
var incVar = Blockly.PHP.variableDB_.getDistinctName(
|
var incVar = Blockly.PHP.getDistinctName(
|
||||||
variable0 + '_inc', Blockly.Variables.NAME_TYPE);
|
variable0 + '_inc', Blockly.Variables.NAME_TYPE);
|
||||||
code += '$' + incVar + ' = ';
|
code += incVar + ' = ';
|
||||||
if (Blockly.isNumber(increment)) {
|
if (Blockly.isNumber(increment)) {
|
||||||
code += Math.abs(increment) + ';\n';
|
code += Math.abs(increment) + ';\n';
|
||||||
} else {
|
} else {
|
||||||
code += 'abs(' + increment + ');\n';
|
code += 'abs(' + increment + ');\n';
|
||||||
}
|
}
|
||||||
code += 'if ($' + startVar + ' > $' + endVar + ') {\n';
|
code += 'if (' + startVar + ' > ' + endVar + ') {\n';
|
||||||
code += Blockly.PHP.INDENT + '$' + incVar + ' = -$' + incVar + ';\n';
|
code += Blockly.PHP.INDENT + incVar + ' = -' + incVar + ';\n';
|
||||||
code += '}\n';
|
code += '}\n';
|
||||||
code += 'for (' + variable0 + ' = $' + startVar + ';\n' +
|
code += 'for (' + variable0 + ' = ' + startVar + ';\n' +
|
||||||
' ' + incVar + ' >= 0 ? $' +
|
' ' + incVar + ' >= 0 ? ' +
|
||||||
variable0 + ' <= ' + endVar + ' : $' +
|
variable0 + ' <= ' + endVar + ' : ' +
|
||||||
variable0 + ' >= ' + endVar + ';\n' +
|
variable0 + ' >= ' + endVar + ';\n' +
|
||||||
' ' + variable0 + ' += $' + incVar + ') {\n' +
|
' ' + variable0 + ' += ' + incVar + ') {\n' +
|
||||||
branch + '}\n';
|
branch + '}\n';
|
||||||
}
|
}
|
||||||
return code;
|
return code;
|
||||||
|
@ -156,15 +156,15 @@ Blockly.PHP['controls_forEach'] = function(block) {
|
||||||
// Cache non-trivial values to variables to prevent repeated look-ups.
|
// Cache non-trivial values to variables to prevent repeated look-ups.
|
||||||
var listVar = argument0;
|
var listVar = argument0;
|
||||||
if (!argument0.match(/^\w+$/)) {
|
if (!argument0.match(/^\w+$/)) {
|
||||||
listVar = Blockly.PHP.variableDB_.getDistinctName(
|
listVar = Blockly.PHP.getDistinctName(
|
||||||
variable0 + '_list', Blockly.Variables.NAME_TYPE);
|
variable0 + '_list', Blockly.Variables.NAME_TYPE);
|
||||||
code += '$' + listVar + ' = ' + argument0 + ';\n';
|
code += listVar + ' = ' + argument0 + ';\n';
|
||||||
}
|
}
|
||||||
var indexVar = Blockly.PHP.variableDB_.getDistinctName(
|
var indexVar = Blockly.PHP.getDistinctName(
|
||||||
variable0 + '_index', Blockly.Variables.NAME_TYPE);
|
variable0 + '_index', Blockly.Variables.NAME_TYPE);
|
||||||
branch = Blockly.PHP.INDENT + variable0 + ' = ' +
|
branch = Blockly.PHP.INDENT + variable0 + ' = ' +
|
||||||
listVar + '[' + indexVar + '];\n' + branch;
|
listVar + '[' + indexVar + '];\n' + branch;
|
||||||
code += 'foreach ($' + listVar + ' as $' + indexVar + ') {\n' + branch + '}\n';
|
code += 'foreach (' + listVar + ' as ' + indexVar + ') {\n' + branch + '}\n';
|
||||||
return code;
|
return code;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -76,14 +76,14 @@ Blockly.PHP['text_length'] = function(block) {
|
||||||
// String length.
|
// String length.
|
||||||
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
|
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||||
Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
|
Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
|
||||||
return ['strlen(' + argument0 + ')', Blockly.PHP.ORDER_MEMBER];
|
return ['strlen(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.PHP['text_isEmpty'] = function(block) {
|
Blockly.PHP['text_isEmpty'] = function(block) {
|
||||||
// Is the string null?
|
// Is the string null?
|
||||||
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
|
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||||
Blockly.PHP.ORDER_MEMBER) || '\'\'';
|
Blockly.PHP.ORDER_MEMBER) || '\'\'';
|
||||||
return ['empty(' + argument0 + ')', Blockly.PHP.ORDER_LOGICAL_NOT];
|
return ['empty(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||||
};
|
};
|
||||||
|
|
||||||
Blockly.PHP['text_indexOf'] = function(block) {
|
Blockly.PHP['text_indexOf'] = function(block) {
|
||||||
|
|
|
@ -31,8 +31,7 @@ goog.require('Blockly.PHP');
|
||||||
|
|
||||||
Blockly.PHP['variables_get'] = function(block) {
|
Blockly.PHP['variables_get'] = function(block) {
|
||||||
// Variable getter.
|
// Variable getter.
|
||||||
var code = Blockly.PHP.variableDB_.getName(block.getFieldValue('VAR'),
|
var code = '$' + block.getFieldValue('VAR');
|
||||||
Blockly.Variables.NAME_TYPE);
|
|
||||||
return [code, Blockly.PHP.ORDER_ATOMIC];
|
return [code, Blockly.PHP.ORDER_ATOMIC];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue