This repository has been archived by the owner on Apr 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
operators.cc
268 lines (250 loc) · 13.9 KB
/
operators.cc
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "lslmini.hh"
//////////////////////////////////////////////
// Integer Constants
LLScriptConstant *LLScriptIntegerConstant::operation(int operation, LLScriptConstant *other_const, YYLTYPE *lloc) {
// unary op
if ( other_const == NULL ) {
int nv;
switch (operation) {
case INC_OP: nv = value + 1; break;
case DEC_OP: nv = value - 1; break;
case '!': nv = !value; break;
case '~': nv = ~value; break;
case '-': nv = -value; break;
default: return NULL;
}
return new LLScriptIntegerConstant( nv );
}
// binary op
switch (other_const->get_node_sub_type()) {
case NODE_INTEGER_CONSTANT: {
int ov = ((LLScriptIntegerConstant*)other_const)->get_value();
int nv;
switch (operation) {
case '+': nv = value + ov; break;
case '-': nv = value - ov; break;
case '*': nv = value * ov; break;
case '/': nv = value / ov; break;
case '%': nv = value % ov; break;
case '&': nv = value & ov; break;
case '|': nv = value | ov; break;
case '^': nv = value ^ ov; break;
case '>': nv = value > ov; break;
case '<': nv = value < ov; break;
case SHIFT_LEFT: nv = value << ov; break;
case SHIFT_RIGHT: nv = value >> ov; break;
case BOOLEAN_AND: nv = value && ov; break;
case BOOLEAN_OR: nv = value || ov; break;
case EQ: nv = value == ov; break;
default: return NULL;
}
return new LLScriptIntegerConstant(nv);
}
case NODE_FLOAT_CONSTANT: {
float ov = ((LLScriptFloatConstant*)other_const)->get_value();
float nv;
switch (operation) {
case '+': nv = value + ov; break;
case '-': nv = value - ov; break;
case '*': nv = value * ov; break;
case '/': nv = value / ov; break;
case '>': return new LLScriptIntegerConstant( value > ov );
case '<': return new LLScriptIntegerConstant( value < ov );
case EQ: return new LLScriptIntegerConstant( value == ov );
default: return NULL;
}
return new LLScriptFloatConstant(nv);
}
default:
return NULL;
}
}
//////////////////////////////////////////////
// Float Constants
LLScriptConstant *LLScriptFloatConstant::operation(int operation, LLScriptConstant *other_const, YYLTYPE *lloc) {
// unary op
if ( other_const == NULL ) {
float nv;
switch (operation) {
case INC_OP: nv = value + 1; break;
case DEC_OP: nv = value - 1; break;
case '-': nv = -value; break;
default: return NULL;
}
return new LLScriptFloatConstant( nv );
}
// binary op
switch (other_const->get_node_sub_type()) {
case NODE_INTEGER_CONSTANT: {
int ov = ((LLScriptIntegerConstant*)other_const)->get_value();
float nv;
switch (operation) {
case '+': nv = value + ov; break;
case '-': nv = value - ov; break;
case '*': nv = value * ov; break;
case '/': nv = value / ov; break;
case '>': return new LLScriptIntegerConstant( value > ov );
case '<': return new LLScriptIntegerConstant( value < ov );
case BOOLEAN_AND: return new LLScriptIntegerConstant( value && ov );
case BOOLEAN_OR: return new LLScriptIntegerConstant( value || ov );
case EQ: return new LLScriptIntegerConstant( value == ov );
default: return NULL;
}
return new LLScriptFloatConstant(nv);
}
case NODE_FLOAT_CONSTANT: {
float ov = ((LLScriptFloatConstant*)other_const)->get_value();
float nv;
switch (operation) {
case '+': nv = value + ov; break;
case '-': nv = value - ov; break;
case '*': nv = value * ov; break;
case '/': nv = value / ov; break;
case '>': return new LLScriptIntegerConstant( value > ov );
case '<': return new LLScriptIntegerConstant( value < ov );
case EQ: return new LLScriptIntegerConstant( value == ov );
default: return NULL;
}
return new LLScriptFloatConstant(nv);
}
default:
return NULL;
}
}
//////////////////////////////////////////////
// String Constants
inline char *join_string( char *left, char *right ) {
char *ns = new char[ strlen(left) + strlen(right) + 1 ];
strcpy( ns, left );
strcat( ns, right );
return ns;
}
LLScriptConstant *LLScriptStringConstant::operation(int operation, LLScriptConstant *other_const, YYLTYPE *lloc) {
// unary op
if ( other_const == NULL ) {
return NULL;
}
// binary op
switch (other_const->get_node_sub_type()) {
case NODE_STRING_CONSTANT: {
char *ov = ((LLScriptStringConstant*)other_const)->get_value();
switch (operation) {
case '+': return new LLScriptStringConstant( join_string(value, ov) );
case EQ: return new LLScriptIntegerConstant( !strcmp(value, ov) );
default: return NULL;
}
}
default:
return NULL;
}
}
//////////////////////////////////////////////
// List Constants
LLScriptConstant *LLScriptListConstant::operation(int operation, LLScriptConstant *other_const, YYLTYPE *lloc) {
// unary op
if ( other_const == NULL ) {
return NULL;
}
// binary op
switch (other_const->get_node_sub_type()) {
case NODE_LIST_CONSTANT: {
LLScriptListConstant *other = ((LLScriptListConstant*)other_const);
switch (operation) {
case EQ:
// warn on list == list
if ( get_length() != 0 && other->get_length() != 0 ) {
ERROR( lloc, W_LIST_COMPARE );
}
return new LLScriptIntegerConstant( get_length() == other->get_length() );
default: return NULL;
}
}
default:
return NULL;
}
}
//////////////////////////////////////////////
// Vector Constants
LLScriptConstant *LLScriptVectorConstant::operation(int operation, LLScriptConstant *other_const, YYLTYPE *lloc) {
// Make sure we have a value
if ( value == NULL )
return NULL;
// unary op
if ( other_const == NULL ) {
switch (operation) {
case '-': return new LLScriptVectorConstant( -value->x, -value->y, -value->z );
default: return NULL;
}
}
// binary op
switch (other_const->get_node_sub_type()) {
case NODE_INTEGER_CONSTANT: {
int ov = ((LLScriptIntegerConstant*)other_const)->get_value();
float nv[3];
switch (operation) {
case '*': nv[0] = value->x * ov; nv[1] = value->y * ov; nv[2] = value->z * ov; break;
case '/': nv[0] = value->x / ov; nv[1] = value->y / ov; nv[2] = value->z / ov; break;
default: return NULL;
}
return new LLScriptVectorConstant( nv[0], nv[1], nv[2] );
}
case NODE_FLOAT_CONSTANT: {
float ov = ((LLScriptFloatConstant*)other_const)->get_value();
float nv[3];
switch (operation) {
case '*': nv[0] = value->x * ov; nv[1] = value->y * ov; nv[2] = value->z * ov; break;
case '/': nv[0] = value->x / ov; nv[1] = value->y / ov; nv[2] = value->z / ov; break;
default: return NULL;
}
return new LLScriptVectorConstant( nv[0], nv[1], nv[2] );
}
case NODE_VECTOR_CONSTANT: {
LLVector *ov = ((LLScriptVectorConstant*)other_const)->get_value();
if ( ov == NULL )
return NULL;
float nv[3];
switch (operation) {
case '+': nv[0] = value->x + ov->x; nv[1] = value->y + ov->y; nv[2] = value->z + ov->z; break;
case '-': nv[0] = value->x - ov->x; nv[1] = value->y - ov->y; nv[2] = value->z - ov->z; break;
case '*': return new LLScriptFloatConstant( (value->x * ov->z) + (value->y * ov->y) + (value->z * ov->x) );
case '%': // cross product
nv[0] = (value->y * ov->z) - (value->z * ov->y);
nv[1] = (value->z * ov->x) - (value->x * ov->z);
nv[2] = (value->x * ov->y) - (value->y * ov->x);
break;
case EQ: return new LLScriptIntegerConstant( (value->x == ov->x) && (value->y == ov->y) && (value->z == ov->z) );
default: return NULL;
}
return new LLScriptVectorConstant( nv[0], nv[1], nv[2] );
}
default:
return NULL;
}
}
//////////////////////////////////////////////
// Quaternion Constants
LLScriptConstant *LLScriptQuaternionConstant::operation(int operation, LLScriptConstant *other_const, YYLTYPE *lloc) {
if ( value == NULL )
return NULL;
if ( other_const == NULL ) {
switch (operation) {
case '-': return new LLScriptQuaternionConstant( -value->x, -value->y, -value->z, -value->s );
default: return NULL;
}
}
// binary op
switch (other_const->get_node_sub_type()) {
case NODE_QUATERNION_CONSTANT: {
LLQuaternion *ov = ((LLScriptQuaternionConstant*)other_const)->get_value();
if ( ov == NULL )
return NULL;
switch (operation) {
case EQ: return new LLScriptIntegerConstant( (value->x == ov->x) && (value->y == ov->y) && (value->z == ov->z) && (value->s == ov->s) );
case '-': return new LLScriptQuaternionConstant( value->x - ov->x, value->y - ov->y, value->z - ov->z, value->s - ov->s );
default: return NULL;
}
}
default:
return NULL;
}
}