Corrected casting numbers and fixed blocks under ifs not running

This commit is contained in:
MathWizz 2013-11-02 00:38:29 -05:00
parent d25c07538a
commit 2778e0a3fd
6 changed files with 107 additions and 82 deletions

View file

@ -139,13 +139,23 @@ Interpreter.prototype.stepActiveThread = function() {
b.primFcn(b);
if (this.yield) { this.activeThread.nextBlock = b; return; }
b = this.activeThread.nextBlock; // refresh local variable b in case primitive did some control flow
while (b == null) {
while (!b) {
// end of a substack; pop the owning control flow block from stack
// Note: This is a loop to handle nested control flow blocks.
b = this.activeThread.stack.pop();
if ((b == null) || (b.isLoop)) {
this.activeThread.nextBlock = b;
return; // yield at the end of a loop or when stack is empty
// yield at the end of a loop or when stack is empty
if (this.activeThread.stack.length === 0) {
this.activeThread.nextBlock = null;
return;
} else {
b = this.activeThread.stack.pop();
debugger;
if (b.isLoop) {
this.activeThread.nextBlock = b; // preserve where it left off
return;
} else {
b = b.nextBlock; // skip and continue for non looping blocks
}
}
}
}
@ -194,7 +204,26 @@ Interpreter.prototype.arg = function(block, index) {
}
return arg;
}
Interpreter.prototype.numarg = function(block, index) {
var arg = parseFloat(this.arg(block, index));
if (arg !== arg) {
return 0;
}
return arg;
}
Interpreter.prototype.boolarg = function(block, index) {
var arg = this.arg(block, index);
if (typeof arg === 'boolean') {
return arg;
} else if (typeof arg === 'string') {
return arg === '' || arg === '0' || arg.toLowerCase() === 'false';
}
return arg;
}
Interpreter.prototype.targetSprite = function() {
return this.activeThread.target;
}
@ -231,14 +260,14 @@ Interpreter.prototype.initPrims = function() {
this.primitiveTable['whenGreenFlag'] = this.primNoop;
this.primitiveTable['whenKeyPressed'] = this.primNoop;
this.primitiveTable['whenClicked'] = this.primNoop;
this.primitiveTable['if'] = function(b) { if (interp.arg(b, 0)) interp.startSubstack(b) };
this.primitiveTable['if'] = function(b) { if (interp.boolarg(b, 0)) interp.startSubstack(b) };
this.primitiveTable['doForever'] = function(b) { interp.startSubstack(b, true) };
this.primitiveTable['doForeverIf'] = function(b) { if (interp.arg(b, 0)) interp.startSubstack(b, true); else interp.yield = true; };
this.primitiveTable['doIf'] = function(b) { if (interp.arg(b, 0)) interp.startSubstack(b); };
this.primitiveTable['doForeverIf'] = function(b) { if (interp.boolarg(b, 0)) interp.startSubstack(b, true); else interp.yield = true; };
this.primitiveTable['doIf'] = function(b) { if (interp.boolarg(b, 0)) interp.startSubstack(b); };
this.primitiveTable['doRepeat'] = this.primRepeat;
this.primitiveTable['doIfElse'] = function(b) { if (interp.arg(b, 0)) interp.startSubstack(b); else interp.startSubstack(b, false, true); };
this.primitiveTable['doWaitUntil'] = function(b) { if (!interp.arg(b, 0)) interp.yield = true };
this.primitiveTable['doUntil'] = function(b) { if (!interp.arg(b, 0)) interp.startSubstack(b, true) };
this.primitiveTable['doIfElse'] = function(b) { if (interp.boolarg(b, 0)) interp.startSubstack(b); else interp.startSubstack(b, false, true); };
this.primitiveTable['doWaitUntil'] = function(b) { if (!interp.boolarg(b, 0)) interp.yield = true };
this.primitiveTable['doUntil'] = function(b) { if (!interp.boolarg(b, 0)) interp.startSubstack(b, true) };
this.primitiveTable['doReturn'] = function(b) { interp.activeThread = new Thread(null); };
this.primitiveTable['stopAll'] = function(b) { interp.activeThread = new Thread(null); interp.threads = []; }
this.primitiveTable['whenIReceive'] = this.primNoop;
@ -264,13 +293,13 @@ Interpreter.prototype.lookupPrim = function(op) {
Interpreter.prototype.primNoop = function(b) { console.log(b.op); }
Interpreter.prototype.primWait = function(b) {
if (interp.activeThread.firstTime) interp.startTimer(interp.arg(b, 0));
if (interp.activeThread.firstTime) interp.startTimer(interp.numarg(b, 0));
else interp.checkTimer();
}
Interpreter.prototype.primRepeat = function(b) {
if (b.tmp == -1) {
b.tmp = Math.max(interp.arg(b, 0), 0); // Initialize repeat count on this block
b.tmp = Math.max(interp.numarg(b, 0), 0); // Initialize repeat count on this block
}
if (b.tmp > 0) {
b.tmp -= 1; // decrement count
@ -323,10 +352,8 @@ Interpreter.prototype.isRunning = function(b) {
Interpreter.prototype.startSubstack = function(b, isLoop, secondSubstack) {
// Start the substack of a control structure command such as if or forever.
if (isLoop) {
b.isLoop = true;
this.activeThread.stack.push(b); // remember the block that started the substack
}
b.isLoop = !!isLoop;
this.activeThread.stack.push(b); // remember the block that started the substack
if(!secondSubstack) {
this.activeThread.nextBlock = b.substack;
} else {

View file

@ -79,8 +79,8 @@ LooksPrims.prototype.primShowCostume = function(b) {
if (i >= 0) {
s.showCostume(i);
} else {
var n = Number(arg);
if (!isNaN(n)) {
var n = parseInt(arg, 10);
if (n === n) { // if n is not NaN
s.showCostume(n - 1);
} else {
return; // arg did not match a costume name nor is a valid number
@ -104,8 +104,8 @@ LooksPrims.prototype.primStartScene = function(b) {
if (i >= 0) {
s.showCostume(i);
} else {
var n = Number(arg);
if (!isNaN(n)) {
var n = parseInt(arg, 10);
if (n === n) { // fast !isNaN check
s.showCostume(n - 1);
} else {
return; // arg did not match a costume name nor is a valid number
@ -123,14 +123,14 @@ LooksPrims.prototype.primCostumeNum = function(b) {
LooksPrims.prototype.primChangeSize = function(b) {
var s = interp.targetSprite();
if (s == null) return;
s.setSize(s.getSize() + interp.arg(b, 0));
s.setSize(s.getSize() + interp.numarg(b, 0));
if (s.visible) interp.redraw();
}
LooksPrims.prototype.primSetSize = function(b) {
var s = interp.targetSprite();
if (s == null) return;
s.setSize(interp.arg(b, 0));
s.setSize(interp.numarg(b, 0));
if (s.visible) interp.redraw();
}
@ -148,7 +148,7 @@ LooksPrims.prototype.primGoFront = function(b) {
LooksPrims.prototype.primGoBack = function(b) {
var s = interp.targetSprite();
runtime.reassignZ(s, interp.arg(b, 0));
runtime.reassignZ(s, interp.numarg(b, 0));
if(s.visible) interp.redraw();
}
@ -168,7 +168,7 @@ var showBubbleAndWait = function(b, type) {
if (s == null) return;
if (interp.activeThread.firstTime) {
var text = interp.arg(b, 0);
var secs = interp.arg(b, 1);
var secs = interp.numarg(b, 1);
s.showBubble(text, type);
if (s.visible) interp.redraw();
interp.startTimer(secs);

View file

@ -57,7 +57,7 @@ MotionAndPenPrims.prototype.addPrimsTo = function(primTable) {
MotionAndPenPrims.prototype.primMove = function(b) {
var s = interp.targetSprite();
var radians = ((Math.PI * (90 - s.direction)) / 180);
var d = interp.arg(b, 0);
var d = interp.numarg(b, 0);
moveSpriteTo(s, s.scratchX + (d * Math.cos(radians)),
s.scratchY + (d * Math.sin(radians)));
@ -66,28 +66,28 @@ MotionAndPenPrims.prototype.primMove = function(b) {
MotionAndPenPrims.prototype.primTurnLeft = function(b) {
var s = interp.targetSprite();
var d = s.direction - interp.arg(b, 0);
var d = s.direction - interp.numarg(b, 0);
s.setDirection(d);
if(s.visible) interp.redraw();
}
MotionAndPenPrims.prototype.primTurnRight = function(b) {
var s = interp.targetSprite();
var d = s.direction + interp.arg(b, 0);
var d = s.direction + interp.numarg(b, 0);
s.setDirection(d);
if(s.visible) interp.redraw();
}
MotionAndPenPrims.prototype.primSetDirection = function(b) {
var s = interp.targetSprite();
s.setDirection(interp.arg(b, 0));
s.setDirection(interp.numarg(b, 0));
if(s.visible) interp.redraw();
}
MotionAndPenPrims.prototype.primPointTowards = function(b) {
var s = interp.targetSprite();
var p = mouseOrSpritePosition(interp.arg(b, 0));
if ((s == null) || (p == null)) return;
var p = mouseOrSpritePosition(interp.numarg(b, 0));
if (s == null) return;
var dx = p.x - s.scratchX;
var dy = p.y - s.scratchY;
var angle = 90 - ((Math.atan2(dy, dx) * 180) / Math.PI);
@ -97,7 +97,7 @@ MotionAndPenPrims.prototype.primPointTowards = function(b) {
MotionAndPenPrims.prototype.primGoTo = function(b) {
var s = interp.targetSprite();
if (s != null) moveSpriteTo(s, interp.arg(b, 0), interp.arg(b, 1));
if (s != null) moveSpriteTo(s, interp.numarg(b, 0), interp.numarg(b, 1));
}
MotionAndPenPrims.prototype.primGoToSpriteOrMouse = function(b) {
@ -111,9 +111,9 @@ MotionAndPenPrims.prototype.primGlide = function(b) {
var s = interp.targetSprite();
if (s == null) return;
if (interp.activeThread.firstTime) {
var secs = interp.arg(b, 0);
var destX = interp.arg(b, 1);
var destY = interp.arg(b, 2);
var secs = interp.numarg(b, 0);
var destX = interp.numarg(b, 1);
var destY = interp.numarg(b, 2);
if (secs <= 0) {
moveSpriteTo(s, destX, destY);
return;
@ -140,22 +140,22 @@ MotionAndPenPrims.prototype.primGlide = function(b) {
MotionAndPenPrims.prototype.primChangeX = function(b) {
var s = interp.targetSprite();
if (s != null) moveSpriteTo(s, s.scratchX + interp.arg(b, 0), s.scratchY);
if (s != null) moveSpriteTo(s, s.scratchX + interp.numarg(b, 0), s.scratchY);
}
MotionAndPenPrims.prototype.primSetX = function(b) {
var s = interp.targetSprite();
if (s != null) moveSpriteTo(s, interp.arg(b, 0), s.scratchY);
if (s != null) moveSpriteTo(s, interp.numarg(b, 0), s.scratchY);
}
MotionAndPenPrims.prototype.primChangeY = function(b) {
var s = interp.targetSprite();
if (s != null) moveSpriteTo(s, s.scratchX, s.scratchY + interp.arg(b, 0));
if (s != null) moveSpriteTo(s, s.scratchX, s.scratchY + interp.numarg(b, 0));
}
MotionAndPenPrims.prototype.primSetY = function(b) {
var s = interp.targetSprite();
if (s != null) moveSpriteTo(s, s.scratchX, interp.arg(b, 0));
if (s != null) moveSpriteTo(s, s.scratchX, interp.numarg(b, 0));
}
MotionAndPenPrims.prototype.primBounceOffEdge = function(b) {
@ -211,38 +211,38 @@ MotionAndPenPrims.prototype.primPenUp = function(b) {
MotionAndPenPrims.prototype.primSetPenColor = function(b) {
var s = interp.targetSprite();
if (s != null) s.setPenColor(interp.arg(b, 0));
if (s != null) s.setPenColor(interp.numarg(b, 0));
}
MotionAndPenPrims.prototype.primSetPenHue = function(b) {
var s = interp.targetSprite();
if (s != null) s.setPenHue(interp.arg(b, 0));
if (s != null) s.setPenHue(interp.numarg(b, 0));
}
MotionAndPenPrims.prototype.primChangePenHue = function(b) {
var s = interp.targetSprite();
if (s != null) s.setPenHue(s.penHue + interp.arg(b, 0));
if (s != null) s.setPenHue(s.penHue + interp.numarg(b, 0));
}
MotionAndPenPrims.prototype.primSetPenShade = function(b) {
var s = interp.targetSprite();
if (s != null) s.setPenShade(interp.arg(b, 0));
if (s != null) s.setPenShade(interp.numarg(b, 0));
}
MotionAndPenPrims.prototype.primChangePenShade = function(b) {
var s = interp.targetSprite();
if (s != null) s.setPenShade(s.penShade + interp.arg(b, 0));
if (s != null) s.setPenShade(s.penShade + interp.numarg(b, 0));
}
MotionAndPenPrims.prototype.primSetPenSize = function(b) {
var s = interp.targetSprite();
var w = Math.max(0, Math.min(interp.arg(b, 0), 100));
var w = Math.max(0, Math.min(interp.numarg(b, 0), 100));
if (s != null) s.penWidth = w;
}
MotionAndPenPrims.prototype.primChangePenSize = function(b) {
var s = interp.targetSprite();
var w = Math.max(0, Math.min(s.penWidth + interp.arg(b, 0), 100));
var w = Math.max(0, Math.min(s.penWidth + interp.numarg(b, 0), 100));
if (s != null) s.penWidth = w;
}
@ -253,7 +253,7 @@ MotionAndPenPrims.prototype.primStamp = function(b) {
MotionAndPenPrims.prototype.primStampTransparent = function(b) {
var s = interp.targetSprite();
var transparency = Math.max(0, Math.min(interp.arg(b, 0), 100));
var transparency = Math.max(0, Math.min(interp.numarg(b, 0), 100));
var alpha = 100 - transparency;
s.stamp(runtime.stage.lineCache, alpha);
}

View file

@ -26,23 +26,23 @@ var Primitives = function() {}
Primitives.prototype.addPrimsTo = function(primTable) {
// Math primitives
primTable["+"] = function(b) { return interp.arg(b, 0) + interp.arg(b, 1) };
primTable["-"] = function(b) { return interp.arg(b, 0) - interp.arg(b, 1) };
primTable["*"] = function(b) { return interp.arg(b, 0) * interp.arg(b, 1) };
primTable["/"] = function(b) { return interp.arg(b, 0) / interp.arg(b, 1) };
primTable["%"] = function(b) { return interp.arg(b, 0) % interp.arg(b, 1) };
primTable["+"] = function(b) { return interp.numarg(b, 0) + interp.numarg(b, 1) };
primTable["-"] = function(b) { return interp.numarg(b, 0) - interp.numarg(b, 1) };
primTable["*"] = function(b) { return interp.numarg(b, 0) * interp.numarg(b, 1) };
primTable["/"] = function(b) { return interp.numarg(b, 0) / interp.numarg(b, 1) };
primTable["%"] = function(b) { return interp.numarg(b, 0) % interp.numarg(b, 1) };
primTable["randomFrom:to:"] = this.primRandom;
primTable["<"] = function(b) { return (interp.arg(b, 0) < interp.arg(b, 1)) };
primTable["<"] = function(b) { return (interp.numarg(b, 0) < interp.numarg(b, 1)) };
primTable["="] = function(b) { return (interp.arg(b, 0) == interp.arg(b, 1)) };
primTable[">"] = function(b) { return (interp.arg(b, 0) > interp.arg(b, 1)) };
primTable["&"] = function(b) { return interp.arg(b, 0) && interp.arg(b, 1) };
primTable["|"] = function(b) { return interp.arg(b, 0) || interp.arg(b, 1) };
primTable["not"] = function(b) { return !interp.arg(b, 0) };
primTable["abs"] = function(b) { return Math.abs(interp.arg(b, 0)) };
primTable["sqrt"] = function(b) { return Math.sqrt(interp.arg(b, 0)) };
primTable[">"] = function(b) { return (interp.numarg(b, 0) > interp.numarg(b, 1)) };
primTable["&"] = function(b) { return interp.boolarg(b, 0) && interp.boolarg(b, 1) };
primTable["|"] = function(b) { return interp.boolarg(b, 0) || interp.boolarg(b, 1) };
primTable["not"] = function(b) { return !interp.boolarg(b, 0) };
primTable["abs"] = function(b) { return Math.abs(interp.numarg(b, 0)) };
primTable["sqrt"] = function(b) { return Math.sqrt(interp.numarg(b, 0)) };
primTable["\\\\"] = this.primModulo;
primTable["rounded"] = function(b) { return Math.round(interp.arg(b, 0)) };
primTable["rounded"] = function(b) { return Math.round(interp.numarg(b, 0)) };
primTable["computeFunction:of:"] = this.primMathFunction;
// String primitives
@ -58,8 +58,8 @@ Primitives.prototype.addPrimsTo = function(primTable) {
}
Primitives.prototype.primRandom = function(b) {
var n1 = interp.arg(b, 0);
var n2 = interp.arg(b, 1);
var n1 = interp.numarg(b, 0);
var n2 = interp.numarg(b, 1);
var low = (n1 <= n2) ? n1 : n2;
var hi = (n1 <= n2) ? n2 : n1;
if(low == hi) return low;
@ -72,21 +72,21 @@ Primitives.prototype.primRandom = function(b) {
Primitives.prototype.primLetterOf = function(b) {
var s = interp.arg(b, 1);
var i = interp.arg(b, 0) - 1;
var i = interp.numarg(b, 0) - 1;
if ((i < 0) || (i >= s.length)) return "";
return s.charAt(i);
}
Primitives.prototype.primModulo = function(b) {
var modulus = interp.arg(b, 1);
var n = interp.arg(b, 0) % modulus;
var modulus = interp.numarg(b, 1);
var n = interp.numarg(b, 0) % modulus;
if (n < 0) n += modulus;
return n;
}
Primitives.prototype.primMathFunction = function(b) {
var op = interp.arg(b, 0);
var n = interp.arg(b, 1);
var op = interp.numarg(b, 0);
var n = interp.numarg(b, 1);
switch(op) {
case "abs": return Math.abs(n);
case "sqrt": return Math.sqrt(n);

View file

@ -155,8 +155,8 @@ SoundPrims.prototype.primPlayNote = function(b) {
var s = interp.targetSprite();
if (s == null) return;
if (interp.activeThread.firstTime) {
var key = interp.arg(b, 0);
var secs = beatsToSeconds(interp.arg(b, 1));
var key = interp.numarg(b, 0);
var secs = beatsToSeconds(interp.numarg(b, 1));
playNote(s.instrument, key, secs, s);
interp.startTimer(secs);
} else {
@ -168,8 +168,8 @@ SoundPrims.prototype.primPlayDrum = function(b) {
var s = interp.targetSprite();
if (s == null) return;
if (interp.activeThread.firstTime) {
var drum = Math.round(interp.arg(b, 0));
var secs = beatsToSeconds(interp.arg(b, 1));
var drum = Math.round(interp.numarg(b, 0));
var secs = beatsToSeconds(interp.numarg(b, 1));
playDrum(drum, secs, s);
interp.startTimer(secs);
} else {
@ -181,7 +181,7 @@ SoundPrims.prototype.primPlayRest = function(b) {
var s = interp.targetSprite();
if (s == null) return;
if (interp.activeThread.firstTime) {
var secs = beatsToSeconds(interp.arg(b, 0));
var secs = beatsToSeconds(interp.numarg(b, 0));
interp.startTimer(secs);
} else {
interp.checkTimer();
@ -199,12 +199,12 @@ SoundPrims.prototype.primStopAllSounds = function(b) {
SoundPrims.prototype.primChangeVolume = function(b) {
var s = interp.targetSprite();
if (s != null) s.volume += interp.arg(b, 0);
if (s != null) s.volume += interp.numarg(b, 0);
}
SoundPrims.prototype.primSetVolume = function(b) {
var s = interp.targetSprite();
if (s != null) s.volume = interp.arg(b, 0);
if (s != null) s.volume = interp.numarg(b, 0);
}
SoundPrims.prototype.primVolume = function(b) {

View file

@ -63,11 +63,9 @@ VarListPrims.prototype.primChangeVar = function(b) {
if (s == null) return;
var targetVar = interp.arg(b, 0);
if (targetVar in s.variables) {
s.variables[targetVar] = parseFloat(s.variables[targetVar]);
s.variables[targetVar] += interp.arg(b, 1);
s.variables[targetVar] = parseFloat(s.variables[targetVar]) + interp.numarg(b, 1);
} else if (targetVar in runtime.stage.variables) {
runtime.stage.variables[targetVar] = parseFloat(runtime.stage.variables[targetVar]);
runtime.stage.variables[targetVar] += interp.arg(b, 1);
runtime.stage.variables[targetVar] = parseFloat(runtime.stage.variables[targetVar]) + interp.numarg(b, 1);
}
}
@ -123,7 +121,7 @@ VarListPrims.prototype.primListDeleteLine = function(b) {
var line = interp.arg(b, 0);
if (line == 'all' || list.length == 0) list.length = 0;
else if (line == 'last') list.splice(list.length - 1, 1);
else if (parseInt(line) - 1 in list) list.splice(parseInt(line) - 1, 1);
else if (parseInt(line, 10) - 1 in list) list.splice(parseInt(line, 10) - 1, 1);
}
VarListPrims.prototype.primListInsertAt = function(b) {
@ -134,7 +132,7 @@ VarListPrims.prototype.primListInsertAt = function(b) {
var position = interp.arg(b, 1);
if (position == 'last') position = list.length;
else if (position == 'random') position = Math.round(Math.random() * list.length);
else position = parseInt(position) - 1;
else position = parseInt(position, 10) - 1;
if (position > list.length) return;
list.splice(position, 0, newItem);
@ -148,7 +146,7 @@ VarListPrims.prototype.primListSetLine = function(b) {
if (position == 'last') position = list.length - 1;
else if (position == 'random') position = Math.floor(Math.random() * list.length);
else position = parseInt(position) - 1;
else position = parseInt(position, 10) - 1;
if (position > list.length - 1) return;
list[position] = newItem;