-
Notifications
You must be signed in to change notification settings - Fork 0
/
Type.operate.js
58 lines (58 loc) · 1.85 KB
/
Type.operate.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
function operate(operator, left, right) {
switch (operator) {
case '*':
case '/':
if (left != 'number' || right != 'number')
throw new Error(`operands of ${operator} must be numbers.`);
return 'number';
case '+':
if (left != undefined) {
if (left != 'number' && left != 'string' || right != 'number' && right != 'string')
throw new Error(`operands of + must be numbers or strings.`);
if (left != right)
throw new Error(`operands of + must be both numbers or both strings.`);
} else
if (right != 'number')
throw new Error(`operand of + must be number.`);
return left;
case '-':
if (left != undefined) {
if (left != 'number' || right != 'number')
throw new Error(`operands of - must be numbers.`);
} else
if (right != 'number')
throw new Error(`operand of - must be number.`);
return 'number';
case '<=':
case '>=':
case '<':
case '>':
if (left != 'number' && left != 'string' || right != 'number' && right != 'string')
throw new Error(`operands of ${operator} must be numbers or strings.`);
if (left != right)
throw new Error(`operands of ${operator} must be both numbers or both strings.`);
return 'boolean';
case '!=':
case '=':
if (left != right)
throw new Error(`operands of ${operator} must be of same type.`);
return 'boolean';
case 'in':
if (typeof left != 'string')
throw new Error(`left operand of in must be primitive.`);
if (!(right instanceof Array))
throw new Error(`right operand of in must be array.`);
if (left != right[0])
throw new Error(`left operand of in must be of same type of element of right operand.`);
return 'boolean';
case '!':
case '&':
case '|':
return 'boolean';
case '#':
if (!(left instanceof Array))
throw new Error(`operand of # must be array.`);
return 'number';
}
}
module.exports = operate;