-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathscratch3_operators.js
154 lines (133 loc) · 4.57 KB
/
scratch3_operators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const Cast = require('../util/cast.js');
const MathUtil = require('../util/math-util.js');
class Scratch3OperatorsBlocks {
constructor (runtime) {
/**
* The runtime instantiating this block package.
* @type {Runtime}
*/
this.runtime = runtime;
}
/**
* Retrieve the block primitives implemented by this package.
* @return {object.<string, Function>} Mapping of opcode to Function.
*/
getPrimitives () {
return {
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_contains: this.contains,
operator_mod: this.mod,
operator_round: this.round,
operator_mathop: this.mathop
};
}
add (args) {
return Cast.toNumber(args.NUM1) + Cast.toNumber(args.NUM2);
}
subtract (args) {
return Cast.toNumber(args.NUM1) - Cast.toNumber(args.NUM2);
}
multiply (args) {
return Cast.toNumber(args.NUM1) * Cast.toNumber(args.NUM2);
}
divide (args) {
return Cast.toNumber(args.NUM1) / Cast.toNumber(args.NUM2);
}
lt (args) {
return Cast.compare(args.OPERAND1, args.OPERAND2) < 0;
}
equals (args) {
return Cast.compare(args.OPERAND1, args.OPERAND2) === 0;
}
gt (args) {
return Cast.compare(args.OPERAND1, args.OPERAND2) > 0;
}
and (args) {
return Cast.toBoolean(args.OPERAND1) && Cast.toBoolean(args.OPERAND2);
}
or (args) {
return Cast.toBoolean(args.OPERAND1) || Cast.toBoolean(args.OPERAND2);
}
not (args) {
return !Cast.toBoolean(args.OPERAND);
}
random (args) {
const nFrom = Cast.toNumber(args.FROM);
const nTo = Cast.toNumber(args.TO);
const low = nFrom <= nTo ? nFrom : nTo;
const high = nFrom <= nTo ? nTo : nFrom;
if (low === high) return low;
// If both arguments are ints, truncate the result to an int.
if (Cast.isInt(args.FROM) && Cast.isInt(args.TO)) {
return low + Math.floor(Math.random() * ((high + 1) - low));
}
return (Math.random() * (high - low)) + low;
}
join (args) {
return Cast.toString(args.STRING1) + Cast.toString(args.STRING2);
}
letterOf (args) {
const index = Cast.toNumber(args.LETTER) - 1;
const str = Cast.toString(args.STRING);
// Out of bounds?
if (index < 0 || index >= str.length) {
return '';
}
return str.charAt(index);
}
length (args) {
return Cast.toString(args.STRING).length;
}
contains (args) {
const format = function (string) {
return Cast.toString(string).toLowerCase();
};
return format(args.STRING1).includes(format(args.STRING2));
}
mod (args) {
const n = Cast.toNumber(args.NUM1);
const modulus = Cast.toNumber(args.NUM2);
let result = n % modulus;
// Scratch mod uses floored division instead of truncated division.
if (result / modulus < 0) result += modulus;
return result;
}
round (args) {
return Math.round(Cast.toNumber(args.NUM));
}
mathop (args) {
const operator = Cast.toString(args.OPERATOR).toLowerCase();
const n = Cast.toNumber(args.NUM);
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);
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);
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;
}
}
module.exports = Scratch3OperatorsBlocks;