Commit 5e7f92ce authored by daarond's avatar daarond

more debugging

parent da2e0710
......@@ -120,6 +120,15 @@ Blockly.PHP.getDistinctName = function(name, type) {
return '$' + safeName;
};
Blockly.PHP.getName = function(name, type) {
var normalized = name.toLowerCase() + '_' + type;
if (normalized in this.variableDB_.db_) {
return this.variableDB_.db_[normalized];
}
var safeName = this.getDistinctName(name, type);
this.variableDB_[normalized] = safeName;
return safeName;
};
/**
* Prepend the generated code with the variable definitions.
......
......@@ -81,7 +81,7 @@ Blockly.PHP['controls_whileUntil'] = function(block) {
Blockly.PHP['controls_for'] = function(block) {
// For loop.
var variable0 = Blockly.PHP.variableDB_.getName(
var variable0 = Blockly.PHP.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
var argument0 = Blockly.PHP.valueToCode(block, 'FROM',
Blockly.PHP.ORDER_ASSIGNMENT) || '0';
......@@ -146,25 +146,14 @@ Blockly.PHP['controls_for'] = function(block) {
Blockly.PHP['controls_forEach'] = function(block) {
// For each loop.
var variable0 = Blockly.PHP.variableDB_.getName(
var variable0 = Blockly.PHP.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
var argument0 = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_ASSIGNMENT) || '[]';
var branch = Blockly.PHP.statementToCode(block, 'DO');
branch = Blockly.PHP.addLoopTrap(branch, block.id);
var code = '';
// Cache non-trivial values to variables to prevent repeated look-ups.
var listVar = argument0;
if (!argument0.match(/^\w+$/)) {
listVar = Blockly.PHP.getDistinctName(
variable0 + '_list', Blockly.Variables.NAME_TYPE);
code += listVar + ' = ' + argument0 + ';\n';
}
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 (' + argument0 + ' as ' + variable0 + ') {\n' + branch + '}\n';
return code;
};
......
......@@ -146,13 +146,13 @@ Blockly.PHP['math_single'] = function(block) {
Blockly.PHP['math_constant'] = function(block) {
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
var CONSTANTS = {
'PI': ['pi()', Blockly.PHP.ORDER_MEMBER],
'E': ['exp', Blockly.PHP.ORDER_FUNCTION_CALL],
'PI': ['pi()', Blockly.PHP.ORDER_FUNCTION_CALL],
'E': ['exp()', Blockly.PHP.ORDER_FUNCTION_CALL],
'GOLDEN_RATIO':
['(1 + sqrt(5)) / 2', Blockly.PHP.ORDER_DIVISION],
'SQRT2': ['sqrt', Blockly.PHP.ORDER_MEMBER],
'SQRT1_2': ['sqrt', Blockly.PHP.ORDER_MEMBER],
'INFINITY': ['Infinity', Blockly.PHP.ORDER_ATOMIC]
'SQRT2': ['M_SQRT2', Blockly.PHP.ORDER_ATOMIC],
'SQRT1_2': ['M_SQRT1_2', Blockly.PHP.ORDER_ATOMIC],
'INFINITY': ['INF', Blockly.PHP.ORDER_ATOMIC]
};
return CONSTANTS[block.getFieldValue('CONSTANT')];
};
......@@ -197,7 +197,7 @@ Blockly.PHP['math_change'] = function(block) {
// Add to a variable in place.
var argument0 = Blockly.PHP.valueToCode(block, 'DELTA',
Blockly.PHP.ORDER_ADDITION) || '0';
var varName = Blockly.PHP.variableDB_.getName(
var varName = Blockly.PHP.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
return varName + ' = (typeof ' + varName + ' == \'number\' ? ' + varName +
' : 0) + ' + argument0 + ';\n';
......
......@@ -31,7 +31,7 @@ goog.require('Blockly.PHP');
Blockly.PHP['procedures_defreturn'] = function(block) {
// Define a procedure with a return value.
var funcName = Blockly.PHP.variableDB_.getName(
var funcName = Blockly.PHP.getName(
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
var branch = Blockly.PHP.statementToCode(block, 'STACK');
if (Blockly.PHP.STATEMENT_PREFIX) {
......@@ -50,7 +50,7 @@ Blockly.PHP['procedures_defreturn'] = function(block) {
}
var args = [];
for (var x = 0; x < block.arguments_.length; x++) {
args[x] = Blockly.PHP.variableDB_.getName(block.arguments_[x],
args[x] = Blockly.PHP.getName(block.arguments_[x],
Blockly.Variables.NAME_TYPE);
}
var code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
......@@ -67,7 +67,7 @@ Blockly.PHP['procedures_defnoreturn'] =
Blockly.PHP['procedures_callreturn'] = function(block) {
// Call a procedure with a return value.
var funcName = Blockly.PHP.variableDB_.getName(
var funcName = Blockly.PHP.getName(
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
var args = [];
for (var x = 0; x < block.arguments_.length; x++) {
......@@ -80,7 +80,7 @@ Blockly.PHP['procedures_callreturn'] = function(block) {
Blockly.PHP['procedures_callnoreturn'] = function(block) {
// Call a procedure with no return value.
var funcName = Blockly.PHP.variableDB_.getName(
var funcName = Blockly.PHP.getName(
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
var args = [];
for (var x = 0; x < block.arguments_.length; x++) {
......
......@@ -65,7 +65,7 @@ Blockly.PHP['text_join'] = function(block) {
Blockly.PHP['text_append'] = function(block) {
// Append to a variable in place.
var varName = Blockly.PHP.variableDB_.getName(
var varName = Blockly.PHP.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
Blockly.PHP.ORDER_NONE) || '\'\'';
......
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