reporterize: support for monads etc.

pull/29/head
jmoenig 2016-12-19 16:34:54 +01:00
rodzic 41e55064a2
commit 9f0a445319
1 zmienionych plików z 47 dodań i 13 usunięć

Wyświetl plik

@ -2638,8 +2638,7 @@ SpriteMorph.prototype.searchBlocks = function (
SpriteMorph.prototype.reporterize = function (expressionString) { SpriteMorph.prototype.reporterize = function (expressionString) {
// highly experimental Christmas Easter Egg 2016 :-) // highly experimental Christmas Easter Egg 2016 :-)
var ast, var ast;
myself = this;
function parseInfix(expression, operator, already) { function parseInfix(expression, operator, already) {
// very basic binary infix parser for arithmetic expressions // very basic binary infix parser for arithmetic expressions
@ -2683,12 +2682,20 @@ SpriteMorph.prototype.reporterize = function (expressionString) {
case ' ': case ' ':
break; break;
case '(': case '(':
inputs[operator ? 1 : 0] = parseInfix(nested()); if (inputs[operator ? 1 : 0].length) {
inputs[operator ? 1 : 0] = [
inputs[operator ? 1 : 0],
parseInfix(nested())
];
} else {
inputs[operator ? 1 : 0] = parseInfix(nested());
}
break; break;
case '-': case '-':
case '+': case '+':
case '*': case '*':
case '/': case '/':
case '%':
if (!operator && !inputs[0].length) { if (!operator && !inputs[0].length) {
inputs[0] += ch; inputs[0] += ch;
} else if (operator) { } else if (operator) {
@ -2713,28 +2720,54 @@ SpriteMorph.prototype.reporterize = function (expressionString) {
} }
function blockFromAST(ast) { function blockFromAST(ast) {
var block, selectors, i, inps; var block, selectors, monads, i, inps;
selectors = { selectors = {
'+': 'reportSum', '+': 'reportSum',
'-': 'reportDifference', '-': 'reportDifference',
'*': 'reportProduct', '*': 'reportProduct',
'/': 'reportQuotient' '/': 'reportQuotient',
'%': 'reportModulus'
}; };
block = myself.blockForSelector(selectors[ast[0]], true); monads = ['abs', 'ceiling', 'floor', 'sqrt', 'sin', 'cos', 'tan',
block.isDraggable = true; 'asin', 'acos', 'atan', 'ln', 'log', 'round'];
inps = block.inputs(); if (contains(monads, ast[0])) { // monad
for (i = 1; i < 3; i += 1) { if (ast[0] === 'round') {
block = SpriteMorph.prototype.blockForSelector('reportRound');
inps = block.inputs();
i = 0;
} else {
block = SpriteMorph.prototype.blockForSelector('reportMonadic');
inps = block.inputs();
inps[0].setContents([ast[0]]);
i = 1;
}
if (ast[i] instanceof Array) { if (ast[i] instanceof Array) {
block.silentReplaceInput(inps[i - 1], blockFromAST(ast[i])); block.silentReplaceInput(inps[i], blockFromAST(ast[i]));
} else if (isString(ast[i])) { } else if (isString(ast[i])) {
block.silentReplaceInput( block.silentReplaceInput(
inps[i - 1], inps[i],
myself.variableBlock(ast[i]) SpriteMorph.prototype.variableBlock(ast[i])
); );
} else { // number } else { // number
inps[i - 1].setContents(ast[i]); inps[i].setContents(ast[i]);
}
} else { // diadic
block = SpriteMorph.prototype.blockForSelector(selectors[ast[0]]);
inps = block.inputs();
for (i = 1; i < 3; i += 1) {
if (ast[i] instanceof Array) {
block.silentReplaceInput(inps[i - 1], blockFromAST(ast[i]));
} else if (isString(ast[i])) {
block.silentReplaceInput(
inps[i - 1],
SpriteMorph.prototype.variableBlock(ast[i])
);
} else { // number
inps[i - 1].setContents(ast[i]);
}
} }
} }
block.isDraggable = true;
block.fixLayout(); block.fixLayout();
block.fixBlockColor(null, true); block.fixBlockColor(null, true);
return block; return block;
@ -6451,6 +6484,7 @@ StageMorph.prototype.palette = SpriteMorph.prototype.palette;
StageMorph.prototype.freshPalette = SpriteMorph.prototype.freshPalette; StageMorph.prototype.freshPalette = SpriteMorph.prototype.freshPalette;
StageMorph.prototype.blocksMatching = SpriteMorph.prototype.blocksMatching; StageMorph.prototype.blocksMatching = SpriteMorph.prototype.blocksMatching;
StageMorph.prototype.searchBlocks = SpriteMorph.prototype.searchBlocks; StageMorph.prototype.searchBlocks = SpriteMorph.prototype.searchBlocks;
StageMorph.prototype.reporterize = SpriteMorph.prototype.reporterize;
StageMorph.prototype.showingWatcher = SpriteMorph.prototype.showingWatcher; StageMorph.prototype.showingWatcher = SpriteMorph.prototype.showingWatcher;
StageMorph.prototype.addVariable = SpriteMorph.prototype.addVariable; StageMorph.prototype.addVariable = SpriteMorph.prototype.addVariable;
StageMorph.prototype.deleteVariable = SpriteMorph.prototype.deleteVariable; StageMorph.prototype.deleteVariable = SpriteMorph.prototype.deleteVariable;