From 608027c957861da15380da77014aed8bde9e1651 Mon Sep 17 00:00:00 2001 From: jmoenig Date: Wed, 1 Dec 2021 19:51:01 +0100 Subject: [PATCH] equality testing for scripts, experimental --- HISTORY.md | 1 + src/blocks.js | 11 ++++++++++- src/threads.js | 17 ++++++++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 440c44da..dc376471 100755 --- a/HISTORY.md +++ b/HISTORY.md @@ -70,6 +70,7 @@ ### 2021-12-01 * blocks, threads: block-assembly support refactorings, experimental +* blocks, threads: equality testing for scripts, experimental ### 2021-11-30 * blocks, threads: block-assembly support, experimental diff --git a/src/blocks.js b/src/blocks.js index 9dd1a4a2..b7aa7fe8 100644 --- a/src/blocks.js +++ b/src/blocks.js @@ -3710,7 +3710,15 @@ BlockMorph.prototype.components = function () { throw new Error('subclass responsility'); }; +BlockMorph.prototype.equalTo = function (other) { + // private - only to be called from a Context + return this.constructor.name === other.constructor.name && + this.selector === other.selector && + this.blockSpec === other.blockSpec; +}; + BlockMorph.prototype.copyWithInputs = function (inputs) { + // private - only to be called from a Context var cpy = this.fullCopy(), slots = cpy.inputs(), dta = inputs.itemsArray().map(inp => @@ -5489,7 +5497,8 @@ CommandBlockMorph.prototype.components = function () { } expr.fixBlockColor(null, true); inputs = expr.inputs(); - if (!inputs.length) { + if (!inputs.length || + inputs.every(slot => slot.isEmptySlot && slot.isEmptySlot())) { return expr.reify(); } parts = new List([expr.reify()]); diff --git a/src/threads.js b/src/threads.js index 78dad994..db5d8394 100644 --- a/src/threads.js +++ b/src/threads.js @@ -87,8 +87,9 @@ const NONNUMBERS = [true, false, '']; })(); function snapEquals(a, b) { - if (a instanceof List || (b instanceof List)) { - if (a instanceof List && (b instanceof List)) { + // lists, functions and blocks + if (a.equalTo || b.equalTo) { + if (a.constructor.name === b.constructor.name) { return a.equalTo(b); } return false; @@ -7012,7 +7013,8 @@ Context.prototype.isInCustomBlock = function () { // Context components - EXPERIMENTAL Context.prototype.components = function () { - var expr = this.expression.components(), + var expr = this.expression.components ? + this.expression.components() : new Context(), parts; if (!this.inputs.length) { return expr; @@ -7023,6 +7025,15 @@ Context.prototype.components = function () { return parts; }; +Context.prototype.equalTo = function (other) { + var c1 = this.components(), + c2 = other.components(); + if (c1 instanceof Context && (c2 instanceof Context)) { + return snapEquals(c1.expression, c2.expression); + } + return snapEquals(c1, c2); +}; + Context.prototype.copyWithInputs = function (inputs) { return this.expression.copyWithInputs(inputs); };