Commit da2e0710 authored by daarond's avatar daarond

fixing some items

parent f0c837bb
......@@ -105,17 +105,22 @@ Blockly.PHP.init = function(workspace) {
} else {
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.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;
}
Blockly.PHP.definitions_['variables'] = defvars.join('\n');
safeName += i;
this.variableDB_.dbReverse_[safeName] = true;
return '$' + safeName;
};
/**
* Prepend the generated code with the variable definitions.
* @param {string} code Generated code.
......
......@@ -61,7 +61,7 @@ Blockly.PHP['lists_repeat'] = function(block) {
Blockly.PHP.ORDER_COMMA) || 'null';
var argument1 = Blockly.PHP.valueToCode(block, 'NUM',
Blockly.PHP.ORDER_COMMA) || '0';
var code = functionName + '($' + argument0 + ', $' + argument1 + ')';
var code = functionName + '(' + argument0 + ', ' + argument1 + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
};
......@@ -197,7 +197,7 @@ Blockly.PHP['lists_setIndex'] = function(block) {
if (list.match(/^\w+$/)) {
return '';
}
var listVar = Blockly.PHP.variableDB_.getDistinctName(
var listVar = Blockly.PHP.getDistinctName(
'tmp_list', Blockly.Variables.NAME_TYPE);
var code = 'var ' + listVar + ' = ' + list + ';\n';
list = listVar;
......@@ -243,7 +243,7 @@ Blockly.PHP['lists_setIndex'] = function(block) {
}
} else if (where == 'RANDOM') {
var code = cacheList();
var xVar = Blockly.PHP.variableDB_.getDistinctName(
var xVar = Blockly.PHP.getDistinctName(
'tmp_x', Blockly.Variables.NAME_TYPE);
code += 'var ' + xVar + ' = Math.floor(Math.random() * ' + list +
'.length);\n';
......
......@@ -34,10 +34,10 @@ Blockly.PHP['controls_repeat'] = function(block) {
var repeats = Number(block.getFieldValue('TIMES'));
var branch = Blockly.PHP.statementToCode(block, 'DO');
branch = Blockly.PHP.addLoopTrap(branch, block.id);
var loopVar = Blockly.PHP.variableDB_.getDistinctName(
var loopVar = Blockly.PHP.getDistinctName(
'count', Blockly.Variables.NAME_TYPE);
var code = 'for ($' + loopVar + ' = 0; $' +
loopVar + ' < ' + repeats + '; $' +
var code = 'for (' + loopVar + ' = 0; ' +
loopVar + ' < ' + repeats + '; ' +
loopVar + '++) {\n' +
branch + '}\n';
return code;
......@@ -50,16 +50,16 @@ Blockly.PHP['controls_repeat_ext'] = function(block) {
var branch = Blockly.PHP.statementToCode(block, 'DO');
branch = Blockly.PHP.addLoopTrap(branch, block.id);
var code = '';
var loopVar = Blockly.PHP.variableDB_.getDistinctName(
var loopVar = Blockly.PHP.getDistinctName(
'count', Blockly.Variables.NAME_TYPE);
var endVar = 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);
code += 'var ' + endVar + ' = ' + repeats + ';\n';
code += endVar + ' = ' + repeats + ';\n';
}
code += 'for ($' + loopVar + ' = 0; $' +
loopVar + ' < $' + endVar + '; $' +
code += 'for (' + loopVar + ' = 0; ' +
loopVar + ' < ' + endVar + '; ' +
loopVar + '++) {\n' +
branch + '}\n';
return code;
......@@ -96,8 +96,8 @@ Blockly.PHP['controls_for'] = function(block) {
Blockly.isNumber(increment)) {
// All arguments are simple numbers.
var up = parseFloat(argument0) <= parseFloat(argument1);
code = 'for ($' + variable0 + ' = ' + argument0 + '; $' +
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; $' +
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
variable0;
var step = Math.abs(parseFloat(increment));
if (step == 1) {
......@@ -111,34 +111,34 @@ Blockly.PHP['controls_for'] = function(block) {
// Cache non-trivial values to variables to prevent repeated look-ups.
var startVar = argument0;
if (!argument0.match(/^\w+$/) && !Blockly.isNumber(argument0)) {
startVar = Blockly.PHP.variableDB_.getDistinctName(
startVar = Blockly.PHP.getDistinctName(
variable0 + '_start', Blockly.Variables.NAME_TYPE);
code += '$' + startVar + ' = ' + argument0 + ';\n';
code += startVar + ' = ' + argument0 + ';\n';
}
var endVar = 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);
code += '$' + endVar + ' = ' + argument1 + ';\n';
code += endVar + ' = ' + argument1 + ';\n';
}
// Determine loop direction at start, in case one of the bounds
// changes during loop execution.
var incVar = Blockly.PHP.variableDB_.getDistinctName(
var incVar = Blockly.PHP.getDistinctName(
variable0 + '_inc', Blockly.Variables.NAME_TYPE);
code += '$' + incVar + ' = ';
code += incVar + ' = ';
if (Blockly.isNumber(increment)) {
code += Math.abs(increment) + ';\n';
} else {
code += 'abs(' + increment + ');\n';
}
code += 'if ($' + startVar + ' > $' + endVar + ') {\n';
code += Blockly.PHP.INDENT + '$' + incVar + ' = -$' + incVar + ';\n';
code += 'if (' + startVar + ' > ' + endVar + ') {\n';
code += Blockly.PHP.INDENT + incVar + ' = -' + incVar + ';\n';
code += '}\n';
code += 'for (' + variable0 + ' = $' + startVar + ';\n' +
' ' + incVar + ' >= 0 ? $' +
variable0 + ' <= ' + endVar + ' : $' +
code += 'for (' + variable0 + ' = ' + startVar + ';\n' +
' ' + incVar + ' >= 0 ? ' +
variable0 + ' <= ' + endVar + ' : ' +
variable0 + ' >= ' + endVar + ';\n' +
' ' + variable0 + ' += $' + incVar + ') {\n' +
' ' + variable0 + ' += ' + incVar + ') {\n' +
branch + '}\n';
}
return code;
......@@ -156,15 +156,15 @@ Blockly.PHP['controls_forEach'] = function(block) {
// Cache non-trivial values to variables to prevent repeated look-ups.
var listVar = argument0;
if (!argument0.match(/^\w+$/)) {
listVar = Blockly.PHP.variableDB_.getDistinctName(
listVar = Blockly.PHP.getDistinctName(
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);
branch = Blockly.PHP.INDENT + variable0 + ' = ' +
listVar + '[' + indexVar + '];\n' + branch;
code += 'foreach ($' + listVar + ' as $' + indexVar + ') {\n' + branch + '}\n';
code += 'foreach (' + listVar + ' as ' + indexVar + ') {\n' + branch + '}\n';
return code;
};
......
......@@ -76,14 +76,14 @@ Blockly.PHP['text_length'] = function(block) {
// String length.
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
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) {
// Is the string null?
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
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) {
......
......@@ -31,8 +31,7 @@ goog.require('Blockly.PHP');
Blockly.PHP['variables_get'] = function(block) {
// Variable getter.
var code = Blockly.PHP.variableDB_.getName(block.getFieldValue('VAR'),
Blockly.Variables.NAME_TYPE);
var code = '$' + block.getFieldValue('VAR');
return [code, Blockly.PHP.ORDER_ATOMIC];
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment