Pārlūkot izejas kodu

* some work towards running triggers

mateuszb 14 gadi atpakaļ
vecāks
revīzija
66837f1ee0
2 mainītis faili ar 56 papildinājumiem un 4 dzēšanām
  1. 43 2
      lib/ERMInterpreter.cpp
  2. 13 2
      lib/ERMInterpreter.h

+ 43 - 2
lib/ERMInterpreter.cpp

@@ -885,6 +885,41 @@ T ERMInterpreter::getIexp( const ERM::TIexp & iexp, /*const*/ Trigger * trig /*=
 	return ret;
 	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::triggerSymbol = "trigger";
 const std::string ERMInterpreter::postTriggerSymbol = "postTrigger";
 const std::string ERMInterpreter::postTriggerSymbol = "postTrigger";
 const std::string ERMInterpreter::defunSymbol = "defun";
 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())
 	if(trig.identifier.is_initialized())
 	{
 	{
+
 		ERM::Tidentifier tid = trig.identifier.get();
 		ERM::Tidentifier tid = trig.identifier.get();
 		std::map< int, std::vector<int> >::const_iterator it = matchToIt.find(tid.size());
 		std::map< int, std::vector<int> >::const_iterator it = matchToIt.find(tid.size());
 		if(it == matchToIt.end())
 		if(it == matchToIt.end())
@@ -925,8 +962,12 @@ bool TriggerIdentifierMatch::tryMatch( Trigger * interptrig, const ERM::Ttrigger
 			{
 			{
 				int val = -1;
 				int val = -1;
 				boost::apply_visitor(TriggerIdMatchHelper(val, ermEnv, interptrig), tid[g]);
 				boost::apply_visitor(TriggerIdMatchHelper(val, ermEnv, interptrig), tid[g]);
-				return pattern[g] == val;
+				if(pattern[g] != val)
+				{
+					return false;
+				}
 			}
 			}
+			return true;
 		}
 		}
 	}
 	}
 	else
 	else

+ 13 - 2
lib/ERMInterpreter.h

@@ -59,6 +59,13 @@ namespace VERMInterpreter
 		{}
 		{}
 	};
 	};
 
 
+	struct ELineProblem : public EInterpreterProblem
+	{
+		ELineProblem(const std::string & desc) :
+			EInterpreterProblem(desc)
+		{}
+	};
+
 
 
 	///main environment class, manages symbols
 	///main environment class, manages symbols
 	class Environment
 	class Environment
@@ -310,23 +317,26 @@ struct TriggerIdentifierMatch
 
 
 	static const int MAX_SUBIDENTIFIERS = 16;
 	static const int MAX_SUBIDENTIFIERS = 16;
 	ERMInterpreter * ermEnv;
 	ERMInterpreter * ermEnv;
-	bool tryMatch(VERMInterpreter::Trigger * interptrig, const ERM::Ttrigger & trig) const;
+	bool tryMatch(VERMInterpreter::Trigger * interptrig) const;
 };
 };
 
 
 class ERMInterpreter
 class ERMInterpreter
 {
 {
 	friend class ScriptScanner;
 	friend class ScriptScanner;
 	friend class TriggerIdMatchHelper;
 	friend class TriggerIdMatchHelper;
+	friend class TriggerIdentifierMatch;
 
 
 	std::vector<VERMInterpreter::FileInfo*> files;
 	std::vector<VERMInterpreter::FileInfo*> files;
 	std::vector< VERMInterpreter::FileInfo* > fileInfos;
 	std::vector< VERMInterpreter::FileInfo* > fileInfos;
 	std::map<VERMInterpreter::LinePointer, ERM::TLine> scripts;
 	std::map<VERMInterpreter::LinePointer, ERM::TLine> scripts;
 	std::map<VERMInterpreter::LexicalPtr, VERMInterpreter::Environment> lexicalEnvs;
 	std::map<VERMInterpreter::LexicalPtr, VERMInterpreter::Environment> lexicalEnvs;
 	ERM::TLine retrieveLine(VERMInterpreter::LinePointer linePtr) const;
 	ERM::TLine retrieveLine(VERMInterpreter::LinePointer linePtr) const;
+	static ERM::Ttrigger retrieveTrigger(ERM::TLine line);
 
 
 	VERMInterpreter::Environment * globalEnv;
 	VERMInterpreter::Environment * globalEnv;
 	VERMInterpreter::ERMEnvironment * ermGlobalEnv;
 	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);
 	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 bool isATrigger(const ERM::TLine & line);
 	static ERM::EVOtions getExpType(const ERM::TVOption & opt);
 	static ERM::EVOtions getExpType(const ERM::TVOption & opt);
 public:
 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 init(); //sets up environment etc.
 	void scanForScripts();
 	void scanForScripts();