Commit 5e7f92ce authored by daarond's avatar daarond

more debugging

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