Przeglądaj źródła

* minor bugfix
* a bit more of function handling

mateuszb 14 lat temu
rodzic
commit
09a51acfc2
2 zmienionych plików z 40 dodań i 4 usunięć
  1. 38 3
      lib/ERMInterpreter.cpp
  2. 2 1
      lib/ERMInterpreter.h

+ 38 - 3
lib/ERMInterpreter.cpp

@@ -463,8 +463,18 @@ ERMInterpreter::ERMInterpreter()
 	globalEnv = new Environment();
 }
 
-void ERMInterpreter::executeTrigger( Trigger & trig )
+void ERMInterpreter::executeTrigger( VERMInterpreter::Trigger & trig, int funNum /*= -1*/, std::vector<int> funParams/*=std::vector<int>()*/ )
 {
+	//function-related logic
+	if(funNum != -1)
+	{
+		curFunc = getFuncVars(funNum);
+		for(int g=1; g<=FunctionLocalVars::NUM_PARAMETERS; ++g)
+		{
+			curFunc->getParam(g) = g < funParams.size() ? funParams[g] : 0;
+		}
+	}
+
 	//skip the first line
 	LinePointer lp = trig.line;
 	++lp;
@@ -476,6 +486,8 @@ void ERMInterpreter::executeTrigger( Trigger & trig )
 
 		executeLine(lp);
 	}
+
+	curFunc = NULL;
 }
 
 bool ERMInterpreter::isATrigger( const ERM::TLine & line )
@@ -827,6 +839,8 @@ void ERMInterpreter::init()
 	ermGlobalEnv = new ERMEnvironment();
 	globalEnv = new Environment();
 	//TODO: reset?
+	for(int g=0; g<TRIG_FUNC_NUM; ++g)
+		funcVars[g].reset();
 }
 
 IexpValStr ERMInterpreter::getVar(std::string toFollow, boost::optional<int> initVal) const
@@ -1102,6 +1116,16 @@ IexpValStr ERMInterpreter::getIexp( const ERM::TIdentifierInternal & tid ) const
 
 void ERMInterpreter::executeTriggerType( VERMInterpreter::TriggerType tt, bool pre, const TIDPattern & identifier )
 {
+	struct HLP
+	{
+		static int calcFunNum(VERMInterpreter::TriggerType tt, const TIDPattern & identifier)
+		{
+			if(tt.type != VERMInterpreter::TriggerType::FU)
+				return -1;
+
+			return identifier.begin()->second[0];
+		}
+	};
 	TtriggerListType & triggerList = pre ? triggers : postTriggers;
 
 	TriggerIdentifierMatch tim;
@@ -1114,7 +1138,7 @@ void ERMInterpreter::executeTriggerType( VERMInterpreter::TriggerType tt, bool p
 		if(tim.tryMatch(&triggersToTry[g]))
 		{
 			curTrigger = &triggersToTry[g];
-			executeTrigger(triggersToTry[g]);
+			executeTrigger(triggersToTry[g], HLP::calcFunNum(tt, identifier));
 		}
 	}
 }
@@ -1332,7 +1356,6 @@ bool TriggerIdentifierMatch::tryMatch( Trigger * interptrig ) const
 					ret = false;
 				}
 			}
-			ret = true;
 		}
 	}
 	else
@@ -1507,6 +1530,18 @@ double & VERMInterpreter::FunctionLocalVars::getFloat( int num )
 	return floats[num-1];
 }
 
+void VERMInterpreter::FunctionLocalVars::reset()
+{
+	for(int g=0; g<ARRAY_COUNT(params); ++g)
+		params[g] = 0;
+	for(int g=0; g<ARRAY_COUNT(locals); ++g)
+		locals[g] = 0;
+	for(int g=0; g<ARRAY_COUNT(strings); ++g)
+		strings[g] = "";
+	for(int g=0; g<ARRAY_COUNT(floats); ++g)
+		floats[g] = 0.0;
+}
+
 void IexpValStr::setTo( const IexpValStr & second )
 {
 	switch(type)

+ 2 - 1
lib/ERMInterpreter.h

@@ -148,6 +148,7 @@ namespace VERMInterpreter
 		int & getLocal(int num);
 		std::string & getString(int num);
 		double & getFloat(int num);
+		void reset();
 	private:
 		int params[NUM_PARAMETERS]; //x-vars
 		int locals[NUM_LOCALS]; //y-vars
@@ -503,7 +504,7 @@ class ERMInterpreter
 	static const std::string triggerSymbol, postTriggerSymbol, defunSymbol;
 
 	void executeLine(const VERMInterpreter::LinePointer & lp);
-	void executeTrigger(VERMInterpreter::Trigger & trig);
+	void executeTrigger(VERMInterpreter::Trigger & trig, int funNum = -1, std::vector<int> funParams=std::vector<int>());
 	static bool isCMDATrigger(const ERM::Tcommand & cmd);
 	static bool isATrigger(const ERM::TLine & line);
 	static ERM::EVOtions getExpType(const ERM::TVOption & opt);