ERMInterpreter.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. void reset();
  129. private:
  130. int params[NUM_PARAMETERS]; //x-vars
  131. int locals[NUM_LOCALS]; //y-vars
  132. std::string strings[NUM_STRINGS]; //z-vars (negative indices)
  133. double floats[NUM_FLOATINGS]; //e-vars (positive indices)
  134. };
  135. struct ERMEnvironment
  136. {
  137. ERMEnvironment();
  138. static const int NUM_QUICKS = 't' - 'f' + 1; //it should be 15
  139. int & getQuickVar(const char letter);
  140. int & getStandardVar(int num); //get v-variable
  141. std::string & getZVar(int num);
  142. bool & getFlag(int num);
  143. static const int NUM_STANDARDS = 1000;
  144. static const int NUM_STRINGS = 1000;
  145. std::map<std::string, ERM::TVarExpNotMacro> macroBindings;
  146. static const int NUM_FLAGS = 1000;
  147. private:
  148. int quickVars[NUM_QUICKS]; //referenced by letter ('f' to 't' inclusive)
  149. int standardVars[NUM_STANDARDS]; //v-vars
  150. std::string strings[NUM_STRINGS]; //z-vars (positive indices)
  151. bool flags[NUM_FLAGS];
  152. };
  153. struct TriggerType
  154. {
  155. //the same order of trigger types in this enum and in validTriggers array is obligatory!
  156. enum ETrigType{AE, BA, BF, BG, BR, CM, CO, FU, GE, GM, HE, HL, HM, IP, LE, MF, MG, MM, MR,
  157. MW, OB, PI, SN, TH, TM} type;
  158. static ETrigType convertTrigger(const std::string & trig)
  159. {
  160. static const std::string validTriggers[] = {"AE", "BA", "BF", "BG", "BR", "CM", "CO", "FU",
  161. "GE", "GM", "HE", "HL", "HM", "IP", "LE", "MF", "MG", "MM", "MR", "MW", "OB", "PI", "SN",
  162. "TH", "TM"};
  163. for(int i=0; i<ARRAY_COUNT(validTriggers); ++i)
  164. {
  165. if(validTriggers[i] == trig)
  166. return static_cast<ETrigType>(i);
  167. }
  168. throw EInvalidTrigger(trig);
  169. }
  170. bool operator<(const TriggerType & t2) const
  171. {
  172. return type < t2.type;
  173. }
  174. TriggerType(const std::string & sym)
  175. {
  176. type = convertTrigger(sym);
  177. }
  178. };
  179. struct FileInfo
  180. {
  181. std::string filename;
  182. int length;
  183. };
  184. struct LinePointer
  185. {
  186. const FileInfo * file; //non-owning
  187. int lineNum;
  188. int realLineNum;
  189. LinePointer() : file(NULL)
  190. {}
  191. LinePointer(const FileInfo * finfo, int line, int _realLineNum) : file(finfo), lineNum(line),
  192. realLineNum(_realLineNum)
  193. {}
  194. //lexicographical order
  195. bool operator<(const LinePointer & rhs) const
  196. {
  197. if(file->filename != rhs.file->filename)
  198. return file->filename < rhs.file->filename;
  199. return lineNum < rhs.lineNum;
  200. }
  201. bool operator!=(const LinePointer & rhs) const
  202. {
  203. return file->filename != rhs.file->filename || lineNum != rhs.lineNum;
  204. }
  205. LinePointer & operator++()
  206. {
  207. ++lineNum;
  208. return *this;
  209. }
  210. bool isValid() const
  211. {
  212. return file && lineNum < file->length;
  213. }
  214. };
  215. struct LexicalPtr
  216. {
  217. LinePointer line; //where to start
  218. std::vector<int> entryPoints; //defines how to pass to current location
  219. bool operator<(const LexicalPtr & sec) const
  220. {
  221. if(line != sec.line)
  222. return line < sec.line;
  223. if(entryPoints.size() != sec.entryPoints.size())
  224. return entryPoints.size() < sec.entryPoints.size();
  225. for(int g=0; g<entryPoints.size(); ++g)
  226. {
  227. if(entryPoints[g] < sec.entryPoints[g])
  228. return true;
  229. }
  230. return false;
  231. }
  232. };
  233. //call stack, represents dynamic range
  234. struct Stack
  235. {
  236. std::vector<LexicalPtr> stack;
  237. };
  238. struct Trigger
  239. {
  240. LinePointer line;
  241. TriggerLocalVars ermLocalVars;
  242. Stack * stack; //where we are stuck at execution
  243. Trigger() : stack(NULL)
  244. {}
  245. };
  246. }
  247. class ERMInterpreter;
  248. struct TriggerIdentifierMatch
  249. {
  250. bool allowNoIdetifier;
  251. std::map< int, std::vector<int> > matchToIt; //match subidentifiers to these numbers
  252. static const int MAX_SUBIDENTIFIERS = 16;
  253. ERMInterpreter * ermEnv;
  254. bool tryMatch(VERMInterpreter::Trigger * interptrig) const;
  255. };
  256. struct IexpValStr
  257. {
  258. private:
  259. union
  260. {
  261. int val;
  262. int * integervar;
  263. double * flvar;
  264. std::string * stringvar;
  265. } val;
  266. public:
  267. enum {WRONGVAL, INT, INTVAR, FLOATVAR, STRINGVAR} type;
  268. void setTo(const IexpValStr & second);
  269. void setTo(int val);
  270. void setTo(float val);
  271. void setTo(const std::string & val);
  272. int getInt() const;
  273. float getFloat() const;
  274. std::string getString() const;
  275. IexpValStr() : type(WRONGVAL)
  276. {}
  277. IexpValStr(int _val) : type(INT)
  278. {
  279. val.val = _val;
  280. }
  281. IexpValStr(int* _val) : type(INTVAR)
  282. {
  283. val.integervar = _val;
  284. }
  285. IexpValStr(double * _val) : type(FLOATVAR)
  286. {
  287. val.flvar = _val;
  288. }
  289. IexpValStr(std::string * _val) : type(STRINGVAR)
  290. {
  291. val.stringvar = _val;
  292. }
  293. #define OPERATOR_DEFINITION_FULL(OPSIGN) \
  294. template<typename T> \
  295. IexpValStr operator OPSIGN(const T & sec) const \
  296. { \
  297. IexpValStr ret = *this; \
  298. switch (type) \
  299. { \
  300. case INT: \
  301. case INTVAR: \
  302. ret.setTo(ret.getInt() OPSIGN sec); \
  303. break; \
  304. case FLOATVAR: \
  305. ret.setTo(ret.getFloat() OPSIGN sec); \
  306. break; \
  307. case STRINGVAR: \
  308. ret.setTo(ret.getString() OPSIGN sec); \
  309. break; \
  310. } \
  311. return ret; \
  312. } \
  313. IexpValStr operator OPSIGN(const IexpValStr & sec) const \
  314. { \
  315. IexpValStr ret = *this; \
  316. switch (type) \
  317. { \
  318. case INT: \
  319. case INTVAR: \
  320. ret.setTo(ret.getInt() OPSIGN sec.getInt()); \
  321. break; \
  322. case FLOATVAR: \
  323. ret.setTo(ret.getFloat() OPSIGN sec.getFloat()); \
  324. break; \
  325. case STRINGVAR: \
  326. ret.setTo(ret.getString() OPSIGN sec.getString()); \
  327. break; \
  328. } \
  329. return ret; \
  330. } \
  331. template<typename T> \
  332. IexpValStr & operator OPSIGN ## = (const T & sec) \
  333. { \
  334. *this = *this OPSIGN sec; \
  335. return *this; \
  336. }
  337. #define OPERATOR_DEFINITION(OPSIGN) \
  338. template<typename T> \
  339. IexpValStr operator OPSIGN(const T & sec) const \
  340. { \
  341. IexpValStr ret = *this; \
  342. switch (type) \
  343. { \
  344. case INT: \
  345. case INTVAR: \
  346. ret.setTo(ret.getInt() OPSIGN sec); \
  347. break; \
  348. case FLOATVAR: \
  349. ret.setTo(ret.getFloat() OPSIGN sec); \
  350. break; \
  351. } \
  352. return ret; \
  353. } \
  354. IexpValStr operator OPSIGN(const IexpValStr & sec) const \
  355. { \
  356. IexpValStr ret = *this; \
  357. switch (type) \
  358. { \
  359. case INT: \
  360. case INTVAR: \
  361. ret.setTo(ret.getInt() OPSIGN sec.getInt()); \
  362. break; \
  363. case FLOATVAR: \
  364. ret.setTo(ret.getFloat() OPSIGN sec.getFloat()); \
  365. break; \
  366. } \
  367. return ret; \
  368. } \
  369. template<typename T> \
  370. IexpValStr & operator OPSIGN ## = (const T & sec) \
  371. { \
  372. *this = *this OPSIGN sec; \
  373. return *this; \
  374. }
  375. #define OPERATOR_DEFINITION_INTEGER(OPSIGN) \
  376. template<typename T> \
  377. IexpValStr operator OPSIGN(const T & sec) const \
  378. { \
  379. IexpValStr ret = *this; \
  380. switch (type) \
  381. { \
  382. case INT: \
  383. case INTVAR: \
  384. ret.setTo(ret.getInt() OPSIGN sec); \
  385. break; \
  386. } \
  387. return ret; \
  388. } \
  389. IexpValStr operator OPSIGN(const IexpValStr & sec) const \
  390. { \
  391. IexpValStr ret = *this; \
  392. switch (type) \
  393. { \
  394. case INT: \
  395. case INTVAR: \
  396. ret.setTo(ret.getInt() OPSIGN sec.getInt()); \
  397. break; \
  398. } \
  399. return ret; \
  400. } \
  401. template<typename T> \
  402. IexpValStr & operator OPSIGN ## = (const T & sec) \
  403. { \
  404. *this = *this OPSIGN sec; \
  405. return *this; \
  406. }
  407. OPERATOR_DEFINITION_FULL(+)
  408. OPERATOR_DEFINITION(-)
  409. OPERATOR_DEFINITION(*)
  410. OPERATOR_DEFINITION(/)
  411. OPERATOR_DEFINITION_INTEGER(%)
  412. };
  413. class ERMInterpreter
  414. {
  415. friend class ScriptScanner;
  416. friend class TriggerIdMatchHelper;
  417. friend class TriggerIdentifierMatch;
  418. friend class ConditionDisemboweler;
  419. friend struct LVL2IexpDisemboweler;
  420. friend struct VR_SPerformer;
  421. friend struct ERMExpDispatch;
  422. friend struct VRPerformer;
  423. std::vector<VERMInterpreter::FileInfo*> files;
  424. std::vector< VERMInterpreter::FileInfo* > fileInfos;
  425. std::map<VERMInterpreter::LinePointer, ERM::TLine> scripts;
  426. std::map<VERMInterpreter::LexicalPtr, VERMInterpreter::Environment> lexicalEnvs;
  427. ERM::TLine retrieveLine(VERMInterpreter::LinePointer linePtr) const;
  428. static ERM::Ttrigger retrieveTrigger(ERM::TLine line);
  429. VERMInterpreter::Environment * globalEnv;
  430. VERMInterpreter::ERMEnvironment * ermGlobalEnv;
  431. typedef std::map<VERMInterpreter::TriggerType, std::vector<VERMInterpreter::Trigger> > TtriggerListType;
  432. TtriggerListType triggers, postTriggers;
  433. VERMInterpreter::Trigger * curTrigger;
  434. VERMInterpreter::FunctionLocalVars * curFunc;
  435. static const int TRIG_FUNC_NUM = 30000;
  436. VERMInterpreter::FunctionLocalVars funcVars[TRIG_FUNC_NUM];
  437. VERMInterpreter::FunctionLocalVars * getFuncVars(int funNum);
  438. IexpValStr getIexp(const ERM::TIexp & iexp) const;
  439. IexpValStr getIexp(const ERM::TMacroUsage & macro) const;
  440. IexpValStr getIexp(const ERM::TIdentifierInternal & tid) const;
  441. static const std::string triggerSymbol, postTriggerSymbol, defunSymbol;
  442. void executeLine(const VERMInterpreter::LinePointer & lp);
  443. void executeTrigger(VERMInterpreter::Trigger & trig, int funNum = -1, std::vector<int> funParams=std::vector<int>());
  444. static bool isCMDATrigger(const ERM::Tcommand & cmd);
  445. static bool isATrigger(const ERM::TLine & line);
  446. static ERM::EVOtions getExpType(const ERM::TVOption & opt);
  447. IexpValStr getVar(std::string toFollow, boost::optional<int> initVal) const;
  448. public:
  449. typedef std::map< int, std::vector<int> > TIDPattern;
  450. void executeInstructions(); //called when starting a new game, before most of the map settings are done
  451. void executeTriggerType(VERMInterpreter::TriggerType tt, bool pre, const TIDPattern & identifier); //use this to run triggers
  452. void executeTriggerType(const char *trigger, int id); //convenience version of above, for pre-trigger when there is only one argument
  453. void executeTriggerType(const char *trigger); //convenience version of above, for pre-trigger when there are no args
  454. void init(); //sets up environment etc.
  455. void scanForScripts();
  456. enum EPrintMode{ALL, ERM_ONLY, VERM_ONLY};
  457. void printScripts(EPrintMode mode = ALL);
  458. void scanScripts(); //scans for functions, triggers etc.
  459. ERMInterpreter();
  460. bool checkCondition( ERM::Tcondition cond );
  461. };