refactor(generators)!: Pass this CodeGenerator to individual generator functions (#7168)

* feat(generators): Pass this CodeGenerator to generator functions

  This implements option 1A of proposal 1 of #7086.

  This commit is not by itself a breaking change, except in the unlikely event that
  developers' custom generator functions take an (optional) second argument of a
  dfferent type.

* feat(generators): Accept generator argument in block functions

  Accept a CodeGenerator instance as parameter two of every
  per-block-type generator function.

* fix(generators): Pass generator when calling other generator functions

  Make sure to pass generator to any other block functions that are
  called recursively.

* refactor(generators)!: Use generator argument in generator functions

  Refactor per-block-type generator functions to use the provided
  generator argument to make recursive calls, rather than depending
  on the closed-over <lang>Generator instance.

  This allows generator functions to be moved between CodeGenerator
  instances (of the same language, at least).

  This commit was created by search-and-replace and addresses most
  but not all recursive references; remaining uses will require
  manual attention and will be dealt with in a following commit.

  BREAKING CHANGE: This commit makes the generator functions we provide
  dependent on the new generator parameter.  Although
  CodeGenerator.prototype.blockToCode has been modified to supply this,
  so this change will not affect most developers, this change will be a
  breaking change where developers make direct calls to these generator
  functions without supplying the generator parameter.  See previous
  commit for an example of the update required.

* refactor(generators): Manual fix for remaining uses of langGenerator

  Manually replace remaining uses of <lang>Generator in block
  generator functions.

* fix(generators): Delete duplicate procedures_callnoreturn generator

  For some reason the generator function for procedures_callnoreturn
  appears twice in generators/javascript/procedures.js.  Delete the
  first copy (since the second one overwrote it anyway).

* chore(generators): Format
This commit is contained in:
Christopher Allen
2023-06-14 23:25:36 +01:00
committed by GitHub
parent 12b91ae49c
commit a3458871db
44 changed files with 1474 additions and 1477 deletions

View File

@@ -15,76 +15,76 @@ import {NameType} from '../../core/names.js';
import {luaGenerator, Order} from '../lua.js';
luaGenerator.forBlock['text'] = function(block) {
luaGenerator.forBlock['text'] = function(block, generator) {
// Text value.
const code = luaGenerator.quote_(block.getFieldValue('TEXT'));
const code = generator.quote_(block.getFieldValue('TEXT'));
return [code, Order.ATOMIC];
};
luaGenerator.forBlock['text_multiline'] = function(block) {
luaGenerator.forBlock['text_multiline'] = function(block, generator) {
// Text value.
const code = luaGenerator.multiline_quote_(block.getFieldValue('TEXT'));
const code = generator.multiline_quote_(block.getFieldValue('TEXT'));
const order =
code.indexOf('..') !== -1 ? Order.CONCATENATION : Order.ATOMIC;
return [code, order];
};
luaGenerator.forBlock['text_join'] = function(block) {
luaGenerator.forBlock['text_join'] = function(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 = luaGenerator.valueToCode(block, 'ADD0', Order.NONE) || "''";
const element = generator.valueToCode(block, 'ADD0', Order.NONE) || "''";
const code = 'tostring(' + element + ')';
return [code, Order.HIGH];
} else if (block.itemCount_ === 2) {
const element0 =
luaGenerator.valueToCode(block, 'ADD0', Order.CONCATENATION) || "''";
generator.valueToCode(block, 'ADD0', Order.CONCATENATION) || "''";
const element1 =
luaGenerator.valueToCode(block, 'ADD1', Order.CONCATENATION) || "''";
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] =
luaGenerator.valueToCode(block, 'ADD' + i, Order.NONE) || "''";
generator.valueToCode(block, 'ADD' + i, Order.NONE) || "''";
}
const code = 'table.concat({' + elements.join(', ') + '})';
return [code, Order.HIGH];
}
};
luaGenerator.forBlock['text_append'] = function(block) {
luaGenerator.forBlock['text_append'] = function(block, generator) {
// Append to a variable in place.
const varName =
luaGenerator.nameDB_.getName(
generator.nameDB_.getName(
block.getFieldValue('VAR'), NameType.VARIABLE);
const value =
luaGenerator.valueToCode(block, 'TEXT', Order.CONCATENATION) || "''";
generator.valueToCode(block, 'TEXT', Order.CONCATENATION) || "''";
return varName + ' = ' + varName + ' .. ' + value + '\n';
};
luaGenerator.forBlock['text_length'] = function(block) {
luaGenerator.forBlock['text_length'] = function(block, generator) {
// String or array length.
const text = luaGenerator.valueToCode(block, 'VALUE', Order.UNARY) || "''";
const text = generator.valueToCode(block, 'VALUE', Order.UNARY) || "''";
return ['#' + text, Order.UNARY];
};
luaGenerator.forBlock['text_isEmpty'] = function(block) {
luaGenerator.forBlock['text_isEmpty'] = function(block, generator) {
// Is the string null or array empty?
const text = luaGenerator.valueToCode(block, 'VALUE', Order.UNARY) || "''";
const text = generator.valueToCode(block, 'VALUE', Order.UNARY) || "''";
return ['#' + text + ' == 0', Order.RELATIONAL];
};
luaGenerator.forBlock['text_indexOf'] = function(block) {
luaGenerator.forBlock['text_indexOf'] = function(block, generator) {
// Search the text for a substring.
const substring = luaGenerator.valueToCode(block, 'FIND', Order.NONE) || "''";
const text = luaGenerator.valueToCode(block, 'VALUE', Order.NONE) || "''";
const substring = generator.valueToCode(block, 'FIND', Order.NONE) || "''";
const text = generator.valueToCode(block, 'VALUE', Order.NONE) || "''";
let functionName;
if (block.getFieldValue('END') === 'FIRST') {
functionName = luaGenerator.provideFunction_('firstIndexOf', `
function ${luaGenerator.FUNCTION_NAME_PLACEHOLDER_}(str, substr)
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
@@ -93,8 +93,8 @@ function ${luaGenerator.FUNCTION_NAME_PLACEHOLDER_}(str, substr)
end
`);
} else {
functionName = luaGenerator.provideFunction_('lastIndexOf', `
function ${luaGenerator.FUNCTION_NAME_PLACEHOLDER_}(str, substr)
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
@@ -107,17 +107,17 @@ end
return [code, Order.HIGH];
};
luaGenerator.forBlock['text_charAt'] = function(block) {
luaGenerator.forBlock['text_charAt'] = function(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 = luaGenerator.valueToCode(block, 'AT', atOrder) || '1';
const text = luaGenerator.valueToCode(block, 'VALUE', 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 = luaGenerator.provideFunction_('text_random_letter', `
function ${luaGenerator.FUNCTION_NAME_PLACEHOLDER_}(str)
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
@@ -142,8 +142,8 @@ end
code = 'string.sub(' + text + ', ' + start + ', ' + start + ')';
} else {
// use function to avoid reevaluation
const functionName = luaGenerator.provideFunction_('text_char_at', `
function ${luaGenerator.FUNCTION_NAME_PLACEHOLDER_}(str, index)
const functionName = generator.provideFunction_('text_char_at', `
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(str, index)
return string.sub(str, index, index)
end
`);
@@ -153,14 +153,14 @@ end
return [code, Order.HIGH];
};
luaGenerator.forBlock['text_getSubstring'] = function(block) {
luaGenerator.forBlock['text_getSubstring'] = function(block, generator) {
// Get substring.
const text = luaGenerator.valueToCode(block, 'STRING', Order.NONE) || "''";
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 = luaGenerator.valueToCode(block, 'AT1', at1Order) || '1';
const at1 = generator.valueToCode(block, 'AT1', at1Order) || '1';
let start;
if (where1 === 'FIRST') {
start = 1;
@@ -175,7 +175,7 @@ luaGenerator.forBlock['text_getSubstring'] = function(block) {
// Get end index.
const where2 = block.getFieldValue('WHERE2');
const at2Order = (where2 === 'FROM_END') ? Order.UNARY : Order.NONE;
const at2 = luaGenerator.valueToCode(block, 'AT2', at2Order) || '1';
const at2 = generator.valueToCode(block, 'AT2', at2Order) || '1';
let end;
if (where2 === 'LAST') {
end = -1;
@@ -190,10 +190,10 @@ luaGenerator.forBlock['text_getSubstring'] = function(block) {
return [code, Order.HIGH];
};
luaGenerator.forBlock['text_changeCase'] = function(block) {
luaGenerator.forBlock['text_changeCase'] = function(block, generator) {
// Change capitalization.
const operator = block.getFieldValue('CASE');
const text = luaGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
let functionName;
if (operator === 'UPPERCASE') {
functionName = 'string.upper';
@@ -203,8 +203,8 @@ luaGenerator.forBlock['text_changeCase'] = function(block) {
// There are shorter versions at
// http://lua-users.org/wiki/SciteTitleCase
// that do not preserve whitespace.
functionName = luaGenerator.provideFunction_('text_titlecase', `
function ${luaGenerator.FUNCTION_NAME_PLACEHOLDER_}(str)
functionName = generator.provideFunction_('text_titlecase', `
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(str)
local buf = {}
local inWord = false
for i = 1, #str do
@@ -227,34 +227,34 @@ end
return [code, Order.HIGH];
};
luaGenerator.forBlock['text_trim'] = function(block) {
luaGenerator.forBlock['text_trim'] = function(block, generator) {
// Trim spaces.
const OPERATORS = {LEFT: '^%s*(,-)', RIGHT: '(.-)%s*$', BOTH: '^%s*(.-)%s*$'};
const operator = OPERATORS[block.getFieldValue('MODE')];
const text = luaGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
const code = 'string.gsub(' + text + ', "' + operator + '", "%1")';
return [code, Order.HIGH];
};
luaGenerator.forBlock['text_print'] = function(block) {
luaGenerator.forBlock['text_print'] = function(block, generator) {
// Print statement.
const msg = luaGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
const msg = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
return 'print(' + msg + ')\n';
};
luaGenerator.forBlock['text_prompt_ext'] = function(block) {
luaGenerator.forBlock['text_prompt_ext'] = function(block, generator) {
// Prompt function.
let msg;
if (block.getField('TEXT')) {
// Internal message.
msg = luaGenerator.quote_(block.getFieldValue('TEXT'));
msg = generator.quote_(block.getFieldValue('TEXT'));
} else {
// External message.
msg = luaGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
msg = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
}
const functionName = luaGenerator.provideFunction_('text_prompt', `
function ${luaGenerator.FUNCTION_NAME_PLACEHOLDER_}(msg)
const functionName = generator.provideFunction_('text_prompt', `
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(msg)
io.write(msg)
io.flush()
return io.read()
@@ -271,11 +271,11 @@ end
luaGenerator.forBlock['text_prompt'] = luaGenerator.forBlock['text_prompt_ext'];
luaGenerator.forBlock['text_count'] = function(block) {
const text = luaGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
const sub = luaGenerator.valueToCode(block, 'SUB', Order.NONE) || "''";
const functionName = luaGenerator.provideFunction_('text_count', `
function ${luaGenerator.FUNCTION_NAME_PLACEHOLDER_}(haystack, needle)
luaGenerator.forBlock['text_count'] = function(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
@@ -296,12 +296,12 @@ end
return [code, Order.HIGH];
};
luaGenerator.forBlock['text_replace'] = function(block) {
const text = luaGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
const from = luaGenerator.valueToCode(block, 'FROM', Order.NONE) || "''";
const to = luaGenerator.valueToCode(block, 'TO', Order.NONE) || "''";
const functionName = luaGenerator.provideFunction_('text_replace', `
function ${luaGenerator.FUNCTION_NAME_PLACEHOLDER_}(haystack, needle, replacement)
luaGenerator.forBlock['text_replace'] = function(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
@@ -322,8 +322,8 @@ end
return [code, Order.HIGH];
};
luaGenerator.forBlock['text_reverse'] = function(block) {
const text = luaGenerator.valueToCode(block, 'TEXT', Order.NONE) || "''";
luaGenerator.forBlock['text_reverse'] = function(block, generator) {
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
const code = 'string.reverse(' + text + ')';
return [code, Order.HIGH];
};