Repository navigation
Expand file tree
/
Copy pathvalue.cpp
More file actions
554 lines (491 loc) · 19.7 KB
/
Copy pathvalue.cpp
File metadata and controls
554 lines (491 loc) · 19.7 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2012 Twan van Laarhoven and Sean Hunt |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <script/value.hpp>
#include <script/to_value.hpp>
#include <script/context.hpp>
#include <gfx/generated_image.hpp>
#include <util/error.hpp>
#include <util/tagged_string.hpp>
#include <boost/pool/singleton_pool.hpp>
using std::make_pair;
// ----------------------------------------------------------------------------- : ScriptValue
// Base cases
String ScriptValue::toString() const { throw ScriptErrorConversion(typeName(), _TYPE_("string" )); }
int ScriptValue::toInt() const { throw ScriptErrorConversion(typeName(), _TYPE_("integer" )); }
bool ScriptValue::toBool() const { throw ScriptErrorConversion(typeName(), _TYPE_("boolean" )); }
double ScriptValue::toDouble() const { throw ScriptErrorConversion(typeName(), _TYPE_("double" )); }
AColor ScriptValue::toColor() const { throw ScriptErrorConversion(typeName(), _TYPE_("color" )); }
wxDateTime ScriptValue::toDateTime() const { throw ScriptErrorConversion(typeName(), _TYPE_("date" )); }
GeneratedImageP ScriptValue::toImage() const { throw ScriptErrorConversion(typeName(), _TYPE_("image" )); }
String ScriptValue::toCode() const { return toString(); }
String ScriptValue::toFriendlyString() const { return toString(); }
ScriptValueP ScriptValue::do_eval(Context&, bool) const { return delay_error(ScriptErrorConversion(typeName(), _TYPE_("function"))); }
ScriptValueP ScriptValue::next(ScriptValueP* key_out) { throw InternalError(_("Can't convert from ")+typeName()+_(" to iterator")); }
ScriptValueP ScriptValue::makeIterator() const { return delay_error(ScriptErrorConversion(typeName(), _TYPE_("collection"))); }
int ScriptValue::itemCount() const { throw ScriptErrorConversion(typeName(), _TYPE_("collection")); }
CompareWhat ScriptValue::compareAs(String& compare_str, void const*& compare_ptr) const {
compare_str = toCode();
return COMPARE_AS_STRING;
}
ScriptValueP ScriptValue::getMember(const String& name) const {
long index;
if (name.ToLong(&index)) {
return getIndex(index);
} else {
return delay_error(ScriptErrorNoMember(typeName(), name));
}
}
ScriptValueP ScriptValue::getIndex(int index) const {
return delay_error(ScriptErrorNoMember(typeName(), String()<<index));
}
ScriptValueP ScriptValue::simplifyClosure(ScriptClosure&) const { return ScriptValueP(); }
ScriptValueP ScriptValue::dependencyMember(const String& name, const Dependency&) const { return dependency_dummy; }
ScriptValueP ScriptValue::dependencyName(const ScriptValue& container, const Dependency& dep) const {
return container.dependencyMember(toString(),dep);
}
ScriptValueP ScriptValue::dependencies(Context&, const Dependency&) const { return dependency_dummy; }
void ScriptValue::dependencyThis(const Dependency& dep) {}
bool approx_equal(double a, double b) {
return a == b || fabs(a - b) < 1e-14;
}
/// compare script values for equallity
bool equal(const ScriptValueP& a, const ScriptValueP& b) {
if (a == b) return true;
ScriptType at = a->type(), bt = b->type();
if (at == bt && at == SCRIPT_INT) {
return a->toInt() == b->toInt();
} else if (at == bt && at == SCRIPT_BOOL) {
return a->toBool() == b->toBool();
} else if ((at == SCRIPT_INT || at == SCRIPT_DOUBLE) &&
(bt == SCRIPT_INT || bt == SCRIPT_DOUBLE)) {
return approx_equal( a->toDouble(), b->toDouble());
} else if (at == SCRIPT_COLLECTION && bt == SCRIPT_COLLECTION) {
// compare each element
if (a->itemCount() != b->itemCount()) return false;
ScriptValueP a_it = a->makeIterator();
ScriptValueP b_it = b->makeIterator();
while (true) {
ScriptValueP a_v = a_it->next();
ScriptValueP b_v = b_it->next();
if (!a_v || !b_v) return a_v == b_v;
if (!equal(a_v, b_v)) return false;
}
} else {
String as, bs;
const void* ap, *bp;
CompareWhat aw = a->compareAs(as, ap);
CompareWhat bw = b->compareAs(bs, bp);
// compare pointers or strings
if (aw == COMPARE_AS_STRING || bw == COMPARE_AS_STRING) {
return as == bs;
} else if (aw == COMPARE_AS_POINTER || bw == COMPARE_AS_POINTER) {
return ap == bp;
} else {
return false;
}
}
}
// ----------------------------------------------------------------------------- : Errors
ScriptType ScriptDelayedError::type() const { return SCRIPT_ERROR; }
String ScriptDelayedError::typeName() const { throw error; }
String ScriptDelayedError::toString() const { throw error; }
double ScriptDelayedError::toDouble() const { throw error; }
int ScriptDelayedError::toInt() const { throw error; }
bool ScriptDelayedError::toBool() const { throw error; }
AColor ScriptDelayedError::toColor() const { throw error; }
int ScriptDelayedError::itemCount() const { throw error; }
CompareWhat ScriptDelayedError::compareAs(String&, void const*&) const { throw error; }
ScriptValueP ScriptDelayedError::getMember(const String&) const { return intrusive_from_existing(const_cast<ScriptDelayedError*>(this)); }
ScriptValueP ScriptDelayedError::dependencyMember(const String&, const Dependency&) const { return intrusive_from_existing(const_cast<ScriptDelayedError*>(this)); }
ScriptValueP ScriptDelayedError::do_eval(Context&, bool) const { return intrusive_from_existing(const_cast<ScriptDelayedError*>(this)); }
ScriptValueP ScriptDelayedError::dependencies(Context&, const Dependency&) const { return intrusive_from_existing(const_cast<ScriptDelayedError*>(this)); }
ScriptValueP ScriptDelayedError::makeIterator() const { return intrusive_from_existing(const_cast<ScriptDelayedError*>(this)); }
// ----------------------------------------------------------------------------- : Iterators
ScriptType ScriptIterator::type() const { return SCRIPT_ITERATOR; }
String ScriptIterator::typeName() const { return _("iterator"); }
CompareWhat ScriptIterator::compareAs(String&, void const*&) const { return COMPARE_NO; }
ScriptValueP ScriptIterator::makeIterator() const { return intrusive_from_existing(const_cast<ScriptIterator*>(this)); }
// Iterator over a range of integers
class ScriptRangeIterator : public ScriptIterator {
public:
// Construct a range iterator with the given bounds (inclusive)
ScriptRangeIterator(int start, int end)
: pos(start), start(start), end(end) {}
virtual ScriptValueP next(ScriptValueP* key_out) {
if (pos <= end) {
if (key_out) *key_out = to_script(pos-start);
return to_script(pos++);
} else {
return ScriptValueP();
}
}
private:
int pos, start, end;
};
ScriptValueP rangeIterator(int start, int end) {
return intrusive(new ScriptRangeIterator(start, end));
}
// ----------------------------------------------------------------------------- : Integers
#define USE_POOL_ALLOCATOR
// Integer values
class ScriptInt : public ScriptValue {
public:
ScriptInt(int v) : value(v) {}
virtual ScriptType type() const { return SCRIPT_INT; }
virtual String typeName() const { return _TYPE_("integer"); }
virtual String toString() const { return String() << value; }
virtual double toDouble() const { return value; }
virtual int toInt() const { return value; }
protected:
#ifdef USE_POOL_ALLOCATOR
virtual void destroy() {
boost::singleton_pool<ScriptValue, sizeof(ScriptInt)>::free(this);
}
#endif
private:
int value;
};
ScriptValueP to_script(int v) {
#ifdef USE_POOL_ALLOCATOR
return intrusive(
new(boost::singleton_pool<ScriptValue, sizeof(ScriptInt)>::malloc())
ScriptInt(v));
#else
return intrusive(new ScriptInt(v));
#endif
}
// ----------------------------------------------------------------------------- : Booleans
// Boolean values
class ScriptBool : public ScriptValue {
public:
ScriptBool(bool v) : value(v) {}
virtual ScriptType type() const { return SCRIPT_BOOL; }
virtual String typeName() const { return _TYPE_("boolean"); }
virtual String toString() const { return value ? _("true") : _("false"); }
// bools don't autoconvert to int
virtual bool toBool() const { return value; }
private:
bool value;
};
// use integers to represent true/false
/* NOTE: previous versions used ScriptInts as booleans, this gives problems
* when we use a pool allocator for them, because the pool is destroyed before these globals.
*/
ScriptValueP script_true (new ScriptBool(true));
ScriptValueP script_false(new ScriptBool(false));
// ----------------------------------------------------------------------------- : Doubles
// Double values
class ScriptDouble : public ScriptValue {
public:
ScriptDouble(double v) : value(v) {}
virtual ScriptType type() const { return SCRIPT_DOUBLE; }
virtual String typeName() const { return _TYPE_("double"); }
virtual String toString() const { return String() << value; }
virtual double toDouble() const { return value; }
virtual int toInt() const { return (int)value; } // TODO: do we want this automatic conversion?
private:
double value;
};
ScriptValueP to_script(double v) {
return intrusive(new ScriptDouble(v));
}
// ----------------------------------------------------------------------------- : String type
String quote_string(String const& str) {
String out;
out.reserve(str.size() + 2);
out += _('"');
for(const auto& c : str) {
if (c == _('"') || c == _('\\')) { out += _('\\'); out += c; }
else if (c == _('\1')) out += _("\\<");
else if (c == _('\n')) out += _("\\n");
else if (c == _('\r')) out += _("\\r");
else if (c == _('\t')) out += _("\\t");
else out += c;
}
out += _('"');
return out;
}
// String values
class ScriptString : public ScriptValue {
public:
ScriptString(const String& v) : value(v) {}
virtual ScriptType type() const { return SCRIPT_STRING; }
virtual String typeName() const { return _TYPE_("string") + _(" (\"") + (value.size() < 30 ? value : value.substr(0,30) + _("...")) + _("\")"); }
virtual String toString() const { return value; }
virtual String toCode() const {
return quote_string(value);
}
virtual String toFriendlyString() const {
// assume that the string can contain tags
// TODO: distinguish between strings with tags and strings without
return untag(value);
}
virtual double toDouble() const {
double d;
if (value.ToDouble(&d)) {
return d;
} else {
throw ScriptErrorConversion(value, typeName(), _TYPE_("double"));
}
}
virtual int toInt() const {
long l;
if (value.ToLong(&l)) {
return l;
} else {
throw ScriptErrorConversion(value, typeName(), _TYPE_("integer"));
}
}
virtual bool toBool() const {
if (value == _("yes") || value == _("true")) {
return true;
} else if (value == _("no") || value == _("false") || value.empty()) {
return false;
} else {
throw ScriptErrorConversion(value, typeName(), _TYPE_("boolean"));
}
}
virtual AColor toColor() const {
AColor c = parse_acolor(value);
if (!c.Ok()) {
throw ScriptErrorConversion(value, typeName(), _TYPE_("color"));
}
return c;
}
virtual wxDateTime toDateTime() const {
wxDateTime date;
if (!date.ParseDateTime(value.c_str())) {
throw ScriptErrorConversion(value, typeName(), _TYPE_("date"));
}
return date;
}
virtual GeneratedImageP toImage() const {
if (value.empty()) {
return intrusive(new BlankImage());
} else {
return intrusive(new PackagedImage(value));
}
}
virtual int itemCount() const { return (int)value.size(); }
virtual ScriptValueP getMember(const String& name) const {
// get member returns characters
long index;
if (name.ToLong(&index) && index >= 0 && (size_t)index < value.size()) {
return to_script(String(1,value[index]));
} else {
return delay_error(_ERROR_2_("has no member value", value, name));
}
}
private:
String value;
};
ScriptValueP to_script(const String& v) {
return intrusive(new ScriptString(v));
}
// ----------------------------------------------------------------------------- : Color
// AColor values
class ScriptAColor : public ScriptValue {
public:
ScriptAColor(const AColor& v) : value(v) {}
virtual ScriptType type() const { return SCRIPT_COLOR; }
virtual String typeName() const { return _TYPE_("color"); }
virtual AColor toColor() const { return value; }
// colors don't auto convert to int, use to_int to force
virtual String toString() const {
return format_acolor(value);
}
private:
AColor value;
};
ScriptValueP to_script(Color v) {
return intrusive(new ScriptAColor(v));
}
ScriptValueP to_script(AColor v) {
return intrusive(new ScriptAColor(v));
}
// ----------------------------------------------------------------------------- : DateTime
// wxDateTime values
class ScriptDateTime : public ScriptValue {
public:
ScriptDateTime(const wxDateTime& v) : value(v) {}
virtual ScriptType type() const { return SCRIPT_DATETIME; }
virtual String typeName() const { return _TYPE_("date"); }
virtual wxDateTime toDateTime() const { return value; }
virtual String toString() const {
return value.Format(_("%Y-%m-%d %H:%M:%S"));
}
private:
wxDateTime value;
};
ScriptValueP to_script(wxDateTime v) {
return intrusive(new ScriptDateTime(v));
}
// ----------------------------------------------------------------------------- : Nil type
// the nil object
class ScriptNil : public ScriptValue {
public:
virtual ScriptType type() const { return SCRIPT_NIL; }
virtual String typeName() const { return _TYPE_("nil"); }
virtual String toString() const { return wxEmptyString; }
virtual String toCode() const { return _("nil"); }
virtual double toDouble() const { return 0.0; }
virtual int toInt() const { return 0; }
virtual bool toBool() const { return false; }
virtual AColor toColor() const { return AColor(0,0,0,0); }
virtual GeneratedImageP toImage() const {
return intrusive(new BlankImage());
}
protected:
virtual ScriptValueP do_eval(Context& ctx, bool) const {
// nil(input) == input
return ctx.getVariable(SCRIPT_VAR_input);
}
};
/// The preallocated nil value
ScriptValueP script_nil(new ScriptNil);
/// Preallocated nil value marked as default
ScriptValueP script_default_nil(new ScriptDefault(script_nil));
// ----------------------------------------------------------------------------- : Collection base
String ScriptCollectionBase::toCode() const {
String ret = _("[");
bool first = true;
ScriptValueP it = makeIterator();
while (ScriptValueP v = it->next()) {
if (!first) ret += _(",");
first = false;
// todo: include keys
ret += v->toCode();
}
ret += _("]");
return ret;
}
// ----------------------------------------------------------------------------- : Custom collection
// Iterator over a custom collection
class ScriptCustomCollectionIterator : public ScriptIterator {
public:
ScriptCustomCollectionIterator(ScriptCustomCollectionP col)
: col(col), pos(0), it(col->key_value.begin()) {}
virtual ScriptValueP next(ScriptValueP* key_out) {
if (pos < col->value.size()) {
if (key_out) *key_out = to_script((int)pos);
return col->value.at(pos++);
} else if (it != col->key_value.end()) {
if (key_out) *key_out = to_script(it->first);
return (it++)->second;
} else {
return ScriptValueP();
}
}
private:
ScriptCustomCollectionP col;
size_t pos;
map<String,ScriptValueP>::const_iterator it;
};
ScriptValueP ScriptCustomCollection::getMember(const String& name) const {
map<String,ScriptValueP>::const_iterator it = key_value.find(name);
if (it != key_value.end()) {
return it->second;
} else {
return ScriptValue::getMember(name);
}
}
ScriptValueP ScriptCustomCollection::getIndex(int index) const {
if (index >= 0 && (size_t)index < value.size()) {
return value.at(index);
} else {
return ScriptValue::getIndex(index);
}
}
ScriptValueP ScriptCustomCollection::makeIterator() const {
return intrusive(new ScriptCustomCollectionIterator(
intrusive_from_existing(const_cast<ScriptCustomCollection*>(this))
));
}
// ----------------------------------------------------------------------------- : Concat collection
// Iterator over a concatenated collection
class ScriptConcatCollectionIterator : public ScriptIterator {
public:
ScriptConcatCollectionIterator(const ScriptValueP& itA, const ScriptValueP& itB)
: itA(itA), itB(itB) {}
virtual ScriptValueP next(ScriptValueP* key_out) {
if (itA) {
ScriptValueP v = itA->next(key_out);
if (v) return v;
else itA = ScriptValueP();
}
// TODO: somehow fix up the keys
return itB->next(key_out);
}
private:
ScriptValueP itA, itB;
};
ScriptValueP ScriptConcatCollection::getMember(const String& name) const {
ScriptValueP member = a->getMember(name);
if (member->type() != SCRIPT_ERROR) return member;
long index;
int itemsInA = a->itemCount();
if (name.ToLong(&index) && index - itemsInA >= 0 && index - itemsInA < b->itemCount()) {
// adjust integer index
return b->getMember(String() << (index - itemsInA));
} else {
return b->getMember(name);
}
}
ScriptValueP ScriptConcatCollection::getIndex(int index) const {
int itemsInA = a->itemCount();
if (index < itemsInA) {
return a->getIndex(index);
} else {
return b->getIndex(index - itemsInA);
}
}
ScriptValueP ScriptConcatCollection::makeIterator() const {
return intrusive(new ScriptConcatCollectionIterator(a->makeIterator(), b->makeIterator()));
}
// ----------------------------------------------------------------------------- : Default arguments / closure
ScriptType ScriptClosure::type() const {
return SCRIPT_FUNCTION;
}
String ScriptClosure::typeName() const {
return fun->typeName() + _(" closure");
}
void ScriptClosure::addBinding(Variable v, const ScriptValueP& value) {
bindings.push_back(make_pair(v,value));
}
ScriptValueP ScriptClosure::getBinding(Variable v) const {
for(const auto& b : bindings) {
if (b.first == v) return b.second;
}
return ScriptValueP();
}
ScriptValueP ScriptClosure::simplify() {
return fun->simplifyClosure(*this);
}
ScriptValueP ScriptClosure::do_eval(Context& ctx, bool openScope) const {
scoped_ptr<LocalScope> scope(openScope ? new LocalScope(ctx) : nullptr);
applyBindings(ctx);
return fun->eval(ctx, openScope);
}
ScriptValueP ScriptClosure::dependencies(Context& ctx, const Dependency& dep) const {
LocalScope scope(ctx);
applyBindings(ctx);
return fun->dependencies(ctx, dep);
}
void ScriptClosure::applyBindings(Context& ctx) const {
for(const auto& b : bindings) {
if (ctx.getVariableScope(b.first) != 0) {
// variables passed as arguments (i.e. in scope 0) override these default bindings
ctx.setVariable(b.first, b.second);
}
}
}
ScriptType ScriptRule::type() const { return SCRIPT_FUNCTION; }
String ScriptRule::typeName() const { return fun->typeName() + _(" rule"); }
ScriptValueP ScriptRule::do_eval(Context& ctx, bool openScope) const {
return ctx.makeClosure(fun);
}