[go: up one dir, main page]

Menu

[r9]: / decker / model / Value.java  Maximize  Restore  History

Download this file

382 lines (289 with data), 12.1 kB

  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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package decker.model;
/** A Value holds a value that is generated during script execution or held in a variable.
* Value objects can hold any type of value. */
public final class Value
{
public final static int CONSTANT = 0, BOOLEAN = 1, INTEGER = 2, STRING = 3, STRUCTURE = 4, FUNCTION = 5, REAL = 6;
private final static String[] TYPE_NAME = {"CONSTANT", "BOOLEAN", "INTEGER", "STRING", "STRUCTURE", "FUNCTION", "REAL" };
private int type;
private boolean bool;
private int integer;
private double real;
private Object object;
private Structure visible_structure;
public Value () {
type = CONSTANT;
object = "UNDEFINED";
}
public Value (final Structure _visible_structure) {
type = CONSTANT;
object = "UNDEFINED";
visible_structure = _visible_structure;
}
/** removes all object references from this Value, to make garbage collection easier */
void destroy () {
object = null;
visible_structure = null;
}
private Value executeExpression () {
return FunctionCall.executeFunctionCall(object, null, (visible_structure==null||!visible_structure.variablesSeeEachOther()) ? null : visible_structure);
}
// set ***************************************************************************************************************
public Value set (Value value) {
// if the value contains a function, execute the function. it's okay for the function to return another function. such a returned function will not be executed but stored in this Value
if (value.type == FUNCTION)
value = value.executeExpression();
if (!equalsAbsolute(value)) {
type = value.type;
bool = value.bool;
integer = value.integer;
real = value.real;
object = value.object;
if(visible_structure != null && !visible_structure.get("structure_type").equals("LOCAL")) // otherwise it's a temporary Value object and there cannot possibly a listener for it
Global.testTriggers();
}
return this;
}
public Value set (final boolean b) {
if (type != BOOLEAN || bool != b) {
type = BOOLEAN;
bool = b;
object = null; // this is done to get rid off references to obsolete objects
if (visible_structure != null && !visible_structure.get("structure_type").equals("LOCAL")) // otherwise it's a temporary Value object and there cannot possibly a listener for it
Global.testTriggers();
}
return this;
}
public Value set (final int i) {
if (type != INTEGER || integer != i) {
type = INTEGER;
integer = i;
object = null; // this is done to get rid off references to obsolete objects
if (visible_structure != null && !visible_structure.get("structure_type").equals("LOCAL")) // otherwise it's a temporary Value object and there cannot possibly a listener for it
Global.testTriggers();
}
return this;
}
public Value set (final double r) {
if (type != REAL || !equals(r)) {
type = REAL;
real = r;
object = null; // this is done to get rid off references to obsolete objects
if (visible_structure != null && !visible_structure.get("structure_type").equals("LOCAL")) // otherwise it's a temporary Value object and there cannot possibly a listener for it
Global.testTriggers();
}
return this;
}
public Value set (final String s) {
if (type != STRING || !((String)object).equals(s)) {
type = STRING;
object = s;
if (visible_structure != null && !visible_structure.get("structure_type").equals("LOCAL")) // otherwise it's a temporary Value object and there cannot possibly a listener for it
Global.testTriggers();
}
return this;
}
public Value set (final Structure s) {
if (object != s) { // no need to check whether type is not STRUCTURE here
type = STRUCTURE;
object = s;
if (visible_structure != null && !visible_structure.get("structure_type").equals("LOCAL")) // otherwise it's a temporary Value object and there cannot possibly a listener for it
Global.testTriggers();
}
return this;
}
public Value set (final Function f) {
if (object != f) { // no need to check whether type is not FUNCTION here
type = FUNCTION;
object = f;
if (visible_structure != null && !visible_structure.get("structure_type").equals("LOCAL")) // otherwise it's a temporary Value object and there cannot possibly a listener for it
Global.testTriggers();
}
return this;
}
public Value setConstant (final String constant) {
if (type != CONSTANT || !((String)object).equals(constant)) {
type = CONSTANT;
object = constant;
if (visible_structure != null && !visible_structure.get("structure_type").equals("LOCAL")) // otherwise it's a temporary Value object and there cannot possibly a listener for it
Global.testTriggers();
}
return this;
}
/** sets this Value to value without implicitly calling the function if v contains one */
public Value setDirectly (final Value value, final boolean test_triggers) {
if (!equalsAbsolute(value)) {
type = value.type;
bool = value.bool;
integer = value.integer;
real = value.real;
object = value.object;
if (test_triggers && visible_structure != null && !visible_structure.get("structure_type").equals("LOCAL")) // otherwise it's a temporary Value object and there cannot possibly a listener for it
Global.testTriggers();
}
return this;
}
// add ***************************************************************************************************************
public void add (final Value value) {
if(type == INTEGER && value.type == INTEGER)
set(integer + value.integer);
else
throw new RuntimeException("can only add up two integers");
}
public void add (final int i) {
if(type == INTEGER)
set(integer + i);
else
throw new RuntimeException("can only add up two integers");
}
// equals ************************************************************************************************************
public boolean equalsAbsolute (final Value value) {
if(type != value.type || type == REAL || value.type == REAL) {
if (( type == INTEGER || type == REAL )&&( value.type == INTEGER || value.type == REAL)) {
final Value v = ScriptNode.getValue("REAL_EQUAL_RANGE");
final double range = (v==null||v.type!=REAL) ? 0.000000001 : v.real;
final double a = (type==INTEGER) ? integer : real;
final double b = (value.type==INTEGER) ? value.integer : value.real;
return (a-b<range) && (b-a<range);
}
return false;
}
switch(type) {
case BOOLEAN :
return bool == value.bool;
case INTEGER :
return integer == value.integer;
// case REAL : // REAL is handled in the if clause above
case CONSTANT :
case STRING :
return ((String)object).equals((String)value.object);
default : // STRUCTURE or FUNCTION
return object == value.object;
}
}
public boolean equals (Value value) {
final Value v = (type==FUNCTION) ? executeExpression() : this;
if (value.type == FUNCTION)
value = value.executeExpression();
if(v.type != value.type || v.type == REAL)
return v.equalsAbsolute(value);
switch(type) {
case BOOLEAN :
return v.bool == value.bool;
case INTEGER :
return v.integer == value.integer;
// case REAL : // this case is handled above by calling equalsAbsolute for REAL values
case CONSTANT :
case STRING :
return ((String)v.object).equals((String)value.object);
default : // STRUCTURE or FUNCTION
return v.object == value.object;
}
}
public boolean equals (final boolean b) {
if (type == FUNCTION) {
final Value v = executeExpression();
return v.type == BOOLEAN && v.bool == b;
}
return type == BOOLEAN && bool == b;
}
public boolean equals (final int i) {
final Value v = (type == FUNCTION) ? executeExpression() : this;
if (v.type == REAL) {
final Value rer = ScriptNode.getValue("REAL_EQUAL_RANGE");
final double range = (rer==null||rer.type!=REAL) ? 0.000000001 : rer.real;
final double difference = v.real-i;
return difference < range && -difference < range;
}
return v.type == INTEGER && v.integer == i;
}
public boolean equals (final double r) {
final Value v = (type == FUNCTION) ? executeExpression() : this;
if (v.type != REAL && v.type != INTEGER)
return false;
final Value rer = ScriptNode.getValue("REAL_EQUAL_RANGE");
final double range = (rer==null||rer.type!=REAL) ? 0.000000001 : rer.real;
final double difference = r - ((v.type==REAL)?v.real:v.integer);
return difference < range && -difference < range;
}
public boolean equals (final String s) {
if (type == FUNCTION) {
final Value v = executeExpression();
return v.type == STRING && v.object.equals(s);
}
return type == STRING && object.equals(s);
}
public boolean equals (final Structure s) {
if (type == FUNCTION) {
final Value v = executeExpression();
return v.type == STRUCTURE && v.object == s;
}
return type == STRUCTURE && object == s;
}
public boolean equalsConstant (final String c) {
if (type == FUNCTION) {
final Value v = executeExpression();
return v.type == CONSTANT && v.object.equals(c);
}
return type == CONSTANT && object.equals(c);
}
// "get" *************************************************************************************************************
public Value get (final String name) {
final Value v = (type!=FUNCTION) ? this : executeExpression();
if(v.type != STRUCTURE)
throw new RuntimeException("Cannot convert "+TYPE_NAME[v.type]+" to STRUCTURE");
return ((Structure)v.object).get(name);
}
public Structure getEnclosingStructure () { return visible_structure; }
public Value getValue () { return (type != FUNCTION) ? this : executeExpression(); }
public boolean isFunction () { return type == FUNCTION; }
public int type () { return (type!=FUNCTION) ? type : executeExpression().type; }
public int typeDirect () { return type; }
public String typeName () { return TYPE_NAME[type()]; }
public String typeNameDirect () { return TYPE_NAME[type]; }
public boolean bool () {
final Value v = (type!=FUNCTION) ? this : executeExpression();
if (v.type != BOOLEAN)
throw new RuntimeException("Cannot convert "+TYPE_NAME[v.type]+" to BOOLEAN");
return v.bool;
}
public int integer () {
final Value v = (type!=FUNCTION) ? this : executeExpression();
if (v.type != INTEGER)
throw new RuntimeException("Cannot convert "+TYPE_NAME[v.type]+" to INTEGER");
return v.integer;
}
public double real () {
final Value v = (type!=FUNCTION) ? this : executeExpression();
if (v.type != REAL)
if (v.type == INTEGER)
return v.integer;
else
throw new RuntimeException("Cannot convert "+TYPE_NAME[v.type]+" to INTEGER");
return v.real;
}
public String string () {
final Value v = (type!=FUNCTION) ? this : executeExpression();
if (v.type != STRING)
throw new RuntimeException("Cannot convert "+TYPE_NAME[v.type]+" to STRING");
return (String) v.object;
}
public Structure structure () {
final Value v = (type!=FUNCTION) ? this : executeExpression();
if (v.type != STRUCTURE)
throw new RuntimeException("Cannot convert "+TYPE_NAME[v.type]+" to STRUCTURE");
return (Structure) v.object;
}
public String constant () {
final Value v = (type!=FUNCTION) ? this : executeExpression();
if (v.type != STRUCTURE)
throw new RuntimeException("Cannot convert "+TYPE_NAME[v.type]+" to STRUCTURE");
return (String) v.object;
}
public Function function () {
if(type != FUNCTION)
throw new RuntimeException("Cannot convert "+TYPE_NAME[type]+" to FUNCTION");
return (Function) object;
}
public String toString () { return (type==INTEGER) ? (integer+"") : ( (type==BOOLEAN) ? (bool+"") : ( (type==REAL) ? (real+"") : object.toString() )); }
}