mirror of
https://github.com/google/blockly.git
synced 2026-01-04 23:50:12 +01:00
* fix(build): Restore erroneously-deleted filter function This was deleted in PR #7406 as it was mainly being used to filter core/ vs. test/mocha/ deps into separate deps files - but it turns out also to be used for filtering error messages too. Oops. * refactor(tests): Migrate advanced compilation test to ES Modules * refactor(build): Migrate main.js to TypeScript This turns out to be pretty straight forward, even if it would cause crashing if one actually tried to import this module instead of just feeding it to Closure Compiler. * chore(build): Remove goog.declareModuleId calls Replace goog.declareModuleId calls with a comment recording the former module ID for posterity (or at least until we decide how to reformat the renamings file. * chore(tests): Delete closure/goog/* For the moment we still need something to serve as base.js for the benefit of closure-make-deps, so we keep a vestigial base.js around, containing only the @provideGoog declaration. * refactor(build): Remove vestigial base.js By changing slightly the command line arguments to closure-make-deps and closure-calculate-chunks the need to have any base.js is eliminated. * chore: Typo fix for PR #7415
329 lines
9.9 KiB
JavaScript
329 lines
9.9 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2016 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Generating Lua for text blocks.
|
|
*/
|
|
|
|
// Former goog.module ID: Blockly.Lua.texts
|
|
|
|
import {NameType} from '../../core/names.js';
|
|
import {Order} from './lua_generator.js';
|
|
|
|
|
|
export function text(block, generator) {
|
|
// Text value.
|
|
const code = generator.quote_(block.getFieldValue('TEXT'));
|
|
return [code, Order.ATOMIC];
|
|
};
|
|
|
|
export function text_multiline(block, generator) {
|
|
// Text value.
|
|
const code = generator.multiline_quote_(block.getFieldValue('TEXT'));
|
|
const order =
|
|
code.indexOf('..') !== -1 ? Order.CONCATENATION : Order.ATOMIC;
|
|
return [code, order];
|
|
};
|
|
|
|
export function text_join(block, generator) {
|
|
// Create a string made up of any number of elements of any type.
|
|
if (block.itemCount_ === 0) {
|
|
return ["''", Order.ATOMIC];
|
|
} else if (block.itemCount_ === 1) {
|
|
const element = generator.valueToCode(block, 'ADD0', Order.NONE) || "''";
|
|
const code = 'tostring(' + element + ')';
|
|
return [code, Order.HIGH];
|
|
} else if (block.itemCount_ === 2) {
|
|
const element0 =
|
|
generator.valueToCode(block, 'ADD0', Order.CONCATENATION) || "''";
|
|
const element1 =
|
|
generator.valueToCode(block, 'ADD1', Order.CONCATENATION) || "''";
|
|
const code = element0 + ' .. ' + element1;
|
|
return [code, Order.CONCATENATION];
|
|
} else {
|
|
const elements = [];
|
|
for (let i = 0; i < block.itemCount_; i++) {
|
|
elements[i] =
|
|
generator.valueToCode(block, 'ADD' + i, Order.NONE) || "''";
|
|
}
|
|
const code = 'table.concat({' + elements.join(', ') + '})';
|
|
return [code, Order.HIGH];
|
|
}
|
|
};
|
|
|
|
export function text_append(block, generator) {
|
|
// Append to a variable in place.
|
|
const varName =
|
|
generator.nameDB_.getName(
|
|
block.getFieldValue('VAR'), NameType.VARIABLE);
|
|
const value =
|
|
generator.valueToCode(block, 'TEXT', Order.CONCATENATION) || "''";
|
|
return varName + ' = ' + varName + ' .. ' + value + '\n';
|
|
};
|
|
|
|
export function text_length(block, generator) {
|
|
// String or array length.
|
|
const text = generator.valueToCode(block, 'VALUE', Order.UNARY) || "''";
|
|
return ['#' + text, Order.UNARY];
|
|
};
|
|
|
|
export function text_isEmpty(block, generator) {
|
|
// Is the string null or array empty?
|
|
const text = generator.valueToCode(block, 'VALUE', Order.UNARY) || "''";
|
|
return ['#' + text + ' == 0', Order.RELATIONAL];
|
|
};
|
|
|
|
export function text_indexOf(block, generator) {
|
|
// Search the text for a substring.
|
|
const substring = generator.valueToCode(block, 'FIND', Order.NONE) || "''";
|
|
const text = generator.valueToCode(block, 'VALUE', Order.NONE) || "''";
|
|
let functionName;
|
|
if (block.getFieldValue('END') === 'FIRST') {
|
|
functionName = generator.provideFunction_('firstIndexOf', `
|
|
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(str, substr)
|
|
local i = string.find(str, substr, 1, true)
|
|
if i == nil then
|
|
return 0
|
|
end
|
|
return i
|
|
end
|
|
`);
|
|
} else {
|
|
functionName = generator.provideFunction_('lastIndexOf', `
|
|
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(str, substr)
|
|
local i = string.find(string.reverse(str), string.reverse(substr), 1, true)
|
|
if i then
|
|
return #str + 2 - i - #substr
|
|
end
|
|
return 0
|
|
end
|
|
`);
|
|
}
|
|
const code = functionName + '(' + text + ', ' + substring + ')';
|
|
return [code, Order.HIGH];
|
|
};
|
|
|
|
export function text_charAt(block, generator) {
|
|
// Get letter at index.
|
|
// Note: Until January 2013 this block did not have the WHERE input.
|
|
const where = block.getFieldValue('WHERE') || 'FROM_START';
|
|
const atOrder = (where === 'FROM_END') ? Order.UNARY : Order.NONE;
|
|
const at = generator.valueToCode(block, 'AT', atOrder) || '1';
|
|
const text = generator.valueToCode(block, 'VALUE', Order.NONE) || "''";
|
|
let code;
|
|
if (where === 'RANDOM') {
|
|
const functionName = generator.provideFunction_('text_random_letter', `
|
|
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(str)
|
|
local index = math.random(string.len(str))
|
|
return string.sub(str, index, index)
|
|
end
|
|
`);
|
|
code = functionName + '(' + text + ')';
|
|
} else {
|
|
let start;
|
|
if (where === 'FIRST') {
|
|
start = '1';
|
|
} else if (where === 'LAST') {
|
|
start = '-1';
|
|
} else {
|
|
if (where === 'FROM_START') {
|
|
start = at;
|
|
} else if (where === 'FROM_END') {
|
|
start = '-' + at;
|
|
} else {
|
|
throw Error('Unhandled option (text_charAt).');
|
|
}
|
|
}
|
|
if (start.match(/^-?\w*$/)) {
|
|
code = 'string.sub(' + text + ', ' + start + ', ' + start + ')';
|
|
} else {
|
|
// use function to avoid reevaluation
|
|
const functionName = generator.provideFunction_('text_char_at', `
|
|
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(str, index)
|
|
return string.sub(str, index, index)
|
|
end
|
|
`);
|
|
code = functionName + '(' + text + ', ' + start + ')';
|
|
}
|
|
}
|
|
return [code, Order.HIGH];
|
|
};
|
|
|
|
export function text_getSubstring(block, generator) {
|
|
// Get substring.
|
|
const text = generator.valueToCode(block, 'STRING', Order.NONE) || "''";
|
|
|
|
// Get start index.
|
|
const where1 = block.getFieldValue('WHERE1');
|
|
const at1Order = (where1 === 'FROM_END') ? Order.UNARY : Order.NONE;
|
|
const at1 = generator.valueToCode(block, 'AT1', at1Order) || '1';
|
|
let start;
|
|
if (where1 === 'FIRST') {
|
|
start = 1;
|
|
} else if (where1 === 'FROM_START') {
|
|
start = at1;
|
|
} else if (where1 === 'FROM_END') {
|
|
start = '-' + at1;
|
|
} else {
|
|
throw Error('Unhandled option (text_getSubstring)');
|
|
}
|
|
|
|
// Get end index.
|
|
const where2 = block.getFieldValue('WHERE2');
|
|
const at2Order = (where2 === 'FROM_END') ? Order.UNARY : Order.NONE;
|
|
const at2 = generator.valueToCode(block, 'AT2', at2Order) || '1';
|
|
let end;
|
|
if (where2 === 'LAST') {
|
|
end = -1;
|
|
} else if (where2 === 'FROM_START') {
|
|
end = at2;
|
|
} else if (where2 === 'FROM_END') {
|
|
end = '-' + at2;
|
|
} else {
|
|
throw Error('Unhandled option (text_getSubstring)');
|
|
}
|
|
const code = 'string.sub(' + text + ', ' + start + ', ' + end + ')';
|
|
return [code, Order.HIGH];
|
|
};
|
|
|
|
export function text_changeCase(block, generator) {
|
|
// Change capitalization.
|
|
const operator = block.getFieldValue('CASE');
|
|
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
|
let functionName;
|
|
if (operator === 'UPPERCASE') {
|
|
functionName = 'string.upper';
|
|
} else if (operator === 'LOWERCASE') {
|
|
functionName = 'string.lower';
|
|
} else if (operator === 'TITLECASE') {
|
|
// There are shorter versions at
|
|
// http://lua-users.org/wiki/SciteTitleCase
|
|
// that do not preserve whitespace.
|
|
functionName = generator.provideFunction_('text_titlecase', `
|
|
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(str)
|
|
local buf = {}
|
|
local inWord = false
|
|
for i = 1, #str do
|
|
local c = string.sub(str, i, i)
|
|
if inWord then
|
|
table.insert(buf, string.lower(c))
|
|
if string.find(c, "%s") then
|
|
inWord = false
|
|
end
|
|
else
|
|
table.insert(buf, string.upper(c))
|
|
inWord = true
|
|
end
|
|
end
|
|
return table.concat(buf)
|
|
end
|
|
`);
|
|
}
|
|
const code = functionName + '(' + text + ')';
|
|
return [code, Order.HIGH];
|
|
};
|
|
|
|
export function text_trim(block, generator) {
|
|
// Trim spaces.
|
|
const OPERATORS = {LEFT: '^%s*(,-)', RIGHT: '(.-)%s*$', BOTH: '^%s*(.-)%s*$'};
|
|
const operator = OPERATORS[block.getFieldValue('MODE')];
|
|
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
|
const code = 'string.gsub(' + text + ', "' + operator + '", "%1")';
|
|
return [code, Order.HIGH];
|
|
};
|
|
|
|
export function text_print(block, generator) {
|
|
// Print statement.
|
|
const msg = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
|
return 'print(' + msg + ')\n';
|
|
};
|
|
|
|
export function text_prompt_ext(block, generator) {
|
|
// Prompt function.
|
|
let msg;
|
|
if (block.getField('TEXT')) {
|
|
// Internal message.
|
|
msg = generator.quote_(block.getFieldValue('TEXT'));
|
|
} else {
|
|
// External message.
|
|
msg = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
|
}
|
|
|
|
const functionName = generator.provideFunction_('text_prompt', `
|
|
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(msg)
|
|
io.write(msg)
|
|
io.flush()
|
|
return io.read()
|
|
end
|
|
`);
|
|
let code = functionName + '(' + msg + ')';
|
|
|
|
const toNumber = block.getFieldValue('TYPE') === 'NUMBER';
|
|
if (toNumber) {
|
|
code = 'tonumber(' + code + ', 10)';
|
|
}
|
|
return [code, Order.HIGH];
|
|
};
|
|
|
|
export const text_prompt = text_prompt_ext;
|
|
|
|
export function text_count(block, generator) {
|
|
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
|
const sub = generator.valueToCode(block, 'SUB', Order.NONE) || "''";
|
|
const functionName = generator.provideFunction_('text_count', `
|
|
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(haystack, needle)
|
|
if #needle == 0 then
|
|
return #haystack + 1
|
|
end
|
|
local i = 1
|
|
local count = 0
|
|
while true do
|
|
i = string.find(haystack, needle, i, true)
|
|
if i == nil then
|
|
break
|
|
end
|
|
count = count + 1
|
|
i = i + #needle
|
|
end
|
|
return count
|
|
end
|
|
`);
|
|
const code = functionName + '(' + text + ', ' + sub + ')';
|
|
return [code, Order.HIGH];
|
|
};
|
|
|
|
export function text_replace(block, generator) {
|
|
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
|
const from = generator.valueToCode(block, 'FROM', Order.NONE) || "''";
|
|
const to = generator.valueToCode(block, 'TO', Order.NONE) || "''";
|
|
const functionName = generator.provideFunction_('text_replace', `
|
|
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(haystack, needle, replacement)
|
|
local buf = {}
|
|
local i = 1
|
|
while i <= #haystack do
|
|
if string.sub(haystack, i, i + #needle - 1) == needle then
|
|
for j = 1, #replacement do
|
|
table.insert(buf, string.sub(replacement, j, j))
|
|
end
|
|
i = i + #needle
|
|
else
|
|
table.insert(buf, string.sub(haystack, i, i))
|
|
i = i + 1
|
|
end
|
|
end
|
|
return table.concat(buf)
|
|
end
|
|
`);
|
|
const code = functionName + '(' + text + ', ' + from + ', ' + to + ')';
|
|
return [code, Order.HIGH];
|
|
};
|
|
|
|
export function text_reverse(block, generator) {
|
|
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
|
const code = 'string.reverse(' + text + ')';
|
|
return [code, Order.HIGH];
|
|
};
|