ERMInterpreter.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #pragma once
  2. #include "../global.h"
  3. #include "ERMParser.h"
  4. /*
  5. * ERMInterpreter.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. namespace VERMInterpreter
  14. {
  15. using namespace ERM;
  16. //different exceptions that can be thrown during interpreting
  17. class EInterpreterProblem : public std::exception
  18. {
  19. std::string problem;
  20. public:
  21. const char * what() const throw() OVERRIDE
  22. {
  23. return problem.c_str();
  24. }
  25. ~EInterpreterProblem() throw()
  26. {}
  27. EInterpreterProblem(const std::string & problemDesc) : problem(problemDesc)
  28. {}
  29. };
  30. struct ESymbolNotFound : public EInterpreterProblem
  31. {
  32. ESymbolNotFound(const std::string & sym) :
  33. EInterpreterProblem(std::string("Symbol \"") + sym + std::string("\" not found!"))
  34. {}
  35. };
  36. struct EInvalidTrigger : public EInterpreterProblem
  37. {
  38. EInvalidTrigger(const std::string & sym) :
  39. EInterpreterProblem(std::string("Trigger \"") + sym + std::string("\" is invalid!"))
  40. {}
  41. };
  42. struct EUsageOfUndefinedMacro : public EInterpreterProblem
  43. {
  44. EUsageOfUndefinedMacro(const std::string & macro) :
  45. EInterpreterProblem(std::string("Macro ") + macro + " is undefined")
  46. {}
  47. };
  48. struct EIexpProblem : public EInterpreterProblem
  49. {
  50. EIexpProblem(const std::string & desc) :
  51. EInterpreterProblem(desc)
  52. {}
  53. };
  54. struct ELineProblem : public EInterpreterProblem
  55. {
  56. ELineProblem(const std::string & desc) :
  57. EInterpreterProblem(desc)
  58. {}
  59. };
  60. struct EExecutionError : public EInterpreterProblem
  61. {
  62. EExecutionError(const std::string & desc) :
  63. EInterpreterProblem(desc)
  64. {}
  65. };
  66. //internal interpreter error related to execution
  67. struct EInterpreterError : public EExecutionError
  68. {
  69. EInterpreterError(const std::string & desc) :
  70. EExecutionError(desc)
  71. {}
  72. };
  73. //wrong script
  74. struct EScriptExecError : public EExecutionError
  75. {
  76. EScriptExecError(const std::string & desc) :
  77. EExecutionError(desc)
  78. {}
  79. };
  80. ///main environment class, manages symbols
  81. class Environment
  82. {
  83. private:
  84. std::map<std::string, TVOption> symbols;
  85. Environment * parent;
  86. public:
  87. bool isBound(const std::string & name, bool globalOnly) const;
  88. TVOption retrieveValue(const std::string & name) const;
  89. enum EUnbindMode{LOCAL, RECURSIVE_UNTIL_HIT, FULLY_RECURSIVE};
  90. ///returns true if symbol was really unbound
  91. bool unbind(const std::string & name, EUnbindMode mode);
  92. };
  93. // All numeric variables are integer variables and have a range of -2147483647...+2147483647
  94. // c stores game active day number //indirect variable
  95. // d current value //not an actual variable but a modifier
  96. // e1..e100 Function floating point variables //local
  97. // e-1..e-100 Trigger local floating point variables //local
  98. // 'f'..'t' Standard variables ('quick variables') //global
  99. // v1..v1000 Standard variables //global
  100. // w1..w100 Hero variables
  101. // w101..w200 Hero variables
  102. // x1..x16 Function parameters //local
  103. // y1..y100 Function local variables //local
  104. // y-1..y-100 Trigger-based local integer variables //local
  105. // z1..z1000 String variables //global
  106. // z-1..z-10 Function local string variables //local
  107. struct TriggerLocalVars
  108. {
  109. static const int EVAR_NUM = 100; //number of evar locals
  110. static const int YVAR_NUM = 100; //number of yvar locals
  111. TriggerLocalVars();
  112. double & getEvar(int num);
  113. int & getYvar(int num);
  114. private:
  115. double evar[EVAR_NUM]; //negative indices
  116. int yvar[YVAR_NUM];
  117. };
  118. struct FunctionLocalVars
  119. {
  120. static const int NUM_PARAMETERS = 16; //number of function parameters
  121. static const int NUM_LOCALS = 100;
  122. static const int NUM_STRINGS = 10;
  123. static const int NUM_FLOATINGS = 100;
  124. int & getParam(int num);
  125. int & getLocal(int num);
  126. std::string & getString(int num);
  127. double & getFloat(int num);
  128. private:
  129. int params[NUM_PARAMETERS]; //x-vars
  130. int locals[NUM_LOCALS]; //y-vars
  131. std::string strings[NUM_STRINGS]; //z-vars (negative indices)
  132. double floats[NUM_FLOATINGS]; //e-vars (positive indices)
  133. };
  134. struct ERMEnvironment
  135. {
  136. ERMEnvironment();
  137. static const int NUM_QUICKS = 't' - 'f' + 1; //it should be 15
  138. int & getQuickVar(const char letter);
  139. int & getStandardVar(int num);
  140. std::string & getZVar(int num);
  141. bool & getFlag(int num);
  142. static const int NUM_STANDARDS = 1000;
  143. static const int NUM_STRINGS = 1000;
  144. std::map<std::string, ERM::TVarExpNotMacro> macroBindings;
  145. static const int NUM_FLAGS = 1000;
  146. private:
  147. int quickVars[NUM_QUICKS]; //referenced by letter ('f' to 't' inclusive)
  148. int standardVars[NUM_STANDARDS]; //v-vars
  149. std::string strings[NUM_STRINGS]; //z-vars (positive indices)
  150. bool flags[NUM_FLAGS];
  151. };
  152. struct TriggerType
  153. {
  154. //the same order of trigger types in this enum and in validTriggers array is obligatory!
  155. enum ETrigType{AE, BA, BF, BG, BR, CM, CO, FU, GE, GM, HE, HL, HM, IP, LE, MF, MG, MM, MR,
  156. MW, OB, PI, SN, TH, TM} type;
  157. static ETrigType convertTrigger(const std::string & trig)
  158. {
  159. static const std::string validTriggers[] = {"AE", "BA", "BF", "BG", "BR", "CM", "CO", "FU",
  160. "GE", "GM", "HE", "HL", "HM", "IP", "LE", "MF", "MG", "MM", "MR", "MW", "OB", "PI", "SN",
  161. "TH", "TM"};
  162. for(int i=0; i<ARRAY_COUNT(validTriggers); ++i)
  163. {
  164. if(validTriggers[i] == trig)
  165. return static_cast<ETrigType>(i);
  166. }
  167. throw EInvalidTrigger(trig);
  168. }
  169. bool operator<(const TriggerType & t2) const
  170. {
  171. return type < t2.type;
  172. }
  173. TriggerType(const std::string & sym)
  174. {
  175. type = convertTrigger(sym);
  176. }
  177. };
  178. struct FileInfo
  179. {
  180. std::string filename;
  181. int length;
  182. };
  183. struct LinePointer
  184. {
  185. const FileInfo * file; //non-owning
  186. int lineNum;
  187. LinePointer() : file(NULL)
  188. {}
  189. LinePointer(const FileInfo * finfo, int line) : file(finfo), lineNum(line)
  190. {}
  191. //lexicographical order
  192. bool operator<(const LinePointer & rhs) const
  193. {
  194. if(file->filename != rhs.file->filename)
  195. return file->filename < rhs.file->filename;
  196. return lineNum < rhs.lineNum;
  197. }
  198. bool operator!=(const LinePointer & rhs) const
  199. {
  200. return file->filename != rhs.file->filename || lineNum != rhs.lineNum;
  201. }
  202. LinePointer & operator++()
  203. {
  204. ++lineNum;
  205. return *this;
  206. }
  207. bool isValid() const
  208. {
  209. return file && lineNum < file->length;
  210. }
  211. };
  212. struct LexicalPtr
  213. {
  214. LinePointer line; //where to start
  215. std::vector<int> entryPoints; //defines how to pass to current location
  216. bool operator<(const LexicalPtr & sec) const
  217. {
  218. if(line != sec.line)
  219. return line < sec.line;
  220. if(entryPoints.size() != sec.entryPoints.size())
  221. return entryPoints.size() < sec.entryPoints.size();
  222. for(int g=0; g<entryPoints.size(); ++g)
  223. {
  224. if(entryPoints[g] < sec.entryPoints[g])
  225. return true;
  226. }
  227. return false;
  228. }
  229. };
  230. //call stack, represents dynamic range
  231. struct Stack
  232. {
  233. std::vector<LexicalPtr> stack;
  234. };
  235. struct Trigger
  236. {
  237. LinePointer line;
  238. TriggerLocalVars ermLocalVars;
  239. Stack * stack; //where we are stuck at execution
  240. Trigger() : stack(NULL)
  241. {}
  242. };
  243. }
  244. class ERMInterpreter;
  245. struct TriggerIdentifierMatch
  246. {
  247. bool allowNoIdetifier;
  248. std::map< int, std::vector<int> > matchToIt; //match subidentifiers to these numbers
  249. static const int MAX_SUBIDENTIFIERS = 16;
  250. ERMInterpreter * ermEnv;
  251. bool tryMatch(VERMInterpreter::Trigger * interptrig) const;
  252. };
  253. struct IexpValStr
  254. {
  255. union
  256. {
  257. int val;
  258. int * integervar;
  259. double * flvar;
  260. std::string * stringvar;
  261. } val;
  262. enum {WRONGVAL, INT, INTVAR, FLOATVAR, STRINGVAR} type;
  263. };
  264. class ERMInterpreter
  265. {
  266. friend class ScriptScanner;
  267. friend class TriggerIdMatchHelper;
  268. friend class TriggerIdentifierMatch;
  269. friend class ConditionDisemboweler;
  270. friend struct LVL2IexpDisemboweler;
  271. friend struct VR_SPerformer;
  272. friend struct ERMExpDispatch;
  273. std::vector<VERMInterpreter::FileInfo*> files;
  274. std::vector< VERMInterpreter::FileInfo* > fileInfos;
  275. std::map<VERMInterpreter::LinePointer, ERM::TLine> scripts;
  276. std::map<VERMInterpreter::LexicalPtr, VERMInterpreter::Environment> lexicalEnvs;
  277. ERM::TLine retrieveLine(VERMInterpreter::LinePointer linePtr) const;
  278. static ERM::Ttrigger retrieveTrigger(ERM::TLine line);
  279. VERMInterpreter::Environment * globalEnv;
  280. VERMInterpreter::ERMEnvironment * ermGlobalEnv;
  281. typedef std::map<VERMInterpreter::TriggerType, std::vector<VERMInterpreter::Trigger> > TtriggerListType;
  282. TtriggerListType triggers, postTriggers;
  283. VERMInterpreter::Trigger * curTrigger;
  284. VERMInterpreter::FunctionLocalVars * curFunc;
  285. template<typename T> void setIexp(const ERM::TIexp & iexp, const T & val, VERMInterpreter::Trigger * trig = NULL);
  286. IexpValStr getIexp(const ERM::TIexp & iexp) const;
  287. static const std::string triggerSymbol, postTriggerSymbol, defunSymbol;
  288. void executeLine(const VERMInterpreter::LinePointer & lp);
  289. void executeTrigger(VERMInterpreter::Trigger & trig);
  290. static bool isCMDATrigger(const ERM::Tcommand & cmd);
  291. static bool isATrigger(const ERM::TLine & line);
  292. static ERM::EVOtions getExpType(const ERM::TVOption & opt);
  293. IexpValStr getVar(std::string toFollow, boost::optional<int> initVal);
  294. public:
  295. void executeTriggerType(VERMInterpreter::TriggerType tt, bool pre, const std::map< int, std::vector<int> > & identifier); //use this to run triggers
  296. void init(); //sets up environment etc.
  297. void scanForScripts();
  298. enum EPrintMode{ALL, ERM_ONLY, VERM_ONLY};
  299. void printScripts(EPrintMode mode = ALL);
  300. void scanScripts(); //scans for functions, triggers etc.
  301. ERMInterpreter();
  302. bool checkCondition( ERM::Tcondition cond );
  303. };