ERMInterpreter.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. ///main environment class, manages symbols
  55. class Environment
  56. {
  57. private:
  58. std::map<std::string, TVOption> symbols;
  59. Environment * parent;
  60. public:
  61. bool isBound(const std::string & name, bool globalOnly) const
  62. {
  63. std::map<std::string, TVOption>::const_iterator it = symbols.find(name);
  64. if(globalOnly && parent)
  65. {
  66. return parent->isBound(name, globalOnly);
  67. }
  68. //we have it; if globalOnly is true, lexical parent is false here so we are global env
  69. if(it != symbols.end())
  70. return true;
  71. //here, we don;t have it; but parent can have
  72. if(parent)
  73. return parent->isBound(name, globalOnly);
  74. return false;
  75. }
  76. TVOption retrieveValue(const std::string & name) const
  77. {
  78. std::map<std::string, TVOption>::const_iterator it = symbols.find(name);
  79. if(it == symbols.end())
  80. {
  81. if(parent)
  82. {
  83. return parent->retrieveValue(name);
  84. }
  85. throw ESymbolNotFound(name);
  86. }
  87. return it->second;
  88. }
  89. enum EUnbindMode{LOCAL, RECURSIVE_UNTIL_HIT, FULLY_RECURSIVE};
  90. ///returns true if symbols was really unbound
  91. bool unbind(const std::string & name, EUnbindMode mode)
  92. {
  93. if(isBound(name, false))
  94. {
  95. if(symbols.find(name) != symbols.end()) //result of isBound could be from higher lexical env
  96. symbols.erase(symbols.find(name));
  97. if(mode == FULLY_RECURSIVE && parent)
  98. parent->unbind(name, mode);
  99. return true;
  100. }
  101. if(parent && (mode == RECURSIVE_UNTIL_HIT || mode == FULLY_RECURSIVE))
  102. return parent->unbind(name, mode);
  103. //neither bound nor have lexical parent
  104. return false;
  105. }
  106. };
  107. // All numeric variables are integer variables and have a range of -2147483647...+2147483647
  108. // c stores game active day number //indirect variable
  109. // d current value //not an actual variable but a modifier
  110. // e1..e100 Function floating point variables //local
  111. // e-1..e-100 Trigger local floating point variables //local
  112. // 'f'..'t' Standard variables ('quick variables') //global
  113. // v1..v1000 Standard variables //global
  114. // w1..w100 Hero variables
  115. // w101..w200 Hero variables
  116. // x1..x16 Function parameters //local
  117. // y1..y100 Function local variables //local
  118. // y-1..y-100 Trigger-based local integer variables //local
  119. // z1..z1000 String variables //global
  120. // z-1..z-10 Function local string variables //local
  121. struct TriggerLocalVars
  122. {
  123. static const int EVAR_NUM = 100; //number of evar locals
  124. double evar[EVAR_NUM]; //negative indices
  125. static const int YVAR_NUM = 100; //number of yvar locals
  126. int yvar[YVAR_NUM];
  127. TriggerLocalVars()
  128. {
  129. for(int g=0; g<EVAR_NUM; ++g)
  130. evar[g] = 0.0;
  131. for(int g=0; g<YVAR_NUM; ++g)
  132. yvar[g] = 0;
  133. }
  134. };
  135. struct FunctionLocalVars
  136. {
  137. static const int NUM_PARAMETERS = 16; //number of function parameters
  138. int params[NUM_PARAMETERS]; //x-vars
  139. static const int NUM_LOCALS = 100;
  140. int locals[NUM_LOCALS]; //y-vars
  141. static const int NUM_STRINGS = 10;
  142. std::string strings[NUM_STRINGS]; //z-vars (negative indices)
  143. static const int NUM_FLOATINGS = 100;
  144. double floats[NUM_FLOATINGS]; //e-vars (positive indices)
  145. };
  146. struct ERMEnvironment
  147. {
  148. static const int NUM_QUICKS = 't' - 'f' + 1; //it should be 15
  149. int quickVars[NUM_QUICKS]; //referenced by letter ('f' to 't' inclusive)
  150. int & getQuickVar(const char letter)
  151. {
  152. assert(letter >= 'f' && letter <= 't'); //it should be check by another function, just makign sure here
  153. return quickVars[letter - 'f'];
  154. }
  155. static const int NUM_STANDARDS = 1000;
  156. int standardVars[NUM_STANDARDS]; //v-vars
  157. static const int NUM_STRINGS = 1000;
  158. std::string strings[NUM_STRINGS]; //z-vars (positive indices)
  159. std::map<std::string, ERM::TVarExpNotMacro> macroBindings;
  160. };
  161. struct TriggerType
  162. {
  163. //the same order of trigger types in this enum and in validTriggers array is obligatory!
  164. enum ETrigType{AE, BA, BF, BG, BR, CM, CO, FU, GE, GM, HE, HL, HM, IP, LE, MF, MG, MM, MR,
  165. MW, OB, PI, SN, TH, TM} type;
  166. static ETrigType convertTrigger(const std::string & trig)
  167. {
  168. static const std::string validTriggers[] = {"AE", "BA", "BF", "BG", "BR", "CM", "CO", "FU",
  169. "GE", "GM", "HE", "HL", "HM", "IP", "LE", "MF", "MG", "MM", "MR", "MW", "OB", "PI", "SN",
  170. "TH", "TM"};
  171. for(int i=0; i<ARRAY_COUNT(validTriggers); ++i)
  172. {
  173. if(validTriggers[i] == trig)
  174. return static_cast<ETrigType>(i);
  175. }
  176. throw EInvalidTrigger(trig);
  177. }
  178. bool operator<(const TriggerType & t2) const
  179. {
  180. return type < t2.type;
  181. }
  182. TriggerType(const std::string & sym)
  183. {
  184. type = convertTrigger(sym);
  185. }
  186. };
  187. struct FileInfo
  188. {
  189. std::string filename;
  190. int length;
  191. };
  192. struct LinePointer
  193. {
  194. const FileInfo * file; //non-owning
  195. int lineNum;
  196. LinePointer() : file(NULL)
  197. {}
  198. LinePointer(const FileInfo * finfo, int line) : file(finfo), lineNum(line)
  199. {}
  200. //lexicographical order
  201. bool operator<(const LinePointer & rhs) const
  202. {
  203. if(file->filename != rhs.file->filename)
  204. return file->filename < rhs.file->filename;
  205. return lineNum < rhs.lineNum;
  206. }
  207. bool operator!=(const LinePointer & rhs) const
  208. {
  209. return file->filename != rhs.file->filename || lineNum != rhs.lineNum;
  210. }
  211. LinePointer & operator++()
  212. {
  213. ++lineNum;
  214. return *this;
  215. }
  216. bool isValid() const
  217. {
  218. return file && lineNum < file->length;
  219. }
  220. };
  221. struct LexicalPtr
  222. {
  223. LinePointer line; //where to start
  224. std::vector<int> entryPoints; //defines how to pass to current location
  225. bool operator<(const LexicalPtr & sec) const
  226. {
  227. if(line != sec.line)
  228. return line < sec.line;
  229. if(entryPoints.size() != sec.entryPoints.size())
  230. return entryPoints.size() < sec.entryPoints.size();
  231. for(int g=0; g<entryPoints.size(); ++g)
  232. {
  233. if(entryPoints[g] < sec.entryPoints[g])
  234. return true;
  235. }
  236. return false;
  237. }
  238. };
  239. //call stack, represents dynamic range
  240. struct Stack
  241. {
  242. std::vector<LexicalPtr> stack;
  243. };
  244. struct Trigger
  245. {
  246. LinePointer line;
  247. TriggerLocalVars ermLocalVars;
  248. Stack * stack; //where we are stuck at execution
  249. Trigger() : stack(NULL)
  250. {}
  251. };
  252. }
  253. class ERMInterpreter;
  254. struct TriggerIdentifierMatch
  255. {
  256. bool allowNoIdetifier;
  257. std::map< int, std::vector<int> > matchToIt; //match subidentifiers to these numbers
  258. static const int MAX_SUBIDENTIFIERS = 16;
  259. ERMInterpreter * ermEnv;
  260. bool tryMatch(VERMInterpreter::Trigger * interptrig, const ERM::Ttrigger & trig) const;
  261. };
  262. class ERMInterpreter
  263. {
  264. friend class ScriptScanner;
  265. friend class TriggerIdMatchHelper;
  266. std::vector<VERMInterpreter::FileInfo*> files;
  267. std::vector< VERMInterpreter::FileInfo* > fileInfos;
  268. std::map<VERMInterpreter::LinePointer, ERM::TLine> scripts;
  269. std::map<VERMInterpreter::LexicalPtr, VERMInterpreter::Environment> lexicalEnvs;
  270. ERM::TLine retrieveLine(VERMInterpreter::LinePointer linePtr) const;
  271. VERMInterpreter::Environment * globalEnv;
  272. VERMInterpreter::ERMEnvironment * ermGlobalEnv;
  273. std::map<VERMInterpreter::TriggerType, std::vector<VERMInterpreter::Trigger> > triggers, postTriggers;
  274. template<typename T> void setIexp(const ERM::TIexp & iexp, const T & val, VERMInterpreter::Trigger * trig = NULL);
  275. template<typename T> T getIexp(const ERM::TIexp & iexp, /*const*/ VERMInterpreter::Trigger * trig = NULL, /*const*/ VERMInterpreter::FunctionLocalVars * fun = NULL) const;
  276. static const std::string triggerSymbol, postTriggerSymbol, defunSymbol;
  277. void executeLine(const VERMInterpreter::LinePointer & lp);
  278. void executeTrigger(VERMInterpreter::Trigger & trig);
  279. static bool isCMDATrigger(const ERM::Tcommand & cmd);
  280. static bool isATrigger(const ERM::TLine & line);
  281. static ERM::EVOtions getExpType(const ERM::TVOption & opt);
  282. public:
  283. void init(); //sets up environment etc.
  284. void scanForScripts();
  285. enum EPrintMode{ALL, ERM_ONLY, VERM_ONLY};
  286. void printScripts(EPrintMode mode = ALL);
  287. void scanScripts(); //scans for functions, triggers etc.
  288. ERMInterpreter();
  289. };