-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsexpr.h
75 lines (69 loc) · 1.54 KB
/
jsexpr.h
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
void operator ++ (int a) {
if (type == varNum) num++;
}
void operator -- (int a) {
if (type == varNum) num--;
}
void operator += (var a) {
if (type == varNum && a.type == varNum) num += a.num;
else if (type == varStr) {
a = a.toString();
self = self + a;
} else
self = NaN;
}
var operator + (var a) {
if (type == varNum) {
if (a.type == varNum) {
return toDouble() + a.toDouble();
} else if (a.type == varStr) {
return toString() + a.toString();
} else {
var R = NaN;
return R;
}
} else if (type == varStr) {
if (a.type != varStr) a = a.toString();
int an = _chr().size, bn = a._chr().size;
jschar
* sum = new jschar [an + bn],
* c = sum,
* A = _chr().s,
* B = a._chr().s;
for (int i = 0; i < an; i++) *c++ = A[i];
for (int i = 0; i < bn; i++) *c++ = B[i];
var R;
R.makeStringToSet();
chr &C = R._chr();
C.size = an + bn;
C.s = sum;
return R;
}
return undefined;
}
#define STD_OP(X, op, Y) var operator op (var a) {\
if (type == varNum) {\
if (a.type == varNum) {\
return X op Y;\
} else return NaN;\
}\
return undefined;\
}
STD_OP(toDouble(), -, a.toDouble())
STD_OP(toDouble(), *, a.toDouble())
STD_OP(toDouble(), /, a.toDouble())
STD_OP(toInt(), |, a.toInt())
STD_OP(toInt(), &, a.toInt())
STD_OP(toDouble(), ||, a.toDouble())
STD_OP(toDouble(), &&, a.toDouble())
STD_OP(toInt(), %, a.toInt())
STD_OP(num, *=, a.toDouble())
STD_OP(num, /=, a.toDouble())
STD_OP(num, -=, a.toDouble())
#undef STD_OP
var round() {
if (type == varNum) {
return (int) toDouble();
}
return undefined;
}