Files
blockly/generators/javascript/lists.js

324 lines
12 KiB
JavaScript

/**
* Visual Blocks Language
*
* Copyright 2012 Google Inc.
* http://blockly.googlecode.com/
*
* 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 JavaScript for list blocks.
* @author fraser@google.com (Neil Fraser)
*/
'use strict';
goog.provide('Blockly.JavaScript.lists');
goog.require('Blockly.JavaScript');
Blockly.JavaScript['lists_create_empty'] = function(block) {
// Create an empty list.
return ['[]', Blockly.JavaScript.ORDER_ATOMIC];
};
Blockly.JavaScript['lists_create_with'] = function(block) {
// Create a list with any number of elements of any type.
var code = new Array(block.itemCount_);
for (var n = 0; n < block.itemCount_; n++) {
code[n] = Blockly.JavaScript.valueToCode(block, 'ADD' + n,
Blockly.JavaScript.ORDER_COMMA) || 'null';
}
code = '[' + code.join(', ') + ']';
return [code, Blockly.JavaScript.ORDER_ATOMIC];
};
Blockly.JavaScript['lists_repeat'] = function(block) {
// Create a list with one element repeated.
if (!Blockly.JavaScript.definitions_['lists_repeat']) {
// Function copied from Closure's goog.array.repeat.
var functionName = Blockly.JavaScript.variableDB_.getDistinctName(
'lists_repeat', Blockly.Generator.NAME_TYPE);
Blockly.JavaScript.lists_repeat.repeat = functionName;
var func = [];
func.push('function ' + functionName + '(value, n) {');
func.push(' var array = [];');
func.push(' for (var i = 0; i < n; i++) {');
func.push(' array[i] = value;');
func.push(' }');
func.push(' return array;');
func.push('}');
Blockly.JavaScript.definitions_['lists_repeat'] = func.join('\n');
}
var argument0 = Blockly.JavaScript.valueToCode(block, 'ITEM',
Blockly.JavaScript.ORDER_COMMA) || 'null';
var argument1 = Blockly.JavaScript.valueToCode(block, 'NUM',
Blockly.JavaScript.ORDER_COMMA) || '0';
var code = Blockly.JavaScript.lists_repeat.repeat +
'(' + argument0 + ', ' + argument1 + ')';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};
Blockly.JavaScript['lists_length'] = function(block) {
// List length.
var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE',
Blockly.JavaScript.ORDER_FUNCTION_CALL) || '\'\'';
return [argument0 + '.length', Blockly.JavaScript.ORDER_MEMBER];
};
Blockly.JavaScript['lists_isEmpty'] = function(block) {
// Is the list empty?
var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE',
Blockly.JavaScript.ORDER_MEMBER) || '[]';
return ['!' + argument0 + '.length', Blockly.JavaScript.ORDER_LOGICAL_NOT];
};
Blockly.JavaScript['lists_indexOf'] = function(block) {
// Find an item in the list.
var operator = block.getTitleValue('END') == 'FIRST' ?
'indexOf' : 'lastIndexOf';
var argument0 = Blockly.JavaScript.valueToCode(block, 'FIND',
Blockly.JavaScript.ORDER_NONE) || '\'\'';
var argument1 = Blockly.JavaScript.valueToCode(block, 'VALUE',
Blockly.JavaScript.ORDER_MEMBER) || '[]';
var code = argument1 + '.' + operator + '(' + argument0 + ') + 1';
return [code, Blockly.JavaScript.ORDER_MEMBER];
};
Blockly.JavaScript['lists_getIndex'] = function(block) {
// Get element at index.
// Note: Until January 2013 this block did not have MODE or WHERE inputs.
var mode = block.getTitleValue('MODE') || 'GET';
var where = block.getTitleValue('WHERE') || 'FROM_START';
var at = Blockly.JavaScript.valueToCode(block, 'AT',
Blockly.JavaScript.ORDER_UNARY_NEGATION) || '1';
var list = Blockly.JavaScript.valueToCode(block, 'VALUE',
Blockly.JavaScript.ORDER_MEMBER) || '[]';
if (where == 'FIRST') {
if (mode == 'GET') {
var code = list + '[0]';
return [code, Blockly.JavaScript.ORDER_MEMBER];
} else if (mode == 'GET_REMOVE') {
var code = list + '.shift()';
return [code, Blockly.JavaScript.ORDER_MEMBER];
} else if (mode == 'REMOVE') {
return list + '.shift();\n';
}
} else if (where == 'LAST') {
if (mode == 'GET') {
var code = list + '.slice(-1)[0]';
return [code, Blockly.JavaScript.ORDER_MEMBER];
} else if (mode == 'GET_REMOVE') {
var code = list + '.pop()';
return [code, Blockly.JavaScript.ORDER_MEMBER];
} else if (mode == 'REMOVE') {
return list + '.pop();\n';
}
} else if (where == 'FROM_START') {
// Blockly uses one-based indicies.
if (Blockly.isNumber(at)) {
// If the index is a naked number, decrement it right now.
at = parseFloat(at) - 1;
} else {
// If the index is dynamic, decrement it in code.
at += ' - 1';
}
if (mode == 'GET') {
var code = list + '[' + at + ']';
return [code, Blockly.JavaScript.ORDER_MEMBER];
} else if (mode == 'GET_REMOVE') {
var code = list + '.splice(' + at + ', 1)[0]';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
} else if (mode == 'REMOVE') {
return list + '.splice(' + at + ', 1);\n';
}
} else if (where == 'FROM_END') {
if (mode == 'GET') {
var code = list + '.slice(-' + at + ')[0]';
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
} else if (mode == 'GET_REMOVE' || mode == 'REMOVE') {
if (!Blockly.JavaScript.definitions_['lists_remove_from_end']) {
var functionName = Blockly.JavaScript.variableDB_.getDistinctName(
'lists_remove_from_end', Blockly.Generator.NAME_TYPE);
Blockly.JavaScript.lists_getIndex.lists_remove_from_end = functionName;
var func = [];
func.push('function ' + functionName + '(list, x) {');
func.push(' x = list.length - x;');
func.push(' return list.splice(x, 1)[0];');
func.push('}');
Blockly.JavaScript.definitions_['lists_remove_from_end'] =
func.join('\n');
}
code = Blockly.JavaScript.lists_getIndex.lists_remove_from_end +
'(' + list + ', ' + at + ')';
if (mode == 'GET_REMOVE') {
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
} else if (mode == 'REMOVE') {
return code + ';\n';
}
}
} else if (where == 'RANDOM') {
if (!Blockly.JavaScript.definitions_['lists_get_random_item']) {
var functionName = Blockly.JavaScript.variableDB_.getDistinctName(
'lists_get_random_item', Blockly.Generator.NAME_TYPE);
Blockly.JavaScript.lists_getIndex.random = functionName;
var func = [];
func.push('function ' + functionName + '(list, remove) {');
func.push(' var x = Math.floor(Math.random() * list.length);');
func.push(' if (remove) {');
func.push(' return list.splice(x, 1)[0];');
func.push(' } else {');
func.push(' return list[x];');
func.push(' }');
func.push('}');
Blockly.JavaScript.definitions_['lists_get_random_item'] =
func.join('\n');
}
code = Blockly.JavaScript.lists_getIndex.random +
'(' + list + ', ' + (mode != 'GET') + ')';
if (mode == 'GET' || mode == 'GET_REMOVE') {
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
} else if (mode == 'REMOVE') {
return code + ';\n';
}
}
throw 'Unhandled combination (lists_getIndex).';
};
Blockly.JavaScript['lists_setIndex'] = function(block) {
// Set element at index.
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
var list = Blockly.JavaScript.valueToCode(block, 'LIST',
Blockly.JavaScript.ORDER_MEMBER) || '[]';
var mode = block.getTitleValue('MODE') || 'GET';
var where = block.getTitleValue('WHERE') || 'FROM_START';
var at = Blockly.JavaScript.valueToCode(block, 'AT',
Blockly.JavaScript.ORDER_NONE) || '1';
var value = Blockly.JavaScript.valueToCode(block, 'TO',
Blockly.JavaScript.ORDER_ASSIGNMENT) || 'null';
// Cache non-trivial values to variables to prevent repeated look-ups.
// Closure, which accesses and modifies 'list'.
function cacheList() {
if (list.match(/^\w+$/)) {
return '';
}
var listVar = Blockly.JavaScript.variableDB_.getDistinctName(
'tmp_list', Blockly.Variables.NAME_TYPE);
var code = 'var ' + listVar + ' = ' + list + ';\n';
list = listVar;
return code;
}
if (where == 'FIRST') {
if (mode == 'SET') {
return list + '[0] = ' + value + ';\n';
} else if (mode == 'INSERT') {
return list + '.unshift(' + value + ');\n';
}
} else if (where == 'LAST') {
if (mode == 'SET') {
var code = cacheList();
code += list + '[' + list + '.length - 1] = ' + value + ';\n';
return code;
} else if (mode == 'INSERT') {
return list + '.push(' + value + ');\n';
}
} else if (where == 'FROM_START') {
// Blockly uses one-based indicies.
if (Blockly.isNumber(at)) {
// If the index is a naked number, decrement it right now.
at = parseFloat(at) - 1;
} else {
// If the index is dynamic, decrement it in code.
at += ' - 1';
}
if (mode == 'SET') {
return list + '[' + at + '] = ' + value + ';\n';
} else if (mode == 'INSERT') {
return list + '.splice(' + at + ', 0, ' + value + ');\n';
}
} else if (where == 'FROM_END') {
var code = cacheList();
if (mode == 'SET') {
code += list + '[' + list + '.length - ' + at + '] = ' + value + ';\n';
return code;
} else if (mode == 'INSERT') {
code += list + '.splice(' + list + '.length - ' + at + ', 0, ' + value +
');\n';
return code;
}
} else if (where == 'RANDOM') {
var code = cacheList();
var xVar = Blockly.JavaScript.variableDB_.getDistinctName(
'tmp_x', Blockly.Variables.NAME_TYPE);
code += 'var ' + xVar + ' = Math.floor(Math.random() * ' + list +
'.length);\n';
if (mode == 'SET') {
code += list + '[' + xVar + '] = ' + value + ';\n';
return code;
} else if (mode == 'INSERT') {
code += list + '.splice(' + xVar + ', 0, ' + value + ');\n';
return code;
}
}
throw 'Unhandled combination (lists_setIndex).';
};
Blockly.JavaScript['lists_getSublist'] = function(block) {
// Get sublist.
var list = Blockly.JavaScript.valueToCode(block, 'LIST',
Blockly.JavaScript.ORDER_MEMBER) || '[]';
var where1 = block.getTitleValue('WHERE1');
var where2 = block.getTitleValue('WHERE2');
var at1 = Blockly.JavaScript.valueToCode(block, 'AT1',
Blockly.JavaScript.ORDER_NONE) || '1';
var at2 = Blockly.JavaScript.valueToCode(block, 'AT2',
Blockly.JavaScript.ORDER_NONE) || '1';
if (where1 == 'FIRST' && where2 == 'LAST') {
var code = list + '.concat()';
} else {
if (!Blockly.JavaScript.definitions_['lists_get_sublist']) {
var functionName = Blockly.JavaScript.variableDB_.getDistinctName(
'lists_get_sublist', Blockly.Generator.NAME_TYPE);
Blockly.JavaScript.lists_getSublist.func = functionName;
var func = [];
func.push('function ' + functionName +
'(list, where1, at1, where2, at2) {');
func.push(' function getAt(where, at) {');
func.push(' if (where == \'FROM_START\') {');
func.push(' at--;');
func.push(' } else if (where == \'FROM_END\') {');
func.push(' at = list.length - at;');
func.push(' } else if (where == \'FIRST\') {');
func.push(' at = 0;');
func.push(' } else if (where == \'LAST\') {');
func.push(' at = list.length - 1;');
func.push(' } else {');
func.push(' throw \'Unhandled option (lists_getSublist).\';');
func.push(' }');
func.push(' return at;');
func.push(' }');
func.push(' at1 = getAt(where1, at1);');
func.push(' at2 = getAt(where2, at2) + 1;');
func.push(' return list.slice(at1, at2);');
func.push('}');
Blockly.JavaScript.definitions_['lists_get_sublist'] =
func.join('\n');
}
var code = Blockly.JavaScript.lists_getSublist.func + '(' + list + ', \'' +
where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
}
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
};