changes from Neil's comments

This commit is contained in:
daarond
2015-05-21 11:26:32 -05:00
parent 6246631240
commit 58b41c9c46
2 changed files with 49 additions and 15 deletions

View File

@@ -178,14 +178,19 @@ Blockly.PHP['lists_getIndex'] = function(block) {
}
} else if (where == 'RANDOM') {
if (mode == 'GET'){
code = list + '[rand(0,count(' + list + ')-1)]';
var functionName = Blockly.PHP.provideFunction_(
'lists_get_random_item',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($list) {',
' return $list[rand(0,count($list)-1)];',
'}']);
code = functionName + '(' + list + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
} else if (mode == 'GET_REMOVE'){
var functionName = Blockly.PHP.provideFunction_(
'lists_get_remove_random_item',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'() {',
' global ' + list + ';',
'(&$list) {',
' $x = rand(0,count($list)-1);',
' unset($list[$x]);',
' return array_values($list);',
@@ -193,8 +198,13 @@ Blockly.PHP['lists_getIndex'] = function(block) {
code = functionName + '(' + list + ')';
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
} else if (mode == 'REMOVE') {
code = 'unset(' + list + '[rand(0,count(' + list + ')-1)])';
return code + ';\n';
var functionName = Blockly.PHP.provideFunction_(
'lists_remove_random_item',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'(&$list) {',
' unset($list[rand(0,count($list)-1)]);',
'}']);
return functionName + '(' + list + ');\n';
}
}
throw 'Unhandled combination (lists_getIndex).';
@@ -231,9 +241,13 @@ Blockly.PHP['lists_setIndex'] = function(block) {
}
} else if (where == 'LAST') {
if (mode == 'SET') {
var code = cacheList();
code += list + '[count(' + list + ') - 1] = ' + value + ';\n';
return code;
var functionName = Blockly.PHP.provideFunction_(
'lists_set_last_item',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'(&$list, $value) {',
' $list[count($list) - 1] = $value;',
'}']);
return functionName + '(' + list + ', ' + value + ');\n';
} else if (mode == 'INSERT') {
return 'array_push(' + list + ', ' + value + ');\n';
}
@@ -252,13 +266,22 @@ Blockly.PHP['lists_setIndex'] = function(block) {
return 'array_splice(' + list + ', ' + at + ', 0, ' + value + ');\n';
}
} else if (where == 'FROM_END') {
var code = cacheList();
if (mode == 'SET') {
code += list + '[count(' + list + ') - ' + at + '] = ' + value + ';\n';
return code;
var functionName = Blockly.PHP.provideFunction_(
'lists_set_from_end',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'(&$list, $at, $value) {',
' $list[count($list) - $at] = $value;',
'}']);
return functionName + '(' + list + ', ' + at + ', ' + value + ');\n';
} else if (mode == 'INSERT') {
code += 'array_splice(' + list + ', count(' + list + ') - ' + at + ', 0, ' + value + ');\n';
return code;
var functionName = Blockly.PHP.provideFunction_(
'lists_insert_from_end',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'(&$list, $at, $value) {',
' return array_splice($list, count($list) - $at, 0, $value);',
'}']);
return functionName + '(' + list + ', ' + at + ', ' + value + ');\n';
}
} else if (where == 'RANDOM') {
var code = cacheList();

View File

@@ -49,6 +49,7 @@
<script src="../blocks/colour.js"></script>
<script src="../blocks/variables.js"></script>
<script src="../blocks/procedures.js"></script>
<script src="../blocks/mosio.js"></script>
<!-- Load the Google Drive SDK Realtime libraries. -->
<script src="https://apis.google.com/js/api.js"></script>
<script>
@@ -423,6 +424,16 @@ h1 {
<sep></sep>
<category name="Variables" custom="VARIABLE"></category>
<category name="Functions" custom="PROCEDURE"></category>
<category name="Mosio">
<block type="phone_tts"></block>
<block type="phone_playfile"></block>
<block type="email_send"></block>
<block type="sms_send"></block>
<block type="sms_ask">
<field name="expected_type">String</field>
<field name="failure_count">3</field>
</block>
</category>
</xml>
<h1>Blockly Playground</h1>
@@ -445,10 +456,10 @@ h1 {
<br>
<input type="button" value="To JavaScript" onclick="toCode('JavaScript')">
&nbsp;
<input type="button" value="To PHP" onclick="toCode('PHP')">
&nbsp;
<input type="button" value="To Python" onclick="toCode('Python')">
&nbsp;
<input type="button" value="To PHP" onclick="toCode('PHP')">
&nbsp;
<input type="button" value="To Dart" onclick="toCode('Dart')">
<br>
<textarea id="importExport" style="width: 26%; height: 12em"></textarea>