Commit 37faa993 authored by daarond's avatar daarond

more debugging

parent 5e7f92ce
...@@ -48,8 +48,6 @@ Blockly.PHP.addReservedWords( ...@@ -48,8 +48,6 @@ Blockly.PHP.addReservedWords(
'__halt_compiler,abstract,and,array,as,break,callable,case,catch,class,clone,const,continue,declare,default,die,do,echo,else,elseif,empty,enddeclare,endfor,endforeach,endif,endswitch,endwhile,eval,exit,extends,final,for,foreach,function,global,goto,if,implements,include,include_once,instanceof,insteadof,interface,isset,list,namespace,new,or,print,private,protected,public,require,require_once,return,static,switch,throw,trait,try,unset,use,var,while,xor,' + '__halt_compiler,abstract,and,array,as,break,callable,case,catch,class,clone,const,continue,declare,default,die,do,echo,else,elseif,empty,enddeclare,endfor,endforeach,endif,endswitch,endwhile,eval,exit,extends,final,for,foreach,function,global,goto,if,implements,include,include_once,instanceof,insteadof,interface,isset,list,namespace,new,or,print,private,protected,public,require,require_once,return,static,switch,throw,trait,try,unset,use,var,while,xor,' +
// http://php.net/manual/en/reserved.constants.php // http://php.net/manual/en/reserved.constants.php
'PHP_VERSION,PHP_MAJOR_VERSION,PHP_MINOR_VERSION,PHP_RELEASE_VERSION,PHP_VERSION_ID,PHP_EXTRA_VERSION,PHP_ZTS,PHP_DEBUG,PHP_MAXPATHLEN,PHP_OS,PHP_SAPI,PHP_EOL,PHP_INT_MAX,PHP_INT_SIZE,DEFAULT_INCLUDE_PATH,PEAR_INSTALL_DIR,PEAR_EXTENSION_DIR,PHP_EXTENSION_DIR,PHP_PREFIX,PHP_BINDIR,PHP_BINARY,PHP_MANDIR,PHP_LIBDIR,PHP_DATADIR,PHP_SYSCONFDIR,PHP_LOCALSTATEDIR,PHP_CONFIG_FILE_PATH,PHP_CONFIG_FILE_SCAN_DIR,PHP_SHLIB_SUFFIX,E_ERROR,E_WARNING,E_PARSE,E_NOTICE,E_CORE_ERROR,E_CORE_WARNING,E_COMPILE_ERROR,E_COMPILE_WARNING,E_USER_ERROR,E_USER_WARNING,E_USER_NOTICE,E_DEPRECATED,E_USER_DEPRECATED,E_ALL,E_STRICT,__COMPILER_HALT_OFFSET__,TRUE,FALSE,NULL,__CLASS__,__DIR__,__FILE__,__FUNCTION__,__LINE__,__METHOD__,__NAMESPACE__,__TRAIT__'); 'PHP_VERSION,PHP_MAJOR_VERSION,PHP_MINOR_VERSION,PHP_RELEASE_VERSION,PHP_VERSION_ID,PHP_EXTRA_VERSION,PHP_ZTS,PHP_DEBUG,PHP_MAXPATHLEN,PHP_OS,PHP_SAPI,PHP_EOL,PHP_INT_MAX,PHP_INT_SIZE,DEFAULT_INCLUDE_PATH,PEAR_INSTALL_DIR,PEAR_EXTENSION_DIR,PHP_EXTENSION_DIR,PHP_PREFIX,PHP_BINDIR,PHP_BINARY,PHP_MANDIR,PHP_LIBDIR,PHP_DATADIR,PHP_SYSCONFDIR,PHP_LOCALSTATEDIR,PHP_CONFIG_FILE_PATH,PHP_CONFIG_FILE_SCAN_DIR,PHP_SHLIB_SUFFIX,E_ERROR,E_WARNING,E_PARSE,E_NOTICE,E_CORE_ERROR,E_CORE_WARNING,E_COMPILE_ERROR,E_COMPILE_WARNING,E_USER_ERROR,E_USER_WARNING,E_USER_NOTICE,E_DEPRECATED,E_USER_DEPRECATED,E_ALL,E_STRICT,__COMPILER_HALT_OFFSET__,TRUE,FALSE,NULL,__CLASS__,__DIR__,__FILE__,__FUNCTION__,__LINE__,__METHOD__,__NAMESPACE__,__TRAIT__');
// there are more than 9,000 internal functions in http://php.net/manual/en/indexes.functions.php
// do we really need to list them here?
/** /**
* Order of operation ENUMs. * Order of operation ENUMs.
...@@ -58,6 +56,7 @@ Blockly.PHP.addReservedWords( ...@@ -58,6 +56,7 @@ Blockly.PHP.addReservedWords(
Blockly.PHP.ORDER_ATOMIC = 0; // 0 "" ... Blockly.PHP.ORDER_ATOMIC = 0; // 0 "" ...
Blockly.PHP.ORDER_CLONE = 1; // clone Blockly.PHP.ORDER_CLONE = 1; // clone
Blockly.PHP.ORDER_NEW = 1; // new Blockly.PHP.ORDER_NEW = 1; // new
Blockly.PHP.ORDER_MEMBER = 2; // ()
Blockly.PHP.ORDER_FUNCTION_CALL = 2; // () Blockly.PHP.ORDER_FUNCTION_CALL = 2; // ()
Blockly.PHP.ORDER_INCREMENT = 3; // ++ Blockly.PHP.ORDER_INCREMENT = 3; // ++
Blockly.PHP.ORDER_DECREMENT = 3; // -- Blockly.PHP.ORDER_DECREMENT = 3; // --
...@@ -110,13 +109,14 @@ Blockly.PHP.init = function(workspace) { ...@@ -110,13 +109,14 @@ Blockly.PHP.init = function(workspace) {
Blockly.PHP.getDistinctName = function(name, type) { Blockly.PHP.getDistinctName = function(name, type) {
var safeName = this.variableDB_.safeName_(name); var safeName = this.variableDB_.safeName_(name);
var i = ''; var i = '';
while (this.variableDB_.dbReverse_[safeName + i] || while ((type == Blockly.Procedures.NAME_TYPE && (safeName + i) in this.variableDB_.reservedDict_)
(safeName + i) in this.variableDB_.reservedDict_) { || this.variableDB_.dbReverse_[safeName + i]) {
// Collision with existing name. Create a unique name. // Collision with existing name. Create a unique name.
i = i ? i + 1 : 2; i = i ? i + 1 : 2;
} }
safeName += i; safeName += i;
this.variableDB_.dbReverse_[safeName] = true; this.variableDB_.dbReverse_[safeName] = true;
if (type === Blockly.Procedures.NAME_TYPE) return safeName;
return '$' + safeName; return '$' + safeName;
}; };
......
...@@ -64,7 +64,7 @@ Blockly.PHP['colour_rgb'] = function(block) { ...@@ -64,7 +64,7 @@ Blockly.PHP['colour_rgb'] = function(block) {
' $hex .= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);', ' $hex .= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);',
' return $hex;', ' return $hex;',
'}']); '}']);
var code = functionName + '($' + red + ', $' + green + ', $' + blue + ')'; var code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
}; };
...@@ -96,6 +96,6 @@ Blockly.PHP['colour_blend'] = function(block) { ...@@ -96,6 +96,6 @@ Blockly.PHP['colour_blend'] = function(block) {
' $hex .= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);', ' $hex .= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);',
' return $hex;', ' return $hex;',
'}']); '}']);
var code = functionName + '($' + c1 + ', $' + c2 + ', $' + ratio + ')'; var code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
}; };
...@@ -50,10 +50,10 @@ Blockly.PHP['lists_repeat'] = function(block) { ...@@ -50,10 +50,10 @@ Blockly.PHP['lists_repeat'] = function(block) {
var functionName = Blockly.PHP.provideFunction_( var functionName = Blockly.PHP.provideFunction_(
'lists_repeat', 'lists_repeat',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + [ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($value, $n) {', '($value, $count) {',
' $array = array();', ' $array = array();',
' for ($i = 0; $i < $n; $i++) {', ' for ($index = 0; $index < $count; $index++) {',
' $array[i] = $value;', ' $array[] = $value;',
' }', ' }',
' return $array;', ' return $array;',
'}']); '}']);
...@@ -69,14 +69,14 @@ Blockly.PHP['lists_length'] = function(block) { ...@@ -69,14 +69,14 @@ Blockly.PHP['lists_length'] = function(block) {
// List length. // List 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 ['count(' + argument0 + ')', Blockly.PHP.ORDER_MEMBER]; return ['count(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
}; };
Blockly.PHP['lists_isEmpty'] = function(block) { Blockly.PHP['lists_isEmpty'] = function(block) {
// Is the list empty? // Is the list empty?
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE', var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_MEMBER) || '[]'; Blockly.PHP.ORDER_FUNCTION_CALL) || '[]';
return ['empty(' + argument0 + ')', Blockly.PHP.ORDER_LOGICAL_NOT]; return ['empty(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
}; };
Blockly.PHP['lists_indexOf'] = function(block) { Blockly.PHP['lists_indexOf'] = function(block) {
...@@ -84,11 +84,11 @@ Blockly.PHP['lists_indexOf'] = function(block) { ...@@ -84,11 +84,11 @@ Blockly.PHP['lists_indexOf'] = function(block) {
var operator = block.getFieldValue('END') == 'FIRST' ? var operator = block.getFieldValue('END') == 'FIRST' ?
'indexOf' : 'lastIndexOf'; 'indexOf' : 'lastIndexOf';
var argument0 = Blockly.PHP.valueToCode(block, 'FIND', var argument0 = Blockly.PHP.valueToCode(block, 'FIND',
Blockly.PHP.ORDER_NONE) || '\'\''; Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
var argument1 = Blockly.PHP.valueToCode(block, 'VALUE', var argument1 = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_MEMBER) || '[]'; Blockly.PHP.ORDER_FUNCTION_CALL) || '[]';
var code = argument1 + '.' + operator + '(' + argument0 + ') + 1'; var code = argument1 + '.' + operator + '(' + argument0 + ') + 1';
return [code, Blockly.PHP.ORDER_MEMBER]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
}; };
Blockly.PHP['lists_getIndex'] = function(block) { Blockly.PHP['lists_getIndex'] = function(block) {
...@@ -99,27 +99,27 @@ Blockly.PHP['lists_getIndex'] = function(block) { ...@@ -99,27 +99,27 @@ Blockly.PHP['lists_getIndex'] = function(block) {
var at = Blockly.PHP.valueToCode(block, 'AT', var at = Blockly.PHP.valueToCode(block, 'AT',
Blockly.PHP.ORDER_UNARY_NEGATION) || '1'; Blockly.PHP.ORDER_UNARY_NEGATION) || '1';
var list = Blockly.PHP.valueToCode(block, 'VALUE', var list = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_MEMBER) || '[]'; Blockly.PHP.ORDER_FUNCTION_CALL) || '[]';
if (where == 'FIRST') { if (where == 'FIRST') {
if (mode == 'GET') { if (mode == 'GET') {
var code = list + '[0]'; var code = list + '[0]';
return [code, Blockly.PHP.ORDER_MEMBER]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
} else if (mode == 'GET_REMOVE') { } else if (mode == 'GET_REMOVE') {
var code = list + '.shift()'; var code = 'array_shift(' + list + ', 1)';
return [code, Blockly.PHP.ORDER_MEMBER]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
} else if (mode == 'REMOVE') { } else if (mode == 'REMOVE') {
return list + '.shift();\n'; var code = 'array_shift(' + list + ', 1)';
} }
} else if (where == 'LAST') { } else if (where == 'LAST') {
if (mode == 'GET') { if (mode == 'GET') {
var code = list + '.slice(-1)[0]'; var code = 'end(' + list + ')';
return [code, Blockly.PHP.ORDER_MEMBER]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
} else if (mode == 'GET_REMOVE') { } else if (mode == 'GET_REMOVE') {
var code = list + '.pop()'; var code = 'array_pop(' + list + ')';
return [code, Blockly.PHP.ORDER_MEMBER]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
} else if (mode == 'REMOVE') { } else if (mode == 'REMOVE') {
return list + '.pop();\n'; return 'array_pop(' + list + ');\n';
} }
} else if (where == 'FROM_START') { } else if (where == 'FROM_START') {
// Blockly uses one-based indicies. // Blockly uses one-based indicies.
...@@ -132,24 +132,23 @@ Blockly.PHP['lists_getIndex'] = function(block) { ...@@ -132,24 +132,23 @@ Blockly.PHP['lists_getIndex'] = function(block) {
} }
if (mode == 'GET') { if (mode == 'GET') {
var code = list + '[' + at + ']'; var code = list + '[' + at + ']';
return [code, Blockly.PHP.ORDER_MEMBER]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
} else if (mode == 'GET_REMOVE') { } else if (mode == 'GET_REMOVE') {
var code = list + '.splice(' + at + ', 1)[0]'; var code = 'array_splice(' + list + ', ' + at + ', 1)[0]';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
} else if (mode == 'REMOVE') { } else if (mode == 'REMOVE') {
return list + '.splice(' + at + ', 1);\n'; return 'array_splice(' + list + ', ' + at + ', 1);\n';
} }
} else if (where == 'FROM_END') { } else if (where == 'FROM_END') {
if (mode == 'GET') { if (mode == 'GET') {
var code = list + '.slice(-' + at + ')[0]'; var code = 'array_slice(' + list + ', -' + at + ', 1)[0]';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
} else if (mode == 'GET_REMOVE' || mode == 'REMOVE') { } else if (mode == 'GET_REMOVE' || mode == 'REMOVE') {
var functionName = Blockly.PHP.provideFunction_( var functionName = Blockly.PHP.provideFunction_(
'lists_remove_from_end', 'lists_remove_from_end',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + [ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'(list, x) {', '($list, $position) {',
' x = list.length - x;', ' return array_splice($list, count($list) - $position, 1)[0];',
' return list.splice(x, 1)[0];',
'}']); '}']);
code = functionName + '(' + list + ', ' + at + ')'; code = functionName + '(' + list + ', ' + at + ')';
if (mode == 'GET_REMOVE') { if (mode == 'GET_REMOVE') {
...@@ -162,13 +161,9 @@ Blockly.PHP['lists_getIndex'] = function(block) { ...@@ -162,13 +161,9 @@ Blockly.PHP['lists_getIndex'] = function(block) {
var functionName = Blockly.PHP.provideFunction_( var functionName = Blockly.PHP.provideFunction_(
'lists_get_random_item', 'lists_get_random_item',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + [ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'(list, remove) {', '($list, $remove) {',
' var x = Math.floor(Math.random() * list.length);', ' $x = rand(0,count($list)-1);',
' if (remove) {', ' return $remove ? array_splice($list, $x, 1)[0] : $list[$x];',
' return list.splice(x, 1)[0];',
' } else {',
' return list[x];',
' }',
'}']); '}']);
code = functionName + '(' + list + ', ' + (mode != 'GET') + ')'; code = functionName + '(' + list + ', ' + (mode != 'GET') + ')';
if (mode == 'GET' || mode == 'GET_REMOVE') { if (mode == 'GET' || mode == 'GET_REMOVE') {
...@@ -199,7 +194,7 @@ Blockly.PHP['lists_setIndex'] = function(block) { ...@@ -199,7 +194,7 @@ Blockly.PHP['lists_setIndex'] = function(block) {
} }
var listVar = Blockly.PHP.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 = listVar + ' = ' + list + ';\n';
list = listVar; list = listVar;
return code; return code;
} }
...@@ -207,15 +202,15 @@ Blockly.PHP['lists_setIndex'] = function(block) { ...@@ -207,15 +202,15 @@ Blockly.PHP['lists_setIndex'] = function(block) {
if (mode == 'SET') { if (mode == 'SET') {
return list + '[0] = ' + value + ';\n'; return list + '[0] = ' + value + ';\n';
} else if (mode == 'INSERT') { } else if (mode == 'INSERT') {
return list + '.unshift(' + value + ');\n'; return 'array_unshift(' + list + ', ' + value + ');\n';
} }
} else if (where == 'LAST') { } else if (where == 'LAST') {
if (mode == 'SET') { if (mode == 'SET') {
var code = cacheList(); var code = cacheList();
code += list + '[' + list + '.length - 1] = ' + value + ';\n'; code += list + '[count(' + list + ') - 1] = ' + value + ';\n';
return code; return code;
} else if (mode == 'INSERT') { } else if (mode == 'INSERT') {
return list + '.push(' + value + ');\n'; return list + 'array_push(' + list + ', ' + value + ');\n';
} }
} else if (where == 'FROM_START') { } else if (where == 'FROM_START') {
// Blockly uses one-based indicies. // Blockly uses one-based indicies.
...@@ -229,29 +224,27 @@ Blockly.PHP['lists_setIndex'] = function(block) { ...@@ -229,29 +224,27 @@ Blockly.PHP['lists_setIndex'] = function(block) {
if (mode == 'SET') { if (mode == 'SET') {
return list + '[' + at + '] = ' + value + ';\n'; return list + '[' + at + '] = ' + value + ';\n';
} else if (mode == 'INSERT') { } else if (mode == 'INSERT') {
return list + '.splice(' + at + ', 0, ' + value + ');\n'; return 'array_splice(' + list + ', ' + at + ', 0, ' + value + ');\n';
} }
} else if (where == 'FROM_END') { } else if (where == 'FROM_END') {
var code = cacheList(); var code = cacheList();
if (mode == 'SET') { if (mode == 'SET') {
code += list + '[' + list + '.length - ' + at + '] = ' + value + ';\n'; code += list + '[count(' + list + ') - ' + at + '] = ' + value + ';\n';
return code; return code;
} else if (mode == 'INSERT') { } else if (mode == 'INSERT') {
code += list + '.splice(' + list + '.length - ' + at + ', 0, ' + value + code += 'array_splice(' + list + ', count(' + list + ') - ' + at + ', 0, ' + value + ');\n';
');\n';
return code; return code;
} }
} else if (where == 'RANDOM') { } else if (where == 'RANDOM') {
var code = cacheList(); var code = cacheList();
var xVar = Blockly.PHP.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 += xVar + ' = rand(0, count(' + list + ')-1);\n';
'.length);\n';
if (mode == 'SET') { if (mode == 'SET') {
code += list + '[' + xVar + '] = ' + value + ';\n'; code += list + '[' + xVar + '] = ' + value + ';\n';
return code; return code;
} else if (mode == 'INSERT') { } else if (mode == 'INSERT') {
code += list + '.splice(' + xVar + ', 0, ' + value + ');\n'; code += 'array_splice(' + list + ', ' + xVar + ', 0, ' + value + ');\n';
return code; return code;
} }
} }
...@@ -274,24 +267,29 @@ Blockly.PHP['lists_getSublist'] = function(block) { ...@@ -274,24 +267,29 @@ Blockly.PHP['lists_getSublist'] = function(block) {
var functionName = Blockly.PHP.provideFunction_( var functionName = Blockly.PHP.provideFunction_(
'lists_get_sublist', 'lists_get_sublist',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + [ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'(list, where1, at1, where2, at2) {', '($list, $where1, $at1, $where2, $at2) {',
' function getAt(where, at) {', ' if ($where1 == \'FROM_START\') {',
' if (where == \'FROM_START\') {', ' $at1--;',
' at--;', ' } else if ($where1 == \'FROM_END\') {',
' } else if (where == \'FROM_END\') {', ' $at1 = count($list) - $at1;',
' at = list.length - at;', ' } else if ($where1 == \'FIRST\') {',
' } else if (where == \'FIRST\') {', ' $at1 = 0;',
' at = 0;', ' } else if ($where1 == \'LAST\') {',
' } else if (where == \'LAST\') {', ' $at1 = count($list) - 1;',
' at = list.length - 1;',
' } else {', ' } else {',
' throw \'Unhandled option (lists_getSublist).\';', ' throw \'Unhandled option (lists_getSublist).\';',
' }', ' }',
' return at;', ' if ($where2 == \'FROM_START\') {',
' } else if ($where2 == \'FROM_END\') {',
' $at2 = count($list) - $at2;',
' } else if ($where2 == \'FIRST\') {',
' $at2 = 0;',
' } else if ($where2 == \'LAST\') {',
' $at2 = count($list);',
' } else {',
' throw \'Unhandled option (lists_getSublist).\';',
' }', ' }',
' at1 = getAt(where1, at1);', ' return array_slice($list, $at1, $at2)[0];',
' at2 = getAt(where2, at2) + 1;',
' return list.slice(at1, at2);',
'}']); '}']);
var code = functionName + '(' + list + ', \'' + var code = functionName + '(' + list + ', \'' +
where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')'; where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
...@@ -310,15 +308,15 @@ Blockly.PHP['lists_split'] = function(block) { ...@@ -310,15 +308,15 @@ Blockly.PHP['lists_split'] = function(block) {
if (!value_input) { if (!value_input) {
value_input = '\'\''; value_input = '\'\'';
} }
var functionName = 'split'; var functionName = 'explode';
} else if (mode == 'JOIN') { } else if (mode == 'JOIN') {
if (!value_input) { if (!value_input) {
value_input = '[]'; value_input = '[]';
} }
var functionName = 'join'; var functionName = 'implode';
} else { } else {
throw 'Unknown mode: ' + mode; throw 'Unknown mode: ' + mode;
} }
var code = value_input + '.' + functionName + '(' + value_delim + ')'; var code = functionName + '('+ value_delim + ', ' + value_input + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
}; };
...@@ -199,8 +199,7 @@ Blockly.PHP['math_change'] = function(block) { ...@@ -199,8 +199,7 @@ Blockly.PHP['math_change'] = function(block) {
Blockly.PHP.ORDER_ADDITION) || '0'; Blockly.PHP.ORDER_ADDITION) || '0';
var varName = Blockly.PHP.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 + ' += ' + argument0 + ';\n';
' : 0) + ' + argument0 + ';\n';
}; };
// Rounding functions have a single operand. // Rounding functions have a single operand.
...@@ -215,17 +214,17 @@ Blockly.PHP['math_on_list'] = function(block) { ...@@ -215,17 +214,17 @@ Blockly.PHP['math_on_list'] = function(block) {
switch (func) { switch (func) {
case 'SUM': case 'SUM':
list = Blockly.PHP.valueToCode(block, 'LIST', list = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_MEMBER) || 'array()'; Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
code = 'array_sum(' + list + ')'; code = 'array_sum(' + list + ')';
break; break;
case 'MIN': case 'MIN':
list = Blockly.PHP.valueToCode(block, 'LIST', list = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_COMMA) || 'array()'; Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
code = 'min(' + list + ')'; code = 'min(' + list + ')';
break; break;
case 'MAX': case 'MAX':
list = Blockly.PHP.valueToCode(block, 'LIST', list = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_COMMA) || 'array()'; Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
code = 'max(' + list + ')'; code = 'max(' + list + ')';
break; break;
case 'AVERAGE': case 'AVERAGE':
...@@ -328,10 +327,7 @@ Blockly.PHP['math_random_int'] = function(block) { ...@@ -328,10 +327,7 @@ Blockly.PHP['math_random_int'] = function(block) {
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + [ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($a, $b) {', '($a, $b) {',
' if ($a > $b) {', ' if ($a > $b) {',
' // Swap a and b to ensure a is smaller.', ' return rand($b, $a);',
' $c = $a;',
' $a = $b;',
' $b = $c;',
' }', ' }',
' return rand($a, $b);', ' return rand($a, $b);',
'}']); '}']);
......
...@@ -28,7 +28,6 @@ goog.provide('Blockly.PHP.procedures'); ...@@ -28,7 +28,6 @@ goog.provide('Blockly.PHP.procedures');
goog.require('Blockly.PHP'); 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.getName( var funcName = Blockly.PHP.getName(
......
...@@ -69,7 +69,7 @@ Blockly.PHP['text_append'] = function(block) { ...@@ -69,7 +69,7 @@ Blockly.PHP['text_append'] = function(block) {
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) || '\'\'';
return varName + ' = ' + varName + ' . ' + argument0 + ';\n'; return varName + ' .= ' + argument0 + ';\n';
}; };
Blockly.PHP['text_length'] = function(block) { Blockly.PHP['text_length'] = function(block) {
...@@ -82,7 +82,7 @@ Blockly.PHP['text_length'] = function(block) { ...@@ -82,7 +82,7 @@ Blockly.PHP['text_length'] = function(block) {
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_FUNCTION_CALL) || '\'\'';
return ['empty(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL]; return ['empty(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
}; };
...@@ -91,20 +91,20 @@ Blockly.PHP['text_indexOf'] = function(block) { ...@@ -91,20 +91,20 @@ Blockly.PHP['text_indexOf'] = function(block) {
var operator = block.getFieldValue('END') == 'FIRST' ? var operator = block.getFieldValue('END') == 'FIRST' ?
'strpos' : 'strrpos'; 'strpos' : 'strrpos';
var argument0 = Blockly.PHP.valueToCode(block, 'FIND', var argument0 = Blockly.PHP.valueToCode(block, 'FIND',
Blockly.PHP.ORDER_NONE) || '\'\''; Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
var argument1 = Blockly.PHP.valueToCode(block, 'VALUE', var argument1 = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_MEMBER) || '\'\''; Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
var code = operator + '(' + argument0 + ', ' + argument1 + ') + 1'; var code = operator + '(' + argument0 + ', ' + argument1 + ') + 1';
return [code, Blockly.PHP.ORDER_MEMBER]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
}; };
Blockly.PHP['text_charAt'] = function(block) { Blockly.PHP['text_charAt'] = function(block) {
// Get letter at index. // Get letter at index.
var where = block.getFieldValue('WHERE') || 'FROM_START'; var where = block.getFieldValue('WHERE') || 'FROM_START';
var at = Blockly.PHP.valueToCode(block, 'AT', var at = Blockly.PHP.valueToCode(block, 'AT',
Blockly.PHP.ORDER_UNARY_NEGATION) || '1'; Blockly.PHP.ORDER_FUNCTION_CALL) || '0';
var text = Blockly.PHP.valueToCode(block, 'VALUE', var text = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_MEMBER) || '\'\''; Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
switch (where) { switch (where) {
case 'FIRST': case 'FIRST':
var code = text + '[0]'; var code = text + '[0]';
...@@ -142,13 +142,13 @@ Blockly.PHP['text_charAt'] = function(block) { ...@@ -142,13 +142,13 @@ Blockly.PHP['text_charAt'] = function(block) {
Blockly.PHP['text_getSubstring'] = function(block) { Blockly.PHP['text_getSubstring'] = function(block) {
// Get substring. // Get substring.
var text = Blockly.PHP.valueToCode(block, 'STRING', var text = Blockly.PHP.valueToCode(block, 'STRING',
Blockly.PHP.ORDER_MEMBER) || '\'\''; Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
var where1 = block.getFieldValue('WHERE1'); var where1 = block.getFieldValue('WHERE1');
var where2 = block.getFieldValue('WHERE2'); var where2 = block.getFieldValue('WHERE2');
var at1 = Blockly.PHP.valueToCode(block, 'AT1', var at1 = Blockly.PHP.valueToCode(block, 'AT1',
Blockly.PHP.ORDER_NONE) || '1'; Blockly.PHP.ORDER_FUNCTION_CALL) || '0';
var at2 = Blockly.PHP.valueToCode(block, 'AT2', var at2 = Blockly.PHP.valueToCode(block, 'AT2',
Blockly.PHP.ORDER_NONE) || '1'; Blockly.PHP.ORDER_FUNCTION_CALL) || '0';
if (where1 == 'FIRST' && where2 == 'LAST') { if (where1 == 'FIRST' && where2 == 'LAST') {
var code = text; var code = text;
} else { } else {
...@@ -159,23 +159,20 @@ Blockly.PHP['text_getSubstring'] = function(block) { ...@@ -159,23 +159,20 @@ Blockly.PHP['text_getSubstring'] = function(block) {
' if ($where1 == \'FROM_START\') {', ' if ($where1 == \'FROM_START\') {',
' $at1--;', ' $at1--;',
' } else if ($where1 == \'FROM_END\') {', ' } else if ($where1 == \'FROM_END\') {',
' $at1 = strlen($text) - $at;', ' $at1 = strlen($text) - $at1;',
' } else if ($where1 == \'FIRST\') {', ' } else if ($where1 == \'FIRST\') {',
' $at1 = 0;', ' $at1 = 0;',
' } else if (where1 == \'LAST\') {', ' } else if ($where1 == \'LAST\') {',
' $at1 = strlen($text) - 1;', ' $at1 = strlen($text) - 1;',
' } else { $at1 = 0; }', ' } else { $at1 = 0; }',
' if ($where2 == \'FROM_START\') {', ' if ($where2 == \'FROM_START\') {',
' $at2--;',
' } else if ($where2 == \'FROM_END\') {', ' } else if ($where2 == \'FROM_END\') {',
' $at2 = strlen($text) - $at;', ' $at2 = strlen($text) - $at2;',
' } else if ($where2 == \'FIRST\') {', ' } else if ($where2 == \'FIRST\') {',
' $at2 = 0;', ' $at2 = 0;',
' } else if (where2 == \'LAST\') {', ' } else if ($where2 == \'LAST\') {',
' $at2 = strlen($text) - 1;', ' $at2 = strlen($text);',
' } else { $at2 = 0; }', ' } else { $at2 = 0; }',
' $at1 = getAt($where1, $at1);',
' $at2 = getAt($where2, $at2) + 1;',
' return substr($text, $at1, $at2);', ' return substr($text, $at1, $at2);',
'}']); '}']);
var code = functionName + '(' + text + ', \'' + var code = functionName + '(' + text + ', \'' +
...@@ -189,15 +186,15 @@ Blockly.PHP['text_changeCase'] = function(block) { ...@@ -189,15 +186,15 @@ Blockly.PHP['text_changeCase'] = function(block) {
var code; var code;
if (block.getFieldValue('CASE')=='UPPERCASE') { if (block.getFieldValue('CASE')=='UPPERCASE') {
var argument0 = Blockly.PHP.valueToCode(block, 'TEXT', var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
Blockly.PHP.ORDER_MEMBER) || '\'\''; Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
code = 'strtoupper(' + argument0 + ')'; code = 'strtoupper(' + argument0 + ')';
} else if (block.getFieldValue('CASE')=='LOWERCASE') { } else if (block.getFieldValue('CASE')=='LOWERCASE') {
var argument0 = Blockly.PHP.valueToCode(block, 'TEXT', var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
Blockly.PHP.ORDER_MEMBER) || '\'\''; Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
code = 'strtolower(' + argument0 + ')'; code = 'strtolower(' + argument0 + ')';
} else if (block.getFieldValue('CASE')=='TITLECASE') { } else if (block.getFieldValue('CASE')=='TITLECASE') {
var argument0 = Blockly.PHP.valueToCode(block, 'TEXT', var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
Blockly.PHP.ORDER_MEMBER) || '\'\''; Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
code = 'ucwords(' + argument0 + ')'; code = 'ucwords(' + argument0 + ')';
} }
return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
...@@ -212,14 +209,14 @@ Blockly.PHP['text_trim'] = function(block) { ...@@ -212,14 +209,14 @@ Blockly.PHP['text_trim'] = function(block) {
}; };
var operator = OPERATORS[block.getFieldValue('MODE')]; var operator = OPERATORS[block.getFieldValue('MODE')];
var argument0 = Blockly.PHP.valueToCode(block, 'TEXT', var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
Blockly.PHP.ORDER_MEMBER) || '\'\''; Blockly.PHP.ORDER_ATOMIC) || '\'\'';
return [ operator + '(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL]; return [ operator + '(' + argument0 + ')', Blockly.PHP.ORDER_ATOMIC];
}; };
Blockly.PHP['text_print'] = function(block) { Blockly.PHP['text_print'] = function(block) {
// Print statement. // Print statement.
var argument0 = Blockly.PHP.valueToCode(block, 'TEXT', var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
Blockly.PHP.ORDER_NONE) || '\'\''; Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
return 'print(' + argument0 + ');\n'; return 'print(' + argument0 + ');\n';
}; };
...@@ -231,17 +228,17 @@ Blockly.PHP['text_prompt'] = function(block) { ...@@ -231,17 +228,17 @@ Blockly.PHP['text_prompt'] = function(block) {
if (toNumber) { if (toNumber) {
code = 'floatval(' + code + ')'; code = 'floatval(' + code + ')';
} }
return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; return [code, Blockly.PHP.ORDER_ATOMIC];
}; };
Blockly.PHP['text_prompt_ext'] = function(block) { Blockly.PHP['text_prompt_ext'] = function(block) {
// Prompt function (external message). // Prompt function (external message).
var msg = Blockly.PHP.valueToCode(block, 'TEXT', var msg = Blockly.PHP.valueToCode(block, 'TEXT',
Blockly.PHP.ORDER_NONE) || '\'\''; Blockly.PHP.ORDER_ATOMIC) || '\'\'';
var code = 'readline(' + msg + ')'; var code = 'readline(' + msg + ')';
var toNumber = block.getFieldValue('TYPE') == 'NUMBER'; var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
if (toNumber) { if (toNumber) {
code = 'floatval(' + code + ')'; code = 'floatval(' + code + ')';
} }
return [code, Blockly.PHP.ORDER_FUNCTION_CALL]; return [code, Blockly.PHP.ORDER_ATOMIC];
}; };
...@@ -39,7 +39,7 @@ Blockly.PHP['variables_set'] = function(block) { ...@@ -39,7 +39,7 @@ Blockly.PHP['variables_set'] = function(block) {
// Variable setter. // Variable setter.
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE', var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_ASSIGNMENT) || '0'; Blockly.PHP.ORDER_ASSIGNMENT) || '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 + ' = ' + argument0 + ';\n'; return varName + ' = ' + argument0 + ';\n';
}; };
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