From d6ced6fa2bc3ca6683400d59f970367580a53c98 Mon Sep 17 00:00:00 2001 From: jmoenig Date: Wed, 21 Dec 2016 10:53:46 +0100 Subject: [PATCH] =?UTF-8?q?added=20Boolean=20operators=20to=20=E2=80=9Crep?= =?UTF-8?q?orterize=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- history.txt | 3 +++ objects.js | 46 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/history.txt b/history.txt index 5006c331..901af8f5 100755 --- a/history.txt +++ b/history.txt @@ -3202,6 +3202,9 @@ http://snap.berkeley.edu/run#cloud:Username=jens&ProjectName=rotation ------ * Objects: tweaked “reporterize” +161221 +------ +* Objects: added Boolean operators to “reporterize” == v4.10 === (in development) diff --git a/objects.js b/objects.js index b2c22e17..6320b20b 100644 --- a/objects.js +++ b/objects.js @@ -2696,6 +2696,11 @@ SpriteMorph.prototype.reporterize = function (expressionString) { case '*': case '/': case '%': + case '=': + case '<': + case '>': + case '&': + case '|': if (!operator && !inputs[0].length) { inputs[0] += ch; } else if (operator) { @@ -2720,21 +2725,32 @@ SpriteMorph.prototype.reporterize = function (expressionString) { } function blockFromAST(ast) { - var block, selectors, monads, alias, key, i, inps; + var block, selectors, monads, alias, key, sel, i, inps; selectors = { '+': 'reportSum', '-': 'reportDifference', '*': 'reportProduct', '/': 'reportQuotient', - '%': 'reportModulus' + '%': 'reportModulus', + '=': 'reportEquals', + '<': 'reportLessThan', + '>': 'reportGreaterThan', + '&': 'reportAnd', + '|': 'reportOr', + round: 'reportRound', + not: 'reportNot' }; monads = ['abs', 'ceiling', 'floor', 'sqrt', 'sin', 'cos', 'tan', - 'asin', 'acos', 'atan', 'ln', 'log', 'round']; - alias = {ceil: 'ceiling'}; + 'asin', 'acos', 'atan', 'ln', 'log', 'round', 'not']; + alias = { + ceil: 'ceiling', + '!' : 'not' + }; key = alias[ast[0]] || ast[0]; if (contains(monads, key)) { - if (key === 'round') { - block = SpriteMorph.prototype.blockForSelector('reportRound'); + sel = selectors[key]; + if (sel) { + block = SpriteMorph.prototype.blockForSelector(sel); inps = block.inputs(); i = 0; } else { @@ -2746,7 +2762,14 @@ SpriteMorph.prototype.reporterize = function (expressionString) { if (ast[1] instanceof Array) { block.silentReplaceInput(inps[i], blockFromAST(ast[1])); } else if (isString(ast[1])) { - if (ast[1] !== '_') { + if (contains(['true', 'false'], ast[1])) { + block.silentReplaceInput( + inps[i], + SpriteMorph.prototype.blockForSelector( + ast[1] === 'true' ? 'reportTrue' : 'reportFalse' + ) + ); + } else if (ast[1] !== '_') { block.silentReplaceInput( inps[i], SpriteMorph.prototype.variableBlock(ast[1]) @@ -2762,7 +2785,14 @@ SpriteMorph.prototype.reporterize = function (expressionString) { if (ast[i] instanceof Array) { block.silentReplaceInput(inps[i - 1], blockFromAST(ast[i])); } else if (isString(ast[i])) { - if (ast[i] !== '_') { + if (contains(['true', 'false'], ast[i])) { + block.silentReplaceInput( + inps[i - 1], + SpriteMorph.prototype.blockForSelector( + ast[i] === 'true' ? 'reportTrue' : 'reportFalse' + ) + ); + } else if (ast[i] !== '_') { block.silentReplaceInput( inps[i - 1], SpriteMorph.prototype.variableBlock(ast[i])