12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316 |
- #include "ERMInterpreter.h"
- #include <boost/filesystem.hpp>
- #include <boost/algorithm/string.hpp>
- #include <boost/foreach.hpp>
- #include <boost/lexical_cast.hpp>
- /*
- * ERMInterpreter.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- namespace spirit = boost::spirit;
- using namespace VERMInterpreter;
- namespace ERMPrinter
- {
- //console printer
- using namespace ERM;
- struct VarPrinterVisitor : boost::static_visitor<>
- {
- void operator()(TVarExpNotMacro const& val) const
- {
- tlog2 << val.varsym;
- if(val.val.is_initialized())
- {
- tlog2 << val.val.get();
- }
- }
- void operator()(TMacroUsage const& val) const
- {
- tlog2 << "$" << val.macro << "&";
- }
- };
- void varPrinter(const TVarExp & var)
- {
- boost::apply_visitor(VarPrinterVisitor(), var);
- }
- struct IExpPrinterVisitor : boost::static_visitor<>
- {
- void operator()(int const & constant) const
- {
- tlog2 << constant;
- }
- void operator()(TVarExp const & var) const
- {
- varPrinter(var);
- }
- };
- void iexpPrinter(const TIexp & exp)
- {
- boost::apply_visitor(IExpPrinterVisitor(), exp);
- }
- struct IdentifierPrinterVisitor : boost::static_visitor<>
- {
- void operator()(TIexp const& iexp) const
- {
- iexpPrinter(iexp);
- }
- void operator()(TArithmeticOp const& arop) const
- {
- iexpPrinter(arop.lhs);
- tlog2 << " " << arop.opcode << " ";
- iexpPrinter(arop.rhs);
- }
- };
- void identifierPrinter(const boost::optional<Tidentifier> & id)
- {
- if(id.is_initialized())
- {
- tlog2 << "identifier: ";
- BOOST_FOREACH(TIdentifierInternal x, id.get())
- {
- tlog2 << "#";
- boost::apply_visitor(IdentifierPrinterVisitor(), x);
- }
- }
- }
- struct ConditionCondPrinterVisitor : boost::static_visitor<>
- {
- void operator()(TComparison const& cmp) const
- {
- iexpPrinter(cmp.lhs);
- tlog2 << " " << cmp.compSign << " ";
- iexpPrinter(cmp.rhs);
- }
- void operator()(int const& flag) const
- {
- tlog2 << "condflag " << flag;
- }
- };
- void conditionPrinter(const boost::optional<Tcondition> & cond)
- {
- if(cond.is_initialized())
- {
- Tcondition condp = cond.get();
- tlog2 << " condition: ";
- boost::apply_visitor(ConditionCondPrinterVisitor(), condp.cond);
- tlog2 << " cond type: " << condp.ctype;
- //recursive call
- if(condp.rhs.is_initialized())
- {
- tlog2 << "rhs: ";
- boost::optional<Tcondition> rhsc = condp.rhs.get().get();
- conditionPrinter(rhsc);
- }
- else
- {
- tlog2 << "no rhs; ";
- }
- }
- }
- struct BodyVarpPrinterVisitor : boost::static_visitor<>
- {
- void operator()(TVarExpNotMacro const& cmp) const
- {
- if(cmp.questionMark.is_initialized())
- {
- tlog2 << cmp.questionMark.get();
- }
- if(cmp.val.is_initialized())
- {
- tlog2 << "val:" << cmp.val.get();
- }
- tlog2 << "varsym: |" << cmp.varsym << "|";
- }
- void operator()(TQMacroUsage const& cmp) const
- {
- tlog2 << "???$$" << cmp.qmacro << "$$";
- }
- };
- struct BodyOptionItemPrinterVisitor : boost::static_visitor<>
- {
- void operator()(TVarConcatString const& cmp) const
- {
- tlog2 << "+concat\"";
- varPrinter(cmp.var);
- tlog2 << " with " << cmp.string.str;
- }
- void operator()(TStringConstant const& cmp) const
- {
- tlog2 << " \"" << cmp.str << "\" ";
- }
- void operator()(TCurriedString const& cmp) const
- {
- tlog2 << "cs: ";
- iexpPrinter(cmp.iexp);
- tlog2 << " '" << cmp.string.str << "' ";
- }
- void operator()(TSemiCompare const& cmp) const
- {
- tlog2 << cmp.compSign << "; rhs: ";
- iexpPrinter(cmp.rhs);
- }
- void operator()(TMacroUsage const& cmp) const
- {
- tlog2 << "$$" << cmp.macro << "$$";
- }
- void operator()(TMacroDef const& cmp) const
- {
- tlog2 << "@@" << cmp.macro << "@@";
- }
- void operator()(TIexp const& cmp) const
- {
- iexpPrinter(cmp);
- }
- void operator()(TVarpExp const& cmp) const
- {
- tlog2 << "varp";
- boost::apply_visitor(BodyVarpPrinterVisitor(), cmp.var);
- }
- void operator()(spirit::unused_type const& cmp) const
- {
- tlog2 << "nothing";
- }
- };
- struct BodyOptionVisitor : boost::static_visitor<>
- {
- void operator()(TVRLogic const& cmp) const
- {
- tlog2 << cmp.opcode << " ";
- iexpPrinter(cmp.var);
- }
- void operator()(TVRArithmetic const& cmp) const
- {
- tlog2 << cmp.opcode << " ";
- iexpPrinter(cmp.rhs);
- }
- void operator()(TNormalBodyOption const& cmp) const
- {
- tlog2 << cmp.optionCode << "~";
- BOOST_FOREACH(TBodyOptionItem optList, cmp.params)
- {
- boost::apply_visitor(BodyOptionItemPrinterVisitor(), optList);
- }
- }
- };
- void bodyPrinter(const Tbody & body)
- {
- tlog2 << " body items: ";
- BOOST_FOREACH(TBodyOption bi, body)
- {
- tlog2 << " (";
- apply_visitor(BodyOptionVisitor(), bi);
- tlog2 << ") ";
- }
- }
- struct CommandPrinterVisitor : boost::static_visitor<>
- {
- void operator()(Ttrigger const& trig) const
- {
- tlog2 << "trigger: " << trig.name << " ";
- identifierPrinter(trig.identifier);
- conditionPrinter(trig.condition);
- }
- void operator()(Tinstruction const& trig) const
- {
- tlog2 << "instruction: " << trig.name << " ";
- identifierPrinter(trig.identifier);
- conditionPrinter(trig.condition);
- bodyPrinter(trig.body);
- }
- void operator()(Treceiver const& trig) const
- {
- tlog2 << "receiver: " << trig.name << " ";
- identifierPrinter(trig.identifier);
- conditionPrinter(trig.condition);
- if(trig.body.is_initialized())
- bodyPrinter(trig.body.get());
- }
- void operator()(TPostTrigger const& trig) const
- {
- tlog2 << "post trigger: " << trig.name << " ";
- identifierPrinter(trig.identifier);
- conditionPrinter(trig.condition);
- }
- };
- struct LinePrinterVisitor : boost::static_visitor<>
- {
- void operator()(Tcommand const& cmd) const
- {
- CommandPrinterVisitor un;
- boost::apply_visitor(un, cmd.cmd);
- std::cout << "Line comment: " << cmd.comment << std::endl;
- }
- void operator()(std::string const& comment) const
- {
- }
- void operator()(spirit::unused_type const& nothing) const
- {
- }
- };
- void printERM(const TERMline & ast)
- {
- tlog2 << "";
- boost::apply_visitor(LinePrinterVisitor(), ast);
- }
- void printTVExp(const TVExp & exp);
- struct VOptionPrinterVisitor : boost::static_visitor<>
- {
- void operator()(TVExp const& cmd) const
- {
- printTVExp(cmd);
- }
- void operator()(TSymbol const& cmd) const
- {
- BOOST_FOREACH(TVModifier mod, cmd.symModifier)
- {
- tlog2 << mod << " ";
- }
- tlog2 << cmd.sym;
- }
- void operator()(char const& cmd) const
- {
- tlog2 << "'" << cmd << "'";
- }
- void operator()(int const& cmd) const
- {
- tlog2 << cmd;
- }
- void operator()(double const& cmd) const
- {
- tlog2 << cmd;
- }
- void operator()(TERMline const& cmd) const
- {
- printERM(cmd);
- }
- void operator()(TStringConstant const& cmd) const
- {
- tlog2 << "^" << cmd.str << "^";
- }
- };
- void printTVExp(const TVExp & exp)
- {
- BOOST_FOREACH(TVModifier mod, exp.modifier)
- {
- tlog2 << mod << " ";
- }
- tlog2 << "[ ";
- BOOST_FOREACH(TVOption opt, exp.children)
- {
- boost::apply_visitor(VOptionPrinterVisitor(), opt);
- tlog2 << " ";
- }
- tlog2 << "]";
- }
- struct TLPrinterVisitor : boost::static_visitor<>
- {
- void operator()(TVExp const& cmd) const
- {
- printTVExp(cmd);
- }
- void operator()(TERMline const& cmd) const
- {
- printERM(cmd);
- }
- };
- void printAST(const TLine & ast)
- {
- boost::apply_visitor(TLPrinterVisitor(), ast);
- tlog2 << std::endl;
- }
- }
- void ERMInterpreter::scanForScripts()
- {
- using namespace boost::filesystem;
- //parser checking
- if(!exists(DATA_DIR "/Data/s/"))
- {
- tlog3 << "Warning: Folder " DATA_DIR "/Data/s/ doesn't exist!\n";
- return;
- }
- directory_iterator enddir;
- for (directory_iterator dir(DATA_DIR "/Data/s"); dir!=enddir; dir++)
- {
- if(is_regular(dir->status()))
- {
- std::string name = dir->path().leaf();
- if( boost::algorithm::ends_with(name, ".erm") ||
- boost::algorithm::ends_with(name, ".verm") )
- {
- ERMParser ep(dir->path().string());
- FileInfo * finfo = new FileInfo;
- finfo->filename = dir->path().string();
- std::vector<ERM::TLine> buf = ep.parseFile();
- finfo->length = buf.size();
- files.push_back(finfo);
- for(int g=0; g<buf.size(); ++g)
- {
- scripts[LinePointer(finfo, g)] = buf[g];
- }
- }
- }
- }
- }
- void ERMInterpreter::printScripts( EPrintMode mode /*= EPrintMode::ALL*/ )
- {
- std::map< LinePointer, ERM::TLine >::const_iterator prevIt;
- for(std::map< LinePointer, ERM::TLine >::const_iterator it = scripts.begin(); it != scripts.end(); ++it)
- {
- if(it == scripts.begin() || it->first.file != prevIt->first.file)
- {
- tlog2 << "----------------- script " << it->first.file->filename << " ------------------\n";
- }
-
- ERMPrinter::printAST(it->second);
- prevIt = it;
- }
- }
- struct ScriptScanner : boost::static_visitor<>
- {
- ERMInterpreter * interpreter;
- LinePointer lp;
- ScriptScanner(ERMInterpreter * interpr, const LinePointer & _lp) : interpreter(interpr), lp(_lp)
- {}
- void operator()(TVExp const& cmd) const
- {
- //
- }
- void operator()(TERMline const& cmd) const
- {
- if(cmd.which() == 0) //TCommand
- {
- Tcommand tcmd = boost::get<Tcommand>(cmd);
- switch (tcmd.cmd.which())
- {
- case 0: //trigger
- {
- Trigger trig;
- trig.line = lp;
- interpreter->triggers[ TriggerType(boost::get<ERM::Ttrigger>(tcmd.cmd).name) ].push_back(trig);
- }
- break;
- case 3: //post trigger
- {
- Trigger trig;
- trig.line = lp;
- interpreter->postTriggers[ TriggerType(boost::get<ERM::TPostTrigger>(tcmd.cmd).name) ].push_back(trig);
- }
- break;
- default:
- break;
- }
- }
-
- }
- };
- void ERMInterpreter::scanScripts()
- {
- for(std::map< LinePointer, ERM::TLine >::const_iterator it = scripts.begin(); it != scripts.end(); ++it)
- {
- boost::apply_visitor(ScriptScanner(this, it->first), it->second);
- }
- }
- ERMInterpreter::ERMInterpreter()
- {
- globalEnv = new Environment();
- }
- void ERMInterpreter::executeTrigger( Trigger & trig )
- {
- //skpi the first line
- LinePointer lp = trig.line;
- ++lp;
- for(; lp.isValid(); ++lp)
- {
- ERM::TLine curLine = retrieveLine(lp);
- if(isATrigger(curLine))
- break;
- executeLine(lp);
- }
- }
- bool ERMInterpreter::isATrigger( const ERM::TLine & line )
- {
- switch(line.which())
- {
- case 0: //v-exp
- {
- TVExp vexp = boost::get<TVExp>(line);
- if(vexp.children.size() == 0)
- return false;
- switch (getExpType(vexp.children[0]))
- {
- case SYMBOL:
- {
- //TODO: what about sym modifiers?
- //TOOD: macros?
- ERM::TSymbol sym = boost::get<ERM::TSymbol>(vexp.children[0]);
- return sym.sym == triggerSymbol || sym.sym == postTriggerSymbol;
- }
- break;
- case TCMD:
- return isCMDATrigger( boost::get<ERM::Tcommand>(vexp.children[0]) );
- break;
- default:
- return false;
- break;
- }
- }
- break;
- case 1: //erm
- {
- TERMline line = boost::get<TERMline>(line);
- switch(line.which())
- {
- case 0: //tcmd
- return isCMDATrigger( boost::get<ERM::Tcommand>(line) );
- break;
- default:
- return false;
- break;
- }
- }
- break;
- default:
- assert(0); //it should never happen
- break;
- }
- assert(0);
- }
- ERM::EVOtions ERMInterpreter::getExpType( const ERM::TVOption & opt )
- {
- //MAINTENANCE: keep it correct!
- return static_cast<ERM::EVOtions>(opt.which());
- }
- bool ERMInterpreter::isCMDATrigger( const ERM::Tcommand & cmd )
- {
- switch (cmd.cmd.which())
- {
- case 0: //trigger
- case 3: //post trigger
- return true;
- break;
- default:
- return false;
- break;
- }
- }
- ERM::TLine ERMInterpreter::retrieveLine( LinePointer linePtr ) const
- {
- return *scripts.find(linePtr);
- }
- /////////
- //code execution
- struct ERMExpDispatch : boost::static_visitor<>
- {
- ERMInterpreter * owner;
- ERMExpDispatch(ERMInterpreter * _owner) : owner(_owner)
- {}
- void operator()(Ttrigger const& trig) const
- {
- throw EInterpreterError("Triggers cannot be executed!");
- }
- void operator()(Tinstruction const& trig) const
- {
- }
- void operator()(Treceiver const& trig) const
- {
- }
- void operator()(TPostTrigger const& trig) const
- {
- throw EInterpreterError("Post-triggers cannot be executed!");
- }
- };
- struct CommandExec : boost::static_visitor<>
- {
- ERMInterpreter * owner;
- CommandExec(ERMInterpreter * _owner) : owner(_owner)
- {}
- void operator()(Tcommand const& cmd) const
- {
- boost::apply_visitor(ERMExpDispatch(owner), cmd.cmd);
- std::cout << "Line comment: " << cmd.comment << std::endl;
- }
- void operator()(std::string const& comment) const
- {
- //comment - do nothing
- }
- void operator()(spirit::unused_type const& nothing) const
- {
- //nothing - do nothing
- }
- };
- struct LineExec : boost::static_visitor<>
- {
- ERMInterpreter * owner;
- LineExec(ERMInterpreter * _owner) : owner(_owner)
- {}
- void operator()(TVExp const& cmd) const
- {
- //printTVExp(cmd);
- }
- void operator()(TERMline const& cmd) const
- {
- boost::apply_visitor(CommandExec(owner), cmd);
- }
- };
- /////////
- void ERMInterpreter::executeLine( const LinePointer & lp )
- {
- boost::apply_visitor(LineExec(this), scripts[lp]);
- }
- void ERMInterpreter::init()
- {
- ermGlobalEnv = new ERMEnvironment();
- globalEnv = new Environment();
- //TODO: reset?
- }
- struct ERMExecEnvironment
- {
- ERMEnvironment * ermGlobalEnv;
- Trigger * trigEnv;
- FunctionLocalVars * funcVars;
- ERMExecEnvironment(ERMEnvironment * erm, Trigger * trig = NULL, FunctionLocalVars * funvars = NULL)
- : ermGlobalEnv(erm), trigEnv(trig), funcVars(funvars)
- {}
- IexpValStr getVar(std::string toFollow, boost::optional<int> initVal)
- {
- IexpValStr ret;
- ret.type = IexpValStr::WRONGVAL;
- int initV;
- bool hasInit = false;
- if(initVal.is_initialized())
- {
- initV = initVal.get();
- hasInit = true;
- }
- int endNum = 0;
- if(toFollow[0] == 'd')
- {
- endNum = 1;
- //TODO: support
- }
- if(toFollow.size() == 0)
- {
- if(hasInit)
- {
- ret.val.val = initV;
- ret.type = IexpValStr::INT;
- }
- else
- throw EIexpProblem("No input to getVar!");
- return ret;
- }
- //now we have at least one element in toFollow
- for(int b=toFollow.size()-1; b>=endNum; --b)
- {
- bool retIt = b == endNum+1; //if we should return the value are currently at
- char cr = toFollow[toFollow.size() - 1];
- if(cr == 'c')//write number of current day
- {
- //TODO
- }
- else if(cr == 'd') //info for external env - add i/o set
- {
- throw EIexpProblem("d inside i-expression not allowed!");
- }
- else if(cr == 'e')
- {
- if(hasInit)
- {
- if(retIt)
- {
- //these C-style cast is here just to shut up compiler errors
- if(initV > 0 && initV <= FunctionLocalVars::NUM_FLOATINGS)
- {
- if(funcVars)
- {
- ret.val.flvar = &funcVars->getFloat(initV);
- ret.type = IexpValStr::FLOATVAR;
- }
- else
- throw EIexpProblem("Function context not available!");
- }
- else if(initV < 0 && initV >= -TriggerLocalVars::EVAR_NUM)
- {
- if(trigEnv)
- {
- ret.val.flvar = &trigEnv->ermLocalVars.getEvar(initV);
- ret.type = IexpValStr::FLOATVAR;
- }
- else
- throw EIexpProblem("No trigger context available!");
- }
- else
- throw EIexpProblem("index " + boost::lexical_cast<std::string>(initV) + " not allowed for e array");
- }
- else
- throw EIexpProblem("e variables cannot appear in this context");
- }
- else
- throw EIexpProblem("e variables cannot appear in this context");
- }
- else if(cr >= 'f' && cr <= 't')
- {
- if(retIt)
- {
- ret.val.integervar = &ermGlobalEnv->getQuickVar(cr);
- ret.type = IexpValStr::INTVAR;
- }
- else
- {
- if(hasInit)
- throw EIexpProblem("quick variables cannot be used in this context");
- else
- {
- initV = ermGlobalEnv->getQuickVar(cr);
- hasInit = true;
- }
- }
- }
- else if(cr == 'v') //standard variables
- {
- if(hasInit)
- {
- if(retIt)
- {
- ret.val.integervar = &ermGlobalEnv->getStandardVar(initV);
- ret.type = IexpValStr::INTVAR;
- }
- else
- initV = ermGlobalEnv->getStandardVar(initV);
- }
- else
- throw EIexpProblem("standard variable cannot be used in this context!");
- }
- else if(cr == 'w') //local hero variables
- {
- //TODO
- }
- else if(cr == 'x') //function parameters
- {
- if(hasInit)
- {
- if(funcVars)
- {
- if(retIt)
- {
- ret.val.integervar = &funcVars->getParam(initV);
- ret.type = IexpValStr::INTVAR;
- }
- else
- initV = funcVars->getParam(initV);
- }
- else throw EIexpProblem("Function parameters cannot be used outside a function!");
- }
- else
- throw EIexpProblem("Specify which function parameter should be used");
- }
- else if(cr == 'y')
- {
- if(hasInit)
- {
- if(initV > 0 && initV <= FunctionLocalVars::NUM_LOCALS)
- {
- if(funcVars)
- {
- if(retIt)
- {
- ret.val.integervar = &funcVars->getLocal(initV);
- ret.type = IexpValStr::INTVAR;
- }
- else
- initV = funcVars->getLocal(initV);
- }
- else
- throw EIexpProblem("Function local variables cannot be used outside a function!");
- }
- else if(initV < 0 && initV >= -TriggerLocalVars::YVAR_NUM)
- {
- if(trigEnv)
- {
- if(retIt)
- {
- ret.val.integervar = &trigEnv->ermLocalVars.getYvar(initV);
- ret.type = IexpValStr::INTVAR;
- }
- else
- initV = trigEnv->ermLocalVars.getYvar(initV);
- }
- else
- throw EIexpProblem("Trigger local variables cannot be used outside triggers!");
- }
- else
- throw EIexpProblem("Wrong argument for function local variable!");
- }
- else
- throw EIexpProblem("y variable cannot be used in this context!");
- }
- else if(cr == 'z')
- {
- if(hasInit)
- {
- if(retIt)
- {
- //these C-style casts are here just to shut up compiler errors
- if(initV > 0 )
- {
- ret.val.stringvar = &ermGlobalEnv->getZVar(initV);
- ret.type = IexpValStr::STRINGVAR;
- }
- else if(initV < 0)
- {
- if(funcVars)
- {
- ret.val.stringvar = &funcVars->getString(initV);
- ret.type = IexpValStr::STRINGVAR;
- }
- else
- throw EIexpProblem("Function local string variables cannot be used outside functions!");
- }
- else
- throw EIexpProblem("Wrong parameter for string variable!");
- }
- else
- throw EIexpProblem("String variables can only be returned!");
- }
- else
- throw EIexpProblem("String variables cannot be used in this context!");
- }
- else
- {
- throw EIexpProblem(std::string("Symbol ") + cr + " is not allowed in this context!");
- }
-
- }
- return ret;
- }
- };
- namespace IexpDisemboweler
- {
- enum EDir{GET, SET};
- }
- struct LVL2IexpDisemboweler : boost::static_visitor<IexpValStr>
- {
- IexpDisemboweler::EDir dir;
- /*const*/ ERMExecEnvironment * env;
- LVL2IexpDisemboweler(/*const*/ ERMExecEnvironment * _env, IexpDisemboweler::EDir _dir)
- : env(_env), dir(_dir) //writes value to given var
- {}
- IexpValStr processNotMacro(const TVarExpNotMacro & val) const
- {
- if(val.questionMark.is_initialized())
- throw EIexpProblem("Question marks ('?') are not allowed in getter i-expressions");
- //const-cast just to do some code-reuse...
- return env->getVar(val.varsym, val.val);
- }
- IexpValStr operator()(TVarExpNotMacro const& val) const
- {
- return processNotMacro(val);
- }
- IexpValStr operator()(TMacroUsage const& val) const
- {
- std::map<std::string, ERM::TVarExpNotMacro>::const_iterator it =
- env->ermGlobalEnv->macroBindings.find(val .macro);
- if(it == env->ermGlobalEnv->macroBindings.end())
- throw EUsageOfUndefinedMacro(val.macro);
-
- return processNotMacro(it->second);
- }
- };
- struct LVL1IexpDisemboweler : boost::static_visitor<IexpValStr>
- {
- IexpDisemboweler::EDir dir;
- /*const*/ ERMExecEnvironment * env;
- LVL1IexpDisemboweler(/*const*/ ERMExecEnvironment * _env, IexpDisemboweler::EDir _dir)
- : env(_env), dir(_dir) //writes value to given var
- {}
- IexpValStr operator()(int const & constant) const
- {
- if(dir == IexpDisemboweler::GET)
- {
- IexpValStr ret;
- ret.val.val = constant;
- ret.type = IexpValStr::INT;
- }
- else
- {
- throw EIexpProblem("Cannot set a constant!");
- }
- }
- IexpValStr operator()(TVarExp const & var) const
- {
- return boost::apply_visitor(LVL2IexpDisemboweler(env, dir), var);
- }
- };
- IexpValStr ERMInterpreter::getIexp( const ERM::TIexp & iexp, /*const*/ Trigger * trig /*= NULL*/, /*const*/ FunctionLocalVars * fun /*= NULL*/) const
- {
- ERMExecEnvironment env(ermGlobalEnv, trig, fun);
- return boost::apply_visitor(LVL1IexpDisemboweler(&env, IexpDisemboweler::GET), iexp);
- }
- void ERMInterpreter::executeTriggerType( VERMInterpreter::TriggerType tt, bool pre, const std::map< int, std::vector<int> > & identifier )
- {
- TtriggerListType & triggerList = pre ? triggers : postTriggers;
- TriggerIdentifierMatch tim;
- tim.allowNoIdetifier = false;
- tim.ermEnv = this;
- tim.matchToIt = identifier;
- std::vector<Trigger> & triggersToTry = triggerList[tt];
- for(int g=0; g<triggersToTry.size(); ++g)
- {
- if(tim.tryMatch(&triggersToTry[g]))
- {
- executeTrigger(triggersToTry[g]);
- }
- }
- }
- ERM::Ttrigger ERMInterpreter::retrieveTrigger( ERM::TLine line )
- {
- if(line.which() == 1)
- {
- ERM::TERMline tl = boost::get<ERM::TERMline>(line);
- if(tl.which() == 0)
- {
- ERM::Tcommand tcm = boost::get<ERM::Tcommand>(line);
- if(tcm.cmd.which() == 0)
- {
- return boost::get<ERM::Ttrigger>(tcm.cmd);
- }
- }
- }
- throw ELineProblem("Given line is not an ERM trigger!");
- }
- template<typename T>
- bool compareExp(const T & lhs, const T & rhs, std::string op)
- {
- if(op == "<")
- {
- return lhs < rhs;
- }
- else if(op == ">")
- {
- return lhs > rhs;
- }
- else if(op == ">=" || op == "=>")
- {
- return lhs >= rhs;
- }
- else if(op == "<=" || op == "=<")
- {
- return lhs <= rhs;
- }
- else if(op == "==")
- {
- return lhs == rhs;
- }
- else if(op == "<>" || op == "><")
- {
- return lhs != rhs;
- }
- else
- throw EScriptExecError(std::string("Wrong comparison sign: ") + op);
- }
- struct ConditionDisemboweler : boost::static_visitor<bool>
- {
- ConditionDisemboweler(ERMInterpreter * _ei) : ei(_ei)
- {}
- bool operator()(TComparison const & cmp) const
- {
- IexpValStr lhs = ei->getIexp(cmp.lhs),
- rhs = ei->getIexp(cmp.rhs);
- switch (lhs.type)
- {
- case IexpValStr::FLOATVAR:
- switch (rhs.type)
- {
- case IexpValStr::FLOATVAR:
- return compareExp(*lhs.val.flvar, *rhs.val.flvar, cmp.compSign);
- break;
- default:
- throw EScriptExecError("Incompatible types for comparison");
- }
- break;
- case IexpValStr::INT:
- switch (rhs.type)
- {
- case IexpValStr::INT:
- return compareExp(lhs.val.val, rhs.val.val, cmp.compSign);
- break;
- case IexpValStr::INTVAR:
- return compareExp(lhs.val.val, *rhs.val.integervar, cmp.compSign);
- default:
- throw EScriptExecError("Incompatible types for comparison");
- }
- break;
- case IexpValStr::INTVAR:
- switch (rhs.type)
- {
- case IexpValStr::INT:
- return compareExp(*lhs.val.integervar, rhs.val.val, cmp.compSign);
- break;
- case IexpValStr::INTVAR:
- return compareExp(*lhs.val.integervar, *rhs.val.integervar, cmp.compSign);
- default:
- throw EScriptExecError("Incompatible types for comparison");
- }
- break;
- case IexpValStr::STRINGVAR:
- switch (rhs.type)
- {
- case IexpValStr::STRINGVAR:
- return compareExp(*lhs.val.stringvar, *rhs.val.stringvar, cmp.compSign);
- break;
- default:
- throw EScriptExecError("Incompatible types for comparison");
- }
- break;
- default:
- throw EScriptExecError("Wrong type of left iexp!");
- }
- //we should never reach this place
- }
- bool operator()(int const & flag) const
- {
- return ei->ermGlobalEnv->getFlag(flag);
- }
- private:
- ERMInterpreter * ei;
- };
- bool ERMInterpreter::checkCondition( ERM::Tcondition cond )
- {
- bool ret = boost::apply_visitor(ConditionDisemboweler(this), cond.cond);
- if(cond.rhs.is_initialized())
- { //taking care of rhs expression
- bool rhs = checkCondition(cond.rhs.get().get());
- switch (cond.ctype)
- {
- case '&':
- ret &= rhs;
- break;
- case '|':
- ret |= rhs;
- break;
- case 'X':
- ret ^= rhs;
- break;
- default:
- throw EInterpreterProblem(std::string("Strange - wrong condition connection (") + cond.ctype + ") !");
- break;
- }
- }
- return ret;
- }
- const std::string ERMInterpreter::triggerSymbol = "trigger";
- const std::string ERMInterpreter::postTriggerSymbol = "postTrigger";
- const std::string ERMInterpreter::defunSymbol = "defun";
- struct TriggerIdMatchHelper : boost::static_visitor<>
- {
- int & ret;
- ERMInterpreter * interpreter;
- Trigger * trig;
- TriggerIdMatchHelper(int & b, ERMInterpreter * ermint, Trigger * _trig)
- : ret(b), interpreter(ermint), trig(_trig)
- {}
- void operator()(TIexp const& iexp) const
- {
- IexpValStr val = interpreter->getIexp(iexp, trig);
- switch(val.type)
- {
- case IexpValStr::INT:
- ret = val.val.val;
- case IexpValStr::INTVAR:
- ret = *val.val.integervar;
- default:
- throw EScriptExecError("Incompatible i-exp type!");
- }
- }
- void operator()(TArithmeticOp const& arop) const
- {
- //error?!?
- }
- };
- bool TriggerIdentifierMatch::tryMatch( Trigger * interptrig ) const
- {
- bool ret = true;
- const ERM::Ttrigger & trig = ERMInterpreter::retrieveTrigger(ermEnv->retrieveLine(interptrig->line));
- if(trig.identifier.is_initialized())
- {
- ERM::Tidentifier tid = trig.identifier.get();
- std::map< int, std::vector<int> >::const_iterator it = matchToIt.find(tid.size());
- if(it == matchToIt.end())
- ret = false;
- else
- {
- const std::vector<int> & pattern = it->second;
- for(int g=0; g<pattern.size(); ++g)
- {
- int val = -1;
- boost::apply_visitor(TriggerIdMatchHelper(val, ermEnv, interptrig), tid[g]);
- if(pattern[g] != val)
- {
- ret = false;
- }
- }
- ret = true;
- }
- }
- else
- {
- ret = allowNoIdetifier;
- }
- //check condition
- if(ret)
- {
- if(trig.condition.is_initialized())
- {
- return ermEnv->checkCondition(trig.condition.get());
- }
- else //no condition
- return true;
- }
- else
- return false;
- }
- VERMInterpreter::ERMEnvironment::ERMEnvironment()
- {
- for(int g=0; g<NUM_QUICKS; ++g)
- quickVars[g] = 0;
- for(int g=0; g<NUM_STANDARDS; ++g)
- standardVars[g] = 0;
- //string should be automatically initialized to ""
- for(int g=0; g<NUM_FLAGS; ++g)
- flags[g] = false;
- }
- int & VERMInterpreter::ERMEnvironment::getQuickVar( const char letter )
- {
- assert(letter >= 'f' && letter <= 't'); //it should be check by another function, just making sure here
- return quickVars[letter - 'f'];
- }
- int & VERMInterpreter::ERMEnvironment::getStandardVar( int num )
- {
- if(num < 1 || num > NUM_STANDARDS)
- throw EScriptExecError("Number of standard variable out of bounds");
- return standardVars[num-1];
- }
- std::string & VERMInterpreter::ERMEnvironment::getZVar( int num )
- {
- if(num < 1 || num > NUM_STRINGS)
- throw EScriptExecError("Number of string variable out of bounds");
- return strings[num-1];
- }
- bool & VERMInterpreter::ERMEnvironment::getFlag( int num )
- {
- if(num < 1 || num > NUM_FLAGS)
- throw EScriptExecError("Number of flag out of bounds");
- return flags[num-1];
- }
- VERMInterpreter::TriggerLocalVars::TriggerLocalVars()
- {
- for(int g=0; g<EVAR_NUM; ++g)
- evar[g] = 0.0;
- for(int g=0; g<YVAR_NUM; ++g)
- yvar[g] = 0;
- }
- double & VERMInterpreter::TriggerLocalVars::getEvar( int num )
- {
- num = -num;
- if(num < 1 || num > EVAR_NUM)
- throw EScriptExecError("Number of trigger local floating point variable out of bounds");
- return evar[num-1];
- }
- int & VERMInterpreter::TriggerLocalVars::getYvar( int num )
- {
- if(num < 1 || num > YVAR_NUM)
- throw EScriptExecError("Number of trigger local variable out of bounds");
- return yvar[num-1];
- }
- bool VERMInterpreter::Environment::isBound( const std::string & name, bool globalOnly ) const
- {
- std::map<std::string, TVOption>::const_iterator it = symbols.find(name);
- if(globalOnly && parent)
- {
- return parent->isBound(name, globalOnly);
- }
- //we have it; if globalOnly is true, lexical parent is false here so we are global env
- if(it != symbols.end())
- return true;
- //here, we don;t have it; but parent can have
- if(parent)
- return parent->isBound(name, globalOnly);
- return false;
- }
- ERM::TVOption VERMInterpreter::Environment::retrieveValue( const std::string & name ) const
- {
- std::map<std::string, TVOption>::const_iterator it = symbols.find(name);
- if(it == symbols.end())
- {
- if(parent)
- {
- return parent->retrieveValue(name);
- }
- throw ESymbolNotFound(name);
- }
- return it->second;
- }
- bool VERMInterpreter::Environment::unbind( const std::string & name, EUnbindMode mode )
- {
- if(isBound(name, false))
- {
- if(symbols.find(name) != symbols.end()) //result of isBound could be from higher lexical env
- symbols.erase(symbols.find(name));
- if(mode == FULLY_RECURSIVE && parent)
- parent->unbind(name, mode);
- return true;
- }
- if(parent && (mode == RECURSIVE_UNTIL_HIT || mode == FULLY_RECURSIVE))
- return parent->unbind(name, mode);
- //neither bound nor have lexical parent
- return false;
- }
- int & VERMInterpreter::FunctionLocalVars::getParam( int num )
- {
- if(num < 1 || num > NUM_PARAMETERS)
- throw EScriptExecError("Number of parameter out of bounds");
- return params[num-1];
- }
- int & VERMInterpreter::FunctionLocalVars::getLocal( int num )
- {
- if(num < 1 || num > NUM_LOCALS)
- throw EScriptExecError("Number of local variable out of bounds");
- return locals[num-1];
- }
- std::string & VERMInterpreter::FunctionLocalVars::getString( int num )
- {
- num = -num; //we deal with negative indices
- if(num < 1 || num > NUM_PARAMETERS)
- throw EScriptExecError("Number of function local string variable out of bounds");
- return strings[num-1];
- }
- double & VERMInterpreter::FunctionLocalVars::getFloat( int num )
- {
- if(num < 1 || num > NUM_FLOATINGS)
- throw EScriptExecError("Number of float var out of bounds");
- return floats[num-1];
- }
|