From 3c74ecb847b4d79ffa721515419988a69adf8de1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20M=C3=B6nig?= Date: Mon, 28 Feb 2022 09:24:12 +0100 Subject: [PATCH] made addition reporter variadic --- HISTORY.md | 6 ++++- snap.html | 8 +++---- src/blocks.js | 59 ++++++++++++++++++++++++++++++++++++++++---------- src/objects.js | 20 ++++++++++++++--- src/store.js | 15 ++++++++++--- src/threads.js | 6 ++++- sw.js | 2 +- 7 files changed, 92 insertions(+), 24 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 417343cb..aa812a4a 100755 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,12 +3,16 @@ ## in development: * **New Features:** + * variadic infix reporters * **Notable Changes:** * **Notable Fixes:** * **Documentation Updates:** * **Translation Updates:** -### 2022-02-276 +### 2022-02-28 +* blocks, objects, threads, store: made addition reporter variadic + +### 2022-02-27 * variadic infix branch ### 2022-02-26 diff --git a/snap.html b/snap.html index 47099c1c..a6908256 100755 --- a/snap.html +++ b/snap.html @@ -16,9 +16,9 @@ - - - + + + @@ -30,7 +30,7 @@ - + diff --git a/src/blocks.js b/src/blocks.js index dbc3ecf3..327e1db1 100644 --- a/src/blocks.js +++ b/src/blocks.js @@ -161,7 +161,7 @@ CostumeIconMorph, SoundIconMorph, SVG_Costume*/ // Global stuff //////////////////////////////////////////////////////// -modules.blocks = '2022-February-25'; +modules.blocks = '2022-February-28'; var SyntaxElementMorph; var BlockMorph; @@ -1014,6 +1014,8 @@ SyntaxElementMorph.prototype.labelParts = { type: 'multi' slots: a slot spec string label: (optional) + infix: (optional) // +++ + collapse: (optional) alternative label to "Input list" tags: 'widget' // doesn't count as "empty" slot implicit parameter min: (optional) number of minimum inputs) or zero max: (optional) number of maximum inputs) or zero @@ -1094,6 +1096,13 @@ SyntaxElementMorph.prototype.labelParts = { slots: '%s', defaults: 1, tags: 'static widget' + }, + '%sum': { // +++ + type: 'multi', + slots: '%n', + min: 2, + infix: '+', + collapse: 'sum' } }; @@ -1250,8 +1259,10 @@ SyntaxElementMorph.prototype.replaceInput = function (oldArg, newArg) { oldArg.inputs().forEach(inp => // preserve nested reporters oldArg.replaceInput(inp, new InputSlotMorph()) ); - if (this.dynamicInputLabels && newArg instanceof ReporterBlockMorph) { - replacement = new ArgLabelMorph(newArg); +// if (this.dynamicInputLabels && newArg instanceof ReporterBlockMorph) { + if ((this.dynamicInputLabels || oldArg.collapse) && + newArg instanceof ReporterBlockMorph) { + replacement = new ArgLabelMorph(newArg, oldArg.collapse); // +++ } } replacement.parent = this; @@ -1732,7 +1743,10 @@ SyntaxElementMorph.prototype.labelPart = function (spec) { info.slots, info.label, info.min || 0, - spec + spec, + null, null, null, null, null, + info.infix, // +++ + info.collapse // +++ ); part.maxInputs = info.max; for (i = 0; i < info.defaults || 0; i += 1) { @@ -4000,7 +4014,7 @@ BlockMorph.prototype.copyWithInputs = function (inputs) { (dta.length === 1) || (cpy.blockSpec === '\xa0' && dta.length > 1)) ) { cpy.setSpec(dta[0]); - return cpy.reify(dta.slice(1)) + return cpy.reify(dta.slice(1)); } // restore input slots @@ -12260,7 +12274,9 @@ function MultiArgMorph( labelColor, shadowColor, shadowOffset, - isTransparent + isTransparent, + infix, // +++ + collapse // +++ ) { this.init( slotSpec, @@ -12271,7 +12287,9 @@ function MultiArgMorph( labelColor, shadowColor, shadowOffset, - isTransparent + isTransparent, + infix, + collapse ); } @@ -12284,7 +12302,9 @@ MultiArgMorph.prototype.init = function ( labelColor, shadowColor, shadowOffset, - isTransparent + isTransparent, + infix, + collapse ) { var label, arrows = new FrameMorph(), @@ -12294,6 +12314,8 @@ MultiArgMorph.prototype.init = function ( this.slotSpec = slotSpec || '%s'; this.labelText = localize(labelTxt || ''); + this.infix = infix || ''; // +++ + this.collapse = collapse || ''; // +++ this.minInputs = min || 0; this.maxInputs = null; this.elementSpec = eSpec || null; @@ -12542,9 +12564,11 @@ MultiArgMorph.prototype.insertNewInputBefore = function (anInput, contents) { // MultiArgMorph arity control: MultiArgMorph.prototype.addInput = function (contents) { - var i, name, - newPart = this.labelPart(this.slotSpec), - idx = this.children.length - 1; + var newPart = this.labelPart(this.slotSpec), + i, name, idx; + + this.addInfix(); // +++ + idx = this.children.length - 1; if (contents) { newPart.setContents(contents); } else if (this.elementSpec === '%scriptVars' || @@ -12582,6 +12606,14 @@ MultiArgMorph.prototype.addInput = function (contents) { return newPart; }; +MultiArgMorph.prototype.addInfix = function () { + var infix; + if (this.infix === '' || !this.inputs().length) {return; } + infix = this.labelPart(this.infix); + infix.parent = this; + this.children.splice(this.children.length - 1, 0, infix); +}; + MultiArgMorph.prototype.removeInput = function () { var oldPart, scripts; if (this.children.length > 1) { @@ -12595,6 +12627,11 @@ MultiArgMorph.prototype.removeInput = function () { } } } + if (this.infix !== '') { // +++ + if (this.children.length > 1) { + this.removeChild(this.children[this.children.length - 2]); + } + } this.fixLayout(); }; diff --git a/src/objects.js b/src/objects.js index 5da7ba37..3829c2b3 100644 --- a/src/objects.js +++ b/src/objects.js @@ -87,7 +87,7 @@ BlockVisibilityDialogMorph, CostumeIconMorph, SoundIconMorph*/ /*jshint esversion: 6*/ -modules.objects = '2022-February-22'; +modules.objects = '2022-February-28'; var SpriteMorph; var StageMorph; @@ -1104,6 +1104,12 @@ SpriteMorph.prototype.initBlocks = function () { category: 'operators', spec: '%n + %n' }, + reportVariadicSum: { // +++ under construction + // +++ dev: true, + type: 'reporter', + category: 'operators', + spec: '%sum' + }, reportDifference: { type: 'reporter', category: 'operators', @@ -1681,6 +1687,10 @@ SpriteMorph.prototype.initBlockMigrations = function () { doSend: { selector: 'doBroadcast', expand: 1 + }, + reportSum: { // +++ + selector: 'reportVariadicSum', + variadic: true } }; }; @@ -2663,7 +2673,8 @@ SpriteMorph.prototype.blockTemplates = function ( blocks.push(block('reifyPredicate')); blocks.push('#'); blocks.push('-'); - blocks.push(block('reportSum')); +// blocks.push(block('reportSum')); + blocks.push(block('reportVariadicSum')); blocks.push(block('reportDifference')); blocks.push(block('reportProduct')); blocks.push(block('reportQuotient')); @@ -2708,6 +2719,7 @@ SpriteMorph.prototype.blockTemplates = function ( blocks.push('-'); blocks.push(block('reportTypeOf')); blocks.push(block('reportTextFunction')); + blocks.push(block('reportVariadicSum')); // +++ } } else if (category === 'variables') { @@ -9088,7 +9100,8 @@ StageMorph.prototype.blockTemplates = function ( blocks.push(block('reifyPredicate')); blocks.push('#'); blocks.push('-'); - blocks.push(block('reportSum')); +// +++ blocks.push(block('reportSum')); + blocks.push(block('reportVariadicSum')); blocks.push(block('reportDifference')); blocks.push(block('reportProduct')); blocks.push(block('reportQuotient')); @@ -9134,6 +9147,7 @@ StageMorph.prototype.blockTemplates = function ( blocks.push('-'); blocks.push(block('reportTypeOf')); blocks.push(block('reportTextFunction')); + blocks.push(block('reportVariadicSum')); // +++ } } diff --git a/src/store.js b/src/store.js index daa934fe..402e317f 100644 --- a/src/store.js +++ b/src/store.js @@ -63,7 +63,7 @@ Project*/ // Global stuff //////////////////////////////////////////////////////// -modules.store = '2022-January-02'; +modules.store = '2022-February-28'; // XML_Serializer /////////////////////////////////////////////////////// /* @@ -1189,7 +1189,8 @@ SnapSerializer.prototype.loadComment = function (model) { SnapSerializer.prototype.loadBlock = function (model, isReporter, object) { // private var block, info, inputs, isGlobal, receiver, migration, - migrationOffset = 0; + migrationOffset = 0, + migratoToVariadic = false; // +++ if (model.tag === 'block') { if (Object.prototype.hasOwnProperty.call( @@ -1206,6 +1207,7 @@ SnapSerializer.prototype.loadBlock = function (model, isReporter, object) { ]; if (migration) { migrationOffset = migration.offset || 0; + migratoToVariadic = migration.variadic; // +++ } } } else if (model.tag === 'custom-block') { @@ -1265,7 +1267,14 @@ SnapSerializer.prototype.loadBlock = function (model, isReporter, object) { } else if (child.tag === 'receiver') { nop(); // ignore } else { - this.loadInput(child, inputs[i + migrationOffset], block, object); + if (migratoToVariadic) { // +++ + // assume all formerly single inputs are now part of the first + // one which is variadic and already expanded to hold them + // example: migrate old infix addition to new variadic infix sum + this.loadInput(child, inputs[0].inputs()[i], inputs[0], object); // +++ + } else { + this.loadInput(child, inputs[i + migrationOffset], block, object); // +++ + } } }); block.cachedInputs = null; diff --git a/src/threads.js b/src/threads.js index 6b0610a4..2991c618 100644 --- a/src/threads.js +++ b/src/threads.js @@ -64,7 +64,7 @@ SnapExtensions, AlignmentMorph, TextMorph, Cloud, HatBlockMorph*/ /*jshint esversion: 6*/ -modules.threads = '2022-February-26'; +modules.threads = '2022-February-28'; var ThreadManager; var Process; @@ -4010,6 +4010,10 @@ Process.prototype.isMatrix = function (data) { // Process math primtives - arithmetic +Process.prototype.reportVariadicSum = function (list) { // +++ + return this.reportListAggregation(list, 'reportSum'); +}; + Process.prototype.reportSum = function (a, b) { return this.hyperDyadic(this.reportBasicSum, a, b); }; diff --git a/sw.js b/sw.js index ddd352a5..8bd93555 100644 --- a/sw.js +++ b/sw.js @@ -1,4 +1,4 @@ -var snapVersion = '7.2.6-dev', +var snapVersion = '7.3.0-dev', cacheName = 'snap-pwa', filesToCache = [ 'snap.html',