ERMInterpreter.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. #include "ERMInterpreter.h"
  2. #include <boost/filesystem.hpp>
  3. #include <boost/algorithm/string.hpp>
  4. #include <boost/foreach.hpp>
  5. #include <boost/lexical_cast.hpp>
  6. /*
  7. * ERMInterpreter.cpp, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. namespace spirit = boost::spirit;
  16. using namespace VERMInterpreter;
  17. namespace ERMPrinter
  18. {
  19. //console printer
  20. using namespace ERM;
  21. struct VarPrinterVisitor : boost::static_visitor<>
  22. {
  23. void operator()(TVarExpNotMacro const& val) const
  24. {
  25. tlog2 << val.varsym;
  26. if(val.val.is_initialized())
  27. {
  28. tlog2 << val.val.get();
  29. }
  30. }
  31. void operator()(TMacroUsage const& val) const
  32. {
  33. tlog2 << "$" << val.macro << "&";
  34. }
  35. };
  36. void varPrinter(const TVarExp & var)
  37. {
  38. boost::apply_visitor(VarPrinterVisitor(), var);
  39. }
  40. struct IExpPrinterVisitor : boost::static_visitor<>
  41. {
  42. void operator()(int const & constant) const
  43. {
  44. tlog2 << constant;
  45. }
  46. void operator()(TVarExp const & var) const
  47. {
  48. varPrinter(var);
  49. }
  50. };
  51. void iexpPrinter(const TIexp & exp)
  52. {
  53. boost::apply_visitor(IExpPrinterVisitor(), exp);
  54. }
  55. struct IdentifierPrinterVisitor : boost::static_visitor<>
  56. {
  57. void operator()(TIexp const& iexp) const
  58. {
  59. iexpPrinter(iexp);
  60. }
  61. void operator()(TArithmeticOp const& arop) const
  62. {
  63. iexpPrinter(arop.lhs);
  64. tlog2 << " " << arop.opcode << " ";
  65. iexpPrinter(arop.rhs);
  66. }
  67. };
  68. void identifierPrinter(const boost::optional<Tidentifier> & id)
  69. {
  70. if(id.is_initialized())
  71. {
  72. tlog2 << "identifier: ";
  73. BOOST_FOREACH(TIdentifierInternal x, id.get())
  74. {
  75. tlog2 << "#";
  76. boost::apply_visitor(IdentifierPrinterVisitor(), x);
  77. }
  78. }
  79. }
  80. struct ConditionCondPrinterVisitor : boost::static_visitor<>
  81. {
  82. void operator()(TComparison const& cmp) const
  83. {
  84. iexpPrinter(cmp.lhs);
  85. tlog2 << " " << cmp.compSign << " ";
  86. iexpPrinter(cmp.rhs);
  87. }
  88. void operator()(int const& flag) const
  89. {
  90. tlog2 << "condflag " << flag;
  91. }
  92. };
  93. void conditionPrinter(const boost::optional<Tcondition> & cond)
  94. {
  95. if(cond.is_initialized())
  96. {
  97. Tcondition condp = cond.get();
  98. tlog2 << " condition: ";
  99. boost::apply_visitor(ConditionCondPrinterVisitor(), condp.cond);
  100. tlog2 << " cond type: " << condp.ctype;
  101. //recursive call
  102. if(condp.rhs.is_initialized())
  103. {
  104. tlog2 << "rhs: ";
  105. boost::optional<Tcondition> rhsc = condp.rhs.get().get();
  106. conditionPrinter(rhsc);
  107. }
  108. else
  109. {
  110. tlog2 << "no rhs; ";
  111. }
  112. }
  113. }
  114. struct BodyVarpPrinterVisitor : boost::static_visitor<>
  115. {
  116. void operator()(TVarExpNotMacro const& cmp) const
  117. {
  118. if(cmp.questionMark.is_initialized())
  119. {
  120. tlog2 << cmp.questionMark.get();
  121. }
  122. if(cmp.val.is_initialized())
  123. {
  124. tlog2 << "val:" << cmp.val.get();
  125. }
  126. tlog2 << "varsym: |" << cmp.varsym << "|";
  127. }
  128. void operator()(TQMacroUsage const& cmp) const
  129. {
  130. tlog2 << "???$$" << cmp.qmacro << "$$";
  131. }
  132. };
  133. struct BodyOptionItemPrinterVisitor : boost::static_visitor<>
  134. {
  135. void operator()(TVarConcatString const& cmp) const
  136. {
  137. tlog2 << "+concat\"";
  138. varPrinter(cmp.var);
  139. tlog2 << " with " << cmp.string.str;
  140. }
  141. void operator()(TStringConstant const& cmp) const
  142. {
  143. tlog2 << " \"" << cmp.str << "\" ";
  144. }
  145. void operator()(TCurriedString const& cmp) const
  146. {
  147. tlog2 << "cs: ";
  148. iexpPrinter(cmp.iexp);
  149. tlog2 << " '" << cmp.string.str << "' ";
  150. }
  151. void operator()(TSemiCompare const& cmp) const
  152. {
  153. tlog2 << cmp.compSign << "; rhs: ";
  154. iexpPrinter(cmp.rhs);
  155. }
  156. void operator()(TMacroUsage const& cmp) const
  157. {
  158. tlog2 << "$$" << cmp.macro << "$$";
  159. }
  160. void operator()(TMacroDef const& cmp) const
  161. {
  162. tlog2 << "@@" << cmp.macro << "@@";
  163. }
  164. void operator()(TIexp const& cmp) const
  165. {
  166. iexpPrinter(cmp);
  167. }
  168. void operator()(TVarpExp const& cmp) const
  169. {
  170. tlog2 << "varp";
  171. boost::apply_visitor(BodyVarpPrinterVisitor(), cmp.var);
  172. }
  173. void operator()(spirit::unused_type const& cmp) const
  174. {
  175. tlog2 << "nothing";
  176. }
  177. };
  178. struct BodyOptionVisitor : boost::static_visitor<>
  179. {
  180. void operator()(TVRLogic const& cmp) const
  181. {
  182. tlog2 << cmp.opcode << " ";
  183. iexpPrinter(cmp.var);
  184. }
  185. void operator()(TVRArithmetic const& cmp) const
  186. {
  187. tlog2 << cmp.opcode << " ";
  188. iexpPrinter(cmp.rhs);
  189. }
  190. void operator()(TNormalBodyOption const& cmp) const
  191. {
  192. tlog2 << cmp.optionCode << "~";
  193. BOOST_FOREACH(TBodyOptionItem optList, cmp.params)
  194. {
  195. boost::apply_visitor(BodyOptionItemPrinterVisitor(), optList);
  196. }
  197. }
  198. };
  199. void bodyPrinter(const Tbody & body)
  200. {
  201. tlog2 << " body items: ";
  202. BOOST_FOREACH(TBodyOption bi, body)
  203. {
  204. tlog2 << " (";
  205. apply_visitor(BodyOptionVisitor(), bi);
  206. tlog2 << ") ";
  207. }
  208. }
  209. struct CommandPrinterVisitor : boost::static_visitor<>
  210. {
  211. void operator()(Ttrigger const& trig) const
  212. {
  213. tlog2 << "trigger: " << trig.name << " ";
  214. identifierPrinter(trig.identifier);
  215. conditionPrinter(trig.condition);
  216. }
  217. void operator()(Tinstruction const& trig) const
  218. {
  219. tlog2 << "instruction: " << trig.name << " ";
  220. identifierPrinter(trig.identifier);
  221. conditionPrinter(trig.condition);
  222. bodyPrinter(trig.body);
  223. }
  224. void operator()(Treceiver const& trig) const
  225. {
  226. tlog2 << "receiver: " << trig.name << " ";
  227. identifierPrinter(trig.identifier);
  228. conditionPrinter(trig.condition);
  229. if(trig.body.is_initialized())
  230. bodyPrinter(trig.body.get());
  231. }
  232. void operator()(TPostTrigger const& trig) const
  233. {
  234. tlog2 << "post trigger: " << trig.name << " ";
  235. identifierPrinter(trig.identifier);
  236. conditionPrinter(trig.condition);
  237. }
  238. };
  239. struct LinePrinterVisitor : boost::static_visitor<>
  240. {
  241. void operator()(Tcommand const& cmd) const
  242. {
  243. CommandPrinterVisitor un;
  244. boost::apply_visitor(un, cmd.cmd);
  245. std::cout << "Line comment: " << cmd.comment << std::endl;
  246. }
  247. void operator()(std::string const& comment) const
  248. {
  249. }
  250. void operator()(spirit::unused_type const& nothing) const
  251. {
  252. }
  253. };
  254. void printERM(const TERMline & ast)
  255. {
  256. tlog2 << "";
  257. boost::apply_visitor(LinePrinterVisitor(), ast);
  258. }
  259. void printTVExp(const TVExp & exp);
  260. struct VOptionPrinterVisitor : boost::static_visitor<>
  261. {
  262. void operator()(TVExp const& cmd) const
  263. {
  264. printTVExp(cmd);
  265. }
  266. void operator()(TSymbol const& cmd) const
  267. {
  268. BOOST_FOREACH(TVModifier mod, cmd.symModifier)
  269. {
  270. tlog2 << mod << " ";
  271. }
  272. tlog2 << cmd.sym;
  273. }
  274. void operator()(char const& cmd) const
  275. {
  276. tlog2 << "'" << cmd << "'";
  277. }
  278. void operator()(int const& cmd) const
  279. {
  280. tlog2 << cmd;
  281. }
  282. void operator()(double const& cmd) const
  283. {
  284. tlog2 << cmd;
  285. }
  286. void operator()(TERMline const& cmd) const
  287. {
  288. printERM(cmd);
  289. }
  290. void operator()(TStringConstant const& cmd) const
  291. {
  292. tlog2 << "^" << cmd.str << "^";
  293. }
  294. };
  295. void printTVExp(const TVExp & exp)
  296. {
  297. BOOST_FOREACH(TVModifier mod, exp.modifier)
  298. {
  299. tlog2 << mod << " ";
  300. }
  301. tlog2 << "[ ";
  302. BOOST_FOREACH(TVOption opt, exp.children)
  303. {
  304. boost::apply_visitor(VOptionPrinterVisitor(), opt);
  305. tlog2 << " ";
  306. }
  307. tlog2 << "]";
  308. }
  309. struct TLPrinterVisitor : boost::static_visitor<>
  310. {
  311. void operator()(TVExp const& cmd) const
  312. {
  313. printTVExp(cmd);
  314. }
  315. void operator()(TERMline const& cmd) const
  316. {
  317. printERM(cmd);
  318. }
  319. };
  320. void printAST(const TLine & ast)
  321. {
  322. boost::apply_visitor(TLPrinterVisitor(), ast);
  323. tlog2 << std::endl;
  324. }
  325. }
  326. void ERMInterpreter::scanForScripts()
  327. {
  328. using namespace boost::filesystem;
  329. //parser checking
  330. if(!exists(DATA_DIR "/Data/s/"))
  331. {
  332. tlog3 << "Warning: Folder " DATA_DIR "/Data/s/ doesn't exist!\n";
  333. return;
  334. }
  335. directory_iterator enddir;
  336. for (directory_iterator dir(DATA_DIR "/Data/s"); dir!=enddir; dir++)
  337. {
  338. if(is_regular(dir->status()))
  339. {
  340. std::string name = dir->path().leaf();
  341. if( boost::algorithm::ends_with(name, ".erm") ||
  342. boost::algorithm::ends_with(name, ".verm") )
  343. {
  344. ERMParser ep(dir->path().string());
  345. FileInfo * finfo = new FileInfo;
  346. finfo->filename = dir->path().string();
  347. std::vector<ERM::TLine> buf = ep.parseFile();
  348. finfo->length = buf.size();
  349. files.push_back(finfo);
  350. for(int g=0; g<buf.size(); ++g)
  351. {
  352. scripts[LinePointer(finfo, g)] = buf[g];
  353. }
  354. }
  355. }
  356. }
  357. }
  358. void ERMInterpreter::printScripts( EPrintMode mode /*= EPrintMode::ALL*/ )
  359. {
  360. std::map< LinePointer, ERM::TLine >::const_iterator prevIt;
  361. for(std::map< LinePointer, ERM::TLine >::const_iterator it = scripts.begin(); it != scripts.end(); ++it)
  362. {
  363. if(it == scripts.begin() || it->first.file != prevIt->first.file)
  364. {
  365. tlog2 << "----------------- script " << it->first.file->filename << " ------------------\n";
  366. }
  367. ERMPrinter::printAST(it->second);
  368. prevIt = it;
  369. }
  370. }
  371. struct ScriptScanner : boost::static_visitor<>
  372. {
  373. ERMInterpreter * interpreter;
  374. LinePointer lp;
  375. ScriptScanner(ERMInterpreter * interpr, const LinePointer & _lp) : interpreter(interpr), lp(_lp)
  376. {}
  377. void operator()(TVExp const& cmd) const
  378. {
  379. //
  380. }
  381. void operator()(TERMline const& cmd) const
  382. {
  383. if(cmd.which() == 0) //TCommand
  384. {
  385. Tcommand tcmd = boost::get<Tcommand>(cmd);
  386. switch (tcmd.cmd.which())
  387. {
  388. case 0: //trigger
  389. {
  390. Trigger trig;
  391. trig.line = lp;
  392. interpreter->triggers[ TriggerType(boost::get<ERM::Ttrigger>(tcmd.cmd).name) ].push_back(trig);
  393. }
  394. break;
  395. case 3: //post trigger
  396. {
  397. Trigger trig;
  398. trig.line = lp;
  399. interpreter->postTriggers[ TriggerType(boost::get<ERM::TPostTrigger>(tcmd.cmd).name) ].push_back(trig);
  400. }
  401. break;
  402. default:
  403. break;
  404. }
  405. }
  406. }
  407. };
  408. void ERMInterpreter::scanScripts()
  409. {
  410. for(std::map< LinePointer, ERM::TLine >::const_iterator it = scripts.begin(); it != scripts.end(); ++it)
  411. {
  412. boost::apply_visitor(ScriptScanner(this, it->first), it->second);
  413. }
  414. }
  415. ERMInterpreter::ERMInterpreter()
  416. {
  417. curFunc = NULL;
  418. curTrigger = NULL;
  419. globalEnv = new Environment();
  420. }
  421. void ERMInterpreter::executeTrigger( Trigger & trig )
  422. {
  423. //skip the first line
  424. LinePointer lp = trig.line;
  425. ++lp;
  426. for(; lp.isValid(); ++lp)
  427. {
  428. ERM::TLine curLine = retrieveLine(lp);
  429. if(isATrigger(curLine))
  430. break;
  431. executeLine(lp);
  432. }
  433. }
  434. bool ERMInterpreter::isATrigger( const ERM::TLine & line )
  435. {
  436. switch(line.which())
  437. {
  438. case 0: //v-exp
  439. {
  440. TVExp vexp = boost::get<TVExp>(line);
  441. if(vexp.children.size() == 0)
  442. return false;
  443. switch (getExpType(vexp.children[0]))
  444. {
  445. case SYMBOL:
  446. {
  447. //TODO: what about sym modifiers?
  448. //TOOD: macros?
  449. ERM::TSymbol sym = boost::get<ERM::TSymbol>(vexp.children[0]);
  450. return sym.sym == triggerSymbol || sym.sym == postTriggerSymbol;
  451. }
  452. break;
  453. case TCMD:
  454. return isCMDATrigger( boost::get<ERM::Tcommand>(vexp.children[0]) );
  455. break;
  456. default:
  457. return false;
  458. break;
  459. }
  460. }
  461. break;
  462. case 1: //erm
  463. {
  464. TERMline line = boost::get<TERMline>(line);
  465. switch(line.which())
  466. {
  467. case 0: //tcmd
  468. return isCMDATrigger( boost::get<ERM::Tcommand>(line) );
  469. break;
  470. default:
  471. return false;
  472. break;
  473. }
  474. }
  475. break;
  476. default:
  477. assert(0); //it should never happen
  478. break;
  479. }
  480. assert(0);
  481. }
  482. ERM::EVOtions ERMInterpreter::getExpType( const ERM::TVOption & opt )
  483. {
  484. //MAINTENANCE: keep it correct!
  485. return static_cast<ERM::EVOtions>(opt.which());
  486. }
  487. bool ERMInterpreter::isCMDATrigger( const ERM::Tcommand & cmd )
  488. {
  489. switch (cmd.cmd.which())
  490. {
  491. case 0: //trigger
  492. case 3: //post trigger
  493. return true;
  494. break;
  495. default:
  496. return false;
  497. break;
  498. }
  499. }
  500. ERM::TLine ERMInterpreter::retrieveLine( LinePointer linePtr ) const
  501. {
  502. return *scripts.find(linePtr);
  503. }
  504. /////////
  505. //code execution
  506. struct VRPerformer;
  507. struct VR_SPerformer : boost::static_visitor<>
  508. {
  509. VRPerformer & owner;
  510. explicit VR_SPerformer(VRPerformer & _owner);
  511. void operator()(TVarConcatString const& cmp) const;
  512. void operator()(TStringConstant const& cmp) const;
  513. void operator()(TCurriedString const& cmp) const;
  514. void operator()(TSemiCompare const& cmp) const;
  515. void operator()(TMacroUsage const& cmp) const;
  516. void operator()(TMacroDef const& cmp) const;
  517. void operator()(TIexp const& cmp) const;
  518. void operator()(TVarpExp const& cmp) const;
  519. void operator()(spirit::unused_type const& cmp) const;
  520. };
  521. struct VRPerformer : boost::static_visitor<>
  522. {
  523. ERMInterpreter * interp;
  524. IexpValStr identifier;
  525. VRPerformer(ERMInterpreter * _interpr, IexpValStr ident) : interp(_interpr), identifier(ident)
  526. {}
  527. void operator()(TVRLogic const& trig) const
  528. {
  529. //TODO
  530. }
  531. void operator()(TVRArithmetic const& trig) const
  532. {
  533. //TODO
  534. }
  535. void operator()(TNormalBodyOption const& trig) const
  536. {
  537. switch(trig.optionCode)
  538. {
  539. case 'S': //setting variable
  540. {
  541. if(trig.params.size() == 1)
  542. {
  543. ERM::TBodyOptionItem boi = trig.params[0];
  544. boost::apply_visitor(VR_SPerformer(*const_cast<VRPerformer*>(this)), boi);
  545. }
  546. else
  547. throw EScriptExecError("VR receiver S option takes exactly 1 parameter!");
  548. }
  549. break;
  550. default:
  551. //not supported or not allowed
  552. break;
  553. }
  554. }
  555. };
  556. VR_SPerformer::VR_SPerformer(VRPerformer & _owner) : owner(_owner)
  557. {}
  558. void VR_SPerformer::operator()(ERM::TIexp const& trig) const
  559. {
  560. owner.identifier.setTo(owner.interp->getIexp(trig));
  561. }
  562. void VR_SPerformer::operator()(TVarConcatString const& cmp) const
  563. {}
  564. void VR_SPerformer::operator()(TStringConstant const& cmp) const
  565. {}
  566. void VR_SPerformer::operator()(TCurriedString const& cmp) const
  567. {}
  568. void VR_SPerformer::operator()(TSemiCompare const& cmp) const
  569. {}
  570. void VR_SPerformer::operator()(TMacroUsage const& cmp) const
  571. {}
  572. void VR_SPerformer::operator()(TMacroDef const& cmp) const
  573. {}
  574. void VR_SPerformer::operator()(TVarpExp const& cmp) const
  575. {}
  576. void VR_SPerformer::operator()(spirit::unused_type const& cmp) const
  577. {}
  578. struct ConditionDisemboweler;
  579. struct ERMExpDispatch : boost::static_visitor<>
  580. {
  581. ERMInterpreter * owner;
  582. ERMExpDispatch(ERMInterpreter * _owner) : owner(_owner)
  583. {}
  584. void operator()(Ttrigger const& trig) const
  585. {
  586. throw EInterpreterError("Triggers cannot be executed!");
  587. }
  588. void operator()(Tinstruction const& trig) const
  589. {
  590. }
  591. void operator()(Treceiver const& trig) const
  592. {
  593. if(trig.name == "VR")
  594. {
  595. //check condition
  596. if(trig.condition.is_initialized())
  597. {
  598. if( !owner->checkCondition(trig.condition.get()) )
  599. return;
  600. }
  601. //perform operations
  602. if(trig.identifier.is_initialized())
  603. {
  604. ERM::Tidentifier ident = trig.identifier.get();
  605. if(ident.size() == 1)
  606. {
  607. IexpValStr ievs = owner->getIexp(ident[0]);
  608. //see body
  609. if(trig.body.is_initialized())
  610. {
  611. ERM::Tbody bo = trig.body.get();
  612. for(int g=0; g<bo.size(); ++g)
  613. {
  614. boost::apply_visitor(VRPerformer(owner, ievs), bo[g]);
  615. }
  616. }
  617. }
  618. else
  619. throw EScriptExecError("VR receiver must be used with exactly one identifier item!");
  620. }
  621. else
  622. throw EScriptExecError("VR receiver must be used with an identifier!");
  623. }
  624. else if(trig.name == "DO")
  625. {
  626. //perform operations
  627. if(trig.identifier.is_initialized())
  628. {
  629. ERM::Tidentifier tid = trig.identifier.get();
  630. if(tid.size() != 4)
  631. {
  632. throw EScriptExecError("DO receiver takes exactly 4 arguments");
  633. }
  634. int funNum = owner->getIexp(tid[0]).getInt(),
  635. startVal = owner->getIexp(tid[1]).getInt(),
  636. stopVal = owner->getIexp(tid[2]).getInt(),
  637. increment = owner->getIexp(tid[3]).getInt();
  638. for(int it = startVal; it < stopVal; it += increment)
  639. {
  640. owner->getFuncVars(funNum)->getParam(16) = it;
  641. //owner->executeTriggerType(TriggerType("FU"), true, );
  642. it = owner->getFuncVars(funNum)->getParam(16);
  643. }
  644. }
  645. }
  646. else
  647. {
  648. //unsupported or invalid trigger
  649. }
  650. }
  651. void operator()(TPostTrigger const& trig) const
  652. {
  653. throw EInterpreterError("Post-triggers cannot be executed!");
  654. }
  655. };
  656. struct CommandExec : boost::static_visitor<>
  657. {
  658. ERMInterpreter * owner;
  659. CommandExec(ERMInterpreter * _owner) : owner(_owner)
  660. {}
  661. void operator()(Tcommand const& cmd) const
  662. {
  663. boost::apply_visitor(ERMExpDispatch(owner), cmd.cmd);
  664. std::cout << "Line comment: " << cmd.comment << std::endl;
  665. }
  666. void operator()(std::string const& comment) const
  667. {
  668. //comment - do nothing
  669. }
  670. void operator()(spirit::unused_type const& nothing) const
  671. {
  672. //nothing - do nothing
  673. }
  674. };
  675. struct LineExec : boost::static_visitor<>
  676. {
  677. ERMInterpreter * owner;
  678. LineExec(ERMInterpreter * _owner) : owner(_owner)
  679. {}
  680. void operator()(TVExp const& cmd) const
  681. {
  682. //printTVExp(cmd);
  683. }
  684. void operator()(TERMline const& cmd) const
  685. {
  686. boost::apply_visitor(CommandExec(owner), cmd);
  687. }
  688. };
  689. /////////
  690. void ERMInterpreter::executeLine( const LinePointer & lp )
  691. {
  692. boost::apply_visitor(LineExec(this), scripts[lp]);
  693. }
  694. void ERMInterpreter::init()
  695. {
  696. ermGlobalEnv = new ERMEnvironment();
  697. globalEnv = new Environment();
  698. //TODO: reset?
  699. }
  700. IexpValStr ERMInterpreter::getVar(std::string toFollow, boost::optional<int> initVal) const
  701. {
  702. IexpValStr ret;
  703. ret.type = IexpValStr::WRONGVAL;
  704. int initV;
  705. bool hasInit = false;
  706. if(initVal.is_initialized())
  707. {
  708. initV = initVal.get();
  709. hasInit = true;
  710. }
  711. int endNum = 0;
  712. if(toFollow[0] == 'd')
  713. {
  714. endNum = 1;
  715. //TODO: support
  716. }
  717. if(toFollow.size() == 0)
  718. {
  719. if(hasInit)
  720. ret = IexpValStr(initV);
  721. else
  722. throw EIexpProblem("No input to getVar!");
  723. return ret;
  724. }
  725. //now we have at least one element in toFollow
  726. for(int b=toFollow.size()-1; b>=endNum; --b)
  727. {
  728. bool retIt = b == endNum+1; //if we should return the value are currently at
  729. char cr = toFollow[toFollow.size() - 1];
  730. if(cr == 'c')//write number of current day
  731. {
  732. //TODO
  733. }
  734. else if(cr == 'd') //info for external env - add i/o set
  735. {
  736. throw EIexpProblem("d inside i-expression not allowed!");
  737. }
  738. else if(cr == 'e')
  739. {
  740. if(hasInit)
  741. {
  742. if(retIt)
  743. {
  744. //these C-style cast is here just to shut up compiler errors
  745. if(initV > 0 && initV <= FunctionLocalVars::NUM_FLOATINGS)
  746. {
  747. if(curFunc)
  748. ret = IexpValStr(&curFunc->getFloat(initV));
  749. else
  750. throw EIexpProblem("Function context not available!");
  751. }
  752. else if(initV < 0 && initV >= -TriggerLocalVars::EVAR_NUM)
  753. {
  754. if(curTrigger)
  755. ret = IexpValStr(&curTrigger->ermLocalVars.getEvar(initV));
  756. else
  757. throw EIexpProblem("No trigger context available!");
  758. }
  759. else
  760. throw EIexpProblem("index " + boost::lexical_cast<std::string>(initV) + " not allowed for e array");
  761. }
  762. else
  763. throw EIexpProblem("e variables cannot appear in this context");
  764. }
  765. else
  766. throw EIexpProblem("e variables cannot appear in this context");
  767. }
  768. else if(cr >= 'f' && cr <= 't')
  769. {
  770. if(retIt)
  771. ret = IexpValStr(&ermGlobalEnv->getQuickVar(cr));
  772. else
  773. {
  774. if(hasInit)
  775. throw EIexpProblem("quick variables cannot be used in this context");
  776. else
  777. {
  778. initV = ermGlobalEnv->getQuickVar(cr);
  779. hasInit = true;
  780. }
  781. }
  782. }
  783. else if(cr == 'v') //standard variables
  784. {
  785. if(hasInit)
  786. {
  787. if(retIt)
  788. ret = IexpValStr(&ermGlobalEnv->getStandardVar(initV));
  789. else
  790. initV = ermGlobalEnv->getStandardVar(initV);
  791. }
  792. else
  793. throw EIexpProblem("standard variable cannot be used in this context!");
  794. }
  795. else if(cr == 'w') //local hero variables
  796. {
  797. //TODO
  798. }
  799. else if(cr == 'x') //function parameters
  800. {
  801. if(hasInit)
  802. {
  803. if(curFunc)
  804. {
  805. if(retIt)
  806. ret = IexpValStr(&curFunc->getParam(initV));
  807. else
  808. initV = curFunc->getParam(initV);
  809. }
  810. else throw EIexpProblem("Function parameters cannot be used outside a function!");
  811. }
  812. else
  813. throw EIexpProblem("Specify which function parameter should be used");
  814. }
  815. else if(cr == 'y')
  816. {
  817. if(hasInit)
  818. {
  819. if(initV > 0 && initV <= FunctionLocalVars::NUM_LOCALS)
  820. {
  821. if(curFunc)
  822. {
  823. if(retIt)
  824. ret = IexpValStr(&curFunc->getLocal(initV));
  825. else
  826. initV = curFunc->getLocal(initV);
  827. }
  828. else
  829. throw EIexpProblem("Function local variables cannot be used outside a function!");
  830. }
  831. else if(initV < 0 && initV >= -TriggerLocalVars::YVAR_NUM)
  832. {
  833. if(curTrigger)
  834. {
  835. if(retIt)
  836. ret = IexpValStr(&curTrigger->ermLocalVars.getYvar(initV));
  837. else
  838. initV = curTrigger->ermLocalVars.getYvar(initV);
  839. }
  840. else
  841. throw EIexpProblem("Trigger local variables cannot be used outside triggers!");
  842. }
  843. else
  844. throw EIexpProblem("Wrong argument for function local variable!");
  845. }
  846. else
  847. throw EIexpProblem("y variable cannot be used in this context!");
  848. }
  849. else if(cr == 'z')
  850. {
  851. if(hasInit)
  852. {
  853. if(retIt)
  854. {
  855. //these C-style casts are here just to shut up compiler errors
  856. if(initV > 0 )
  857. ret = IexpValStr(&ermGlobalEnv->getZVar(initV));
  858. else if(initV < 0)
  859. {
  860. if(curFunc)
  861. ret = IexpValStr(&curFunc->getString(initV));
  862. else
  863. throw EIexpProblem("Function local string variables cannot be used outside functions!");
  864. }
  865. else
  866. throw EIexpProblem("Wrong parameter for string variable!");
  867. }
  868. else
  869. throw EIexpProblem("String variables can only be returned!");
  870. }
  871. else
  872. throw EIexpProblem("String variables cannot be used in this context!");
  873. }
  874. else
  875. {
  876. throw EIexpProblem(std::string("Symbol ") + cr + " is not allowed in this context!");
  877. }
  878. }
  879. return ret;
  880. }
  881. namespace IexpDisemboweler
  882. {
  883. enum EDir{GET, SET};
  884. }
  885. struct LVL2IexpDisemboweler : boost::static_visitor<IexpValStr>
  886. {
  887. IexpDisemboweler::EDir dir;
  888. /*const*/ ERMInterpreter * env;
  889. LVL2IexpDisemboweler(/*const*/ ERMInterpreter * _env, IexpDisemboweler::EDir _dir)
  890. : env(_env), dir(_dir) //writes value to given var
  891. {}
  892. IexpValStr processNotMacro(const TVarExpNotMacro & val) const
  893. {
  894. if(val.questionMark.is_initialized())
  895. throw EIexpProblem("Question marks ('?') are not allowed in getter i-expressions");
  896. //const-cast just to do some code-reuse...
  897. return env->getVar(val.varsym, val.val);
  898. }
  899. IexpValStr operator()(TVarExpNotMacro const& val) const
  900. {
  901. return processNotMacro(val);
  902. }
  903. IexpValStr operator()(TMacroUsage const& val) const
  904. {
  905. return env->getIexp(val);
  906. }
  907. };
  908. struct LVL1IexpDisemboweler : boost::static_visitor<IexpValStr>
  909. {
  910. IexpDisemboweler::EDir dir;
  911. /*const*/ ERMInterpreter * env;
  912. LVL1IexpDisemboweler(/*const*/ ERMInterpreter * _env, IexpDisemboweler::EDir _dir)
  913. : env(_env), dir(_dir) //writes value to given var
  914. {}
  915. IexpValStr operator()(int const & constant) const
  916. {
  917. if(dir == IexpDisemboweler::GET)
  918. {
  919. IexpValStr ret = IexpValStr(constant);
  920. }
  921. else
  922. {
  923. throw EIexpProblem("Cannot set a constant!");
  924. }
  925. }
  926. IexpValStr operator()(TVarExp const & var) const
  927. {
  928. return boost::apply_visitor(LVL2IexpDisemboweler(env, dir), var);
  929. }
  930. };
  931. IexpValStr ERMInterpreter::getIexp( const ERM::TIexp & iexp ) const
  932. {
  933. return boost::apply_visitor(LVL1IexpDisemboweler(const_cast<ERMInterpreter*>(this), IexpDisemboweler::GET), iexp);
  934. }
  935. IexpValStr ERMInterpreter::getIexp( const ERM::TMacroUsage & macro ) const
  936. {
  937. std::map<std::string, ERM::TVarExpNotMacro>::const_iterator it =
  938. ermGlobalEnv->macroBindings.find(macro.macro);
  939. if(it == ermGlobalEnv->macroBindings.end())
  940. throw EUsageOfUndefinedMacro(macro.macro);
  941. return getVar(it->second.varsym, it->second.val);
  942. }
  943. IexpValStr ERMInterpreter::getIexp( const ERM::TIdentifierInternal & tid ) const
  944. {
  945. if(tid.which() == 0)
  946. {
  947. IexpValStr ievs = getIexp(boost::get<ERM::TIexp>(tid));
  948. }
  949. else
  950. throw EScriptExecError("Identifier must be a valid i-expression to perform this operation!");
  951. }
  952. void ERMInterpreter::executeTriggerType( VERMInterpreter::TriggerType tt, bool pre, const std::map< int, std::vector<int> > & identifier )
  953. {
  954. TtriggerListType & triggerList = pre ? triggers : postTriggers;
  955. TriggerIdentifierMatch tim;
  956. tim.allowNoIdetifier = false;
  957. tim.ermEnv = this;
  958. tim.matchToIt = identifier;
  959. std::vector<Trigger> & triggersToTry = triggerList[tt];
  960. for(int g=0; g<triggersToTry.size(); ++g)
  961. {
  962. if(tim.tryMatch(&triggersToTry[g]))
  963. {
  964. curTrigger = &triggersToTry[g];
  965. executeTrigger(triggersToTry[g]);
  966. }
  967. }
  968. }
  969. ERM::Ttrigger ERMInterpreter::retrieveTrigger( ERM::TLine line )
  970. {
  971. if(line.which() == 1)
  972. {
  973. ERM::TERMline tl = boost::get<ERM::TERMline>(line);
  974. if(tl.which() == 0)
  975. {
  976. ERM::Tcommand tcm = boost::get<ERM::Tcommand>(line);
  977. if(tcm.cmd.which() == 0)
  978. {
  979. return boost::get<ERM::Ttrigger>(tcm.cmd);
  980. }
  981. }
  982. }
  983. throw ELineProblem("Given line is not an ERM trigger!");
  984. }
  985. template<typename T>
  986. bool compareExp(const T & lhs, const T & rhs, std::string op)
  987. {
  988. if(op == "<")
  989. {
  990. return lhs < rhs;
  991. }
  992. else if(op == ">")
  993. {
  994. return lhs > rhs;
  995. }
  996. else if(op == ">=" || op == "=>")
  997. {
  998. return lhs >= rhs;
  999. }
  1000. else if(op == "<=" || op == "=<")
  1001. {
  1002. return lhs <= rhs;
  1003. }
  1004. else if(op == "==")
  1005. {
  1006. return lhs == rhs;
  1007. }
  1008. else if(op == "<>" || op == "><")
  1009. {
  1010. return lhs != rhs;
  1011. }
  1012. else
  1013. throw EScriptExecError(std::string("Wrong comparison sign: ") + op);
  1014. }
  1015. struct ConditionDisemboweler : boost::static_visitor<bool>
  1016. {
  1017. ConditionDisemboweler(ERMInterpreter * _ei) : ei(_ei)
  1018. {}
  1019. bool operator()(TComparison const & cmp) const
  1020. {
  1021. IexpValStr lhs = ei->getIexp(cmp.lhs),
  1022. rhs = ei->getIexp(cmp.rhs);
  1023. switch (lhs.type)
  1024. {
  1025. case IexpValStr::FLOATVAR:
  1026. switch (rhs.type)
  1027. {
  1028. case IexpValStr::FLOATVAR:
  1029. return compareExp(lhs.getFloat(), rhs.getFloat(), cmp.compSign);
  1030. break;
  1031. default:
  1032. throw EScriptExecError("Incompatible types for comparison");
  1033. }
  1034. break;
  1035. case IexpValStr::INT:
  1036. case IexpValStr::INTVAR:
  1037. switch (rhs.type)
  1038. {
  1039. case IexpValStr::INT:
  1040. case IexpValStr::INTVAR:
  1041. return compareExp(lhs.getInt(), rhs.getInt(), cmp.compSign);
  1042. break;
  1043. default:
  1044. throw EScriptExecError("Incompatible types for comparison");
  1045. }
  1046. break;
  1047. case IexpValStr::STRINGVAR:
  1048. switch (rhs.type)
  1049. {
  1050. case IexpValStr::STRINGVAR:
  1051. return compareExp(lhs.getString(), rhs.getString(), cmp.compSign);
  1052. break;
  1053. default:
  1054. throw EScriptExecError("Incompatible types for comparison");
  1055. }
  1056. break;
  1057. default:
  1058. throw EScriptExecError("Wrong type of left iexp!");
  1059. }
  1060. //we should never reach this place
  1061. }
  1062. bool operator()(int const & flag) const
  1063. {
  1064. return ei->ermGlobalEnv->getFlag(flag);
  1065. }
  1066. private:
  1067. ERMInterpreter * ei;
  1068. };
  1069. bool ERMInterpreter::checkCondition( ERM::Tcondition cond )
  1070. {
  1071. bool ret = boost::apply_visitor(ConditionDisemboweler(this), cond.cond);
  1072. if(cond.rhs.is_initialized())
  1073. { //taking care of rhs expression
  1074. bool rhs = checkCondition(cond.rhs.get().get());
  1075. switch (cond.ctype)
  1076. {
  1077. case '&':
  1078. ret &= rhs;
  1079. break;
  1080. case '|':
  1081. ret |= rhs;
  1082. break;
  1083. case 'X':
  1084. ret ^= rhs;
  1085. break;
  1086. default:
  1087. throw EInterpreterProblem(std::string("Strange - wrong condition connection (") + cond.ctype + ") !");
  1088. break;
  1089. }
  1090. }
  1091. return ret;
  1092. }
  1093. FunctionLocalVars * ERMInterpreter::getFuncVars( int funNum )
  1094. {
  1095. return funcVars + funNum - 1;
  1096. }
  1097. const std::string ERMInterpreter::triggerSymbol = "trigger";
  1098. const std::string ERMInterpreter::postTriggerSymbol = "postTrigger";
  1099. const std::string ERMInterpreter::defunSymbol = "defun";
  1100. struct TriggerIdMatchHelper : boost::static_visitor<>
  1101. {
  1102. int & ret;
  1103. ERMInterpreter * interpreter;
  1104. Trigger * trig;
  1105. TriggerIdMatchHelper(int & b, ERMInterpreter * ermint, Trigger * _trig)
  1106. : ret(b), interpreter(ermint), trig(_trig)
  1107. {}
  1108. void operator()(TIexp const& iexp) const
  1109. {
  1110. IexpValStr val = interpreter->getIexp(iexp);
  1111. switch(val.type)
  1112. {
  1113. case IexpValStr::INT:
  1114. case IexpValStr::INTVAR:
  1115. ret = val.getInt();
  1116. break;
  1117. default:
  1118. throw EScriptExecError("Incompatible i-exp type!");
  1119. break;
  1120. }
  1121. }
  1122. void operator()(TArithmeticOp const& arop) const
  1123. {
  1124. //error?!?
  1125. }
  1126. };
  1127. bool TriggerIdentifierMatch::tryMatch( Trigger * interptrig ) const
  1128. {
  1129. bool ret = true;
  1130. const ERM::Ttrigger & trig = ERMInterpreter::retrieveTrigger(ermEnv->retrieveLine(interptrig->line));
  1131. if(trig.identifier.is_initialized())
  1132. {
  1133. ERM::Tidentifier tid = trig.identifier.get();
  1134. std::map< int, std::vector<int> >::const_iterator it = matchToIt.find(tid.size());
  1135. if(it == matchToIt.end())
  1136. ret = false;
  1137. else
  1138. {
  1139. const std::vector<int> & pattern = it->second;
  1140. for(int g=0; g<pattern.size(); ++g)
  1141. {
  1142. int val = -1;
  1143. boost::apply_visitor(TriggerIdMatchHelper(val, ermEnv, interptrig), tid[g]);
  1144. if(pattern[g] != val)
  1145. {
  1146. ret = false;
  1147. }
  1148. }
  1149. ret = true;
  1150. }
  1151. }
  1152. else
  1153. {
  1154. ret = allowNoIdetifier;
  1155. }
  1156. //check condition
  1157. if(ret)
  1158. {
  1159. if(trig.condition.is_initialized())
  1160. {
  1161. return ermEnv->checkCondition(trig.condition.get());
  1162. }
  1163. else //no condition
  1164. return true;
  1165. }
  1166. else
  1167. return false;
  1168. }
  1169. VERMInterpreter::ERMEnvironment::ERMEnvironment()
  1170. {
  1171. for(int g=0; g<NUM_QUICKS; ++g)
  1172. quickVars[g] = 0;
  1173. for(int g=0; g<NUM_STANDARDS; ++g)
  1174. standardVars[g] = 0;
  1175. //string should be automatically initialized to ""
  1176. for(int g=0; g<NUM_FLAGS; ++g)
  1177. flags[g] = false;
  1178. }
  1179. int & VERMInterpreter::ERMEnvironment::getQuickVar( const char letter )
  1180. {
  1181. assert(letter >= 'f' && letter <= 't'); //it should be check by another function, just making sure here
  1182. return quickVars[letter - 'f'];
  1183. }
  1184. int & VERMInterpreter::ERMEnvironment::getStandardVar( int num )
  1185. {
  1186. if(num < 1 || num > NUM_STANDARDS)
  1187. throw EScriptExecError("Number of standard variable out of bounds");
  1188. return standardVars[num-1];
  1189. }
  1190. std::string & VERMInterpreter::ERMEnvironment::getZVar( int num )
  1191. {
  1192. if(num < 1 || num > NUM_STRINGS)
  1193. throw EScriptExecError("Number of string variable out of bounds");
  1194. return strings[num-1];
  1195. }
  1196. bool & VERMInterpreter::ERMEnvironment::getFlag( int num )
  1197. {
  1198. if(num < 1 || num > NUM_FLAGS)
  1199. throw EScriptExecError("Number of flag out of bounds");
  1200. return flags[num-1];
  1201. }
  1202. VERMInterpreter::TriggerLocalVars::TriggerLocalVars()
  1203. {
  1204. for(int g=0; g<EVAR_NUM; ++g)
  1205. evar[g] = 0.0;
  1206. for(int g=0; g<YVAR_NUM; ++g)
  1207. yvar[g] = 0;
  1208. }
  1209. double & VERMInterpreter::TriggerLocalVars::getEvar( int num )
  1210. {
  1211. num = -num;
  1212. if(num < 1 || num > EVAR_NUM)
  1213. throw EScriptExecError("Number of trigger local floating point variable out of bounds");
  1214. return evar[num-1];
  1215. }
  1216. int & VERMInterpreter::TriggerLocalVars::getYvar( int num )
  1217. {
  1218. if(num < 1 || num > YVAR_NUM)
  1219. throw EScriptExecError("Number of trigger local variable out of bounds");
  1220. return yvar[num-1];
  1221. }
  1222. bool VERMInterpreter::Environment::isBound( const std::string & name, bool globalOnly ) const
  1223. {
  1224. std::map<std::string, TVOption>::const_iterator it = symbols.find(name);
  1225. if(globalOnly && parent)
  1226. {
  1227. return parent->isBound(name, globalOnly);
  1228. }
  1229. //we have it; if globalOnly is true, lexical parent is false here so we are global env
  1230. if(it != symbols.end())
  1231. return true;
  1232. //here, we don;t have it; but parent can have
  1233. if(parent)
  1234. return parent->isBound(name, globalOnly);
  1235. return false;
  1236. }
  1237. ERM::TVOption VERMInterpreter::Environment::retrieveValue( const std::string & name ) const
  1238. {
  1239. std::map<std::string, TVOption>::const_iterator it = symbols.find(name);
  1240. if(it == symbols.end())
  1241. {
  1242. if(parent)
  1243. {
  1244. return parent->retrieveValue(name);
  1245. }
  1246. throw ESymbolNotFound(name);
  1247. }
  1248. return it->second;
  1249. }
  1250. bool VERMInterpreter::Environment::unbind( const std::string & name, EUnbindMode mode )
  1251. {
  1252. if(isBound(name, false))
  1253. {
  1254. if(symbols.find(name) != symbols.end()) //result of isBound could be from higher lexical env
  1255. symbols.erase(symbols.find(name));
  1256. if(mode == FULLY_RECURSIVE && parent)
  1257. parent->unbind(name, mode);
  1258. return true;
  1259. }
  1260. if(parent && (mode == RECURSIVE_UNTIL_HIT || mode == FULLY_RECURSIVE))
  1261. return parent->unbind(name, mode);
  1262. //neither bound nor have lexical parent
  1263. return false;
  1264. }
  1265. int & VERMInterpreter::FunctionLocalVars::getParam( int num )
  1266. {
  1267. if(num < 1 || num > NUM_PARAMETERS)
  1268. throw EScriptExecError("Number of parameter out of bounds");
  1269. return params[num-1];
  1270. }
  1271. int & VERMInterpreter::FunctionLocalVars::getLocal( int num )
  1272. {
  1273. if(num < 1 || num > NUM_LOCALS)
  1274. throw EScriptExecError("Number of local variable out of bounds");
  1275. return locals[num-1];
  1276. }
  1277. std::string & VERMInterpreter::FunctionLocalVars::getString( int num )
  1278. {
  1279. num = -num; //we deal with negative indices
  1280. if(num < 1 || num > NUM_PARAMETERS)
  1281. throw EScriptExecError("Number of function local string variable out of bounds");
  1282. return strings[num-1];
  1283. }
  1284. double & VERMInterpreter::FunctionLocalVars::getFloat( int num )
  1285. {
  1286. if(num < 1 || num > NUM_FLOATINGS)
  1287. throw EScriptExecError("Number of float var out of bounds");
  1288. return floats[num-1];
  1289. }
  1290. void IexpValStr::setTo( const IexpValStr & second )
  1291. {
  1292. switch(type)
  1293. {
  1294. case IexpValStr::FLOATVAR:
  1295. *val.flvar = second.getFloat();
  1296. break;
  1297. case IexpValStr::INT:
  1298. throw EScriptExecError("VR S: value not assignable!");
  1299. break;
  1300. case IexpValStr::INTVAR:
  1301. *val.integervar = second.getInt();
  1302. break;
  1303. case IexpValStr::STRINGVAR:
  1304. *val.stringvar = second.getString();
  1305. break;
  1306. default:
  1307. throw EScriptExecError("Wrong type of identifier iexp!");
  1308. }
  1309. }
  1310. void IexpValStr::setTo( int val )
  1311. {
  1312. switch(type)
  1313. {
  1314. case INTVAR:
  1315. *this->val.integervar = val;
  1316. break;
  1317. default:
  1318. throw EIexpProblem("Incompatible type!");
  1319. break;
  1320. }
  1321. }
  1322. void IexpValStr::setTo( float val )
  1323. {
  1324. switch(type)
  1325. {
  1326. case FLOATVAR:
  1327. *this->val.flvar = val;
  1328. break;
  1329. default:
  1330. throw EIexpProblem("Incompatible type!");
  1331. break;
  1332. }
  1333. }
  1334. void IexpValStr::setTo( const std::string & val )
  1335. {
  1336. switch(type)
  1337. {
  1338. case STRINGVAR:
  1339. *this->val.stringvar = val;
  1340. break;
  1341. default:
  1342. throw EIexpProblem("Incompatible type!");
  1343. break;
  1344. }
  1345. }
  1346. int IexpValStr::getInt() const
  1347. {
  1348. switch(type)
  1349. {
  1350. case IexpValStr::INT:
  1351. return val.val;
  1352. break;
  1353. case IexpValStr::INTVAR:
  1354. return *val.integervar;
  1355. break;
  1356. default:
  1357. throw EIexpProblem("Cannot get iexp as int!");
  1358. break;
  1359. }
  1360. }
  1361. float IexpValStr::getFloat() const
  1362. {
  1363. switch(type)
  1364. {
  1365. case IexpValStr::FLOATVAR:
  1366. return *val.flvar;
  1367. break;
  1368. default:
  1369. throw EIexpProblem("Cannot get iexp as float!");
  1370. break;
  1371. }
  1372. }
  1373. std::string IexpValStr::getString() const
  1374. {
  1375. switch(type)
  1376. {
  1377. case IexpValStr::STRINGVAR:
  1378. return *val.stringvar;
  1379. break;
  1380. default:
  1381. throw EScriptExecError("Cannot get iexp as string!");
  1382. break;
  1383. }
  1384. }