adapted formula editor for variadic infix reporters

snap8
Jens Mönig 2022-03-01 15:39:32 +01:00
rodzic a7741a7275
commit 2be6a6095f
2 zmienionych plików z 16 dodań i 8 usunięć

Wyświetl plik

@ -15,6 +15,7 @@
* blocks: refactored adding and removing inputs in variadic slots * blocks: refactored adding and removing inputs in variadic slots
* blocks: adjusted inserting / deleting single inputs in variadic infix slots * blocks: adjusted inserting / deleting single inputs in variadic infix slots
* objects: adjusted block-search-bar for variadic infix reporters * objects: adjusted block-search-bar for variadic infix reporters
* objects: adapted formula editor for variadic infix reporters
### 2022-02-28 ### 2022-02-28
* blocks, objects, threads, store: made addition reporter variadic * blocks, objects, threads, store: made addition reporter variadic

Wyświetl plik

@ -3634,7 +3634,7 @@ SpriteMorph.prototype.reporterize = function (expressionString) {
var ast; var ast;
function parseInfix(expression, operator, already) { function parseInfix(expression, operator, already) {
// very basic diadic infix parser for arithmetic expressions // very basic dyadic infix parser for arithmetic expressions
// with strict left-to-right operator precedence (as in Smalltalk) // with strict left-to-right operator precedence (as in Smalltalk)
// which can be overriden by - nested - parentheses. // which can be overriden by - nested - parentheses.
// assumes well-formed expressions, no graceful error handling yet. // assumes well-formed expressions, no graceful error handling yet.
@ -3723,13 +3723,13 @@ SpriteMorph.prototype.reporterize = function (expressionString) {
} }
function blockFromAST(ast) { function blockFromAST(ast) {
var block, selectors, monads, alias, key, sel, i, inps, var block, target, selectors, monads, alias, key, sel, i, inps,
off = 1, off = 1,
reverseDict = {}; reverseDict = {};
selectors = { selectors = {
'+': 'reportSum', '+': 'reportVariadicSum',
'-': 'reportDifference', '-': 'reportDifference',
'*': 'reportProduct', '*': 'reportVariadicProduct',
'/': 'reportQuotient', '/': 'reportQuotient',
'%': 'reportModulus', '%': 'reportModulus',
'^': 'reportPower', '^': 'reportPower',
@ -3763,18 +3763,24 @@ SpriteMorph.prototype.reporterize = function (expressionString) {
inps[0].setContents([key]); inps[0].setContents([key]);
off = 0; off = 0;
} }
target = block; // +++
} else { // dyadic } else { // dyadic
block = SpriteMorph.prototype.blockForSelector(selectors[key]); block = SpriteMorph.prototype.blockForSelector(selectors[key]);
inps = block.inputs(); target = block;
inps = block.inputs(); // +++++
if (inps[0] instanceof MultiArgMorph) { // infix sum or product
target = inps[0];
inps = target.inputs();
}
} }
for (i = 1; i < ast.length; i += 1) { for (i = 1; i < ast.length; i += 1) {
if (ast[i] instanceof Array) { if (ast[i] instanceof Array) {
block.replaceInput(inps[i - off], blockFromAST(ast[i])); target.replaceInput(inps[i - off], blockFromAST(ast[i])); // +++
} else if (isString(ast[i])) { } else if (isString(ast[i])) {
if (contains( if (contains(
['true', 'false'], reverseDict[ast[i]] || ast[i]) ['true', 'false'], reverseDict[ast[i]] || ast[i])
) { ) {
block.replaceInput( target.replaceInput( // +++
inps[i - off], inps[i - off],
SpriteMorph.prototype.blockForSelector( SpriteMorph.prototype.blockForSelector(
(reverseDict[ast[i]] || ast[i]) === 'true' ? (reverseDict[ast[i]] || ast[i]) === 'true' ?
@ -3782,7 +3788,7 @@ SpriteMorph.prototype.reporterize = function (expressionString) {
) )
); );
} else if (ast[i] !== '_') { } else if (ast[i] !== '_') {
block.replaceInput( target.replaceInput( // +++
inps[i - off], inps[i - off],
SpriteMorph.prototype.variableBlock(ast[i]) SpriteMorph.prototype.variableBlock(ast[i])
); );
@ -3791,6 +3797,7 @@ SpriteMorph.prototype.reporterize = function (expressionString) {
inps[i - off].setContents(ast[i]); inps[i - off].setContents(ast[i]);
} }
} }
target.fixLayout();
block.isDraggable = true; block.isDraggable = true;
block.fixLayout(); block.fixLayout();
block.fixBlockColor(null, true); block.fixBlockColor(null, true);