Commit 0faf2b7a authored by daarond's avatar daarond

still working on tests, lists are the only unfinished

parent 40c491e3
......@@ -32,8 +32,10 @@ goog.provide('Blockly.Names');
* @param {string} reservedWords A comma-separated string of words that are
* illegal for use as names in a language (e.g. 'new,if,this,...').
* @constructor
* @param namesPrependDollar boolean indicating whether the languages requires dollar signs ($) before a variable
*/
Blockly.Names = function(reservedWords) {
Blockly.Names = function(reservedWords, namesPrependDollar) {
this.prependDollar = namesPrependDollar || false;
this.reservedDict_ = Object.create(null);
if (reservedWords) {
var splitWords = reservedWords.split(',');
......@@ -70,11 +72,12 @@ Blockly.Names.prototype.reset = function() {
*/
Blockly.Names.prototype.getName = function(name, type) {
var normalized = name.toLowerCase() + '_' + type;
var prepend = type=='VARIABLE' && this.prependDollar ? '$' : '';
if (normalized in this.db_) {
return this.db_[normalized];
return prepend + this.db_[normalized];
}
var safeName = this.getDistinctName(name, type);
this.db_[normalized] = safeName;
this.db_[normalized] = type=='VARIABLE' && this.prependDollar ? safeName.substr(1) : safeName;
return safeName;
};
......@@ -98,7 +101,8 @@ Blockly.Names.prototype.getDistinctName = function(name, type) {
}
safeName += i;
this.dbReverse_[safeName] = true;
return safeName;
var prepend = type=='VARIABLE' && this.prependDollar ? '$' : '';
return prepend + safeName;
};
/**
......
This diff is collapsed.
......@@ -68,48 +68,73 @@ Blockly.PHP['lists_repeat'] = function(block) {
Blockly.PHP['lists_length'] = function(block) {
// List length.
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_FUNCTION_CALL) || '[]';
Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
return ['count(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
};
Blockly.PHP['lists_isEmpty'] = function(block) {
// Is the list empty?
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_FUNCTION_CALL) || '[]';
Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
return ['empty(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
};
Blockly.PHP['lists_indexOf'] = function(block) {
// Find an item in the list.
var operator = block.getFieldValue('END') == 'FIRST' ?
'indexOf' : 'lastIndexOf';
var operator = block.getFieldValue('END');
var argument0 = Blockly.PHP.valueToCode(block, 'FIND',
Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
var argument1 = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_FUNCTION_CALL) || '[]';
var code = argument1 + '.' + operator + '(' + argument0 + ') + 1';
Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
var code;
if (operator == 'FIRST'){
var functionName = Blockly.PHP.provideFunction_(
'indexOf',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($list, $item) {',
' for($i=0; $i < count($list); $i++){',
' if ($list[$i] == $item) { return $i + 1; }',
' }',
' return 0;',
'}']);
code = functionName + '(' + argument1 + ', ' + argument0 + ')';
} else {
var functionName = Blockly.PHP.provideFunction_(
'lastIndexOf',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($list, $item) {',
' $last = -1;',
' for($i=0; $i < count($list); $i++){',
' if ($list[$i] == $item) { $last = $i; }',
' }',
' return $last;',
'}']);
code = functionName + '(' + argument1 + ', ' + argument0 + ') + 1';
}
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
};
Blockly.PHP['lists_getIndex'] = function(block) {
// Get element at index.
// Note: Until January 2013 this block did not have MODE or WHERE inputs.
var mode = block.getFieldValue('MODE') || 'GET';
var where = block.getFieldValue('WHERE') || 'FROM_START';
var at = Blockly.PHP.valueToCode(block, 'AT',
Blockly.PHP.ORDER_UNARY_NEGATION) || '1';
var list = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_FUNCTION_CALL) || '[]';
Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
if (where == 'FIRST') {
if (mode == 'GET') {
var code = list + '[0]';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
} else if (mode == 'GET_REMOVE') {
var code = 'array_shift(' + list + ', 1)';
var code = 'array_shift(' + list + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
} else if (mode == 'REMOVE') {
var code = 'array_shift(' + list + ', 1)';
var code = 'array_shift(' + list + ');\n';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
}
} else if (where == 'LAST') {
if (mode == 'GET') {
......@@ -179,7 +204,7 @@ Blockly.PHP['lists_setIndex'] = function(block) {
// Set element at index.
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
var list = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_MEMBER) || '[]';
Blockly.PHP.ORDER_MEMBER) || 'array()';
var mode = block.getFieldValue('MODE') || 'GET';
var where = block.getFieldValue('WHERE') || 'FROM_START';
var at = Blockly.PHP.valueToCode(block, 'AT',
......@@ -192,7 +217,7 @@ Blockly.PHP['lists_setIndex'] = function(block) {
if (list.match(/^\w+$/)) {
return '';
}
var listVar = Blockly.PHP.getDistinctName(
var listVar = Blockly.PHP.variableDB_.getDistinctName(
'tmp_list', Blockly.Variables.NAME_TYPE);
var code = listVar + ' = ' + list + ';\n';
list = listVar;
......@@ -210,7 +235,7 @@ Blockly.PHP['lists_setIndex'] = function(block) {
code += list + '[count(' + list + ') - 1] = ' + value + ';\n';
return code;
} else if (mode == 'INSERT') {
return list + 'array_push(' + list + ', ' + value + ');\n';
return 'array_push(' + list + ', ' + value + ');\n';
}
} else if (where == 'FROM_START') {
// Blockly uses one-based indicies.
......@@ -237,7 +262,7 @@ Blockly.PHP['lists_setIndex'] = function(block) {
}
} else if (where == 'RANDOM') {
var code = cacheList();
var xVar = Blockly.PHP.getDistinctName(
var xVar = Blockly.PHP.variableDB_.getDistinctName(
'tmp_x', Blockly.Variables.NAME_TYPE);
code += xVar + ' = rand(0, count(' + list + ')-1);\n';
if (mode == 'SET') {
......@@ -254,7 +279,7 @@ Blockly.PHP['lists_setIndex'] = function(block) {
Blockly.PHP['lists_getSublist'] = function(block) {
// Get sublist.
var list = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_MEMBER) || '[]';
Blockly.PHP.ORDER_MEMBER) || 'array()';
var where1 = block.getFieldValue('WHERE1');
var where2 = block.getFieldValue('WHERE2');
var at1 = Blockly.PHP.valueToCode(block, 'AT1',
......@@ -262,7 +287,7 @@ Blockly.PHP['lists_getSublist'] = function(block) {
var at2 = Blockly.PHP.valueToCode(block, 'AT2',
Blockly.PHP.ORDER_NONE) || '1';
if (where1 == 'FIRST' && where2 == 'LAST') {
var code = list + '.concat()';
var code = list;
} else {
var functionName = Blockly.PHP.provideFunction_(
'lists_get_sublist',
......@@ -311,7 +336,7 @@ Blockly.PHP['lists_split'] = function(block) {
var functionName = 'explode';
} else if (mode == 'JOIN') {
if (!value_input) {
value_input = '[]';
value_input = 'array()';
}
var functionName = 'implode';
} else {
......
......@@ -34,7 +34,7 @@ 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.getDistinctName(
var loopVar = Blockly.PHP.variableDB_.getDistinctName(
'count', Blockly.Variables.NAME_TYPE);
var code = 'for (' + loopVar + ' = 0; ' +
loopVar + ' < ' + repeats + '; ' +
......@@ -50,11 +50,11 @@ 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.getDistinctName(
var loopVar = Blockly.PHP.variableDB_.getDistinctName(
'count', Blockly.Variables.NAME_TYPE);
var endVar = repeats;
if (!repeats.match(/^\w+$/) && !Blockly.isNumber(repeats)) {
var endVar = Blockly.PHP.getDistinctName(
var endVar = Blockly.PHP.variableDB_.getDistinctName(
'repeat_end', Blockly.Variables.NAME_TYPE);
code += endVar + ' = ' + repeats + ';\n';
}
......@@ -81,7 +81,7 @@ Blockly.PHP['controls_whileUntil'] = function(block) {
Blockly.PHP['controls_for'] = function(block) {
// For loop.
var variable0 = Blockly.PHP.getName(
var variable0 = Blockly.PHP.variableDB_.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
var argument0 = Blockly.PHP.valueToCode(block, 'FROM',
Blockly.PHP.ORDER_ASSIGNMENT) || '0';
......@@ -111,19 +111,19 @@ 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.getDistinctName(
startVar = Blockly.PHP.variableDB_.getDistinctName(
variable0 + '_start', Blockly.Variables.NAME_TYPE);
code += startVar + ' = ' + argument0 + ';\n';
}
var endVar = argument1;
if (!argument1.match(/^\w+$/) && !Blockly.isNumber(argument1)) {
var endVar = Blockly.PHP.getDistinctName(
var endVar = Blockly.PHP.variableDB_.getDistinctName(
variable0 + '_end', Blockly.Variables.NAME_TYPE);
code += endVar + ' = ' + argument1 + ';\n';
}
// Determine loop direction at start, in case one of the bounds
// changes during loop execution.
var incVar = Blockly.PHP.getDistinctName(
var incVar = Blockly.PHP.variableDB_.getDistinctName(
variable0 + '_inc', Blockly.Variables.NAME_TYPE);
code += incVar + ' = ';
if (Blockly.isNumber(increment)) {
......@@ -146,7 +146,7 @@ Blockly.PHP['controls_for'] = function(block) {
Blockly.PHP['controls_forEach'] = function(block) {
// For each loop.
var variable0 = Blockly.PHP.getName(
var variable0 = Blockly.PHP.variableDB_.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
var argument0 = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_ASSIGNMENT) || '[]';
......
......@@ -52,7 +52,7 @@ Blockly.PHP['math_arithmetic'] = function(block) {
var code;
// Power in PHP requires a special case since it has no operator.
if (!operator) {
code = 'Math.pow(' + argument0 + ', ' + argument1 + ')';
code = 'pow(' + argument0 + ', ' + argument1 + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
}
code = argument0 + operator + argument1;
......@@ -146,8 +146,8 @@ 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_FUNCTION_CALL],
'E': ['exp()', Blockly.PHP.ORDER_FUNCTION_CALL],
'PI': ['M_PI', Blockly.PHP.ORDER_ATOMIC],
'E': ['M_E', Blockly.PHP.ORDER_ATOMIC],
'GOLDEN_RATIO':
['(1 + sqrt(5)) / 2', Blockly.PHP.ORDER_DIVISION],
'SQRT2': ['M_SQRT2', Blockly.PHP.ORDER_ATOMIC],
......@@ -164,7 +164,32 @@ Blockly.PHP['math_number_property'] = function(block) {
Blockly.PHP.ORDER_MODULUS) || '0';
var dropdown_property = block.getFieldValue('PROPERTY');
var code;
if (dropdown_property == 'PRIME') {
// Prime is a special case as it is not a one-liner test.
var functionName = Blockly.PHP.provideFunction_(
'math_isPrime',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($n) {',
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
' if ($n == 2 || $n == 3) {',
' return true;',
' }',
' // False if n is NaN, negative, is 1, or not whole.',
' // And false if n is divisible by 2 or 3.',
' if (!is_numeric($n) || $n <= 1 || $n % 1 != 0 || $n % 2 == 0 ||' +
' $n % 3 == 0) {',
' return false;',
' }',
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
' for ($x = 6; $x <= sqrt($n) + 1; $x += 6) {',
' if ($n % ($x - 1) == 0 || $n % ($x + 1) == 0) {',
' return false;',
' }',
' }',
' return true;',
'}']);
code = functionName + '(' + number_to_check + ')';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
}
switch (dropdown_property) {
case 'EVEN':
code = number_to_check + ' % 2 == 0';
......@@ -172,11 +197,8 @@ Blockly.PHP['math_number_property'] = function(block) {
case 'ODD':
code = number_to_check + ' % 2 == 1';
break;
case 'PRIME':
code = 'mp_prob_prime('+number_to_check + ')';
break;
case 'WHOLE':
code = number_to_check + ' % 1 == 0';
code = 'is_int(' + number_to_check + ')';
break;
case 'POSITIVE':
code = number_to_check + ' > 0';
......@@ -197,7 +219,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.getName(
var varName = Blockly.PHP.variableDB_.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
return varName + ' += ' + argument0 + ';\n';
};
......@@ -228,7 +250,6 @@ Blockly.PHP['math_on_list'] = function(block) {
code = 'max(' + list + ')';
break;
case 'AVERAGE':
// math_median([null,null,1,3]) == 2.0.
var functionName = Blockly.PHP.provideFunction_(
'math_mean',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
......@@ -240,14 +261,13 @@ Blockly.PHP['math_on_list'] = function(block) {
code = functionName + '(' + list + ')';
break;
case 'MEDIAN':
// math_median([null,null,1,3]) == 2.0.
var functionName = Blockly.PHP.provideFunction_(
'math_median',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($myList) {',
' rsort($myList);',
' $middle = round(count($myList) / 2);',
' return $myList[$middle-1]; ',
'($arr) {',
' sort($arr,SORT_NUMERIC);',
' return (count($arr) % 2) ? $arr[floor(count($arr)/2)] : ',
' ($arr[floor(count($arr)/2)] + $arr[floor(count($arr)/2) - 1]) / 2;',
'}']);
list = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_NONE) || '[]';
......@@ -264,23 +284,33 @@ Blockly.PHP['math_on_list'] = function(block) {
' $v = array_count_values($values);',
' arsort($v);',
' foreach($v as $k => $v){$total = $k; break;}',
' return $total;',
' return array($total);',
'}']);
list = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_NONE) || '[]';
code = functionName + '(' + list + ')';
break;
case 'STD_DEV':
var functionName = Blockly.PHP.provideFunction_(
'math_standard_deviation',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($numbers) {',
' $n = count($numbers);',
' if (!$n) return null;',
' $mean = array_sum($numbers) / count($numbers);',
' foreach($numbers as $key => $num) $devs[$key] = pow($num - $mean, 2);',
' return sqrt(array_sum($devs) / (count($devs) - 1));',
'}']);
list = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_MEMBER) || 'array()';
code = 'stats_standard_deviation(' + list + ')';
Blockly.PHP.ORDER_NONE) || '[]';
code = functionName + '(' + list + ')';
break;
case 'RANDOM':
var functionName = Blockly.PHP.provideFunction_(
'math_random_list',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($list) {',
' $x = floor(rand() * count($list));',
' $x = rand(0, count($list)-1);',
' return $list[$x];',
'}']);
list = Blockly.PHP.valueToCode(block, 'LIST',
......
......@@ -30,7 +30,22 @@ goog.require('Blockly.PHP');
Blockly.PHP['procedures_defreturn'] = function(block) {
// Define a procedure with a return value.
var funcName = Blockly.PHP.getName(
// First, add a 'global' statement for every variable that is assigned.
var globals = Blockly.Variables.allVariables(block);
for (var i = globals.length - 1; i >= 0; i--) {
var varName = globals[i];
if (block.arguments_.indexOf(varName) == -1) {
globals[i] = Blockly.PHP.variableDB_.getName(varName,
Blockly.Variables.NAME_TYPE);
} else {
// This variable is actually a parameter name. Do not include it in
// the list of globals, thus allowing it be of local scope.
globals.splice(i, 1);
}
}
globals = globals.length ? ' global ' + globals.join(', ') + ';\n' : '';
var funcName = Blockly.PHP.variableDB_.getName(
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
var branch = Blockly.PHP.statementToCode(block, 'STACK');
if (Blockly.PHP.STATEMENT_PREFIX) {
......@@ -49,11 +64,11 @@ Blockly.PHP['procedures_defreturn'] = function(block) {
}
var args = [];
for (var x = 0; x < block.arguments_.length; x++) {
args[x] = Blockly.PHP.getName(block.arguments_[x],
args[x] = Blockly.PHP.variableDB_.getName(block.arguments_[x],
Blockly.Variables.NAME_TYPE);
}
var code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
branch + returnValue + '}';
globals + branch + returnValue + '}';
code = Blockly.PHP.scrub_(block, code);
Blockly.PHP.definitions_[funcName] = code;
return null;
......@@ -66,7 +81,7 @@ Blockly.PHP['procedures_defnoreturn'] =
Blockly.PHP['procedures_callreturn'] = function(block) {
// Call a procedure with a return value.
var funcName = Blockly.PHP.getName(
var funcName = Blockly.PHP.variableDB_.getName(
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
var args = [];
for (var x = 0; x < block.arguments_.length; x++) {
......@@ -79,7 +94,7 @@ Blockly.PHP['procedures_callreturn'] = function(block) {
Blockly.PHP['procedures_callnoreturn'] = function(block) {
// Call a procedure with no return value.
var funcName = Blockly.PHP.getName(
var funcName = Blockly.PHP.variableDB_.getName(
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
var args = [];
for (var x = 0; x < block.arguments_.length; x++) {
......
......@@ -58,14 +58,14 @@ Blockly.PHP['text_join'] = function(block) {
code[n] = Blockly.PHP.valueToCode(block, 'ADD' + n,
Blockly.PHP.ORDER_COMMA) || '\'\'';
}
code = 'implode(\',\'' + code.join(',') + ')';
code = 'implode(\'\', array(' + code.join(',') + '))';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
}
};
Blockly.PHP['text_append'] = function(block) {
// Append to a variable in place.
var varName = Blockly.PHP.getName(
var varName = Blockly.PHP.variableDB_.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
Blockly.PHP.ORDER_NONE) || '\'\'';
......@@ -94,7 +94,16 @@ Blockly.PHP['text_indexOf'] = function(block) {
Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
var argument1 = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
var code = operator + '(' + argument0 + ', ' + argument1 + ') + 1';
var code = operator + '(' + argument1 + ', ' + argument0 + ') + 1';
var functionName = Blockly.PHP.provideFunction_(
block.getFieldValue('END')=='FIRST'?'text_indexOf':'text_lastIndexOf',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($text, $search) {',
' $pos = ' + operator + '($text, $search);',
' return $pos===false?0:$pos+1;',
'}']);
code = functionName + '(' + argument1 + ', ' + argument0 + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
};
......@@ -156,6 +165,15 @@ Blockly.PHP['text_getSubstring'] = function(block) {
'text_get_substring',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($text, $where1, $at1, $where2, $at2) {',
' if ($where2 == \'FROM_START\') {',
' $at2--;',
' } else if ($where2 == \'FROM_END\') {',
' $at2 = $at2 - $at1;',
' } else if ($where2 == \'FIRST\') {',
' $at2 = 0;',
' } else if ($where2 == \'LAST\') {',
' $at2 = strlen($text);',
' } else { $at2 = 0; }',
' if ($where1 == \'FROM_START\') {',
' $at1--;',
' } else if ($where1 == \'FROM_END\') {',
......@@ -165,14 +183,6 @@ Blockly.PHP['text_getSubstring'] = function(block) {
' } else if ($where1 == \'LAST\') {',
' $at1 = strlen($text) - 1;',
' } else { $at1 = 0; }',
' if ($where2 == \'FROM_START\') {',
' } else if ($where2 == \'FROM_END\') {',
' $at2 = strlen($text) - $at2;',
' } else if ($where2 == \'FIRST\') {',
' $at2 = 0;',
' } else if ($where2 == \'LAST\') {',
' $at2 = strlen($text);',
' } else { $at2 = 0; }',
' return substr($text, $at1, $at2);',
'}']);
var code = functionName + '(' + text + ', \'' +
......@@ -195,7 +205,7 @@ Blockly.PHP['text_changeCase'] = function(block) {
} else if (block.getFieldValue('CASE')=='TITLECASE') {
var argument0 = Blockly.PHP.valueToCode(block, 'TEXT',
Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
code = 'ucwords(' + argument0 + ')';
code = 'ucwords(strtolower(' + argument0 + '))';
}
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
};
......
......@@ -30,16 +30,17 @@ goog.require('Blockly.PHP');
Blockly.PHP['variables_get'] = function(block) {
// Variable getter.
var code = '$' + block.getFieldValue('VAR');
return [code, Blockly.PHP.ORDER_ATOMIC];
// Variable getter.
var code = Blockly.PHP.variableDB_.getName(block.getFieldValue('VAR'),
Blockly.Variables.NAME_TYPE);
return [code, Blockly.PHP.ORDER_ATOMIC];
};
Blockly.PHP['variables_set'] = function(block) {
// Variable setter.
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_ASSIGNMENT) || '0';
var varName = Blockly.PHP.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
return varName + ' = ' + argument0 + ';\n';
};
// Variable setter.
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
Blockly.PHP.ORDER_ASSIGNMENT) || '0';
var varName = Blockly.PHP.variableDB_.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
return varName + ' = ' + argument0 + ';\n';
};
\ No newline at end of file
......@@ -24,6 +24,18 @@
<script src="../../generators/python/colour.js"></script>
<script src="../../generators/python/variables.js"></script>
<script src="../../generators/python/procedures.js"></script>
<script src="../../generators/php.js"></script>
<script src="unittest_php.js"></script>
<script src="../../generators/php/logic.js"></script>
<script src="../../generators/php/loops.js"></script>
<script src="../../generators/php/math.js"></script>
<script src="../../generators/php/text.js"></script>
<script src="../../generators/php/lists.js"></script>
<script src="../../generators/php/colour.js"></script>
<script src="../../generators/php/variables.js"></script>
<script src="../../generators/php/procedures.js"></script>
<script src="../../generators/dart.js"></script>
<script src="unittest_dart.js"></script>
<script src="../../generators/dart/logic.js"></script>
......@@ -126,6 +138,11 @@ function toPython() {
setOutput(code);
}
function toPhp() {
var code = Blockly.PHP.workspaceToCode(workspace);
setOutput(code);
}
function toDart() {
var code = Blockly.Dart.workspaceToCode(workspace);
setOutput(code);
......@@ -406,6 +423,7 @@ h1 {
<input type="button" value="XML" onclick="toXml()">
<input type="button" value="JavaScript" onclick="toJavaScript()">
<input type="button" value="Python" onclick="toPython()">
<input type="button" value="PHP" onclick="toPhp()">
<input type="button" value="Dart" onclick="toDart()">
</p>
</td></tr><tr><td height="99%">
......
/**
* @license
* Visual Blocks Language
*
* Copyright 2015 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Generating PHP for unit test blocks.
* @author daarond@gmail.com (Neil Fraser)
*/
'use strict';
Blockly.PHP['unittest_main'] = function(block) {
// Container for unit tests.
var resultsVar = Blockly.PHP.variableDB_.getName('unittestResults',
Blockly.Variables.NAME_TYPE);
var functionName = Blockly.PHP.provideFunction_(
'unittest_report',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '() {',
'global ' + resultsVar + ';',
' // Create test report.',
' $report = array();',
' $summary = array();',
' $fails = 0;',
' for ($x = 0; $x < count(' + resultsVar + '); $x++) {',
' if (' + resultsVar + '[$x][0]) {',
' array_push($summary, ".");',
' } else {',
' array_push($summary, "F");',
' $fails++;',
' array_push($report,"");',
' array_push($report, "FAIL: " . ' + resultsVar + '[$x][2]);',
' array_push($report, ' + resultsVar + '[$x][1]);',
' }',
' }',
' array_unshift($report, implode("",$summary));',
' array_push($report, "");',
' array_push($report, "Number of tests run: " . count(' + resultsVar + '));',
' array_push($report, "");',
' if ($fails) {',
' array_push($report, "FAILED (failures=" . $fails + ")");',
' } else {',
' array_push($report, "OK");',
' }',
' return implode("\\n", $report);',
'}']);
// Setup global to hold test results.
var code = resultsVar + ' = array();\n';
// Run tests (unindented).
code += Blockly.PHP.statementToCode(block, 'DO')
.replace(/^ /, '').replace(/\n /g, '\n');
var reportVar = Blockly.PHP.variableDB_.getDistinctName(
'report', Blockly.Variables.NAME_TYPE);
code += reportVar + ' = ' + functionName + '();\n';
// Destroy results.
code += resultsVar + ' = null;\n';
// Send the report to the console (that's where errors will go anyway).
code += 'print(' + reportVar + ');\n';
return code;
};
Blockly.PHP['unittest_main'].defineAssert_ = function(block) {
var resultsVar = Blockly.PHP.variableDB_.getName('unittestResults',
Blockly.Variables.NAME_TYPE);
var functionName = Blockly.PHP.provideFunction_(
'assertEquals',
[ ' function equals($a, $b) {',
' if ($a === $b) {',
' return true;',
' } else if ((is_numeric($a)) && (is_numeric($b)) &&',
' (round($a,15) == round($b,15))) {',
' return true;',
' } else if (is_array($a) && is_array($b)) {',
' if (count($a) != count($b)) {',
' return false;',
' }',
' for ($i = 0; $i < count($a); $i++) {',
' if (!equals($a[$i], $b[$i])) {',
' return false;',
' }',
' }',
' return true;',
' }',
' return false;',
' }',
'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($actual, $expected, $message) {',
'global ' + resultsVar + ';',
' // Asserts that a value equals another value.',
' if (!is_array(' + resultsVar + ')) {',
' throw new Exception("Orphaned assert: " . $message);',
' }',
' if (equals($actual, $expected)) {',
' array_push(' + resultsVar + ', [true, "OK", $message]);',
' } else {',
' array_push(' + resultsVar + ', [false, ' +
'"Expected: " . $expected . "\\nActual: " . $actual, $message]);',
' }',
'}']);
return functionName;
};
Blockly.PHP['unittest_assertequals'] = function(block) {
// Asserts that a value equals another value.
var message = Blockly.PHP.quote_(block.getFieldValue('MESSAGE'));
var actual = Blockly.PHP.valueToCode(block, 'ACTUAL',
Blockly.PHP.ORDER_COMMA) || 'null';
var expected = Blockly.PHP.valueToCode(block, 'EXPECTED',
Blockly.PHP.ORDER_COMMA) || 'null';
return Blockly.PHP['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ');\n';
};
Blockly.PHP['unittest_assertvalue'] = function(block) {
// Asserts that a value is true, false, or null.
var message = Blockly.PHP.quote_(block.getFieldValue('MESSAGE'));
var actual = Blockly.PHP.valueToCode(block, 'ACTUAL',
Blockly.PHP.ORDER_COMMA) || 'null';
var expected = block.getFieldValue('EXPECTED');
if (expected == 'TRUE') {
expected = 'true';
} else if (expected == 'FALSE') {
expected = 'false';
} else if (expected == 'NULL') {
expected = 'null';
}
return Blockly.PHP['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ');\n';
};
Blockly.PHP['unittest_fail'] = function(block) {
// Always assert an error.
var resultsVar = Blockly.PHP.variableDB_.getName('unittestResults',
Blockly.Variables.NAME_TYPE);
var message = Blockly.PHP.quote_(block.getFieldValue('MESSAGE'));
var functionName = Blockly.PHP.provideFunction_(
'unittest_fail',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($message) {',
'global ' + resultsVar + ';',
' // Always assert an error.',
' if (!' + resultsVar + ') {',
' throw new Exception("Orphaned assert fail: " . $message);',
' }',
' array_push(' + resultsVar + ', [false, "Fail.", $message]);',
'}']);
return functionName + '(' + message + ');\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