Added ^ power operator,

Added abs, random_int, random_real, random_shuffle, random_select script functions.
Made == comparison of doubles use a small epsilon, so things like 3/2 == 1.5 are actually true.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1013 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2008-07-09 15:51:07 +00:00
parent 69fed99633
commit 7cb2292d36
10 changed files with 167 additions and 26 deletions
+33 -8
View File
@@ -24,7 +24,6 @@ ScriptValue::operator bool() const { throw Script
ScriptValue::operator double() const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("double" ))); }
ScriptValue::operator AColor() const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("color" ))); }
ScriptValueP ScriptValue::eval(Context&) const { return delayError(_ERROR_2_("can't convert", typeName(), _TYPE_("function"))); }
ScriptValueP ScriptValue::getMember(const String& name) const { return delayError(_ERROR_2_("has no member", typeName(), name)); }
ScriptValueP ScriptValue::next() { throw InternalError(_("Can't convert from ")+typeName()+_(" to iterator")); }
ScriptValueP ScriptValue::makeIterator(const ScriptValueP&) const { return delayError(_ERROR_2_("can't convert", typeName(), _TYPE_("collection"))); }
int ScriptValue::itemCount() const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("collection"))); }
@@ -32,12 +31,28 @@ CompareWhat ScriptValue::compareAs(String& compare_str, void const*& compare_pt
compare_str = toString();
return COMPARE_AS_STRING;
}
ScriptValueP ScriptValue::getMember(const String& name) const {
long index;
if (name.ToLong(&index)) {
return getIndex(index);
} else {
return delayError(_ERROR_2_("has no member", typeName(), name));
}
}
ScriptValueP ScriptValue::getIndex(int index) const {
return delayError(_ERROR_2_("has no member", typeName(), String()<<index));
}
ScriptValueP ScriptValue::simplifyClosure(ScriptClosure&) const { return ScriptValueP(); }
ScriptValueP ScriptValue::dependencyMember(const String& name, const Dependency&) const { return dependency_dummy; }
ScriptValueP ScriptValue::dependencies(Context&, const Dependency&) const { return dependency_dummy; }
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;
@@ -48,7 +63,7 @@ bool equal(const ScriptValueP& a, const ScriptValueP& b) {
return (bool)*a == (bool)*b;
} else if ((at == SCRIPT_INT || at == SCRIPT_DOUBLE) &&
(bt == SCRIPT_INT || bt == SCRIPT_DOUBLE)) {
return (double)*a == (double)*b;
return approx_equal( (double)*a, (double)*b);
} else if (at == SCRIPT_COLLECTION && bt == SCRIPT_COLLECTION) {
// compare each element
if (a->itemCount() != b->itemCount()) return false;
@@ -338,12 +353,14 @@ ScriptValueP ScriptCustomCollection::getMember(const String& name) const {
if (it != key_value.end()) {
return it->second;
} else {
long index;
if (name.ToLong(&index) && index >= 0 && (size_t)index < value.size()) {
return value.at(index);
} else {
return ScriptValue::getMember(name);
}
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 ScriptValueP& thisP) const {
@@ -381,6 +398,14 @@ ScriptValueP ScriptConcatCollection::getMember(const String& name) const {
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 ScriptValueP& thisP) const {
return new_intrusive2<ScriptConcatCollectionIterator>(a->makeIterator(a), b->makeIterator(b));
}