mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
Optimize default arguments (@ operator) into script rules.
The plan is to deprecate rule form completely, so instead of replace_rule(match:..) write replace_text@(match: ...) git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@987 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -374,7 +374,7 @@ Image BuiltInImage::generate(const Options& opt) const {
|
||||
// TODO : use opt.width and opt.height?
|
||||
Image img = load_resource_image(name);
|
||||
if (!img.Ok()) {
|
||||
throw ScriptError(_("There is no build in image '") + name + _("'"));
|
||||
throw ScriptError(_("There is no built in image '") + name + _("'"));
|
||||
}
|
||||
return img;
|
||||
}
|
||||
|
||||
+6
-40
@@ -12,8 +12,6 @@
|
||||
#include <util/error.hpp>
|
||||
#include <iostream>
|
||||
|
||||
DECLARE_TYPEOF_COLLECTION(pair<Variable COMMA ScriptValueP>);
|
||||
|
||||
// ----------------------------------------------------------------------------- : Context
|
||||
|
||||
Context::Context()
|
||||
@@ -290,41 +288,6 @@ class ScriptCompose : public ScriptValue {
|
||||
ScriptValueP a,b;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Closures
|
||||
|
||||
/// A closure around a function
|
||||
class ScriptClosure : public ScriptValue {
|
||||
public:
|
||||
ScriptClosure(ScriptValueP fun) : fun(fun) {}
|
||||
|
||||
/// Add a binding
|
||||
void bind(Variable v, const ScriptValueP& value) {
|
||||
bindings.push_back(make_pair(v,value));
|
||||
}
|
||||
/// Apply the bindings
|
||||
void applyBindings(Context& ctx) const {
|
||||
FOR_EACH_CONST(b, bindings) {
|
||||
if (ctx.getVariableScope(b.first) != 0) {
|
||||
ctx.setVariable(b.first, b.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual ScriptType type() const { return SCRIPT_FUNCTION; }
|
||||
virtual String typeName() const { return _("function closure"); }
|
||||
virtual ScriptValueP eval(Context& ctx) const {
|
||||
applyBindings(ctx);
|
||||
return fun->eval(ctx);
|
||||
}
|
||||
virtual ScriptValueP dependencies(Context& ctx, const Dependency& dep) const {
|
||||
applyBindings(ctx);
|
||||
return fun->dependencies(ctx, dep);
|
||||
}
|
||||
private:
|
||||
ScriptValueP fun;
|
||||
vector<pair<Variable,ScriptValueP> > bindings;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Simple instructions : binary
|
||||
|
||||
// operator on ints
|
||||
@@ -459,11 +422,14 @@ void Context::makeObject(size_t n) {
|
||||
void Context::makeClosure(size_t n, const Instruction*& instr) {
|
||||
intrusive_ptr<ScriptClosure> closure(new ScriptClosure(stack[stack.size() - n - 1]));
|
||||
for (size_t j = 0 ; j < n ; ++j) {
|
||||
closure->bind((Variable)instr[n - j - 1].data, stack.back());
|
||||
closure->addBinding((Variable)instr[n - j - 1].data, stack.back());
|
||||
stack.pop_back();
|
||||
}
|
||||
// skip arguments
|
||||
instr += n;
|
||||
// set value
|
||||
stack.back() = closure;
|
||||
// set value, try to simplify
|
||||
stack.back() = closure->simplify();
|
||||
if (!stack.back()) {
|
||||
stack.back() = closure;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -611,9 +611,18 @@ ScriptValueP match_rule(Context& ctx) {
|
||||
SCRIPT_FUNCTION(match_rule) {
|
||||
return match_rule(ctx);
|
||||
}
|
||||
SCRIPT_FUNCTION(match) {
|
||||
SCRIPT_FUNCTION_WITH_SIMPLIFY(match) {
|
||||
return match_rule(ctx)->eval(ctx);
|
||||
}
|
||||
SCRIPT_FUNCTION_SIMPLIFY_CLOSURE(match) {
|
||||
ScriptValueP match = closure.getBinding(SCRIPT_VAR_match);
|
||||
if (match) {
|
||||
intrusive_ptr<ScriptMatchRule> ret(new ScriptMatchRule);
|
||||
from_script(match, ret->regex);
|
||||
return ret;
|
||||
}
|
||||
return ScriptValueP();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Rules : sort text
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ void init_script_editor_functions(Context& ctx);
|
||||
void init_script_export_functions(Context& ctx);
|
||||
void init_script_english_functions(Context& ctx);
|
||||
|
||||
/// Initialize all build in functions for a context
|
||||
/// Initialize all built in functions for a context
|
||||
inline void init_script_functions(Context& ctx) {
|
||||
init_script_basic_functions(ctx);
|
||||
init_script_image_functions(ctx);
|
||||
|
||||
@@ -42,20 +42,27 @@
|
||||
SCRIPT_FUNCTION_AUX(name, virtual ScriptValueP dependencies(Context&, const Dependency&) const;)
|
||||
|
||||
#define SCRIPT_FUNCTION_DEPENDENCIES(name) \
|
||||
ScriptValueP ScriptBuildin_##name::dependencies(Context& ctx, const Dependency& dep) const
|
||||
ScriptValueP ScriptBuiltIn_##name::dependencies(Context& ctx, const Dependency& dep) const
|
||||
|
||||
/// Macro to declare a new script function with custom closure simplification
|
||||
#define SCRIPT_FUNCTION_WITH_SIMPLIFY(name) \
|
||||
SCRIPT_FUNCTION_AUX(name, virtual ScriptValueP simplifyClosure(ScriptClosure&) const;)
|
||||
|
||||
#define SCRIPT_FUNCTION_SIMPLIFY_CLOSURE(name) \
|
||||
ScriptValueP ScriptBuiltIn_##name::simplifyClosure(ScriptClosure& closure) const
|
||||
|
||||
// helper for SCRIPT_FUNCTION and SCRIPT_FUNCTION_DEP
|
||||
#define SCRIPT_FUNCTION_AUX(name,dep) \
|
||||
class ScriptBuildin_##name : public ScriptValue { \
|
||||
class ScriptBuiltIn_##name : public ScriptValue { \
|
||||
dep \
|
||||
virtual ScriptType type() const \
|
||||
{ return SCRIPT_FUNCTION; } \
|
||||
virtual String typeName() const \
|
||||
{ return _("build in function '") _(#name) _("'"); } \
|
||||
{ return _("built-in function '") _(#name) _("'"); } \
|
||||
virtual ScriptValueP eval(Context&) const; \
|
||||
}; \
|
||||
ScriptValueP script_##name(new ScriptBuildin_##name); \
|
||||
ScriptValueP ScriptBuildin_##name::eval(Context& ctx) const
|
||||
ScriptValueP script_##name(new ScriptBuiltIn_##name); \
|
||||
ScriptValueP ScriptBuiltIn_##name::eval(Context& ctx) const
|
||||
|
||||
/// Return a value from a SCRIPT_FUNCTION
|
||||
#define SCRIPT_RETURN(value) return to_script(value)
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <script/value.hpp>
|
||||
#include <script/script.hpp>
|
||||
#include <util/reflect.hpp>
|
||||
#include <util/error.hpp>
|
||||
#include <util/io/get_member.hpp>
|
||||
@@ -290,6 +291,36 @@ class ScriptObject : public ScriptValue {
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Default arguments / closure
|
||||
|
||||
/// A wrapper around a function that gives default arguments
|
||||
class ScriptClosure : public ScriptValue {
|
||||
public:
|
||||
ScriptClosure(ScriptValueP fun) : fun(fun) {}
|
||||
|
||||
virtual ScriptType type() const;
|
||||
virtual String typeName() const;
|
||||
virtual ScriptValueP eval(Context& ctx) const;
|
||||
virtual ScriptValueP dependencies(Context& ctx, const Dependency& dep) const;
|
||||
|
||||
/// Add a binding
|
||||
void addBinding(Variable v, const ScriptValueP& value);
|
||||
/// Is there a binding for the given variable? If so, retrieve it
|
||||
ScriptValueP getBinding(Variable v) const;
|
||||
|
||||
/// Try to simplify this closure, returns a value if successful
|
||||
ScriptValueP simplify();
|
||||
|
||||
/// The wrapped function
|
||||
ScriptValueP fun;
|
||||
/// The default argument bindings
|
||||
vector<pair<Variable,ScriptValueP> > bindings;
|
||||
|
||||
private:
|
||||
/// Apply the bindings in a context
|
||||
void applyBindings(Context& ctx) const;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Creating
|
||||
|
||||
/// Convert a value to a script value
|
||||
@@ -329,5 +360,7 @@ template <> inline bool from_script<bool> (const ScriptValueP& va
|
||||
template <> inline Color from_script<Color> (const ScriptValueP& value) { return (AColor)*value; }
|
||||
template <> inline AColor from_script<AColor> (const ScriptValueP& value) { return *value; }
|
||||
|
||||
void from_script(const ScriptValueP& value, wxRegEx& out);
|
||||
|
||||
// ----------------------------------------------------------------------------- : EOF
|
||||
#endif
|
||||
|
||||
@@ -9,9 +9,12 @@
|
||||
#include <util/prec.hpp>
|
||||
#include <script/value.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
#include <script/context.hpp>
|
||||
#include <util/error.hpp>
|
||||
#include <boost/pool/singleton_pool.hpp>
|
||||
|
||||
DECLARE_TYPEOF_COLLECTION(pair<Variable COMMA ScriptValueP>);
|
||||
|
||||
// ----------------------------------------------------------------------------- : ScriptValue
|
||||
// Base cases
|
||||
|
||||
@@ -29,6 +32,8 @@ CompareWhat ScriptValue::compareAs(String& compare_str, void const*& compare_pt
|
||||
return COMPARE_AS_STRING;
|
||||
}
|
||||
|
||||
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; }
|
||||
|
||||
@@ -357,3 +362,50 @@ ScriptValueP ScriptConcatCollection::getMember(const String& name) const {
|
||||
ScriptValueP ScriptConcatCollection::makeIterator(const ScriptValueP& thisP) const {
|
||||
return new_intrusive2<ScriptConcatCollectionIterator>(a->makeIterator(a), b->makeIterator(b));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : 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_EACH_CONST(b, bindings) {
|
||||
if (b.first == v) return b.second;
|
||||
}
|
||||
return ScriptValueP();
|
||||
}
|
||||
|
||||
ScriptValueP ScriptClosure::simplify() {
|
||||
return fun->simplifyClosure(*this);
|
||||
}
|
||||
|
||||
ScriptValueP ScriptClosure::eval(Context& ctx) const {
|
||||
applyBindings(ctx);
|
||||
return fun->eval(ctx);
|
||||
}
|
||||
ScriptValueP ScriptClosure::dependencies(Context& ctx, const Dependency& dep) const {
|
||||
applyBindings(ctx);
|
||||
return fun->dependencies(ctx, dep);
|
||||
}
|
||||
void ScriptClosure::applyBindings(Context& ctx) const {
|
||||
FOR_EACH_CONST(b, bindings) {
|
||||
if (ctx.getVariableScope(b.first) != 0) {
|
||||
ctx.setVariable(b.first, b.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Destructing
|
||||
|
||||
void from_script(const ScriptValueP& value, wxRegEx& regex) {
|
||||
if (!regex.Compile(*value, wxRE_ADVANCED)) {
|
||||
throw ScriptError(_ERROR_2_("can't convert", value->typeName(), _TYPE_("regex")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <gfx/color.hpp>
|
||||
class Context;
|
||||
class Dependency;
|
||||
class ScriptClosure;
|
||||
|
||||
// ----------------------------------------------------------------------------- : ScriptValue
|
||||
|
||||
@@ -80,6 +81,11 @@ class ScriptValue : public IntrusivePtrBaseWithDelete {
|
||||
/// Mark the scripts that this function depends on
|
||||
/** Return value is an abstract version of the return value of eval */
|
||||
virtual ScriptValueP dependencies(Context&, const Dependency&) const;
|
||||
/// Simplify/optimize a default argument closure of this function.
|
||||
/** Should return a simplification of the closure or null to keep the closure.
|
||||
* Alternatively, the closure may be modified in place.
|
||||
*/
|
||||
virtual ScriptValueP simplifyClosure(ScriptClosure&) const;
|
||||
|
||||
/// Return an iterator for the current collection, an iterator is a value that has next()
|
||||
/** thisP can be used to prevent destruction of the collection */
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <wx/wx.h>
|
||||
#include <wx/image.h>
|
||||
#include <wx/datetime.h>
|
||||
#include <wx/regex.h>
|
||||
|
||||
// Std headers
|
||||
#include <vector>
|
||||
|
||||
Reference in New Issue
Block a user