Merge branch 'master' of github.com:MathWizz/scratch-html5 into test

Conflicts:
	js/Interpreter.js
	js/primitives/LooksPrims.js
	js/primitives/MotionAndPenPrims.js
	js/primitives/Primitives.js
	js/primitives/SoundPrims.js
	js/primitives/VarListPrims.js

Tested with:
  http://scratch.mit.edu/projects/13841687/
  http://scratch.mit.edu/projects/2192048/
This commit is contained in:
Nathan Dinsmore 2013-11-14 22:21:49 -05:00
commit 07415badbe
6 changed files with 179 additions and 155 deletions

View file

@ -139,10 +139,11 @@ Interpreter.prototype.stepActiveThread = function() {
// Advance the "program counter" to the next block before running the primitive. // Advance the "program counter" to the next block before running the primitive.
// Control flow primitives (e.g. if) may change activeThread.nextBlock. // Control flow primitives (e.g. if) may change activeThread.nextBlock.
this.activeThread.nextBlock = b.nextBlock; this.activeThread.nextBlock = b.nextBlock;
if(this.debugOps && this.debugFunc) { if (this.debugOps && this.debugFunc) {
var finalArgs = new Array(b.args.length); var finalArgs = [];
for(var i=0; i<b.args.length; ++i) for (var i = 0; i < b.args.length; ++i) {
finalArgs[i] = this.arg(b, i); finalArgs.push(this.arg(b, i));
}
this.debugFunc(this.opCount2, b.op, finalArgs); this.debugFunc(this.opCount2, b.op, finalArgs);
++this.opCount2; ++this.opCount2;
@ -150,13 +151,22 @@ Interpreter.prototype.stepActiveThread = function() {
b.primFcn(b); b.primFcn(b);
if (this.yield) { this.activeThread.nextBlock = b; return; } if (this.yield) { this.activeThread.nextBlock = b; return; }
b = this.activeThread.nextBlock; // refresh local variable b in case primitive did some control flow 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 // end of a substack; pop the owning control flow block from stack
// Note: This is a loop to handle nested control flow blocks. // Note: This is a loop to handle nested control flow blocks.
b = this.activeThread.stack.pop();
if ((b == null) || (b.isLoop)) { // yield at the end of a loop or when stack is empty
this.activeThread.nextBlock = b; if (this.activeThread.stack.length === 0) {
return; // yield at the end of a loop or when stack is empty this.activeThread.nextBlock = null;
return;
} else {
b = this.activeThread.stack.pop();
if (b.isLoop) {
this.activeThread.nextBlock = b; // preserve where it left off
return;
} else {
b = b.nextBlock; // skip and continue for non looping blocks
}
} }
} }
} }
@ -206,6 +216,24 @@ Interpreter.prototype.arg = function(block, index) {
return arg; return arg;
}; };
Interpreter.prototype.numarg = function(block, index) {
var arg = Number(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 Boolean(arg);
};
Interpreter.prototype.targetSprite = function() { Interpreter.prototype.targetSprite = function() {
return this.activeThread.target; return this.activeThread.target;
}; };
@ -242,14 +270,14 @@ Interpreter.prototype.initPrims = function() {
this.primitiveTable['whenGreenFlag'] = this.primNoop; this.primitiveTable['whenGreenFlag'] = this.primNoop;
this.primitiveTable['whenKeyPressed'] = this.primNoop; this.primitiveTable['whenKeyPressed'] = this.primNoop;
this.primitiveTable['whenClicked'] = 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['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['doForeverIf'] = function(b) { if (interp.boolarg(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['doIf'] = function(b) { if (interp.boolarg(b, 0)) interp.startSubstack(b); };
this.primitiveTable['doRepeat'] = this.primRepeat; 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['doIfElse'] = function(b) { if (interp.boolarg(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['doWaitUntil'] = function(b) { if (!interp.boolarg(b, 0)) interp.yield = true; };
this.primitiveTable['doUntil'] = function(b) { if (!interp.arg(b, 0)) interp.startSubstack(b, 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['doReturn'] = function(b) { interp.activeThread = new Thread(null); };
this.primitiveTable['stopAll'] = function(b) { interp.activeThread = new Thread(null); interp.threads = []; } this.primitiveTable['stopAll'] = function(b) { interp.activeThread = new Thread(null); interp.threads = []; }
this.primitiveTable['whenIReceive'] = this.primNoop; this.primitiveTable['whenIReceive'] = this.primNoop;
@ -275,13 +303,13 @@ Interpreter.prototype.lookupPrim = function(op) {
Interpreter.prototype.primNoop = function(b) { console.log(b.op); }; Interpreter.prototype.primNoop = function(b) { console.log(b.op); };
Interpreter.prototype.primWait = function(b) { 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(); else interp.checkTimer();
}; };
Interpreter.prototype.primRepeat = function(b) { Interpreter.prototype.primRepeat = function(b) {
if (b.tmp == -1) { 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) { if (b.tmp > 0) {
b.tmp -= 1; // decrement count b.tmp -= 1; // decrement count
@ -299,7 +327,7 @@ Interpreter.prototype.broadcast = function(b, waitFlag) {
var receivers = []; var receivers = [];
var msg = String(interp.arg(b, 0)).toLowerCase(); var msg = String(interp.arg(b, 0)).toLowerCase();
var findReceivers = function(stack, target) { var findReceivers = function(stack, target) {
if ((stack.op == "whenIReceive") && (stack.args[0].toLowerCase() == msg)) { if ((stack.op == 'whenIReceive') && (stack.args[0].toLowerCase() == msg)) {
receivers.push([stack, target]); receivers.push([stack, target]);
} }
} }
@ -334,10 +362,8 @@ Interpreter.prototype.isRunning = function(b) {
Interpreter.prototype.startSubstack = function(b, isLoop, secondSubstack) { Interpreter.prototype.startSubstack = function(b, isLoop, secondSubstack) {
// Start the substack of a control structure command such as if or forever. // Start the substack of a control structure command such as if or forever.
if (isLoop) { b.isLoop = !!isLoop;
b.isLoop = true; this.activeThread.stack.push(b); // remember the block that started the substack
this.activeThread.stack.push(b); // remember the block that started the substack
}
if (!secondSubstack) { if (!secondSubstack) {
this.activeThread.nextBlock = b.substack; this.activeThread.nextBlock = b.substack;
} else { } else {

View file

@ -18,35 +18,35 @@
var LooksPrims = function() {}; var LooksPrims = function() {};
LooksPrims.prototype.addPrimsTo = function(primTable) { LooksPrims.prototype.addPrimsTo = function(primTable) {
primTable["show"] = this.primShow; primTable['show'] = this.primShow;
primTable["hide"] = this.primHide; primTable['hide'] = this.primHide;
primTable["nextCostume"] = this.primNextCostume; primTable['nextCostume'] = this.primNextCostume;
primTable["lookLike:"] = this.primShowCostume; primTable['lookLike:'] = this.primShowCostume;
primTable["costumeIndex"] = this.primCostumeNum; primTable['costumeIndex'] = this.primCostumeNum;
primTable["nextScene"] = this.primNextCostume; primTable['nextScene'] = this.primNextCostume;
primTable["showBackground:"] = this.primShowCostume; primTable['showBackground:'] = this.primShowCostume;
primTable["backgroundIndex"] = this.primCostumeNum; primTable['backgroundIndex'] = this.primCostumeNum;
primTable["startScene"] = this.primStartScene; primTable['startScene'] = this.primStartScene;
primTable["backgroundIndex"] = this.primCostumeNum; primTable['backgroundIndex'] = this.primCostumeNum;
primTable["changeSizeBy:"] = this.primChangeSize; primTable['changeSizeBy:'] = this.primChangeSize;
primTable["setSizeTo:"] = this.primSetSize; primTable['setSizeTo:'] = this.primSetSize;
primTable["scale"] = this.primSize; primTable['scale'] = this.primSize;
primTable["comeToFront"] = this.primGoFront; primTable['comeToFront'] = this.primGoFront;
primTable["goBackByLayers:"] = this.primGoBack; primTable['goBackByLayers:'] = this.primGoBack;
primTable["changeGraphicEffect:by:"] = this.primChangeEffect; primTable['changeGraphicEffect:by:'] = this.primChangeEffect;
primTable["setGraphicEffect:to:"] = this.primSetEffect; primTable['setGraphicEffect:to:'] = this.primSetEffect;
primTable["filterReset"] = this.primClearEffects; primTable['filterReset'] = this.primClearEffects;
primTable["say:"] = function(b) { showBubble(b, 'say'); }; primTable['say:'] = function(b) { showBubble(b, 'say'); };
primTable["say:duration:elapsed:from:"] = function(b) { showBubbleAndWait(b, 'say'); }; primTable['say:duration:elapsed:from:'] = function(b) { showBubbleAndWait(b, 'say'); };
primTable["think:"] = function(b) { showBubble(b, 'think'); }; primTable['think:'] = function(b) { showBubble(b, 'think'); };
primTable["think:duration:elapsed:from:"] = function(b) { showBubbleAndWait(b, 'think'); }; primTable['think:duration:elapsed:from:'] = function(b) { showBubbleAndWait(b, 'think'); };
}; };
LooksPrims.prototype.primShow = function(b) { LooksPrims.prototype.primShow = function(b) {
@ -71,7 +71,7 @@ LooksPrims.prototype.primShowCostume = function(b) {
if (typeof(arg) == 'number') { if (typeof(arg) == 'number') {
s.showCostume(arg - 1); s.showCostume(arg - 1);
} else { } else {
if ((arg == 'CAMERA') || (arg == "CAMERA - MIRROR")) { if ((arg == 'CAMERA') || (arg == 'CAMERA - MIRROR')) {
s.showCostumeNamed(arg); s.showCostumeNamed(arg);
return; return;
} }
@ -79,8 +79,8 @@ LooksPrims.prototype.primShowCostume = function(b) {
if (i >= 0) { if (i >= 0) {
s.showCostume(i); s.showCostume(i);
} else { } else {
var n = Number(arg); var n = parseInt(arg, 10);
if (!isNaN(n)) { if (n === n) { // if n is not NaN
s.showCostume(n - 1); s.showCostume(n - 1);
} else { } else {
return; // arg did not match a costume name nor is a valid number return; // arg did not match a costume name nor is a valid number
@ -96,7 +96,7 @@ LooksPrims.prototype.primStartScene = function(b) {
if (typeof(arg) == 'number') { if (typeof(arg) == 'number') {
s.showCostume(arg - 1); s.showCostume(arg - 1);
} else { } else {
if ((arg == 'CAMERA') || (arg == "CAMERA - MIRROR")) { if ((arg == 'CAMERA') || (arg == 'CAMERA - MIRROR')) {
s.showCostumeNamed(arg); s.showCostumeNamed(arg);
return; return;
} }
@ -104,8 +104,8 @@ LooksPrims.prototype.primStartScene = function(b) {
if (i >= 0) { if (i >= 0) {
s.showCostume(i); s.showCostume(i);
} else { } else {
var n = Number(arg); var n = parseInt(arg, 10);
if (!isNaN(n)) { if (n === n) { // fast !isNaN check
s.showCostume(n - 1); s.showCostume(n - 1);
} else { } else {
return; // arg did not match a costume name nor is a valid number 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) { LooksPrims.prototype.primChangeSize = function(b) {
var s = interp.targetSprite(); var s = interp.targetSprite();
if (s == null) return; 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(); if (s.visible) interp.redraw();
}; };
LooksPrims.prototype.primSetSize = function(b) { LooksPrims.prototype.primSetSize = function(b) {
var s = interp.targetSprite(); var s = interp.targetSprite();
if (s == null) return; if (s == null) return;
s.setSize(interp.arg(b, 0)); s.setSize(interp.numarg(b, 0));
if (s.visible) interp.redraw(); if (s.visible) interp.redraw();
}; };
@ -148,8 +148,8 @@ LooksPrims.prototype.primGoFront = function(b) {
LooksPrims.prototype.primGoBack = function(b) { LooksPrims.prototype.primGoBack = function(b) {
var s = interp.targetSprite(); var s = interp.targetSprite();
runtime.reassignZ(s, interp.arg(b, 0)); runtime.reassignZ(s, interp.numarg(b, 0));
if (s.visible) interp.redraw(); if(s.visible) interp.redraw();
}; };
LooksPrims.prototype.primChangeEffect = function(b) {}; LooksPrims.prototype.primChangeEffect = function(b) {};
@ -168,7 +168,7 @@ var showBubbleAndWait = function(b, type) {
if (s == null) return; if (s == null) return;
if (interp.activeThread.firstTime) { if (interp.activeThread.firstTime) {
var text = interp.arg(b, 0); var text = interp.arg(b, 0);
var secs = interp.arg(b, 1); var secs = interp.numarg(b, 1);
s.showBubble(text, type); s.showBubble(text, type);
if (s.visible) interp.redraw(); if (s.visible) interp.redraw();
interp.startTimer(secs); interp.startTimer(secs);

View file

@ -22,42 +22,42 @@ MotionAndPenPrims.prototype.addPrimsTo = function(primTable) {
primTable['turnLeft:'] = this.primTurnLeft; primTable['turnLeft:'] = this.primTurnLeft;
primTable['turnRight:'] = this.primTurnRight; primTable['turnRight:'] = this.primTurnRight;
primTable['heading:'] = this.primSetDirection; primTable['heading:'] = this.primSetDirection;
primTable["pointTowards:"] = this.primPointTowards; primTable['pointTowards:'] = this.primPointTowards;
primTable["gotoX:y:"] = this.primGoTo; primTable['gotoX:y:'] = this.primGoTo;
primTable["gotoSpriteOrMouse:"] = this.primGoToSpriteOrMouse; primTable['gotoSpriteOrMouse:'] = this.primGoToSpriteOrMouse;
primTable["glideSecs:toX:y:elapsed:from:"] = this.primGlide; primTable['glideSecs:toX:y:elapsed:from:'] = this.primGlide;
primTable["changeXposBy:"] = this.primChangeX; primTable['changeXposBy:'] = this.primChangeX;
primTable["xpos:"] = this.primSetX; primTable['xpos:'] = this.primSetX;
primTable["changeYposBy:"] = this.primChangeY; primTable['changeYposBy:'] = this.primChangeY;
primTable["ypos:"] = this.primSetY; primTable['ypos:'] = this.primSetY;
primTable["bounceOffEdge"] = this.primBounceOffEdge; primTable['bounceOffEdge'] = this.primBounceOffEdge;
primTable["setRotationStyle"] = this.primSetRotationStyle; primTable['setRotationStyle'] = this.primSetRotationStyle;
primTable["xpos"] = this.primXPosition; primTable['xpos'] = this.primXPosition;
primTable["ypos"] = this.primYPosition; primTable['ypos'] = this.primYPosition;
primTable["heading"] = this.primDirection; primTable['heading'] = this.primDirection;
primTable["clearPenTrails"] = this.primClear; primTable['clearPenTrails'] = this.primClear;
primTable["putPenDown"] = this.primPenDown; primTable['putPenDown'] = this.primPenDown;
primTable["putPenUp"] = this.primPenUp; primTable['putPenUp'] = this.primPenUp;
primTable["penColor:"] = this.primSetPenColor; primTable['penColor:'] = this.primSetPenColor;
primTable["setPenHueTo:"] = this.primSetPenHue; primTable['setPenHueTo:'] = this.primSetPenHue;
primTable["changePenHueBy:"] = this.primChangePenHue; primTable['changePenHueBy:'] = this.primChangePenHue;
primTable["setPenShadeTo:"] = this.primSetPenShade; primTable['setPenShadeTo:'] = this.primSetPenShade;
primTable["changePenShadeBy:"] = this.primChangePenShade; primTable['changePenShadeBy:'] = this.primChangePenShade;
primTable["penSize:"] = this.primSetPenSize; primTable['penSize:'] = this.primSetPenSize;
primTable["changePenSizeBy:"] = this.primChangePenSize; primTable['changePenSizeBy:'] = this.primChangePenSize;
primTable["stampCostume"] = this.primStamp; primTable['stampCostume'] = this.primStamp;
primTable["stampTransparent"] = this.primStampTransparent; primTable['stampTransparent'] = this.primStampTransparent;
}; };
MotionAndPenPrims.prototype.primMove = function(b) { MotionAndPenPrims.prototype.primMove = function(b) {
var s = interp.targetSprite(); var s = interp.targetSprite();
var radians = (90 - s.direction) * Math.PI / 180; var radians = (90 - s.direction) * Math.PI / 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)); moveSpriteTo(s, s.scratchX + d * Math.cos(radians), s.scratchY + d * Math.sin(radians));
if (s.visible) interp.redraw(); if (s.visible) interp.redraw();
@ -65,21 +65,21 @@ MotionAndPenPrims.prototype.primMove = function(b) {
MotionAndPenPrims.prototype.primTurnLeft = function(b) { MotionAndPenPrims.prototype.primTurnLeft = function(b) {
var s = interp.targetSprite(); var s = interp.targetSprite();
var d = s.direction - interp.arg(b, 0); var d = s.direction - interp.numarg(b, 0);
s.setDirection(d); s.setDirection(d);
if (s.visible) interp.redraw(); if (s.visible) interp.redraw();
}; };
MotionAndPenPrims.prototype.primTurnRight = function(b) { MotionAndPenPrims.prototype.primTurnRight = function(b) {
var s = interp.targetSprite(); var s = interp.targetSprite();
var d = s.direction + interp.arg(b, 0); var d = s.direction + interp.numarg(b, 0);
s.setDirection(d); s.setDirection(d);
if (s.visible) interp.redraw(); if (s.visible) interp.redraw();
}; };
MotionAndPenPrims.prototype.primSetDirection = function(b) { MotionAndPenPrims.prototype.primSetDirection = function(b) {
var s = interp.targetSprite(); var s = interp.targetSprite();
s.setDirection(interp.arg(b, 0)); s.setDirection(interp.numarg(b, 0));
if (s.visible) interp.redraw(); if (s.visible) interp.redraw();
}; };
@ -96,7 +96,7 @@ MotionAndPenPrims.prototype.primPointTowards = function(b) {
MotionAndPenPrims.prototype.primGoTo = function(b) { MotionAndPenPrims.prototype.primGoTo = function(b) {
var s = interp.targetSprite(); 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) { MotionAndPenPrims.prototype.primGoToSpriteOrMouse = function(b) {
@ -110,9 +110,9 @@ MotionAndPenPrims.prototype.primGlide = function(b) {
var s = interp.targetSprite(); var s = interp.targetSprite();
if (s == null) return; if (s == null) return;
if (interp.activeThread.firstTime) { if (interp.activeThread.firstTime) {
var secs = interp.arg(b, 0); var secs = interp.numarg(b, 0);
var destX = interp.arg(b, 1); var destX = interp.numarg(b, 1);
var destY = interp.arg(b, 2); var destY = interp.numarg(b, 2);
if (secs <= 0) { if (secs <= 0) {
moveSpriteTo(s, destX, destY); moveSpriteTo(s, destX, destY);
return; return;
@ -138,22 +138,22 @@ MotionAndPenPrims.prototype.primGlide = function(b) {
MotionAndPenPrims.prototype.primChangeX = function(b) { MotionAndPenPrims.prototype.primChangeX = function(b) {
var s = interp.targetSprite(); 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) { MotionAndPenPrims.prototype.primSetX = function(b) {
var s = interp.targetSprite(); 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) { MotionAndPenPrims.prototype.primChangeY = function(b) {
var s = interp.targetSprite(); 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) { MotionAndPenPrims.prototype.primSetY = function(b) {
var s = interp.targetSprite(); 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) { MotionAndPenPrims.prototype.primBounceOffEdge = function(b) {
@ -209,38 +209,38 @@ MotionAndPenPrims.prototype.primPenUp = function(b) {
MotionAndPenPrims.prototype.primSetPenColor = function(b) { MotionAndPenPrims.prototype.primSetPenColor = function(b) {
var s = interp.targetSprite(); 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) { MotionAndPenPrims.prototype.primSetPenHue = function(b) {
var s = interp.targetSprite(); 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) { MotionAndPenPrims.prototype.primChangePenHue = function(b) {
var s = interp.targetSprite(); 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) { MotionAndPenPrims.prototype.primSetPenShade = function(b) {
var s = interp.targetSprite(); 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) { MotionAndPenPrims.prototype.primChangePenShade = function(b) {
var s = interp.targetSprite(); 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) { MotionAndPenPrims.prototype.primSetPenSize = function(b) {
var s = interp.targetSprite(); 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; if (s != null) s.penWidth = w;
}; };
MotionAndPenPrims.prototype.primChangePenSize = function(b) { MotionAndPenPrims.prototype.primChangePenSize = function(b) {
var s = interp.targetSprite(); 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; if (s != null) s.penWidth = w;
}; };
@ -251,7 +251,7 @@ MotionAndPenPrims.prototype.primStamp = function(b) {
MotionAndPenPrims.prototype.primStampTransparent = function(b) { MotionAndPenPrims.prototype.primStampTransparent = function(b) {
var s = interp.targetSprite(); 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; var alpha = 100 - transparency;
s.stamp(runtime.stage.lineCache, alpha); s.stamp(runtime.stage.lineCache, alpha);
}; };
@ -263,7 +263,7 @@ var stroke = function(s, oldX, oldY, newX, newY) {
}; };
var mouseOrSpritePosition = function(arg) { var mouseOrSpritePosition = function(arg) {
if (arg == "_mouse_") { if (arg == '_mouse_') {
var w = runtime.stage; var w = runtime.stage;
return new Point(runtime.mousePos[0], runtime.mousePos[1]); return new Point(runtime.mousePos[0], runtime.mousePos[1]);
} else { } else {

View file

@ -26,29 +26,29 @@ var Primitives = function() {}
Primitives.prototype.addPrimsTo = function(primTable) { Primitives.prototype.addPrimsTo = function(primTable) {
// Math primitives // Math primitives
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.numarg(b, 0) - interp.numarg(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.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.numarg(b, 0) % interp.numarg(b, 1); };
primTable["randomFrom:to:"] = this.primRandom; 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.numarg(b, 0) > interp.numarg(b, 1)); };
primTable["&"] = function(b) { return interp.arg(b, 0) && interp.arg(b, 1); }; primTable['&'] = function(b) { return interp.boolarg(b, 0) && interp.boolarg(b, 1); };
primTable["|"] = function(b) { return interp.arg(b, 0) || interp.arg(b, 1); }; primTable['|'] = function(b) { return interp.boolarg(b, 0) || interp.boolarg(b, 1); };
primTable["not"] = function(b) { return !interp.arg(b, 0) }; primTable['not'] = function(b) { return !interp.boolarg(b, 0); };
primTable["abs"] = function(b) { return Math.abs(interp.arg(b, 0)) }; primTable['abs'] = function(b) { return Math.abs(interp.numarg(b, 0)); };
primTable["sqrt"] = function(b) { return Math.sqrt(interp.arg(b, 0)) }; primTable['sqrt'] = function(b) { return Math.sqrt(interp.numarg(b, 0)); };
primTable["\\\\"] = this.primModulo; 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; primTable['computeFunction:of:'] = this.primMathFunction;
// String primitives // String primitives
primTable["concatenate:with:"] = function(b) { return "" + interp.arg(b, 0) + interp.arg(b, 1); }; primTable['concatenate:with:'] = function(b) { return '' + interp.arg(b, 0) + interp.arg(b, 1); };
primTable["letter:of:"] = this.primLetterOf; primTable['letter:of:'] = this.primLetterOf;
primTable["stringLength:"] = function(b) { return interp.arg(b, 0).length; }; primTable['stringLength:'] = function(b) { return interp.arg(b, 0).length; };
new VarListPrims().addPrimsTo(primTable); new VarListPrims().addPrimsTo(primTable);
new MotionAndPenPrims().addPrimsTo(primTable); new MotionAndPenPrims().addPrimsTo(primTable);
@ -58,8 +58,8 @@ Primitives.prototype.addPrimsTo = function(primTable) {
} }
Primitives.prototype.primRandom = function(b) { Primitives.prototype.primRandom = function(b) {
var n1 = interp.arg(b, 0); var n1 = interp.numarg(b, 0);
var n2 = interp.arg(b, 1); var n2 = interp.numarg(b, 1);
var low = n1 <= n2 ? n1 : n2; var low = n1 <= n2 ? n1 : n2;
var hi = n1 <= n2 ? n2 : n1; var hi = n1 <= n2 ? n2 : n1;
if (low == hi) return low; if (low == hi) return low;
@ -72,34 +72,34 @@ Primitives.prototype.primRandom = function(b) {
Primitives.prototype.primLetterOf = function(b) { Primitives.prototype.primLetterOf = function(b) {
var s = interp.arg(b, 1); 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 ""; if (i < 0 || i >= s.length) return '';
return s.charAt(i); return s.charAt(i);
} }
Primitives.prototype.primModulo = function(b) { Primitives.prototype.primModulo = function(b) {
var modulus = interp.arg(b, 1); var modulus = interp.numarg(b, 1);
var n = interp.arg(b, 0) % modulus; var n = interp.numarg(b, 0) % modulus;
if (n < 0) n += modulus; if (n < 0) n += modulus;
return n; return n;
} }
Primitives.prototype.primMathFunction = function(b) { Primitives.prototype.primMathFunction = function(b) {
var op = interp.arg(b, 0); var op = interp.numarg(b, 0);
var n = interp.arg(b, 1); var n = interp.numarg(b, 1);
switch(op) { switch(op) {
case "abs": return Math.abs(n); case 'abs': return Math.abs(n);
case "sqrt": return Math.sqrt(n); case 'sqrt': return Math.sqrt(n);
case "sin": return Math.sin(n * Math.PI / 180); case 'sin': return Math.sin(n * Math.PI / 180);
case "cos": return Math.cos(n * Math.PI / 180); case 'cos': return Math.cos(n * Math.PI / 180);
case "tan": return Math.tan(n * Math.PI / 180); case 'tan': return Math.tan(n * Math.PI / 180);
case "asin": return Math.asin(n) * 180 / Math.PI; case 'asin': return Math.asin(n) * 180 / Math.PI;
case "acos": return Math.acos(n) * 180 / Math.PI; case 'acos': return Math.acos(n) * 180 / Math.PI;
case "atan": return Math.atan(n) * 180 / Math.PI; case 'atan': return Math.atan(n) * 180 / Math.PI;
case "ln": return Math.log(n); case 'ln': return Math.log(n);
case "log": return Math.log(n) / Math.LN10; case 'log': return Math.log(n) / Math.LN10;
case "e ^": return Math.exp(n); case 'e ^': return Math.exp(n);
case "10 ^": return Math.exp(n * Math.LN10); case '10 ^': return Math.exp(n * Math.LN10);
} }
return 0; return 0;
} }

View file

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

View file

@ -67,11 +67,9 @@ VarListPrims.prototype.primChangeVar = function(b) {
if (s == null) return; if (s == null) return;
var targetVar = interp.arg(b, 0); var targetVar = interp.arg(b, 0);
if (targetVar in s.variables) { if (targetVar in s.variables) {
s.variables[targetVar] = parseFloat(s.variables[targetVar]); s.variables[targetVar] = parseFloat(s.variables[targetVar]) + interp.numarg(b, 1);
s.variables[targetVar] += interp.arg(b, 1);
} else if (targetVar in runtime.stage.variables) { } else if (targetVar in runtime.stage.variables) {
runtime.stage.variables[targetVar] = parseFloat(runtime.stage.variables[targetVar]); runtime.stage.variables[targetVar] = parseFloat(runtime.stage.variables[targetVar]) + interp.numarg(b, 1);
runtime.stage.variables[targetVar] += interp.arg(b, 1);
} }
}; };
@ -127,7 +125,7 @@ VarListPrims.prototype.primListDeleteLine = function(b) {
var line = interp.arg(b, 0); var line = interp.arg(b, 0);
if (line == 'all' || list.length == 0) list.length = 0; if (line == 'all' || list.length == 0) list.length = 0;
else if (line == 'last') list.splice(list.length - 1, 1); 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) { VarListPrims.prototype.primListInsertAt = function(b) {
@ -138,7 +136,7 @@ VarListPrims.prototype.primListInsertAt = function(b) {
var position = interp.arg(b, 1); var position = interp.arg(b, 1);
if (position == 'last') position = list.length; if (position == 'last') position = list.length;
else if (position == 'random') position = Math.round(Math.random() * 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; if (position > list.length) return;
list.splice(position, 0, newItem); list.splice(position, 0, newItem);
@ -152,7 +150,7 @@ VarListPrims.prototype.primListSetLine = function(b) {
if (position == 'last') position = list.length - 1; if (position == 'last') position = list.length - 1;
else if (position == 'random') position = Math.floor(Math.random() * list.length); 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; if (position > list.length - 1) return;