1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00

* some work towards running triggers

This commit is contained in:
mateuszb 2011-04-21 20:06:42 +00:00
parent 66b708c168
commit 66837f1ee0
2 changed files with 56 additions and 4 deletions

View File

@ -885,6 +885,41 @@ T ERMInterpreter::getIexp( const ERM::TIexp & iexp, /*const*/ Trigger * trig /*=
return ret;
}
void ERMInterpreter::executeTriggerType( VERMInterpreter::TriggerType tt, bool pre, const std::map< int, std::vector<int> > & identifier )
{
TtriggerListType & triggerList = pre ? triggers : postTriggers;
TriggerIdentifierMatch tim;
tim.allowNoIdetifier = false;
tim.ermEnv = this;
tim.matchToIt = identifier;
std::vector<Trigger> triggersToTry = triggerList[tt];
for(int g=0; g<triggersToTry.size(); ++g)
{
if(tim.tryMatch(&triggersToTry[g]))
{
executeTrigger(triggersToTry[g]);
}
}
}
ERM::Ttrigger ERMInterpreter::retrieveTrigger( ERM::TLine line )
{
if(line.which() == 1)
{
ERM::TERMline tl = boost::get<ERM::TERMline>(line);
if(tl.which() == 0)
{
ERM::Tcommand tcm = boost::get<ERM::Tcommand>(line);
if(tcm.cmd.which() == 0)
{
return boost::get<ERM::Ttrigger>(tcm.cmd);
}
}
}
throw ELineProblem("Given line is not an ERM trigger!");
}
const std::string ERMInterpreter::triggerSymbol = "trigger";
const std::string ERMInterpreter::postTriggerSymbol = "postTrigger";
const std::string ERMInterpreter::defunSymbol = "defun";
@ -910,10 +945,12 @@ struct TriggerIdMatchHelper : boost::static_visitor<>
}
};
bool TriggerIdentifierMatch::tryMatch( Trigger * interptrig, const ERM::Ttrigger & trig ) const
bool TriggerIdentifierMatch::tryMatch( Trigger * interptrig ) const
{
const ERM::Ttrigger & trig = ERMInterpreter::retrieveTrigger(ermEnv->retrieveLine(interptrig->line));
if(trig.identifier.is_initialized())
{
ERM::Tidentifier tid = trig.identifier.get();
std::map< int, std::vector<int> >::const_iterator it = matchToIt.find(tid.size());
if(it == matchToIt.end())
@ -925,8 +962,12 @@ bool TriggerIdentifierMatch::tryMatch( Trigger * interptrig, const ERM::Ttrigger
{
int val = -1;
boost::apply_visitor(TriggerIdMatchHelper(val, ermEnv, interptrig), tid[g]);
return pattern[g] == val;
if(pattern[g] != val)
{
return false;
}
}
return true;
}
}
else

View File

@ -59,6 +59,13 @@ namespace VERMInterpreter
{}
};
struct ELineProblem : public EInterpreterProblem
{
ELineProblem(const std::string & desc) :
EInterpreterProblem(desc)
{}
};
///main environment class, manages symbols
class Environment
@ -310,23 +317,26 @@ struct TriggerIdentifierMatch
static const int MAX_SUBIDENTIFIERS = 16;
ERMInterpreter * ermEnv;
bool tryMatch(VERMInterpreter::Trigger * interptrig, const ERM::Ttrigger & trig) const;
bool tryMatch(VERMInterpreter::Trigger * interptrig) const;
};
class ERMInterpreter
{
friend class ScriptScanner;
friend class TriggerIdMatchHelper;
friend class TriggerIdentifierMatch;
std::vector<VERMInterpreter::FileInfo*> files;
std::vector< VERMInterpreter::FileInfo* > fileInfos;
std::map<VERMInterpreter::LinePointer, ERM::TLine> scripts;
std::map<VERMInterpreter::LexicalPtr, VERMInterpreter::Environment> lexicalEnvs;
ERM::TLine retrieveLine(VERMInterpreter::LinePointer linePtr) const;
static ERM::Ttrigger retrieveTrigger(ERM::TLine line);
VERMInterpreter::Environment * globalEnv;
VERMInterpreter::ERMEnvironment * ermGlobalEnv;
std::map<VERMInterpreter::TriggerType, std::vector<VERMInterpreter::Trigger> > triggers, postTriggers;
typedef std::map<VERMInterpreter::TriggerType, std::vector<VERMInterpreter::Trigger> > TtriggerListType;
TtriggerListType triggers, postTriggers;
template<typename T> void setIexp(const ERM::TIexp & iexp, const T & val, VERMInterpreter::Trigger * trig = NULL);
@ -340,6 +350,7 @@ class ERMInterpreter
static bool isATrigger(const ERM::TLine & line);
static ERM::EVOtions getExpType(const ERM::TVOption & opt);
public:
void executeTriggerType(VERMInterpreter::TriggerType tt, bool pre, const std::map< int, std::vector<int> > & identifier); //use this to run triggers
void init(); //sets up environment etc.
void scanForScripts();