simplified reporterize>>blockFromAST

pull/29/head
jmoenig 2016-12-22 14:08:05 +01:00
rodzic d6ced6fa2b
commit b015294208
2 zmienionych plików z 27 dodań i 42 usunięć

Wyświetl plik

@ -3206,6 +3206,10 @@ http://snap.berkeley.edu/run#cloud:Username=jens&ProjectName=rotation
------ ------
* Objects: added Boolean operators to “reporterize” * Objects: added Boolean operators to “reporterize”
161222
------
* Objects: simplified reporterize>>blockFromAST
== v4.10 === (in development) == v4.10 === (in development)
Features: Features:

Wyświetl plik

@ -82,7 +82,7 @@ SpeechBubbleMorph, RingMorph, isNil, FileReader, TableDialogMorph,
BlockEditorMorph, BlockDialogMorph, PrototypeHatBlockMorph, localize, BlockEditorMorph, BlockDialogMorph, PrototypeHatBlockMorph, localize,
TableMorph, TableFrameMorph, normalizeCanvas, BooleanSlotMorph*/ TableMorph, TableFrameMorph, normalizeCanvas, BooleanSlotMorph*/
modules.objects = '2016-December-21'; modules.objects = '2016-December-22';
var SpriteMorph; var SpriteMorph;
var StageMorph; var StageMorph;
@ -2641,7 +2641,7 @@ SpriteMorph.prototype.reporterize = function (expressionString) {
var ast; var ast;
function parseInfix(expression, operator, already) { function parseInfix(expression, operator, already) {
// very basic binary infix parser for arithmetic expressions // very basic diadic infix parser for arithmetic expressions
// with strict left-to-right operator precedence (like in Smalltalk) // with strict left-to-right operator precedence (like 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.
@ -2725,7 +2725,8 @@ SpriteMorph.prototype.reporterize = function (expressionString) {
} }
function blockFromAST(ast) { function blockFromAST(ast) {
var block, selectors, monads, alias, key, sel, i, inps; var block, selectors, monads, alias, key, sel, i, inps,
off = 1;
selectors = { selectors = {
'+': 'reportSum', '+': 'reportSum',
'-': 'reportDifference', '-': 'reportDifference',
@ -2747,60 +2748,40 @@ SpriteMorph.prototype.reporterize = function (expressionString) {
'!' : 'not' '!' : 'not'
}; };
key = alias[ast[0]] || ast[0]; key = alias[ast[0]] || ast[0];
if (contains(monads, key)) { if (contains(monads, key)) { // monadic
sel = selectors[key]; sel = selectors[key];
if (sel) { if (sel) { // single input
block = SpriteMorph.prototype.blockForSelector(sel); block = SpriteMorph.prototype.blockForSelector(sel);
inps = block.inputs(); inps = block.inputs();
i = 0; } else { // two inputs, first is function name
} else {
block = SpriteMorph.prototype.blockForSelector('reportMonadic'); block = SpriteMorph.prototype.blockForSelector('reportMonadic');
inps = block.inputs(); inps = block.inputs();
inps[0].setContents([key]); inps[0].setContents([key]);
i = 1; off = 0;
}
if (ast[1] instanceof Array) {
block.silentReplaceInput(inps[i], blockFromAST(ast[1]));
} else if (isString(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])
);
}
} else { // number
inps[i].setContents(ast[1]);
} }
} else { // diadic } else { // diadic
block = SpriteMorph.prototype.blockForSelector(selectors[ast[0]]); block = SpriteMorph.prototype.blockForSelector(selectors[key]);
inps = block.inputs(); inps = block.inputs();
for (i = 1; i < 3; i += 1) { }
for (i = 1; i < ast.length; i += 1) {
if (ast[i] instanceof Array) { if (ast[i] instanceof Array) {
block.silentReplaceInput(inps[i - 1], blockFromAST(ast[i])); block.silentReplaceInput(inps[i - off], blockFromAST(ast[i]));
} else if (isString(ast[i])) { } else if (isString(ast[i])) {
if (contains(['true', 'false'], ast[i])) { if (contains(['true', 'false'], ast[i])) {
block.silentReplaceInput( block.silentReplaceInput(
inps[i - 1], inps[i - off],
SpriteMorph.prototype.blockForSelector( SpriteMorph.prototype.blockForSelector(
ast[i] === 'true' ? 'reportTrue' : 'reportFalse' ast[i] === 'true' ? 'reportTrue' : 'reportFalse'
) )
); );
} else if (ast[i] !== '_') { } else if (ast[i] !== '_') {
block.silentReplaceInput( block.silentReplaceInput(
inps[i - 1], inps[i - off],
SpriteMorph.prototype.variableBlock(ast[i]) SpriteMorph.prototype.variableBlock(ast[i])
); );
} }
} else { // number } else { // number
inps[i - 1].setContents(ast[i]); inps[i - off].setContents(ast[i]);
}
} }
} }
block.isDraggable = true; block.isDraggable = true;