1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/scripting/erm/ERMInterpreter.h

319 lines
7.7 KiB
C++
Raw Normal View History

/*
* ERMInterpreter.h, 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
*
*/
#pragma once
#include "ERMParser.h"
#include "ERMScriptModule.h"
class ERMInterpreter;
2011-04-10 19:39:34 +03:00
namespace VERMInterpreter
{
using namespace ERM;
//different exceptions that can be thrown during interpreting
class EInterpreterProblem : public std::exception
{
std::string problem;
public:
2013-06-26 17:25:23 +03:00
const char * what() const throw() override
2011-04-10 19:39:34 +03:00
{
return problem.c_str();
}
2011-04-16 20:39:38 +03:00
~EInterpreterProblem() throw()
{}
2011-04-10 19:39:34 +03:00
EInterpreterProblem(const std::string & problemDesc) : problem(problemDesc)
{}
};
struct ESymbolNotFound : public EInterpreterProblem
{
ESymbolNotFound(const std::string & sym) :
EInterpreterProblem(std::string("Symbol \"") + sym + std::string("\" not found!"))
{}
};
struct EInvalidTrigger : public EInterpreterProblem
{
EInvalidTrigger(const std::string & sym) :
EInterpreterProblem(std::string("Trigger \"") + sym + std::string("\" is invalid!"))
{}
};
2011-04-16 20:39:38 +03:00
struct EUsageOfUndefinedMacro : public EInterpreterProblem
{
EUsageOfUndefinedMacro(const std::string & macro) :
EInterpreterProblem(std::string("Macro ") + macro + " is undefined")
{}
};
struct EIexpProblem : public EInterpreterProblem
2011-04-16 20:39:38 +03:00
{
EIexpProblem(const std::string & desc) :
2011-04-16 20:39:38 +03:00
EInterpreterProblem(desc)
{}
};
2011-04-21 23:06:42 +03:00
struct ELineProblem : public EInterpreterProblem
{
ELineProblem(const std::string & desc) :
EInterpreterProblem(desc)
{}
};
2011-04-22 23:33:34 +03:00
struct EExecutionError : public EInterpreterProblem
{
EExecutionError(const std::string & desc) :
EInterpreterProblem(desc)
{}
};
//internal interpreter error related to execution
struct EInterpreterError : public EExecutionError
{
EInterpreterError(const std::string & desc) :
EExecutionError(desc)
{}
};
//wrong script
struct EScriptExecError : public EExecutionError
{
EScriptExecError(const std::string & desc) :
EExecutionError(desc)
{}
};
2011-06-04 21:16:32 +03:00
//wrong script
struct EVermScriptExecError : public EScriptExecError
2011-04-10 19:39:34 +03:00
{
2011-06-04 21:16:32 +03:00
EVermScriptExecError(const std::string & desc) :
EScriptExecError(desc)
{}
2011-04-10 19:39:34 +03:00
};
// All numeric variables are integer variables and have a range of -2147483647...+2147483647
// c stores game active day number //indirect variable
// d current value //not an actual variable but a modifier
// e1..e100 Function floating point variables //local
// e-1..e-100 Trigger local floating point variables //local
// 'f'..'t' Standard variables ('quick variables') //global
// v1..v1000 Standard variables //global
// w1..w100 Hero variables
// w101..w200 Hero variables
// x1..x16 Function parameters //local
// y1..y100 Function local variables //local
// y-1..y-100 Trigger-based local integer variables //local
// z1..z1000 String variables //global
// z-1..z-10 Function local string variables //local
struct TriggerType
{
//the same order of trigger types in this enum and in validTriggers array is obligatory!
enum ETrigType
{
AE, BA, BF, BG, BR, CM, CO, FU, GE, GM, HE, HL, HM, IP, LE, MF, MG, MM, MR, MW, OB, PI, SN, TH, TM
};
ETrigType type;
2011-04-10 19:39:34 +03:00
static ETrigType convertTrigger(const std::string & trig)
{
static const std::string validTriggers[] =
{
"AE", "BA", "BF", "BG", "BR", "CM", "CO", "FU",
2011-04-10 19:39:34 +03:00
"GE", "GM", "HE", "HL", "HM", "IP", "LE", "MF", "MG", "MM", "MR", "MW", "OB", "PI", "SN",
"TH", "TM"
};
2011-04-10 19:39:34 +03:00
for(int i=0; i<std::size(validTriggers); ++i)
2011-04-10 19:39:34 +03:00
{
if(validTriggers[i] == trig)
return static_cast<ETrigType>(i);
}
throw EInvalidTrigger(trig);
}
2011-04-16 20:39:38 +03:00
bool operator<(const TriggerType & t2) const
{
return type < t2.type;
}
2011-04-10 19:39:34 +03:00
TriggerType(const std::string & sym)
{
type = convertTrigger(sym);
}
};
struct LinePointer
{
int lineNum;
2011-05-11 22:53:55 +03:00
int realLineNum;
int fileLength;
2011-05-11 22:53:55 +03:00
LinePointer()
: fileLength(-1)
2011-04-16 20:39:38 +03:00
{}
LinePointer(int _fileLength, int line, int _realLineNum)
: fileLength(_fileLength),
lineNum(line),
2011-05-11 22:53:55 +03:00
realLineNum(_realLineNum)
2011-04-10 19:39:34 +03:00
{}
bool operator<(const LinePointer & rhs) const
{
return lineNum < rhs.lineNum;
}
bool operator!=(const LinePointer & rhs) const
{
return lineNum != rhs.lineNum;
2011-04-10 19:39:34 +03:00
}
LinePointer & operator++()
{
++lineNum;
return *this;
}
bool isValid() const
{
return fileLength > 0 && lineNum < fileLength;
2011-04-10 19:39:34 +03:00
}
};
struct Trigger
{
LinePointer line;
Trigger()
2011-04-16 20:39:38 +03:00
{}
2011-04-10 19:39:34 +03:00
};
2011-05-29 21:16:49 +03:00
//verm goodies
struct VSymbol
{
std::string text;
VSymbol(const std::string & txt) : text(txt)
{}
};
2011-06-04 21:16:32 +03:00
struct VNode;
struct VOptionList;
2011-05-29 21:16:49 +03:00
struct VNIL
{};
2011-06-04 21:16:32 +03:00
2023-04-15 03:33:00 +02:00
typedef std::variant<char, double, int, std::string> TLiteral;
2011-06-04 21:16:32 +03:00
2023-04-15 03:33:00 +02:00
typedef std::variant<VNIL, boost::recursive_wrapper<VNode>, VSymbol, TLiteral, ERM::Tcommand> VOption; //options in v-expression, VNIl should be the default
2011-05-29 21:16:49 +03:00
2011-06-04 21:16:32 +03:00
template<typename T, typename SecType>
T& getAs(SecType & opt)
{
if(opt.type() == typeid(T))
2023-04-15 03:33:00 +02:00
return std::get<T>(opt);
2011-06-04 21:16:32 +03:00
else
throw EVermScriptExecError("Wrong type!");
}
template<typename T, typename SecType>
2011-06-10 15:49:31 +03:00
bool isA(const SecType & opt)
2011-06-04 21:16:32 +03:00
{
if(opt.type() == typeid(T))
return true;
else
return false;
}
2011-05-29 21:16:49 +03:00
struct VermTreeIterator
{
private:
friend struct VOptionList;
VOptionList * parent;
enum Estate {NORM, CAR} state;
2011-06-04 21:16:32 +03:00
int basePos; //car/cdr offset
2011-05-29 21:16:49 +03:00
public:
2011-06-15 04:36:36 +03:00
VermTreeIterator(VOptionList & _parent) : parent(&_parent), state(NORM), basePos(0)
{}
2013-06-26 17:25:23 +03:00
VermTreeIterator() : parent(nullptr), state(NORM)
2011-05-29 21:16:49 +03:00
{}
VermTreeIterator & operator=(const VOption & opt);
VermTreeIterator & operator=(const std::vector<VOption> & opt);
VermTreeIterator & operator=(const VOptionList & opt);
2011-06-04 21:16:32 +03:00
VOption & getAsItem();
VOptionList getAsList();
2011-06-04 21:16:32 +03:00
size_t size() const;
VermTreeIterator& operator=(const VermTreeIterator & rhs)
{
if(this == &rhs)
{
return *this;
}
parent = rhs.parent;
state = rhs.state;
basePos = rhs.basePos;
return *this;
}
2011-05-29 21:16:49 +03:00
};
2011-06-04 21:16:32 +03:00
struct VOptionList : public std::vector<VOption>
2011-05-29 21:16:49 +03:00
{
2011-06-04 21:16:32 +03:00
private:
friend struct VermTreeIterator;
public:
VermTreeIterator car();
VermTreeIterator cdr();
};
2011-05-29 21:16:49 +03:00
struct VNode
{
private:
void processModifierList(const std::vector<TVModifier> & modifierList, bool asSymbol);
2011-05-29 21:16:49 +03:00
public:
VOptionList children;
VNode( const ERM::TVExp & exp);
VNode( const VOptionList & cdren );
VNode( const ERM::TSymbol & sym ); //only in case sym has modifiers!
VNode( const VOption & first, const VOptionList & rest); //merges given arguments into [a, rest];
void setVnode( const VOption & first, const VOptionList & rest);
};
2011-04-10 19:39:34 +03:00
}
class ERMInterpreter
{
/*not so*/ public:
2011-04-10 19:39:34 +03:00
std::map<VERMInterpreter::LinePointer, ERM::TLine> scripts;
2011-04-21 23:06:42 +03:00
typedef std::map<VERMInterpreter::TriggerType, std::vector<VERMInterpreter::Trigger> > TtriggerListType;
TtriggerListType triggers;
TtriggerListType postTriggers;
std::vector<VERMInterpreter::LinePointer> instructions;
2011-04-10 19:39:34 +03:00
static bool isCMDATrigger(const ERM::Tcommand & cmd);
static bool isATrigger(const ERM::TLine & line);
static ERM::EVOtions getExpType(const ERM::TVOption & opt);
ERM::TLine & retrieveLine(const VERMInterpreter::LinePointer & linePtr);
static ERM::TTriggerBase & retrieveTrigger(ERM::TLine & line);
public:
vstd::CLoggerBase * logger;
ERMInterpreter(vstd::CLoggerBase * logger_);
virtual ~ERMInterpreter();
std::string loadScript(const std::string & name, const std::string & source);
2011-04-11 01:06:05 +03:00
};