2016-08-11 18:47:34 -04:00
|
|
|
var Cast = require('../util/cast.js');
|
2017-01-31 19:05:54 -05:00
|
|
|
var MathUtil = require('../util/math-util.js');
|
2016-08-11 18:47:34 -04:00
|
|
|
|
2016-10-23 17:55:31 -04:00
|
|
|
var Scratch3OperatorsBlocks = function (runtime) {
|
2016-06-09 14:29:07 -04:00
|
|
|
/**
|
|
|
|
* The runtime instantiating this block package.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
2016-06-09 14:29:07 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the block primitives implemented by this package.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @return {object.<string, Function>} Mapping of opcode to Function.
|
2016-06-09 14:29:07 -04:00
|
|
|
*/
|
2016-10-23 12:41:45 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.getPrimitives = function () {
|
2016-06-09 14:29:07 -04:00
|
|
|
return {
|
2016-10-24 11:02:19 -04:00
|
|
|
operator_add: this.add,
|
|
|
|
operator_subtract: this.subtract,
|
|
|
|
operator_multiply: this.multiply,
|
|
|
|
operator_divide: this.divide,
|
|
|
|
operator_lt: this.lt,
|
|
|
|
operator_equals: this.equals,
|
|
|
|
operator_gt: this.gt,
|
|
|
|
operator_and: this.and,
|
|
|
|
operator_or: this.or,
|
|
|
|
operator_not: this.not,
|
|
|
|
operator_random: this.random,
|
|
|
|
operator_join: this.join,
|
|
|
|
operator_letter_of: this.letterOf,
|
|
|
|
operator_length: this.length,
|
|
|
|
operator_mod: this.mod,
|
|
|
|
operator_round: this.round,
|
|
|
|
operator_mathop: this.mathop
|
2016-06-09 14:29:07 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-06-10 10:36:05 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.add = function (args) {
|
2016-08-11 18:47:34 -04:00
|
|
|
return Cast.toNumber(args.NUM1) + Cast.toNumber(args.NUM2);
|
2016-06-09 14:29:07 -04:00
|
|
|
};
|
|
|
|
|
2016-06-20 14:42:06 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.subtract = function (args) {
|
2016-08-11 18:47:34 -04:00
|
|
|
return Cast.toNumber(args.NUM1) - Cast.toNumber(args.NUM2);
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.multiply = function (args) {
|
2016-08-11 18:47:34 -04:00
|
|
|
return Cast.toNumber(args.NUM1) * Cast.toNumber(args.NUM2);
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.divide = function (args) {
|
2016-08-11 18:47:34 -04:00
|
|
|
return Cast.toNumber(args.NUM1) / Cast.toNumber(args.NUM2);
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.lt = function (args) {
|
2016-08-11 18:47:34 -04:00
|
|
|
return Cast.compare(args.OPERAND1, args.OPERAND2) < 0;
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
2016-06-10 10:36:05 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.equals = function (args) {
|
2016-10-23 17:55:31 -04:00
|
|
|
return Cast.compare(args.OPERAND1, args.OPERAND2) === 0;
|
2016-06-10 10:36:05 -04:00
|
|
|
};
|
|
|
|
|
2016-06-20 14:42:06 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.gt = function (args) {
|
2016-08-11 18:47:34 -04:00
|
|
|
return Cast.compare(args.OPERAND1, args.OPERAND2) > 0;
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.and = function (args) {
|
2016-09-12 13:14:16 -04:00
|
|
|
return Cast.toBoolean(args.OPERAND1) && Cast.toBoolean(args.OPERAND2);
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.or = function (args) {
|
2016-09-12 13:14:16 -04:00
|
|
|
return Cast.toBoolean(args.OPERAND1) || Cast.toBoolean(args.OPERAND2);
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.not = function (args) {
|
2016-09-12 13:14:16 -04:00
|
|
|
return !Cast.toBoolean(args.OPERAND);
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
2016-06-30 16:57:12 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.random = function (args) {
|
2016-09-12 13:14:16 -04:00
|
|
|
var nFrom = Cast.toNumber(args.FROM);
|
|
|
|
var nTo = Cast.toNumber(args.TO);
|
|
|
|
var low = nFrom <= nTo ? nFrom : nTo;
|
|
|
|
var high = nFrom <= nTo ? nTo : nFrom;
|
2016-10-23 17:55:31 -04:00
|
|
|
if (low === high) return low;
|
2016-09-13 17:53:17 -04:00
|
|
|
// If both arguments are ints, truncate the result to an int.
|
|
|
|
if (Cast.isInt(args.FROM) && Cast.isInt(args.TO)) {
|
2016-10-23 17:55:31 -04:00
|
|
|
return low + parseInt(Math.random() * ((high + 1) - low), 10);
|
2016-06-30 19:04:15 -04:00
|
|
|
}
|
|
|
|
return (Math.random() * (high - low)) + low;
|
2016-06-17 15:10:28 -04:00
|
|
|
};
|
|
|
|
|
2016-08-11 12:05:58 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.join = function (args) {
|
2016-08-11 19:00:11 -04:00
|
|
|
return Cast.toString(args.STRING1) + Cast.toString(args.STRING2);
|
2016-08-11 12:05:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.letterOf = function (args) {
|
2016-08-11 18:47:34 -04:00
|
|
|
var index = Cast.toNumber(args.LETTER) - 1;
|
|
|
|
var str = Cast.toString(args.STRING);
|
2016-08-11 12:05:58 -04:00
|
|
|
// Out of bounds?
|
|
|
|
if (index < 0 || index >= str.length) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return str.charAt(index);
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.length = function (args) {
|
2016-08-11 18:47:34 -04:00
|
|
|
return Cast.toString(args.STRING).length;
|
2016-08-11 12:05:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.mod = function (args) {
|
2016-08-11 18:47:34 -04:00
|
|
|
var n = Cast.toNumber(args.NUM1);
|
|
|
|
var modulus = Cast.toNumber(args.NUM2);
|
2016-08-11 12:05:58 -04:00
|
|
|
var result = n % modulus;
|
|
|
|
// Scratch mod is kept positive.
|
|
|
|
if (result / modulus < 0) result += modulus;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.round = function (args) {
|
2016-08-11 18:47:34 -04:00
|
|
|
return Math.round(Cast.toNumber(args.NUM));
|
2016-08-11 12:05:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.mathop = function (args) {
|
2016-08-11 18:47:34 -04:00
|
|
|
var operator = Cast.toString(args.OPERATOR).toLowerCase();
|
|
|
|
var n = Cast.toNumber(args.NUM);
|
2016-08-11 12:05:58 -04:00
|
|
|
switch (operator) {
|
|
|
|
case 'abs': return Math.abs(n);
|
|
|
|
case 'floor': return Math.floor(n);
|
|
|
|
case 'ceiling': return Math.ceil(n);
|
|
|
|
case 'sqrt': return Math.sqrt(n);
|
2017-01-31 19:05:54 -05:00
|
|
|
case 'sin': return parseFloat(Math.sin((Math.PI * n) / 180).toFixed(10));
|
|
|
|
case 'cos': return parseFloat(Math.cos((Math.PI * n) / 180).toFixed(10));
|
|
|
|
case 'tan': return MathUtil.tan(n);
|
2016-08-11 12:05:58 -04:00
|
|
|
case 'asin': return (Math.asin(n) * 180) / Math.PI;
|
|
|
|
case 'acos': return (Math.acos(n) * 180) / Math.PI;
|
|
|
|
case 'atan': return (Math.atan(n) * 180) / Math.PI;
|
|
|
|
case 'ln': return Math.log(n);
|
|
|
|
case 'log': return Math.log(n) / Math.LN10;
|
|
|
|
case 'e ^': return Math.exp(n);
|
|
|
|
case '10 ^': return Math.pow(10, n);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2016-06-09 14:29:07 -04:00
|
|
|
module.exports = Scratch3OperatorsBlocks;
|