mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
The DECLARE_TYPEOF(()) calls don't work in MSVC, I changed it to use a COMMA macro instead of ,
If this doesn't work in GCC, the COMMA definition could be made only for MSVC, then GCC sees DECLARE_TYPEOF(map<int COMMA string>). GCC doesn't need DECLARE_TYPEOF anyway. Keyword expansion now works, still todo: - marking parameters, e.g. "Cycling 2W" -> "Cycling <param-mana>2W</param-mana>" - user interface for toggling reminder text - user interface for keywords git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@210 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -215,7 +215,7 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) {
|
||||
a = toScript(-(int)*a);
|
||||
break;
|
||||
case I_NOT:
|
||||
a = toScript(!(int)*a);
|
||||
a = toScript(!(bool)*a);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
+61
-29
@@ -12,6 +12,7 @@
|
||||
#include <util/tagged_string.hpp>
|
||||
#include <data/set.hpp>
|
||||
#include <data/game.hpp>
|
||||
#include <data/keyword.hpp>
|
||||
#include <data/field/text.hpp>
|
||||
#include <wx/regex.h>
|
||||
|
||||
@@ -265,7 +266,7 @@ String spec_sort(const String& spec, const String& input) {
|
||||
ScriptValueP ScriptRule_##funname::eval(Context& ctx) const
|
||||
|
||||
// Utility for defining a script rule with two parameters
|
||||
#define SCRIPT_RULE_2(funname, type1, name1, type2, name2) \
|
||||
#define SCRIPT_RULE_2_N(funname, type1, str1, name1, type2, str2, name2) \
|
||||
class ScriptRule_##funname: public ScriptValue { \
|
||||
public: \
|
||||
inline ScriptRule_##funname(const type1& name1, const type2& name2) \
|
||||
@@ -278,16 +279,18 @@ String spec_sort(const String& spec, const String& input) {
|
||||
type2 name2; \
|
||||
}; \
|
||||
SCRIPT_FUNCTION(funname##_rule) { \
|
||||
SCRIPT_PARAM(type1, name1); \
|
||||
SCRIPT_PARAM(type2, name2); \
|
||||
SCRIPT_PARAM_N(type1, str1, name1); \
|
||||
SCRIPT_PARAM_N(type2, str2, name2); \
|
||||
return new_intrusive2<ScriptRule_##funname>(name1, name2); \
|
||||
} \
|
||||
SCRIPT_FUNCTION(funname) { \
|
||||
SCRIPT_PARAM(type1, name1); \
|
||||
SCRIPT_PARAM(type2, name2); \
|
||||
SCRIPT_PARAM_N(type1, str1, name1); \
|
||||
SCRIPT_PARAM_N(type2, str2, name2); \
|
||||
return ScriptRule_##funname(name1, name2).eval(ctx); \
|
||||
} \
|
||||
ScriptValueP ScriptRule_##funname::eval(Context& ctx) const
|
||||
#define SCRIPT_RULE_2(funname, type1, name1, type2, name2) \
|
||||
SCRIPT_RULE_2_N(funname, type1, _(#name1), name1, type2, _(#name2), name2)
|
||||
|
||||
|
||||
// Create a rule for spec_sorting strings
|
||||
@@ -339,10 +342,21 @@ SCRIPT_FUNCTION(contains) {
|
||||
SCRIPT_RETURN(input.find(match) != String::npos);
|
||||
}
|
||||
|
||||
SCRIPT_FUNCTION(format) {
|
||||
SCRIPT_PARAM(String, format);
|
||||
SCRIPT_PARAM(String, input);
|
||||
SCRIPT_RETURN(format_string(_("%") + replace_all(format, _("%"), _("")), input));
|
||||
SCRIPT_RULE_1(format, String, format) {
|
||||
String fmt = _("%") + replace_all(format, _("%"), _(""));
|
||||
// determine type expected by format string
|
||||
if (format.find_first_of(_("DdIiOoXx")) != String.npos) {
|
||||
SCRIPT_PARAM(int, input);
|
||||
SCRIPT_RETURN(String::Format(fmt, input));
|
||||
} else if (format.find_first_of(_("EeFfGg")) != String.npos) {
|
||||
SCRIPT_PARAM(double, input);
|
||||
SCRIPT_RETURN(String::Format(fmt, input));
|
||||
} else if (format.find_first_of(_("Ss")) != String.npos) {
|
||||
SCRIPT_PARAM(String, input);
|
||||
SCRIPT_RETURN(format_string(fmt, input));
|
||||
} else {
|
||||
throw ScriptError(_ERROR_1_("unsupported format", format));
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Tagged stuff
|
||||
@@ -381,7 +395,23 @@ SCRIPT_RULE_1(tag_remove, String, tag) {
|
||||
SCRIPT_RETURN(remove_tag(input, tag));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Vector stuff
|
||||
// ----------------------------------------------------------------------------- : Keywords
|
||||
|
||||
SCRIPT_RULE_2_N(expand_keywords, ScriptValueP, _("default expand"), default_expand,
|
||||
ScriptValueP, _("combine"), combine) {
|
||||
SCRIPT_PARAM(String, input);
|
||||
SCRIPT_PARAM(Set*, set);
|
||||
KeywordDatabase& db = set->keyword_db;
|
||||
if (db.empty()) {
|
||||
db.add(set->game->keywords);
|
||||
db.add(set->keywords);
|
||||
db.prepare_parameters(set->game->keyword_parameter_types, set->game->keywords);
|
||||
db.prepare_parameters(set->game->keyword_parameter_types, set->keywords);
|
||||
}
|
||||
SCRIPT_RETURN(db.expand(input, default_expand, combine, ctx));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Collection stuff
|
||||
|
||||
/// compare script values for equallity
|
||||
bool equal(const ScriptValue& a, const ScriptValue& b) {
|
||||
@@ -497,7 +527,7 @@ SCRIPT_FUNCTION_DEP(combined_editor) {
|
||||
size_t pos = value.find(_("<sep"));
|
||||
while (pos != String::npos) {
|
||||
value_parts.push_back(value.substr(0, pos));
|
||||
value = value.substr(min(skip_tag(value,match_close_tag(value,pos)), value.size()));
|
||||
value = value.substr(min(match_close_tag_end(value,pos), value.size()));
|
||||
pos = value.find(_("<sep"));
|
||||
}
|
||||
value_parts.push_back(value);
|
||||
@@ -579,23 +609,25 @@ ScriptValueP ScriptBuildin_combined_editor::dependencies(Context& ctx, const Dep
|
||||
// ----------------------------------------------------------------------------- : Initialize functions
|
||||
|
||||
void init_script_functions(Context& ctx) {
|
||||
ctx.setVariable(_("replace rule"), script_replace_rule);
|
||||
ctx.setVariable(_("filter rule"), script_filter_rule);
|
||||
ctx.setVariable(_("sort"), script_sort);
|
||||
ctx.setVariable(_("sort rule"), script_sort_rule);
|
||||
ctx.setVariable(_("to upper"), script_to_upper);
|
||||
ctx.setVariable(_("to lower"), script_to_lower);
|
||||
ctx.setVariable(_("to title"), script_to_title);
|
||||
ctx.setVariable(_("substring"), script_substring);
|
||||
ctx.setVariable(_("contains"), script_contains);
|
||||
ctx.setVariable(_("format"), script_format);
|
||||
ctx.setVariable(_("tag contents"), script_tag_contents);
|
||||
ctx.setVariable(_("remove tag"), script_tag_remove);
|
||||
ctx.setVariable(_("tag contents rule"), script_tag_contents_rule);
|
||||
ctx.setVariable(_("tag remove rule"), script_tag_remove_rule);
|
||||
ctx.setVariable(_("position"), script_position_of);
|
||||
ctx.setVariable(_("number of items"), script_number_of_items);
|
||||
ctx.setVariable(_("forward editor"), script_combined_editor);
|
||||
ctx.setVariable(_("combined editor"), script_combined_editor);
|
||||
ctx.setVariable(_("replace rule"), script_replace_rule);
|
||||
ctx.setVariable(_("filter rule"), script_filter_rule);
|
||||
ctx.setVariable(_("sort"), script_sort);
|
||||
ctx.setVariable(_("sort rule"), script_sort_rule);
|
||||
ctx.setVariable(_("to upper"), script_to_upper);
|
||||
ctx.setVariable(_("to lower"), script_to_lower);
|
||||
ctx.setVariable(_("to title"), script_to_title);
|
||||
ctx.setVariable(_("substring"), script_substring);
|
||||
ctx.setVariable(_("contains"), script_contains);
|
||||
ctx.setVariable(_("format"), script_format);
|
||||
ctx.setVariable(_("tag contents"), script_tag_contents);
|
||||
ctx.setVariable(_("remove tag"), script_tag_remove);
|
||||
ctx.setVariable(_("tag contents rule"), script_tag_contents_rule);
|
||||
ctx.setVariable(_("tag remove rule"), script_tag_remove_rule);
|
||||
ctx.setVariable(_("expand keywords rule"), script_expand_keywords_rule);
|
||||
ctx.setVariable(_("expand keywords"), script_expand_keywords);
|
||||
ctx.setVariable(_("position"), script_position_of);
|
||||
ctx.setVariable(_("number of items"), script_number_of_items);
|
||||
ctx.setVariable(_("forward editor"), script_combined_editor);
|
||||
ctx.setVariable(_("combined editor"), script_combined_editor);
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ ScriptImageP to_script_image(const ScriptValueP& value) {
|
||||
/// Is the given image up to date?
|
||||
bool script_image_up_to_date(const ScriptValueP& value) {
|
||||
if (value->type() == SCRIPT_INT) {
|
||||
return (int)*value; // boolean up-to-dateness from parameter
|
||||
return (bool)*value; // boolean up-to-dateness from parameter
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -22,8 +22,8 @@ DECLARE_TYPEOF(Contexts);
|
||||
DECLARE_TYPEOF_COLLECTION(CardP);
|
||||
DECLARE_TYPEOF_COLLECTION(FieldP);
|
||||
DECLARE_TYPEOF_COLLECTION(Dependency);
|
||||
DECLARE_TYPEOF_NO_REV((IndexMap<FieldP,StyleP>));
|
||||
DECLARE_TYPEOF_NO_REV((IndexMap<FieldP,ValueP>));
|
||||
DECLARE_TYPEOF_NO_REV(IndexMap<FieldP COMMA StyleP>);
|
||||
DECLARE_TYPEOF_NO_REV(IndexMap<FieldP COMMA ValueP>);
|
||||
|
||||
// initialize functions, from functions.cpp
|
||||
void init_script_functions(Context& ctx);
|
||||
|
||||
+10
-8
@@ -53,13 +53,13 @@ class ScriptCollection : public ScriptValue {
|
||||
public:
|
||||
inline ScriptCollection(const Collection* v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_COLLECTION; }
|
||||
virtual String typeName() const { return _("collection"); }
|
||||
virtual String typeName() const { return _TYPE_("collection"); }
|
||||
virtual ScriptValueP getMember(const String& name) const {
|
||||
long index;
|
||||
if (name.ToLong(&index) && index >= 0 && (size_t)index < value->size()) {
|
||||
return toScript(value->at(index));
|
||||
} else {
|
||||
throw ScriptError(_("Collection has no member ") + name);
|
||||
return ScriptValue::getMember(name);
|
||||
}
|
||||
}
|
||||
virtual ScriptValueP makeIterator() const {
|
||||
@@ -79,7 +79,7 @@ ScriptValueP get_member(const map<String,V>& m, const String& name) {
|
||||
if (it != m.end()) {
|
||||
return toScript(it->second);
|
||||
} else {
|
||||
throw ScriptError(_ERROR_1_("collection has no member", name));
|
||||
throw ScriptError(_ERROR_2_("has no member", _TYPE_("collection"), name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ ScriptValueP get_member(const IndexMap<K,V>& m, const String& name) {
|
||||
if (it != m.end()) {
|
||||
return toScript(*it);
|
||||
} else {
|
||||
throw ScriptError(_ERROR_1_("collection has no member", name));
|
||||
throw ScriptError(_ERROR_2_("has no member", _TYPE_("collection"), name));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ class ScriptMap : public ScriptValue {
|
||||
public:
|
||||
inline ScriptMap(const Collection* v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_COLLECTION; }
|
||||
virtual String typeName() const { return _("collection"); }
|
||||
virtual String typeName() const { return _TYPE_("collection"); }
|
||||
virtual ScriptValueP getMember(const String& name) const {
|
||||
return get_member(*value, name);
|
||||
}
|
||||
@@ -132,7 +132,7 @@ class ScriptObject : public ScriptValue {
|
||||
public:
|
||||
inline ScriptObject(const T& v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_OBJECT; }
|
||||
virtual String typeName() const { return _("object"); }
|
||||
virtual String typeName() const { return _TYPE_("object"); }
|
||||
virtual operator String() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator String(); }
|
||||
virtual operator double() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator double(); }
|
||||
virtual operator int() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator int(); }
|
||||
@@ -147,7 +147,7 @@ class ScriptObject : public ScriptValue {
|
||||
if (d) {
|
||||
return d->getMember(name);
|
||||
} else {
|
||||
throw ScriptError(_ERROR_1_("object has no member", name));
|
||||
throw ScriptValue::getMember(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -239,7 +239,9 @@ inline ScriptValueP toScript(const Defaultable<T>& v) { return toScript(v()); }
|
||||
* Throws an error if the parameter is not found.
|
||||
*/
|
||||
#define SCRIPT_PARAM(Type, name) \
|
||||
Type name = getParam<Type>(ctx.getVariable(_(#name)))
|
||||
SCRIPT_PARAM_N(Type, _(#name), name)
|
||||
#define SCRIPT_PARAM_N(Type, str, name) \
|
||||
Type name = getParam<Type>(ctx.getVariable(str))
|
||||
|
||||
template <typename T>
|
||||
inline T getParam (const ScriptValueP& value) {
|
||||
|
||||
+17
-17
@@ -15,14 +15,14 @@
|
||||
// Base cases
|
||||
|
||||
ScriptValue::operator String() const { return _("[[") + typeName() + _("]]"); }
|
||||
ScriptValue::operator int() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to integer number")); }
|
||||
ScriptValue::operator double() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to real number" )); }
|
||||
ScriptValue::operator Color() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to color" )); }
|
||||
ScriptValueP ScriptValue::eval(Context&) const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to function" )); }
|
||||
ScriptValueP ScriptValue::getMember(const String& name) const { throw ScriptError(typeName() + _(" has no member '") + name + _("'")); }
|
||||
ScriptValue::operator int() const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("integer" ))); }
|
||||
ScriptValue::operator double() const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("real" ))); }
|
||||
ScriptValue::operator Color() const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("color" ))); }
|
||||
ScriptValueP ScriptValue::eval(Context&) const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("function"))); }
|
||||
ScriptValueP ScriptValue::getMember(const String& name) const { throw ScriptError(_ERROR_2_("has no member", typeName(), name)); }
|
||||
ScriptValueP ScriptValue::next() { throw InternalError(_("Can't convert from ")+typeName()+_(" to iterator")); }
|
||||
ScriptValueP ScriptValue::makeIterator() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to collection")); }
|
||||
int ScriptValue::itemCount() const { throw ScriptError( _("Can't convert from ")+typeName()+_(" to collection")); }
|
||||
ScriptValueP ScriptValue::makeIterator() const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("collection"))); }
|
||||
int ScriptValue::itemCount() const { throw ScriptError(_ERROR_2_("can't convert", typeName(), _TYPE_("collection"))); }
|
||||
|
||||
ScriptValueP ScriptValue::dependencyMember(const String& name, const Dependency&) const { return dependency_dummy; }
|
||||
ScriptValueP ScriptValue::dependencies(Context&, const Dependency&) const { return dependency_dummy; }
|
||||
@@ -63,7 +63,7 @@ class ScriptInt : public ScriptValue {
|
||||
public:
|
||||
ScriptInt(int v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_INT; }
|
||||
virtual String typeName() const { return _("integer number"); }
|
||||
virtual String typeName() const { return _TYPE_("integer"); }
|
||||
virtual operator String() const { return String() << value; }
|
||||
virtual operator double() const { return value; }
|
||||
virtual operator int() const { return value; }
|
||||
@@ -108,7 +108,7 @@ class ScriptBool : public ScriptValue {
|
||||
public:
|
||||
ScriptBool(bool v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_INT; }
|
||||
virtual String typeName() const { return _("boolean"); }
|
||||
virtual String typeName() const { return _TYPE_("boolean"); }
|
||||
virtual operator String() const { return value ? _("true") : _("false"); }
|
||||
virtual operator int() const { return value; }
|
||||
private:
|
||||
@@ -130,7 +130,7 @@ class ScriptDouble : public ScriptValue {
|
||||
public:
|
||||
ScriptDouble(double v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_DOUBLE; }
|
||||
virtual String typeName() const { return _("real number"); }
|
||||
virtual String typeName() const { return _TYPE_("real"); }
|
||||
virtual operator String() const { return String() << value; }
|
||||
virtual operator double() const { return value; }
|
||||
virtual operator int() const { return (int)value; }
|
||||
@@ -149,14 +149,14 @@ class ScriptString : public ScriptValue {
|
||||
public:
|
||||
ScriptString(const String& v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_STRING; }
|
||||
virtual String typeName() const { return _("string"); }
|
||||
virtual String typeName() const { return _TYPE_("string"); }
|
||||
virtual operator String() const { return value; }
|
||||
virtual operator double() const {
|
||||
double d;
|
||||
if (value.ToDouble(&d)) {
|
||||
return d;
|
||||
} else {
|
||||
throw ScriptError(_("Not a number: '") + value + _("'"));
|
||||
throw ScriptError(_ERROR_3_("can't convert value", value, typeName(), _TYPE_("double")));
|
||||
}
|
||||
}
|
||||
virtual operator int() const {
|
||||
@@ -168,7 +168,7 @@ class ScriptString : public ScriptValue {
|
||||
} else if (value == _("no") || value == _("false") || value.empty()) {
|
||||
return false;
|
||||
} else {
|
||||
throw ScriptError(_("Not a number: '") + value + _("'"));
|
||||
throw ScriptError(_ERROR_3_("can't convert value", value, typeName(), _TYPE_("integer")));
|
||||
}
|
||||
}
|
||||
virtual operator Color() const {
|
||||
@@ -176,7 +176,7 @@ class ScriptString : public ScriptValue {
|
||||
if (wxSscanf(value.c_str(),_("rgb(%u,%u,%u)"),&r,&g,&b)) {
|
||||
return Color(r, g, b);
|
||||
} else {
|
||||
throw ScriptError(_("Not a color: '") + value + _("'"));
|
||||
throw ScriptError(_ERROR_3_("can't convert value", value, typeName(), _TYPE_("color")));
|
||||
}
|
||||
}
|
||||
virtual int itemCount() const { return (int)value.size(); }
|
||||
@@ -186,7 +186,7 @@ class ScriptString : public ScriptValue {
|
||||
if (name.ToLong(&index) && index >= 0 && (size_t)index < value.size()) {
|
||||
return toScript(String(1,value[index]));
|
||||
} else {
|
||||
throw ScriptError(_("String \"") + value + _("\" has no member ") + name);
|
||||
throw ScriptError(_ERROR_2_("has no member value", value, name));
|
||||
}
|
||||
}
|
||||
private:
|
||||
@@ -205,7 +205,7 @@ class ScriptColor : public ScriptValue {
|
||||
public:
|
||||
ScriptColor(const Color& v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_COLOR; }
|
||||
virtual String typeName() const { return _("color"); }
|
||||
virtual String typeName() const { return _TYPE_("color"); }
|
||||
virtual operator Color() const { return value; }
|
||||
virtual operator String() const {
|
||||
return String::Format(_("rgb(%u,%u,%u)"), value.Red(), value.Green(), value.Blue());
|
||||
@@ -225,7 +225,7 @@ ScriptValueP toScript(const Color& v) {
|
||||
class ScriptNil : public ScriptValue {
|
||||
public:
|
||||
virtual ScriptType type() const { return SCRIPT_NIL; }
|
||||
virtual String typeName() const { return _("nil"); }
|
||||
virtual String typeName() const { return _TYPE_("nil"); }
|
||||
virtual operator String() const { return wxEmptyString; }
|
||||
virtual operator double() const { return 0.0; }
|
||||
virtual operator int() const { return 0; }
|
||||
|
||||
@@ -48,6 +48,8 @@ class ScriptValue : public IntrusivePtrBase {
|
||||
virtual operator double() const;
|
||||
/// Convert this value to an integer
|
||||
virtual operator int() const;
|
||||
/// Convert this value to a boolean
|
||||
inline operator bool() const { return (int)*this; }
|
||||
/// Convert this value to a color
|
||||
virtual operator Color() const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user