mirror of
https://github.com/google/blockly.git
synced 2026-01-08 01:20:12 +01:00
refactor(generators): Restructure generator modules to contain side effects (#7173)
* refactor(generators): Move lang.js -> lang/lang_gernator.js
Move the LangGenerator definitions into their respective
subdirectories and add a _generator suffix to their filenames,
i.e. generators/javascript.js becomes
generators/javascript/javascript_generator.js.
This is to keep related code together and allow the `lang/all.js`
entrypoints to be moved to the top level generators/ directory.
No goog module IDs were changed, so playground and test code
that accesses this modules by filename does not need to be modified.
* refactor(generators) Move lang/all.js -> lang.js
- Move the entrypoints in generators/*/all.js to correspondingly-named
files in generators/ instead—i.e., generators/javascript/all.js
becomes generators/javascript.js.
- Update build_tasks.js accordingly.
* fix(generators): Add missing exports for LuaGenerator, PhpGenerator
These were inadvertently omitted from #7161 and #7162, respectively.
* refactor(generators): Make block generator modules side-effect free
- Move declaration of <lang>Generator instance from
generators/<lang>/<lang>_generator.js to generators/<lang>.js.
- Move .addReservedWords() calls from generators/<lang>/*.js to
generators/<lang>.js
- Modify generators/<lang>/*.js to export block generator functions
individually, rather than installing on <lang>Generator instance.
- Modify generators/<lang>.js to import and install block generator
functions on <lang>Generator instance.
* fix(tests): Fix tests broken by restructuring of generators
Where these tests needed block generator functions preinstalled
they should have been importing the Blockly.<Lang>.all module.
Where they do not need the provided block generator functions
they can now create their own empty <Lang>Generator instances.
* chore: Update renamings file
- Fix a malformation in previous entries that was not detected by
the renaming file validator test.
- Add entries describing the work done in this and related recent
PRs.
* fix: Correct minor errors in PR #7173
- Fix a search-and-replace error in renamings.json5
- Fix an incorrect-but-usable import in generator_test.js
This commit is contained in:
committed by
GitHub
parent
021a560ef0
commit
130989763c
@@ -1,26 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2021 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Complete helper functions for generating Lua for
|
||||
* blocks. This is the entrypoint for lua_compressed.js.
|
||||
* @suppress {extraRequire}
|
||||
*/
|
||||
|
||||
import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Lua.all');
|
||||
|
||||
import './colour.js';
|
||||
import './lists.js';
|
||||
import './logic.js';
|
||||
import './loops.js';
|
||||
import './math.js';
|
||||
import './procedures.js';
|
||||
import './text.js';
|
||||
import './variables.js';
|
||||
import './variables_dynamic.js';
|
||||
|
||||
export * from '../lua.js';
|
||||
@@ -11,22 +11,22 @@
|
||||
import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Lua.colour');
|
||||
|
||||
import {luaGenerator, Order} from '../lua.js';
|
||||
import {Order} from './lua_generator.js';
|
||||
|
||||
|
||||
luaGenerator.forBlock['colour_picker'] = function(block, generator) {
|
||||
export function colour_picker(block, generator) {
|
||||
// Colour picker.
|
||||
const code = generator.quote_(block.getFieldValue('COLOUR'));
|
||||
return [code, Order.ATOMIC];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['colour_random'] = function(block, generator) {
|
||||
export function colour_random(block, generator) {
|
||||
// Generate a random colour.
|
||||
const code = 'string.format("#%06x", math.random(0, 2^24 - 1))';
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['colour_rgb'] = function(block, generator) {
|
||||
export function colour_rgb(block, generator) {
|
||||
// Compose a colour from RGB components expressed as percentages.
|
||||
const functionName = generator.provideFunction_('colour_rgb', `
|
||||
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(r, g, b)
|
||||
@@ -43,7 +43,7 @@ end
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['colour_blend'] = function(block, generator) {
|
||||
export function colour_blend(block, generator) {
|
||||
// Blend two colours together.
|
||||
const functionName = generator.provideFunction_('colour_blend', `
|
||||
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(colour1, colour2, ratio)
|
||||
|
||||
@@ -12,15 +12,15 @@ import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Lua.lists');
|
||||
|
||||
import {NameType} from '../../core/names.js';
|
||||
import {luaGenerator, Order} from '../lua.js';
|
||||
import {Order} from './lua_generator.js';
|
||||
|
||||
|
||||
luaGenerator.forBlock['lists_create_empty'] = function(block, generator) {
|
||||
export function lists_create_empty(block, generator) {
|
||||
// Create an empty list.
|
||||
return ['{}', Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['lists_create_with'] = function(block, generator) {
|
||||
export function lists_create_with(block, generator) {
|
||||
// Create a list with any number of elements of any type.
|
||||
const elements = new Array(block.itemCount_);
|
||||
for (let i = 0; i < block.itemCount_; i++) {
|
||||
@@ -31,7 +31,7 @@ luaGenerator.forBlock['lists_create_with'] = function(block, generator) {
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['lists_repeat'] = function(block, generator) {
|
||||
export function lists_repeat(block, generator) {
|
||||
// Create a list with one element repeated.
|
||||
const functionName = generator.provideFunction_('create_list_repeated', `
|
||||
function ${generator.FUNCTION_NAME_PLACEHOLDER_}(item, count)
|
||||
@@ -48,20 +48,20 @@ end
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['lists_length'] = function(block, generator) {
|
||||
export function lists_length(block, generator) {
|
||||
// String or array length.
|
||||
const list = generator.valueToCode(block, 'VALUE', Order.UNARY) || '{}';
|
||||
return ['#' + list, Order.UNARY];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['lists_isEmpty'] = function(block, generator) {
|
||||
export function lists_isEmpty(block, generator) {
|
||||
// Is the string null or array empty?
|
||||
const list = generator.valueToCode(block, 'VALUE', Order.UNARY) || '{}';
|
||||
const code = '#' + list + ' == 0';
|
||||
return [code, Order.RELATIONAL];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['lists_indexOf'] = function(block, generator) {
|
||||
export function lists_indexOf(block, generator) {
|
||||
// Find an item in the list.
|
||||
const item = generator.valueToCode(block, 'FIND', Order.NONE) || "''";
|
||||
const list = generator.valueToCode(block, 'VALUE', Order.NONE) || '{}';
|
||||
@@ -114,7 +114,7 @@ const getListIndex = function(listName, where, opt_at) {
|
||||
}
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['lists_getIndex'] = function(block, generator) {
|
||||
export function lists_getIndex(block, generator) {
|
||||
// Get element at index.
|
||||
// Note: Until January 2013 this block did not have MODE or WHERE inputs.
|
||||
const mode = block.getFieldValue('MODE') || 'GET';
|
||||
@@ -194,7 +194,7 @@ luaGenerator.forBlock['lists_getIndex'] = function(block, generator) {
|
||||
}
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['lists_setIndex'] = function(block, generator) {
|
||||
export function lists_setIndex(block, generator) {
|
||||
// Set element at index.
|
||||
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
|
||||
let list = generator.valueToCode(block, 'LIST', Order.HIGH) || '{}';
|
||||
@@ -227,7 +227,7 @@ luaGenerator.forBlock['lists_setIndex'] = function(block, generator) {
|
||||
return code + '\n';
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['lists_getSublist'] = function(block, generator) {
|
||||
export function lists_getSublist(block, generator) {
|
||||
// Get sublist.
|
||||
const list = generator.valueToCode(block, 'LIST', Order.NONE) || '{}';
|
||||
const where1 = block.getFieldValue('WHERE1');
|
||||
@@ -262,7 +262,7 @@ end
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['lists_sort'] = function(block, generator) {
|
||||
export function lists_sort(block, generator) {
|
||||
// Block for sorting a list.
|
||||
const list = generator.valueToCode(block, 'LIST', Order.NONE) || '{}';
|
||||
const direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
|
||||
@@ -296,7 +296,7 @@ end
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['lists_split'] = function(block, generator) {
|
||||
export function lists_split(block, generator) {
|
||||
// Block for splitting text into a list, or joining a list into text.
|
||||
let input = generator.valueToCode(block, 'INPUT', Order.NONE);
|
||||
const delimiter =
|
||||
@@ -336,7 +336,7 @@ end
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['lists_reverse'] = function(block, generator) {
|
||||
export function lists_reverse(block, generator) {
|
||||
// Block for reversing a list.
|
||||
const list = generator.valueToCode(block, 'LIST', Order.NONE) || '{}';
|
||||
const functionName = generator.provideFunction_('list_reverse', `
|
||||
|
||||
@@ -11,10 +11,10 @@
|
||||
import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Lua.logic');
|
||||
|
||||
import {luaGenerator, Order} from '../lua.js';
|
||||
import {Order} from './lua_generator.js';
|
||||
|
||||
|
||||
luaGenerator.forBlock['controls_if'] = function(block, generator) {
|
||||
export function controls_if(block, generator) {
|
||||
// If/elseif/else condition.
|
||||
let n = 0;
|
||||
let code = '';
|
||||
@@ -51,9 +51,9 @@ luaGenerator.forBlock['controls_if'] = function(block, generator) {
|
||||
return code + 'end\n';
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['controls_ifelse'] = luaGenerator.forBlock['controls_if'];
|
||||
export const controls_ifelse = controls_if;
|
||||
|
||||
luaGenerator.forBlock['logic_compare'] = function(block, generator) {
|
||||
export function logic_compare(block, generator) {
|
||||
// Comparison operator.
|
||||
const OPERATORS =
|
||||
{'EQ': '==', 'NEQ': '~=', 'LT': '<', 'LTE': '<=', 'GT': '>', 'GTE': '>='};
|
||||
@@ -66,7 +66,7 @@ luaGenerator.forBlock['logic_compare'] = function(block, generator) {
|
||||
return [code, Order.RELATIONAL];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['logic_operation'] = function(block, generator) {
|
||||
export function logic_operation(block, generator) {
|
||||
// Operations 'and', 'or'.
|
||||
const operator = (block.getFieldValue('OP') === 'AND') ? 'and' : 'or';
|
||||
const order = (operator === 'and') ? Order.AND : Order.OR;
|
||||
@@ -90,7 +90,7 @@ luaGenerator.forBlock['logic_operation'] = function(block, generator) {
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['logic_negate'] = function(block, generator) {
|
||||
export function logic_negate(block, generator) {
|
||||
// Negation.
|
||||
const argument0 =
|
||||
generator.valueToCode(block, 'BOOL', Order.UNARY) || 'true';
|
||||
@@ -98,18 +98,18 @@ luaGenerator.forBlock['logic_negate'] = function(block, generator) {
|
||||
return [code, Order.UNARY];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['logic_boolean'] = function(block, generator) {
|
||||
export function logic_boolean(block, generator) {
|
||||
// Boolean values true and false.
|
||||
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'true' : 'false';
|
||||
return [code, Order.ATOMIC];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['logic_null'] = function(block, generator) {
|
||||
export function logic_null(block, generator) {
|
||||
// Null data type.
|
||||
return ['nil', Order.ATOMIC];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['logic_ternary'] = function(block, generator) {
|
||||
export function logic_ternary(block, generator) {
|
||||
// Ternary operator.
|
||||
const value_if = generator.valueToCode(block, 'IF', Order.AND) || 'false';
|
||||
const value_then =
|
||||
|
||||
@@ -13,7 +13,7 @@ goog.declareModuleId('Blockly.Lua.loops');
|
||||
|
||||
import * as stringUtils from '../../core/utils/string.js';
|
||||
import {NameType} from '../../core/names.js';
|
||||
import {luaGenerator, Order} from '../lua.js';
|
||||
import {Order} from './lua_generator.js';
|
||||
|
||||
|
||||
/**
|
||||
@@ -43,7 +43,7 @@ function addContinueLabel(branch, indent) {
|
||||
}
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['controls_repeat_ext'] = function(block, generator) {
|
||||
export function controls_repeat_ext(block, generator) {
|
||||
// Repeat n times.
|
||||
let repeats;
|
||||
if (block.getField('TIMES')) {
|
||||
@@ -67,10 +67,9 @@ luaGenerator.forBlock['controls_repeat_ext'] = function(block, generator) {
|
||||
return code;
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['controls_repeat'] =
|
||||
luaGenerator.forBlock['controls_repeat_ext'];
|
||||
export const controls_repeat = controls_repeat_ext;
|
||||
|
||||
luaGenerator.forBlock['controls_whileUntil'] = function(block, generator) {
|
||||
export function controls_whileUntil(block, generator) {
|
||||
// Do while/until loop.
|
||||
const until = block.getFieldValue('MODE') === 'UNTIL';
|
||||
let argument0 =
|
||||
@@ -86,7 +85,7 @@ luaGenerator.forBlock['controls_whileUntil'] = function(block, generator) {
|
||||
return 'while ' + argument0 + ' do\n' + branch + 'end\n';
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['controls_for'] = function(block, generator) {
|
||||
export function controls_for(block, generator) {
|
||||
// For loop.
|
||||
const variable0 =
|
||||
generator.nameDB_.getName(
|
||||
@@ -128,7 +127,7 @@ luaGenerator.forBlock['controls_for'] = function(block, generator) {
|
||||
return code;
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['controls_forEach'] = function(block, generator) {
|
||||
export function controls_forEach(block, generator) {
|
||||
// For each loop.
|
||||
const variable0 =
|
||||
generator.nameDB_.getName(
|
||||
@@ -142,7 +141,7 @@ luaGenerator.forBlock['controls_forEach'] = function(block, generator) {
|
||||
return code;
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['controls_flow_statements'] = function(block, generator) {
|
||||
export function controls_flow_statements(block, generator) {
|
||||
// Flow statements: continue, break.
|
||||
let xfix = '';
|
||||
if (generator.STATEMENT_PREFIX) {
|
||||
|
||||
213
generators/lua/lua_generator.js
Normal file
213
generators/lua/lua_generator.js
Normal file
@@ -0,0 +1,213 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2016 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Helper functions for generating Lua for blocks.
|
||||
* Based on Ellen Spertus's blocky-lua project.
|
||||
* @suppress {checkTypes|globalThis}
|
||||
*/
|
||||
|
||||
import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Lua');
|
||||
|
||||
import * as stringUtils from '../../core/utils/string.js';
|
||||
// import type {Block} from '../../core/block.js';
|
||||
import {CodeGenerator} from '../../core/generator.js';
|
||||
import {Names} from '../../core/names.js';
|
||||
// import type {Workspace} from '../../core/workspace.js';
|
||||
import {inputTypes} from '../../core/inputs/input_types.js';
|
||||
|
||||
|
||||
/**
|
||||
* Order of operation ENUMs.
|
||||
* http://www.lua.org/manual/5.3/manual.html#3.4.8
|
||||
* @enum {number}
|
||||
*/
|
||||
export const Order = {
|
||||
ATOMIC: 0, // literals
|
||||
// The next level was not explicit in documentation and inferred by Ellen.
|
||||
HIGH: 1, // Function calls, tables[]
|
||||
EXPONENTIATION: 2, // ^
|
||||
UNARY: 3, // not # - ~
|
||||
MULTIPLICATIVE: 4, // * / %
|
||||
ADDITIVE: 5, // + -
|
||||
CONCATENATION: 6, // ..
|
||||
RELATIONAL: 7, // < > <= >= ~= ==
|
||||
AND: 8, // and
|
||||
OR: 9, // or
|
||||
NONE: 99,
|
||||
};
|
||||
|
||||
/**
|
||||
* Lua code generator class.
|
||||
*
|
||||
* Note: Lua is not supporting zero-indexing since the language itself is
|
||||
* one-indexed, so the generator does not repoct the oneBasedIndex configuration
|
||||
* option used for lists and text.
|
||||
*/
|
||||
export class LuaGenerator extends CodeGenerator {
|
||||
constructor(name) {
|
||||
super(name ?? 'Lua');
|
||||
this.isInitialized = false;
|
||||
|
||||
// Copy Order values onto instance for backwards compatibility
|
||||
// while ensuring they are not part of the publically-advertised
|
||||
// API.
|
||||
//
|
||||
// TODO(#7085): deprecate these in due course. (Could initially
|
||||
// replace data properties with get accessors that call
|
||||
// deprecate.warn().)
|
||||
for (const key in Order) {
|
||||
this['ORDER_' + key] = Order[key];
|
||||
}
|
||||
|
||||
// List of illegal variable names. This is not intended to be a
|
||||
// security feature. Blockly is 100% client-side, so bypassing
|
||||
// this list is trivial. This is intended to prevent users from
|
||||
// accidentally clobbering a built-in object or function.
|
||||
this.addReservedWords(
|
||||
// Special character
|
||||
'_,' +
|
||||
// From theoriginalbit's script:
|
||||
// https://github.com/espertus/blockly-lua/issues/6
|
||||
'__inext,assert,bit,colors,colours,coroutine,disk,dofile,error,fs,' +
|
||||
'fetfenv,getmetatable,gps,help,io,ipairs,keys,loadfile,loadstring,math,' +
|
||||
'native,next,os,paintutils,pairs,parallel,pcall,peripheral,print,' +
|
||||
'printError,rawequal,rawget,rawset,read,rednet,redstone,rs,select,' +
|
||||
'setfenv,setmetatable,sleep,string,table,term,textutils,tonumber,' +
|
||||
'tostring,turtle,type,unpack,vector,write,xpcall,_VERSION,__indext,' +
|
||||
// Not included in the script, probably because it wasn't enabled:
|
||||
'HTTP,' +
|
||||
// Keywords (http://www.lua.org/pil/1.3.html).
|
||||
'and,break,do,else,elseif,end,false,for,function,if,in,local,nil,not,' +
|
||||
'or,repeat,return,then,true,until,while,' +
|
||||
// Metamethods (http://www.lua.org/manual/5.2/manual.html).
|
||||
'add,sub,mul,div,mod,pow,unm,concat,len,eq,lt,le,index,newindex,call,' +
|
||||
// Basic functions (http://www.lua.org/manual/5.2/manual.html,
|
||||
// section 6.1).
|
||||
'assert,collectgarbage,dofile,error,_G,getmetatable,inpairs,load,' +
|
||||
'loadfile,next,pairs,pcall,print,rawequal,rawget,rawlen,rawset,select,' +
|
||||
'setmetatable,tonumber,tostring,type,_VERSION,xpcall,' +
|
||||
// Modules (http://www.lua.org/manual/5.2/manual.html, section 6.3).
|
||||
'require,package,string,table,math,bit32,io,file,os,debug'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {!Workspace} workspace Workspace to generate code from.
|
||||
*/
|
||||
init(workspace) {
|
||||
// Call Blockly.CodeGenerator's init.
|
||||
super.init();
|
||||
|
||||
if (!this.nameDB_) {
|
||||
this.nameDB_ = new Names(this.RESERVED_WORDS_);
|
||||
} else {
|
||||
this.nameDB_.reset();
|
||||
}
|
||||
this.nameDB_.setVariableMap(workspace.getVariableMap());
|
||||
this.nameDB_.populateVariables(workspace);
|
||||
this.nameDB_.populateProcedures(workspace);
|
||||
|
||||
this.isInitialized = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Prepend the generated code with the variable definitions.
|
||||
* @param {string} code Generated code.
|
||||
* @return {string} Completed code.
|
||||
*/
|
||||
finish(code) {
|
||||
// Convert the definitions dictionary into a list.
|
||||
const definitions = Object.values(this.definitions_);
|
||||
// Call Blockly.CodeGenerator's finish.
|
||||
code = super.finish(code);
|
||||
this.isInitialized = false;
|
||||
|
||||
this.nameDB_.reset();
|
||||
return definitions.join('\n\n') + '\n\n\n' + code;
|
||||
};
|
||||
|
||||
/**
|
||||
* Naked values are top-level blocks with outputs that aren't plugged into
|
||||
* anything. In Lua, an expression is not a legal statement, so we must assign
|
||||
* the value to the (conventionally ignored) _.
|
||||
* http://lua-users.org/wiki/ExpressionsAsStatements
|
||||
* @param {string} line Line of generated code.
|
||||
* @return {string} Legal line of code.
|
||||
*/
|
||||
scrubNakedValue(line) {
|
||||
return 'local _ = ' + line + '\n';
|
||||
};
|
||||
|
||||
/**
|
||||
* Encode a string as a properly escaped Lua string, complete with
|
||||
* quotes.
|
||||
* @param {string} string Text to encode.
|
||||
* @return {string} Lua string.
|
||||
* @protected
|
||||
*/
|
||||
quote_(string) {
|
||||
string = string.replace(/\\/g, '\\\\')
|
||||
.replace(/\n/g, '\\\n')
|
||||
.replace(/'/g, '\\\'');
|
||||
return '\'' + string + '\'';
|
||||
};
|
||||
|
||||
/**
|
||||
* Encode a string as a properly escaped multiline Lua string, complete with
|
||||
* quotes.
|
||||
* @param {string} string Text to encode.
|
||||
* @return {string} Lua string.
|
||||
* @protected
|
||||
*/
|
||||
multiline_quote_(string) {
|
||||
const lines = string.split(/\n/g).map(this.quote_);
|
||||
// Join with the following, plus a newline:
|
||||
// .. '\n' ..
|
||||
return lines.join(' .. \'\\n\' ..\n');
|
||||
};
|
||||
|
||||
/**
|
||||
* Common tasks for generating Lua from blocks.
|
||||
* Handles comments for the specified block and any connected value blocks.
|
||||
* Calls any statements following this block.
|
||||
* @param {!Block} block The current block.
|
||||
* @param {string} code The Lua code created for this block.
|
||||
* @param {boolean=} opt_thisOnly True to generate code for only this statement.
|
||||
* @return {string} Lua code with comments and subsequent blocks added.
|
||||
* @protected
|
||||
*/
|
||||
scrub_(block, code, opt_thisOnly) {
|
||||
let commentCode = '';
|
||||
// Only collect comments for blocks that aren't inline.
|
||||
if (!block.outputConnection || !block.outputConnection.targetConnection) {
|
||||
// Collect comment for this block.
|
||||
let comment = block.getCommentText();
|
||||
if (comment) {
|
||||
comment = stringUtils.wrap(comment, this.COMMENT_WRAP - 3);
|
||||
commentCode += this.prefixLines(comment, '-- ') + '\n';
|
||||
}
|
||||
// Collect comments for all value arguments.
|
||||
// Don't collect comments for nested statements.
|
||||
for (let i = 0; i < block.inputList.length; i++) {
|
||||
if (block.inputList[i].type === inputTypes.VALUE) {
|
||||
const childBlock = block.inputList[i].connection.targetBlock();
|
||||
if (childBlock) {
|
||||
comment = this.allNestedComments(childBlock);
|
||||
if (comment) {
|
||||
commentCode += this.prefixLines(comment, '-- ');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const nextBlock = block.nextConnection && block.nextConnection.targetBlock();
|
||||
const nextCode = opt_thisOnly ? '' : this.blockToCode(nextBlock);
|
||||
return commentCode + code + nextCode;
|
||||
};
|
||||
}
|
||||
@@ -12,17 +12,17 @@ import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Lua.math');
|
||||
|
||||
import {NameType} from '../../core/names.js';
|
||||
import {luaGenerator, Order} from '../lua.js';
|
||||
import {Order} from './lua_generator.js';
|
||||
|
||||
|
||||
luaGenerator.forBlock['math_number'] = function(block, generator) {
|
||||
export function math_number(block, generator) {
|
||||
// Numeric value.
|
||||
const code = Number(block.getFieldValue('NUM'));
|
||||
const order = code < 0 ? Order.UNARY : Order.ATOMIC;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['math_arithmetic'] = function(block, generator) {
|
||||
export function math_arithmetic(block, generator) {
|
||||
// Basic arithmetic operators, and power.
|
||||
const OPERATORS = {
|
||||
'ADD': [' + ', Order.ADDITIVE],
|
||||
@@ -40,7 +40,7 @@ luaGenerator.forBlock['math_arithmetic'] = function(block, generator) {
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['math_single'] = function(block, generator) {
|
||||
export function math_single(block, generator) {
|
||||
// Math operators with single operand.
|
||||
const operator = block.getFieldValue('OP');
|
||||
let arg;
|
||||
@@ -110,7 +110,7 @@ luaGenerator.forBlock['math_single'] = function(block, generator) {
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['math_constant'] = function(block, generator) {
|
||||
export function math_constant(block, generator) {
|
||||
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
|
||||
const CONSTANTS = {
|
||||
'PI': ['math.pi', Order.HIGH],
|
||||
@@ -123,7 +123,7 @@ luaGenerator.forBlock['math_constant'] = function(block, generator) {
|
||||
return CONSTANTS[block.getFieldValue('CONSTANT')];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['math_number_property'] = function(block, generator) {
|
||||
export function math_number_property(block, generator) {
|
||||
// Check if a number is even, odd, prime, whole, positive, or negative
|
||||
// or if it is divisible by certain number. Returns true or false.
|
||||
const PROPERTIES = {
|
||||
@@ -181,7 +181,7 @@ end
|
||||
return [code, outputOrder];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['math_change'] = function(block, generator) {
|
||||
export function math_change(block, generator) {
|
||||
// Add to a variable in place.
|
||||
const argument0 =
|
||||
generator.valueToCode(block, 'DELTA', Order.ADDITIVE) || '0';
|
||||
@@ -192,11 +192,11 @@ luaGenerator.forBlock['math_change'] = function(block, generator) {
|
||||
};
|
||||
|
||||
// Rounding functions have a single operand.
|
||||
luaGenerator.forBlock['math_round'] = luaGenerator.forBlock['math_single'];
|
||||
export const math_round = math_single;
|
||||
// Trigonometry functions have a single operand.
|
||||
luaGenerator.forBlock['math_trig'] = luaGenerator.forBlock['math_single'];
|
||||
export const math_trig = math_single;
|
||||
|
||||
luaGenerator.forBlock['math_on_list'] = function(block, generator) {
|
||||
export function math_on_list(block, generator) {
|
||||
// Math functions for lists.
|
||||
const func = block.getFieldValue('OP');
|
||||
const list = generator.valueToCode(block, 'LIST', Order.NONE) || '{}';
|
||||
@@ -363,7 +363,7 @@ end
|
||||
return [functionName + '(' + list + ')', Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['math_modulo'] = function(block, generator) {
|
||||
export function math_modulo(block, generator) {
|
||||
// Remainder computation.
|
||||
const argument0 =
|
||||
generator.valueToCode(block, 'DIVIDEND', Order.MULTIPLICATIVE) || '0';
|
||||
@@ -373,7 +373,7 @@ luaGenerator.forBlock['math_modulo'] = function(block, generator) {
|
||||
return [code, Order.MULTIPLICATIVE];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['math_constrain'] = function(block, generator) {
|
||||
export function math_constrain(block, generator) {
|
||||
// Constrain a number between two limits.
|
||||
const argument0 = generator.valueToCode(block, 'VALUE', Order.NONE) || '0';
|
||||
const argument1 =
|
||||
@@ -385,7 +385,7 @@ luaGenerator.forBlock['math_constrain'] = function(block, generator) {
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['math_random_int'] = function(block, generator) {
|
||||
export function math_random_int(block, generator) {
|
||||
// Random integer between [X] and [Y].
|
||||
const argument0 = generator.valueToCode(block, 'FROM', Order.NONE) || '0';
|
||||
const argument1 = generator.valueToCode(block, 'TO', Order.NONE) || '0';
|
||||
@@ -393,12 +393,12 @@ luaGenerator.forBlock['math_random_int'] = function(block, generator) {
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['math_random_float'] = function(block, generator) {
|
||||
export function math_random_float(block, generator) {
|
||||
// Random fraction between 0 and 1.
|
||||
return ['math.random()', Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['math_atan2'] = function(block, generator) {
|
||||
export function math_atan2(block, generator) {
|
||||
// Arctangent of point (X, Y) in degrees from -180 to 180.
|
||||
const argument0 = generator.valueToCode(block, 'X', Order.NONE) || '0';
|
||||
const argument1 = generator.valueToCode(block, 'Y', Order.NONE) || '0';
|
||||
|
||||
@@ -12,10 +12,10 @@ import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Lua.procedures');
|
||||
|
||||
import {NameType} from '../../core/names.js';
|
||||
import {luaGenerator, Order} from '../lua.js';
|
||||
import {Order} from './lua_generator.js';
|
||||
|
||||
|
||||
luaGenerator.forBlock['procedures_defreturn'] = function(block, generator) {
|
||||
export function procedures_defreturn(block, generator) {
|
||||
// Define a procedure with a return value.
|
||||
const funcName =
|
||||
generator.nameDB_.getName(
|
||||
@@ -63,10 +63,9 @@ luaGenerator.forBlock['procedures_defreturn'] = function(block, generator) {
|
||||
|
||||
// Defining a procedure without a return value uses the same generator as
|
||||
// a procedure with a return value.
|
||||
luaGenerator.forBlock['procedures_defnoreturn'] =
|
||||
luaGenerator.forBlock['procedures_defreturn'];
|
||||
export const procedures_defnoreturn = procedures_defreturn;
|
||||
|
||||
luaGenerator.forBlock['procedures_callreturn'] = function(block, generator) {
|
||||
export function procedures_callreturn(block, generator) {
|
||||
// Call a procedure with a return value.
|
||||
const funcName =
|
||||
generator.nameDB_.getName(
|
||||
@@ -80,7 +79,7 @@ luaGenerator.forBlock['procedures_callreturn'] = function(block, generator) {
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['procedures_callnoreturn'] = function(block, generator) {
|
||||
export function procedures_callnoreturn(block, generator) {
|
||||
// Call a procedure with no return value.
|
||||
// Generated code is for a function call as a statement is the same as a
|
||||
// function call as a value, with the addition of line ending.
|
||||
@@ -88,7 +87,7 @@ luaGenerator.forBlock['procedures_callnoreturn'] = function(block, generator) {
|
||||
return tuple[0] + '\n';
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['procedures_ifreturn'] = function(block, generator) {
|
||||
export function procedures_ifreturn(block, generator) {
|
||||
// Conditionally return value from a procedure.
|
||||
const condition =
|
||||
generator.valueToCode(block, 'CONDITION', Order.NONE) || 'false';
|
||||
|
||||
@@ -12,16 +12,16 @@ import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Lua.texts');
|
||||
|
||||
import {NameType} from '../../core/names.js';
|
||||
import {luaGenerator, Order} from '../lua.js';
|
||||
import {Order} from './lua_generator.js';
|
||||
|
||||
|
||||
luaGenerator.forBlock['text'] = function(block, generator) {
|
||||
export function text(block, generator) {
|
||||
// Text value.
|
||||
const code = generator.quote_(block.getFieldValue('TEXT'));
|
||||
return [code, Order.ATOMIC];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_multiline'] = function(block, generator) {
|
||||
export function text_multiline(block, generator) {
|
||||
// Text value.
|
||||
const code = generator.multiline_quote_(block.getFieldValue('TEXT'));
|
||||
const order =
|
||||
@@ -29,7 +29,7 @@ luaGenerator.forBlock['text_multiline'] = function(block, generator) {
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_join'] = function(block, generator) {
|
||||
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];
|
||||
@@ -55,7 +55,7 @@ luaGenerator.forBlock['text_join'] = function(block, generator) {
|
||||
}
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_append'] = function(block, generator) {
|
||||
export function text_append(block, generator) {
|
||||
// Append to a variable in place.
|
||||
const varName =
|
||||
generator.nameDB_.getName(
|
||||
@@ -65,19 +65,19 @@ luaGenerator.forBlock['text_append'] = function(block, generator) {
|
||||
return varName + ' = ' + varName + ' .. ' + value + '\n';
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_length'] = function(block, generator) {
|
||||
export function text_length(block, generator) {
|
||||
// String or array length.
|
||||
const text = generator.valueToCode(block, 'VALUE', Order.UNARY) || "''";
|
||||
return ['#' + text, Order.UNARY];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_isEmpty'] = function(block, generator) {
|
||||
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];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_indexOf'] = function(block, generator) {
|
||||
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) || "''";
|
||||
@@ -107,7 +107,7 @@ end
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_charAt'] = function(block, generator) {
|
||||
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';
|
||||
@@ -153,7 +153,7 @@ end
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_getSubstring'] = function(block, generator) {
|
||||
export function text_getSubstring(block, generator) {
|
||||
// Get substring.
|
||||
const text = generator.valueToCode(block, 'STRING', Order.NONE) || "''";
|
||||
|
||||
@@ -190,7 +190,7 @@ luaGenerator.forBlock['text_getSubstring'] = function(block, generator) {
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_changeCase'] = function(block, generator) {
|
||||
export function text_changeCase(block, generator) {
|
||||
// Change capitalization.
|
||||
const operator = block.getFieldValue('CASE');
|
||||
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
||||
@@ -227,7 +227,7 @@ end
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_trim'] = function(block, generator) {
|
||||
export function text_trim(block, generator) {
|
||||
// Trim spaces.
|
||||
const OPERATORS = {LEFT: '^%s*(,-)', RIGHT: '(.-)%s*$', BOTH: '^%s*(.-)%s*$'};
|
||||
const operator = OPERATORS[block.getFieldValue('MODE')];
|
||||
@@ -236,13 +236,13 @@ luaGenerator.forBlock['text_trim'] = function(block, generator) {
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_print'] = function(block, generator) {
|
||||
export function text_print(block, generator) {
|
||||
// Print statement.
|
||||
const msg = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
||||
return 'print(' + msg + ')\n';
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_prompt_ext'] = function(block, generator) {
|
||||
export function text_prompt_ext(block, generator) {
|
||||
// Prompt function.
|
||||
let msg;
|
||||
if (block.getField('TEXT')) {
|
||||
@@ -269,9 +269,9 @@ end
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_prompt'] = luaGenerator.forBlock['text_prompt_ext'];
|
||||
export const text_prompt = text_prompt_ext;
|
||||
|
||||
luaGenerator.forBlock['text_count'] = function(block, generator) {
|
||||
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', `
|
||||
@@ -296,7 +296,7 @@ end
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_replace'] = function(block, generator) {
|
||||
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) || "''";
|
||||
@@ -322,7 +322,7 @@ end
|
||||
return [code, Order.HIGH];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['text_reverse'] = function(block, generator) {
|
||||
export function text_reverse(block, generator) {
|
||||
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
|
||||
const code = 'string.reverse(' + text + ')';
|
||||
return [code, Order.HIGH];
|
||||
|
||||
@@ -12,10 +12,10 @@ import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Lua.variables');
|
||||
|
||||
import {NameType} from '../../core/names.js';
|
||||
import {luaGenerator, Order} from '../lua.js';
|
||||
import {Order} from './lua_generator.js';
|
||||
|
||||
|
||||
luaGenerator.forBlock['variables_get'] = function(block, generator) {
|
||||
export function variables_get(block, generator) {
|
||||
// Variable getter.
|
||||
const code =
|
||||
generator.nameDB_.getName(
|
||||
@@ -23,7 +23,7 @@ luaGenerator.forBlock['variables_get'] = function(block, generator) {
|
||||
return [code, Order.ATOMIC];
|
||||
};
|
||||
|
||||
luaGenerator.forBlock['variables_set'] = function(block, generator) {
|
||||
export function variables_set(block, generator) {
|
||||
// Variable setter.
|
||||
const argument0 = generator.valueToCode(block, 'VALUE', Order.NONE) || '0';
|
||||
const varName =
|
||||
|
||||
@@ -11,12 +11,9 @@
|
||||
import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Lua.variablesDynamic');
|
||||
|
||||
import {luaGenerator} from '../lua.js';
|
||||
import './variables.js';
|
||||
|
||||
|
||||
// Lua is dynamically typed.
|
||||
luaGenerator.forBlock['variables_get_dynamic'] =
|
||||
luaGenerator.forBlock['variables_get'];
|
||||
luaGenerator.forBlock['variables_set_dynamic'] =
|
||||
luaGenerator.forBlock['variables_set'];
|
||||
export {
|
||||
variables_get as variables_get_dynamic,
|
||||
variables_set as variables_set_dynamic,
|
||||
} from './variables.js';
|
||||
|
||||
Reference in New Issue
Block a user