ERMInterpreter.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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); //get v-variable
  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. private:
  256. union
  257. {
  258. int val;
  259. int * integervar;
  260. double * flvar;
  261. std::string * stringvar;
  262. } val;
  263. public:
  264. enum {WRONGVAL, INT, INTVAR, FLOATVAR, STRINGVAR} type;
  265. void setTo(const IexpValStr & second);
  266. void setTo(int val);
  267. void setTo(float val);
  268. void setTo(const std::string & val);
  269. int getInt() const;
  270. float getFloat() const;
  271. std::string getString() const;
  272. IexpValStr() : type(WRONGVAL)
  273. {}
  274. IexpValStr(int _val) : type(INT)
  275. {
  276. val.val = _val;
  277. }
  278. IexpValStr(int* _val) : type(INTVAR)
  279. {
  280. val.integervar = _val;
  281. }
  282. IexpValStr(double * _val) : type(FLOATVAR)
  283. {
  284. val.flvar = _val;
  285. }
  286. IexpValStr(std::string * _val) : type(STRINGVAR)
  287. {
  288. val.stringvar = _val;
  289. }
  290. #define OPERATOR_DEFINITION_FULL(OPSIGN) \
  291. template<typename T> \
  292. IexpValStr operator OPSIGN(const T & sec) const \
  293. { \
  294. IexpValStr ret = *this; \
  295. switch (type) \
  296. { \
  297. case INT: \
  298. case INTVAR: \
  299. ret.setTo(ret.getInt() OPSIGN sec); \
  300. break; \
  301. case FLOATVAR: \
  302. ret.setTo(ret.getFloat() OPSIGN sec); \
  303. break; \
  304. case STRINGVAR: \
  305. ret.setTo(ret.getString() OPSIGN sec); \
  306. break; \
  307. } \
  308. return ret; \
  309. } \
  310. IexpValStr operator OPSIGN(const IexpValStr & sec) const \
  311. { \
  312. IexpValStr ret = *this; \
  313. switch (type) \
  314. { \
  315. case INT: \
  316. case INTVAR: \
  317. ret.setTo(ret.getInt() OPSIGN sec.getInt()); \
  318. break; \
  319. case FLOATVAR: \
  320. ret.setTo(ret.getFloat() OPSIGN sec.getFloat()); \
  321. break; \
  322. case STRINGVAR: \
  323. ret.setTo(ret.getString() OPSIGN sec.getString()); \
  324. break; \
  325. } \
  326. return ret; \
  327. } \
  328. template<typename T> \
  329. IexpValStr & operator OPSIGN ## = (const T & sec) \
  330. { \
  331. *this = *this OPSIGN sec; \
  332. return *this; \
  333. }
  334. #define OPERATOR_DEFINITION(OPSIGN) \
  335. template<typename T> \
  336. IexpValStr operator OPSIGN(const T & sec) const \
  337. { \
  338. IexpValStr ret = *this; \
  339. switch (type) \
  340. { \
  341. case INT: \
  342. case INTVAR: \
  343. ret.setTo(ret.getInt() OPSIGN sec); \
  344. break; \
  345. case FLOATVAR: \
  346. ret.setTo(ret.getFloat() OPSIGN sec); \
  347. break; \
  348. } \
  349. return ret; \
  350. } \
  351. IexpValStr operator OPSIGN(const IexpValStr & sec) const \
  352. { \
  353. IexpValStr ret = *this; \
  354. switch (type) \
  355. { \
  356. case INT: \
  357. case INTVAR: \
  358. ret.setTo(ret.getInt() OPSIGN sec.getInt()); \
  359. break; \
  360. case FLOATVAR: \
  361. ret.setTo(ret.getFloat() OPSIGN sec.getFloat()); \
  362. break; \
  363. } \
  364. return ret; \
  365. } \
  366. template<typename T> \
  367. IexpValStr & operator OPSIGN ## = (const T & sec) \
  368. { \
  369. *this = *this OPSIGN sec; \
  370. return *this; \
  371. }
  372. #define OPERATOR_DEFINITION_INTEGER(OPSIGN) \
  373. template<typename T> \
  374. IexpValStr operator OPSIGN(const T & sec) const \
  375. { \
  376. IexpValStr ret = *this; \
  377. switch (type) \
  378. { \
  379. case INT: \
  380. case INTVAR: \
  381. ret.setTo(ret.getInt() OPSIGN sec); \
  382. break; \
  383. } \
  384. return ret; \
  385. } \
  386. IexpValStr operator OPSIGN(const IexpValStr & sec) const \
  387. { \
  388. IexpValStr ret = *this; \
  389. switch (type) \
  390. { \
  391. case INT: \
  392. case INTVAR: \
  393. ret.setTo(ret.getInt() OPSIGN sec.getInt()); \
  394. break; \
  395. } \
  396. return ret; \
  397. } \
  398. template<typename T> \
  399. IexpValStr & operator OPSIGN ## = (const T & sec) \
  400. { \
  401. *this = *this OPSIGN sec; \
  402. return *this; \
  403. }
  404. OPERATOR_DEFINITION_FULL(+)
  405. OPERATOR_DEFINITION(-)
  406. OPERATOR_DEFINITION(*)
  407. OPERATOR_DEFINITION(/)
  408. OPERATOR_DEFINITION_INTEGER(%)
  409. };
  410. class ERMInterpreter
  411. {
  412. friend class ScriptScanner;
  413. friend class TriggerIdMatchHelper;
  414. friend class TriggerIdentifierMatch;
  415. friend class ConditionDisemboweler;
  416. friend struct LVL2IexpDisemboweler;
  417. friend struct VR_SPerformer;
  418. friend struct ERMExpDispatch;
  419. friend struct VRPerformer;
  420. std::vector<VERMInterpreter::FileInfo*> files;
  421. std::vector< VERMInterpreter::FileInfo* > fileInfos;
  422. std::map<VERMInterpreter::LinePointer, ERM::TLine> scripts;
  423. std::map<VERMInterpreter::LexicalPtr, VERMInterpreter::Environment> lexicalEnvs;
  424. ERM::TLine retrieveLine(VERMInterpreter::LinePointer linePtr) const;
  425. static ERM::Ttrigger retrieveTrigger(ERM::TLine line);
  426. VERMInterpreter::Environment * globalEnv;
  427. VERMInterpreter::ERMEnvironment * ermGlobalEnv;
  428. typedef std::map<VERMInterpreter::TriggerType, std::vector<VERMInterpreter::Trigger> > TtriggerListType;
  429. TtriggerListType triggers, postTriggers;
  430. VERMInterpreter::Trigger * curTrigger;
  431. VERMInterpreter::FunctionLocalVars * curFunc;
  432. static const int TRIG_FUNC_NUM = 30000;
  433. VERMInterpreter::FunctionLocalVars funcVars[TRIG_FUNC_NUM];
  434. VERMInterpreter::FunctionLocalVars * getFuncVars(int funNum);
  435. IexpValStr getIexp(const ERM::TIexp & iexp) const;
  436. IexpValStr getIexp(const ERM::TMacroUsage & macro) const;
  437. IexpValStr getIexp(const ERM::TIdentifierInternal & tid) const;
  438. static const std::string triggerSymbol, postTriggerSymbol, defunSymbol;
  439. void executeLine(const VERMInterpreter::LinePointer & lp);
  440. void executeTrigger(VERMInterpreter::Trigger & trig);
  441. static bool isCMDATrigger(const ERM::Tcommand & cmd);
  442. static bool isATrigger(const ERM::TLine & line);
  443. static ERM::EVOtions getExpType(const ERM::TVOption & opt);
  444. IexpValStr getVar(std::string toFollow, boost::optional<int> initVal) const;
  445. public:
  446. typedef std::map< int, std::vector<int> > TIDPattern;
  447. void executeTriggerType(VERMInterpreter::TriggerType tt, bool pre, const TIDPattern & identifier); //use this to run triggers
  448. void init(); //sets up environment etc.
  449. void scanForScripts();
  450. enum EPrintMode{ALL, ERM_ONLY, VERM_ONLY};
  451. void printScripts(EPrintMode mode = ALL);
  452. void scanScripts(); //scans for functions, triggers etc.
  453. ERMInterpreter();
  454. bool checkCondition( ERM::Tcondition cond );
  455. };