summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjmoenig <jens@moenig.org>2014-12-04 15:45:18 +0100
committerjmoenig <jens@moenig.org>2014-12-04 15:45:18 +0100
commitad1fe34d1ed900e1e4fd75e5c5666f6ab28b9c80 (patch)
tree07a26fe13d14f44bc2cff96e493db77b1a026cce
parent17b6ae839b5c15c942440f38fc07bfcc45bc0811 (diff)
downloadsnap-yow-ad1fe34d1ed900e1e4fd75e5c5666f6ab28b9c80.tar.gz
snap-yow-ad1fe34d1ed900e1e4fd75e5c5666f6ab28b9c80.zip
Experimental “ForEach” primitive (hidden in dev mode)
-rw-r--r--objects.js13
-rw-r--r--threads.js22
2 files changed, 33 insertions, 2 deletions
diff --git a/objects.js b/objects.js
index 4ad376c..7277719 100644
--- a/objects.js
+++ b/objects.js
@@ -125,7 +125,7 @@ PrototypeHatBlockMorph*/
// Global stuff ////////////////////////////////////////////////////////
-modules.objects = '2014-December-03';
+modules.objects = '2014-December-04';
var SpriteMorph;
var StageMorph;
@@ -1151,6 +1151,13 @@ SpriteMorph.prototype.initBlocks = function () {
category: 'lists',
spec: 'map %repRing over %l'
},
+ doForEach: {
+ dev: true,
+ type: 'command',
+ category: 'lists',
+ spec: 'for %upvar in %l %cs',
+ defaults: [localize('each item')]
+ },
// Code mapping - experimental
doMapCodeOrHeader: { // experimental
@@ -2063,6 +2070,8 @@ SpriteMorph.prototype.blockTemplates = function (category) {
blocks.push(txt);
blocks.push('-');
blocks.push(block('reportMap'));
+ blocks.push('-');
+ blocks.push(block('doForEach'));
}
/////////////////////////////////
@@ -5198,6 +5207,8 @@ StageMorph.prototype.blockTemplates = function (category) {
blocks.push(txt);
blocks.push('-');
blocks.push(block('reportMap'));
+ blocks.push('-');
+ blocks.push(block('doForEach'));
}
/////////////////////////////////
diff --git a/threads.js b/threads.js
index 9f9a221..c21375e 100644
--- a/threads.js
+++ b/threads.js
@@ -83,7 +83,7 @@ ArgLabelMorph, localize, XML_Element, hex_sha512*/
// Global stuff ////////////////////////////////////////////////////////
-modules.threads = '2014-December-03';
+modules.threads = '2014-December-04';
var ThreadManager;
var Process;
@@ -1679,6 +1679,26 @@ Process.prototype.reportMap = function (reporter, list) {
}
};
+Process.prototype.doForEach = function (upvar, list, script) {
+ // perform a script for each element of a list, assigning the
+ // current iteration's element to a variable with the name
+ // specified in the "upvar" parameter, so it can be referenced
+ // within the script. Uses the context's - unused - fourth
+ // element as temporary storage for the current list index
+
+ if (isNil(this.context.inputs[3])) {this.context.inputs[3] = 1; }
+ var index = this.context.inputs[3];
+ this.context.outerContext.variables.addVar(upvar);
+ this.context.outerContext.variables.setVar(
+ upvar,
+ list.at(index)
+ );
+ if (index > list.length()) {return; }
+ this.context.inputs[3] += 1;
+ this.pushContext();
+ this.evaluate(script, new List(), true);
+};
+
// Process interpolated primitives
Process.prototype.doWait = function (secs) {