1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-06 09:09:40 +02:00

- Compile fix for editor - Renamed /Editor to /editor and Scripting/ERM to scripting/erm - Removed unused ipch folder and format file - Removed ancient lua folder

This commit is contained in:
beegee1
2013-04-13 11:07:32 +00:00
parent 2d89e643c6
commit b1428bcd24
31 changed files with 29 additions and 93 deletions

View File

@@ -0,0 +1,15 @@
project(vcmiERM)
cmake_minimum_required(VERSION 2.6)
include_directories(${CMAKE_HOME_DIRECTORY} ${CMAKE_CURRENT_SOURCE_DIRECTORY})
set(lib_SRCS
ERMInterpreter.cpp
ERMParser.cpp
ERMScriptModule.cpp
)
add_library(vcmiERM SHARED ${lib_SRCS})
target_link_libraries(vcmiERM ${Boost_LIBRARIES})
install(TARGETS vcmiERM DESTINATION ${SCRIPTING_LIB_DIR})

80
scripting/erm/ERM.cbp Normal file
View File

@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="ERM" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="../ERM" imp_lib="$(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).a" def_file="$(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).def" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="3" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O" />
<Add option="-ggdb" />
</Compiler>
</Target>
<Target title="Release">
<Option output="../ERM" imp_lib="$(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).a" def_file="$(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).def" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="3" />
<Option compiler="gcc" />
<Compiler>
<Add option="-fomit-frame-pointer" />
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wextra" />
<Add option="-Wall" />
<Add option="-fexceptions" />
<Add option="-Wpointer-arith" />
<Add option="-Wno-switch" />
<Add option="-Wno-sign-compare" />
<Add option="-Wno-unused-parameter" />
<Add option="-Wno-overloaded-virtual" />
<Add directory="$(#boost.include)" />
</Compiler>
<Linker>
<Add option="-lboost_program_options$(#boost.libsuffix)" />
<Add option="-lboost_filesystem$(#boost.libsuffix)" />
<Add option="-lboost_system$(#boost.libsuffix)" />
<Add option="-lboost_thread$(#boost.libsuffix)" />
<Add option="-lboost_chrono$(#boost.libsuffix)" />
<Add option="-lVCMI_lib" />
<Add directory="$(#boost.lib)" />
<Add directory="../.." />
</Linker>
<Unit filename="ERMInterpreter.cpp" />
<Unit filename="ERMInterpreter.h" />
<Unit filename="ERMParser.cpp" />
<Unit filename="ERMParser.h" />
<Unit filename="ERMScriptModule.cpp" />
<Unit filename="ERMScriptModule.h" />
<Unit filename="StdInc.h">
<Option compile="1" />
<Option weight="0" />
</Unit>
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
<DoxyBlocks>
<comment_style block="0" line="0" />
<doxyfile_project />
<doxyfile_build />
<doxyfile_warnings />
<doxyfile_output />
<doxyfile_dot />
<general />
</DoxyBlocks>
</Extensions>
</Project>
</CodeBlocks_project_file>

144
scripting/erm/ERM.vcxproj Normal file
View File

@@ -0,0 +1,144 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="RD|Win32">
<Configuration>RD</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="RD|x64">
<Configuration>RD</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{8F202F43-106D-4F63-AD9D-B1D43E803E8C}</ProjectGuid>
<RootNamespace>ERM</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v110</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v110</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='RD|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v110_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='RD|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v110_xp</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\..\VCMI_global_debug.props" />
<Import Project="..\..\VCMI_global.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\..\VCMI_global_debug.props" />
<Import Project="..\..\VCMI_global.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='RD|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\..\VCMI_global_release.props" />
<Import Project="..\..\VCMI_global.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='RD|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\..\VCMI_global_release.props" />
<Import Project="..\..\VCMI_global.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<OutDir>$(VCMI_Out)\scripting\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(VCMI_Out)\scripting\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='RD|Win32'">
<OutDir>$(VCMI_Out)\scripting\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='RD|x64'">
<OutDir>$(VCMI_Out)\scripting\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>StdInc.h</PrecompiledHeaderFile>
<AdditionalOptions>/Zm218 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<AdditionalDependencies>VCMI_lib.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='RD|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<AdditionalOptions>/Zm218 %(AdditionalOptions)</AdditionalOptions>
<PrecompiledHeaderFile>StdInc.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<AdditionalDependencies>VCMI_lib.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<AdditionalOptions>/Zm218 %(AdditionalOptions)</AdditionalOptions>
<PrecompiledHeaderFile>StdInc.h</PrecompiledHeaderFile>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='RD|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<AdditionalOptions>/Zm218 %(AdditionalOptions)</AdditionalOptions>
<PrecompiledHeaderFile>StdInc.h</PrecompiledHeaderFile>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\..\Global.h" />
<ClInclude Include="ERMInterpreter.h" />
<ClInclude Include="ERMParser.h" />
<ClInclude Include="ERMScriptModule.h" />
<ClInclude Include="StdInc.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="ERMInterpreter.cpp" />
<ClCompile Include="ERMParser.cpp" />
<ClCompile Include="ERMScriptModule.cpp" />
<ClCompile Include="StdInc.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeaderFile Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">StdInc.h</PrecompiledHeaderFile>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='RD|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='RD|x64'">Create</PrecompiledHeader>
</ClCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,861 @@
#pragma once
#include "ERMParser.h"
#include "ERMScriptModule.h"
/*
* 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
*
*/
namespace VERMInterpreter
{
using namespace ERM;
//different exceptions that can be thrown during interpreting
class EInterpreterProblem : public std::exception
{
std::string problem;
public:
const char * what() const throw() OVERRIDE
{
return problem.c_str();
}
~EInterpreterProblem() throw()
{}
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!"))
{}
};
struct EUsageOfUndefinedMacro : public EInterpreterProblem
{
EUsageOfUndefinedMacro(const std::string & macro) :
EInterpreterProblem(std::string("Macro ") + macro + " is undefined")
{}
};
struct EIexpProblem : public EInterpreterProblem
{
EIexpProblem(const std::string & desc) :
EInterpreterProblem(desc)
{}
};
struct ELineProblem : public EInterpreterProblem
{
ELineProblem(const std::string & desc) :
EInterpreterProblem(desc)
{}
};
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)
{}
};
//wrong script
struct EVermScriptExecError : public EScriptExecError
{
EVermScriptExecError(const std::string & desc) :
EScriptExecError(desc)
{}
};
// 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 TriggerLocalVars
{
static const int EVAR_NUM = 100; //number of evar locals
static const int YVAR_NUM = 100; //number of yvar locals
TriggerLocalVars();
double & getEvar(int num);
int & getYvar(int num);
private:
double evar[EVAR_NUM]; //negative indices
int yvar[YVAR_NUM];
};
struct FunctionLocalVars
{
static const int NUM_PARAMETERS = 16; //number of function parameters
static const int NUM_LOCALS = 100;
static const int NUM_STRINGS = 10;
static const int NUM_FLOATINGS = 100;
int & getParam(int num);
int & getLocal(int num);
std::string & getString(int num);
double & getFloat(int num);
void reset();
private:
int params[NUM_PARAMETERS]; //x-vars
int locals[NUM_LOCALS]; //y-vars
std::string strings[NUM_STRINGS]; //z-vars (negative indices)
double floats[NUM_FLOATINGS]; //e-vars (positive indices)
};
struct ERMEnvironment
{
ERMEnvironment();
static const int NUM_QUICKS = 't' - 'f' + 1; //it should be 15
int & getQuickVar(const char letter); //'f' - 't' variables
int & getStandardVar(int num); //get v-variable
std::string & getZVar(int num);
bool & getFlag(int num);
static const int NUM_STANDARDS = 10000;
static const int NUM_STRINGS = 1000;
std::map<std::string, ERM::TVarExpNotMacro> macroBindings;
static const int NUM_FLAGS = 1000;
private:
int quickVars[NUM_QUICKS]; //referenced by letter ('f' to 't' inclusive)
int standardVars[NUM_STANDARDS]; //v-vars
std::string strings[NUM_STRINGS]; //z-vars (positive indices)
bool flags[NUM_FLAGS];
};
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} type;
static ETrigType convertTrigger(const std::string & trig)
{
static const std::string validTriggers[] = {"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"};
for(int i=0; i<ARRAY_COUNT(validTriggers); ++i)
{
if(validTriggers[i] == trig)
return static_cast<ETrigType>(i);
}
throw EInvalidTrigger(trig);
}
bool operator<(const TriggerType & t2) const
{
return type < t2.type;
}
TriggerType(const std::string & sym)
{
type = convertTrigger(sym);
}
};
struct FileInfo
{
std::string filename;
int length;
};
struct LinePointer
{
const FileInfo * file; //non-owning
int lineNum;
int realLineNum;
LinePointer() : file(NULL)
{}
LinePointer(const FileInfo * finfo, int line, int _realLineNum) : file(finfo), lineNum(line),
realLineNum(_realLineNum)
{}
//lexicographical order
bool operator<(const LinePointer & rhs) const
{
if(file->filename != rhs.file->filename)
return file->filename < rhs.file->filename;
return lineNum < rhs.lineNum;
}
bool operator!=(const LinePointer & rhs) const
{
return file->filename != rhs.file->filename || lineNum != rhs.lineNum;
}
LinePointer & operator++()
{
++lineNum;
return *this;
}
bool isValid() const
{
return file && lineNum < file->length;
}
};
struct LexicalPtr
{
LinePointer line; //where to start
std::vector<int> entryPoints; //defines how to pass to current location
bool operator<(const LexicalPtr & sec) const
{
if(line != sec.line)
return line < sec.line;
if(entryPoints.size() != sec.entryPoints.size())
return entryPoints.size() < sec.entryPoints.size();
for(int g=0; g<entryPoints.size(); ++g)
{
if(entryPoints[g] < sec.entryPoints[g])
return true;
}
return false;
}
};
//call stack, represents dynamic range
struct Stack
{
std::vector<LexicalPtr> stack;
};
struct Trigger
{
LinePointer line;
TriggerLocalVars ermLocalVars;
Stack * stack; //where we are stuck at execution
Trigger() : stack(NULL)
{}
};
//verm goodies
struct VSymbol
{
std::string text;
VSymbol(const std::string & txt) : text(txt)
{}
};
struct VNode;
struct VOptionList;
struct VNIL
{};
typedef boost::variant<char, double, int, std::string> TLiteral;
//for operator <, but this one seems to be implemented in boost alerady
struct _opLTvis : boost::static_visitor<bool>
{
const TLiteral & lhs;
_opLTvis(const TLiteral & _lhs) : lhs(_lhs)
{}
template<typename OP>
bool operator()(OP const & rhs) const
{
return boost::get<OP>(lhs) < rhs;
}
};
// bool operator<(const TLiteral & t1, const TLiteral & t2)
// {
// if(t1.type() == t2.type())
// {
// return boost::apply_visitor(_opLTvis(t1), t2);
// }
// throw EVermScriptExecError("These types are incomparable!");
// }
//for operator <=
struct _opLEvis : boost::static_visitor<bool>
{
const TLiteral & lhs;
_opLEvis(const TLiteral & _lhs) : lhs(_lhs)
{}
template<typename OP>
bool operator()(OP const & rhs) const
{
return boost::get<OP>(lhs) <= rhs;
}
};
bool operator<=(const TLiteral & t1, const TLiteral & t2);
//operator >
struct _opGTvis : boost::static_visitor<bool>
{
const TLiteral & lhs;
_opGTvis(const TLiteral & _lhs) : lhs(_lhs)
{}
template<typename OP>
bool operator()(OP const & rhs) const
{
return boost::get<OP>(lhs) > rhs;
}
};
bool operator>(const TLiteral & t1, const TLiteral & t2);
//operator >=
struct _opGEvis : boost::static_visitor<bool>
{
const TLiteral & lhs;
_opGEvis(const TLiteral & _lhs) : lhs(_lhs)
{}
template<typename OP>
bool operator()(OP const & rhs) const
{
return boost::get<OP>(lhs) >= rhs;
}
};
bool operator>=(const TLiteral & t1, const TLiteral & t2);
//operator =
struct _opEQvis : boost::static_visitor<bool>
{
const TLiteral & lhs;
_opEQvis(const TLiteral & _lhs) : lhs(_lhs)
{}
template<typename OP>
bool operator()(OP const & rhs) const
{
return boost::get<OP>(lhs) == rhs;
}
};
//VFunc
struct VFunc;
//VOption & stuff
typedef boost::variant<VNIL, boost::recursive_wrapper<VNode>, VSymbol, TLiteral, ERM::Tcommand, boost::recursive_wrapper<VFunc> > VOption; //options in v-expression, VNIl should be the default
template<typename T, typename SecType>
T& getAs(SecType & opt)
{
if(opt.type() == typeid(T))
return boost::get<T>(opt);
else
throw EVermScriptExecError("Wrong type!");
}
template<typename T, typename SecType>
bool isA(const SecType & opt)
{
if(opt.type() == typeid(T))
return true;
else
return false;
}
//why it doesn't work?
// template<typename TBasicVariant>
// struct IntVarinant : public TBasicVariant
// {
// template<typename T>
// bool isA() const
// {
// return type() == typeid(T);
// }
// template<typename T>
// T getAs()
// {
// if(isA<T>())
// return boost::get<T>(*this);
// else
// throw EVermScriptExecError("Getting improved variant with wrongly specified type");
// }
//
// IntVarinant(const VNode & val) : TBasicVariant(val)
// {}
// IntVarinant(const VNIL & val) : TBasicVariant(val)
// {}
// IntVarinant(const TLiteral & val) : TBasicVariant(val)
// {}
// IntVarinant(const VSymbol & val) : TBasicVariant(val)
// {}
// IntVarinant(const int & val) : TBasicVariant(val)
// {}
// IntVarinant(const char & val) : TBasicVariant(val)
// {}
// IntVarinant(const double & val) : TBasicVariant(val)
// {}
// IntVarinant(const ERM::Tcommand & val) : TBasicVariant(val)
// {}
// TBasicVariant & getAsPlaintVariant()
// {
// return *this;
// }
//
// IntVarinant()
// {}
// };
///main environment class, manages symbols
class Environment
{
private:
std::map<std::string, VOption> symbols;
Environment * parent;
public:
Environment() : parent(NULL)
{}
void setPatent(Environment * _parent);
Environment * getPatent() const;
enum EIsBoundMode {GLOBAL_ONLY, LOCAL_ONLY, ANYWHERE};
bool isBound(const std::string & name, EIsBoundMode mode) const;
VOption & retrieveValue(const std::string & name);
enum EUnbindMode{LOCAL, RECURSIVE_UNTIL_HIT, FULLY_RECURSIVE};
///returns true if symbol was really unbound
bool unbind(const std::string & name, EUnbindMode mode);
void localBind(std::string name, const VOption & sym);
void bindAtFirstHit(std::string name, const VOption & sym); //if symbol is locally defines, it gets overwritten; otherwise it is bind globally
};
//this class just introduces a new dynamic range when instantiated, nothing more
class IntroduceDynamicEnv
{
public:
IntroduceDynamicEnv();
~IntroduceDynamicEnv();
};
struct VermTreeIterator
{
private:
friend struct VOptionList;
VOptionList * parent;
enum Estate {NORM, CAR} state;
int basePos; //car/cdr offset
public:
VermTreeIterator(VOptionList & _parent) : parent(&_parent), state(NORM), basePos(0)
{}
VermTreeIterator() : parent(NULL), state(NORM)
{}
VermTreeIterator & operator=(const VOption & opt);
VermTreeIterator & operator=(const std::vector<VOption> & opt);
VermTreeIterator & operator=(const VOptionList & opt);
VOption & getAsItem();
VermTreeIterator getAsCDR();
VOptionList getAsList();
VOption & getIth(int i);
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;
}
};
struct VOptionList : public std::vector<VOption>
{
private:
friend struct VermTreeIterator;
public:
VermTreeIterator car();
VermTreeIterator cdr();
bool isNil() const;
};
struct VFunc
{
enum Eopt {DEFAULT, LT, GT, LE, GE, EQ, ADD, SUB, MULT, DIV, MOD} option;
std::vector<VSymbol> args;
VOptionList body;
bool macro; //true - act as macro, false - act as function
VFunc(const VOptionList & _body, bool asMacro = false) : option(DEFAULT), body(_body), macro(asMacro)
{}
VFunc(Eopt func) : option(func), macro(false)
{}
VFunc& operator=(const VFunc & rhs)
{
if(this == &rhs)
{
return *this;
}
args = rhs.args;
body = rhs.body;
return *this;
}
VOption operator()(VermTreeIterator params);
};
struct OptionConverterVisitor : boost::static_visitor<VOption>
{
VOption operator()(ERM::TVExp const& cmd) const;
VOption operator()(ERM::TSymbol const& cmd) const;
VOption operator()(char const& cmd) const;
VOption operator()(double const& cmd) const;
VOption operator()(int const& cmd) const;
VOption operator()(ERM::Tcommand const& cmd) const;
VOption operator()(ERM::TStringConstant const& cmd) const;
};
struct VNode
{
private:
void processModifierList(const std::vector<TVModifier> & modifierList, bool asSymbol);
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);
};
//v printer
void printVOption(const VOption & opt);
}
class ERMInterpreter;
struct TriggerIdentifierMatch
{
bool allowNoIdetifier;
std::map< int, std::vector<int> > matchToIt; //match subidentifiers to these numbers
static const int MAX_SUBIDENTIFIERS = 16;
ERMInterpreter * ermEnv;
bool tryMatch(VERMInterpreter::Trigger * interptrig) const;
};
struct IexpValStr
{
private:
union
{
int val;
int * integervar;
double * flvar;
std::string * stringvar;
} val;
public:
std::string name;
std::string getName() const;
enum {WRONGVAL, INT, INTVAR, FLOATVAR, STRINGVAR} type;
void setTo(const IexpValStr & second);
void setTo(int val);
void setTo(double val);
void setTo(const std::string & val);
int getInt() const;
double getFloat() const;
std::string getString() const;
IexpValStr() : type(WRONGVAL)
{}
IexpValStr(int _val) : type(INT)
{
val.val = _val;
}
IexpValStr(int* _val) : type(INTVAR)
{
val.integervar = _val;
}
IexpValStr(double * _val) : type(FLOATVAR)
{
val.flvar = _val;
}
IexpValStr(std::string * _val) : type(STRINGVAR)
{
val.stringvar = _val;
}
#define OPERATOR_DEFINITION_FULL(OPSIGN) \
template<typename T> \
IexpValStr operator OPSIGN(const T & sec) const \
{ \
IexpValStr ret = *this; \
switch (type) \
{ \
case INT: \
case INTVAR: \
ret.setTo(ret.getInt() OPSIGN sec); \
break; \
case FLOATVAR: \
ret.setTo(ret.getFloat() OPSIGN sec); \
break; \
case STRINGVAR: \
ret.setTo(ret.getString() OPSIGN sec); \
break; \
} \
return ret; \
} \
IexpValStr operator OPSIGN(const IexpValStr & sec) const \
{ \
IexpValStr ret = *this; \
switch (type) \
{ \
case INT: \
case INTVAR: \
ret.setTo(ret.getInt() OPSIGN sec.getInt()); \
break; \
case FLOATVAR: \
ret.setTo(ret.getFloat() OPSIGN sec.getFloat()); \
break; \
case STRINGVAR: \
ret.setTo(ret.getString() OPSIGN sec.getString()); \
break; \
} \
return ret; \
} \
template<typename T> \
IexpValStr & operator OPSIGN ## = (const T & sec) \
{ \
*this = *this OPSIGN sec; \
return *this; \
}
#define OPERATOR_DEFINITION(OPSIGN) \
template<typename T> \
IexpValStr operator OPSIGN(const T & sec) const \
{ \
IexpValStr ret = *this; \
switch (type) \
{ \
case INT: \
case INTVAR: \
ret.setTo(ret.getInt() OPSIGN sec); \
break; \
case FLOATVAR: \
ret.setTo(ret.getFloat() OPSIGN sec); \
break; \
} \
return ret; \
} \
IexpValStr operator OPSIGN(const IexpValStr & sec) const \
{ \
IexpValStr ret = *this; \
switch (type) \
{ \
case INT: \
case INTVAR: \
ret.setTo(ret.getInt() OPSIGN sec.getInt()); \
break; \
case FLOATVAR: \
ret.setTo(ret.getFloat() OPSIGN sec.getFloat()); \
break; \
} \
return ret; \
} \
template<typename T> \
IexpValStr & operator OPSIGN ## = (const T & sec) \
{ \
*this = *this OPSIGN sec; \
return *this; \
}
#define OPERATOR_DEFINITION_INTEGER(OPSIGN) \
template<typename T> \
IexpValStr operator OPSIGN(const T & sec) const \
{ \
IexpValStr ret = *this; \
switch (type) \
{ \
case INT: \
case INTVAR: \
ret.setTo(ret.getInt() OPSIGN sec); \
break; \
} \
return ret; \
} \
IexpValStr operator OPSIGN(const IexpValStr & sec) const \
{ \
IexpValStr ret = *this; \
switch (type) \
{ \
case INT: \
case INTVAR: \
ret.setTo(ret.getInt() OPSIGN sec.getInt()); \
break; \
} \
return ret; \
} \
template<typename T> \
IexpValStr & operator OPSIGN ## = (const T & sec) \
{ \
*this = *this OPSIGN sec; \
return *this; \
}
OPERATOR_DEFINITION_FULL(+)
OPERATOR_DEFINITION(-)
OPERATOR_DEFINITION(*)
OPERATOR_DEFINITION(/)
OPERATOR_DEFINITION_INTEGER(%)
};
class ERMInterpreter : public CScriptingModule
{
/*not so*/ public:
// friend class ScriptScanner;
// friend class TriggerIdMatchHelper;
// friend class TriggerIdentifierMatch;
// friend class ConditionDisemboweler;
// friend struct LVL2IexpDisemboweler;
// friend struct VR_SPerformer;
// friend struct ERMExpDispatch;
// friend struct VRPerformer;
std::vector<VERMInterpreter::FileInfo*> files;
std::vector< VERMInterpreter::FileInfo* > fileInfos;
std::map<VERMInterpreter::LinePointer, ERM::TLine> scripts;
std::map<VERMInterpreter::LexicalPtr, VERMInterpreter::Environment> lexicalEnvs;
ERM::TLine &retrieveLine(VERMInterpreter::LinePointer linePtr);
static ERM::TTriggerBase & retrieveTrigger(ERM::TLine &line);
VERMInterpreter::Environment * globalEnv;
VERMInterpreter::ERMEnvironment * ermGlobalEnv;
typedef std::map<VERMInterpreter::TriggerType, std::vector<VERMInterpreter::Trigger> > TtriggerListType;
TtriggerListType triggers, postTriggers;
VERMInterpreter::Trigger * curTrigger;
VERMInterpreter::FunctionLocalVars * curFunc;
static const int TRIG_FUNC_NUM = 30000;
VERMInterpreter::FunctionLocalVars funcVars[TRIG_FUNC_NUM + 1]; //+1 because we use [0] as a global set of y-vars
VERMInterpreter::FunctionLocalVars * getFuncVars(int funNum); //0 is a global func-like set
IexpValStr getIexp(const ERM::TIexp & iexp) const;
IexpValStr getIexp(const ERM::TMacroUsage & macro) const;
IexpValStr getIexp(const ERM::TIdentifierInternal & tid) const;
IexpValStr getIexp(const ERM::TVarpExp & tid) const;
IexpValStr getIexp(const ERM::TBodyOptionItem & opit) const;
static const std::string triggerSymbol, postTriggerSymbol, defunSymbol;
void executeLine(const VERMInterpreter::LinePointer & lp);
void executeLine(const ERM::TLine &line);
void executeTrigger(VERMInterpreter::Trigger & trig, int funNum = -1, std::vector<int> funParams=std::vector<int>());
static bool isCMDATrigger(const ERM::Tcommand & cmd);
static bool isATrigger(const ERM::TLine & line);
static ERM::EVOtions getExpType(const ERM::TVOption & opt);
IexpValStr getVar(std::string toFollow, boost::optional<int> initVal) const;
std::string processERMString(std::string ermstring);
VERMInterpreter::VOption eval( VERMInterpreter::VOption line, VERMInterpreter::Environment * env = NULL );
VERMInterpreter::VOptionList evalEach( VERMInterpreter::VermTreeIterator list, VERMInterpreter::Environment * env = NULL );
public:
typedef std::map< int, std::vector<int> > TIDPattern;
void executeInstructions(); //called when starting a new game, before most of the map settings are done
void executeTriggerType(VERMInterpreter::TriggerType tt, bool pre, const TIDPattern & identifier, const std::vector<int> &funParams=std::vector<int>()); //use this to run triggers
void executeTriggerType(const char *trigger, int id); //convenience version of above, for pre-trigger when there is only one argument
void executeTriggerType(const char *trigger); //convenience version of above, for pre-trigger when there are no args
void setCurrentlyVisitedObj(int3 pos); //sets v998 - v1000 to given value
void scanForScripts();
enum EPrintMode{ALL, ERM_ONLY, VERM_ONLY};
void printScripts(EPrintMode mode = ALL);
void scanScripts(); //scans for functions, triggers etc.
ERMInterpreter();
bool checkCondition( ERM::Tcondition cond );
int getRealLine(const VERMInterpreter::LinePointer &lp);
//overload CScriptingModule
virtual void heroVisit(const CGHeroInstance *visitor, const CGObjectInstance *visitedObj, bool start) OVERRIDE;
virtual void init() OVERRIDE;//sets up environment etc.
virtual void executeUserCommand(const std::string &cmd) OVERRIDE;
virtual void giveInfoCB(CPrivilagedInfoCallback *cb) OVERRIDE;
virtual void giveActionCB(IGameEventRealizer *cb) OVERRIDE;
virtual void battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side) OVERRIDE;
const CGObjectInstance *getObjFrom(int3 pos);
template <typename T>
const T *getObjFromAs(int3 pos)
{
const T* obj = dynamic_cast<const T*>(getObjFrom(pos));
if(obj)
return obj;
else
throw VERMInterpreter::EScriptExecError("Wrong cast attempted, object is not of a desired type!");
}
};

556
scripting/erm/ERMParser.cpp Normal file
View File

@@ -0,0 +1,556 @@
#include "StdInc.h"
#include "ERMParser.h"
//To make compilation with older boost versions possible
//Don't know exact version - 1.46 works while 1.42 not
#if BOOST_VERSION >= 104600
/*
* ERMParser.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
*
*/
//Greenspun's Tenth Rule of Programming:
//Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified,
//bug-ridden, slow implementation of half of Common Lisp.
//actually these macros help in dealing with boost::variant
CERMPreprocessor::CERMPreprocessor(const std::string &Fname) : fname(Fname), file(Fname.c_str()), lineNo(0), version(INVALID)
{
if(!file.is_open())
{
logGlobal->errorStream() << "File " << Fname << " not found or unable to open";
return;
}
//check header
std::string header;
getline(header);
if(header == "ZVSE")
version = ERM;
else if(header == "VERM")
version = VERM;
else
{
logGlobal->errorStream() << "File " << fname << " has wrong header";
return;
}
}
class ParseErrorException : public std::exception
{
};
std::string CERMPreprocessor::retreiveCommandLine()
{
std::string wholeCommand;
//parse file
bool verm = false;
bool openedString = false;
int openedBraces = 0;
while(file.good())
{
std::string line ;
getline(line); //reading line
int dash = line.find_first_of('^');
bool inTheMiddle = openedBraces || openedString;
if(!inTheMiddle)
{
if(line.size() < 2)
continue;
if(line[0] != '!' ) //command lines must begin with ! -> otherwise treat as comment
continue;
verm = line[1] == '[';
}
if(openedString)
{
wholeCommand += "\\n";
if(dash != std::string::npos)
{
wholeCommand += line.substr(0, dash);
line.erase(0,dash);
}
else //no closing marker -> the whole line is further part of string
{
wholeCommand += line;
continue;
}
}
int i = 0;
for(; i < line.length(); i++)
{
char c = line[i];
if(!openedString)
{
if(c == '[')
openedBraces++;
else if(c == ']')
{
openedBraces--;
if(!openedBraces) //the last brace has been matched -> stop "parsing", everything else in the line is comment
{
i++;
break;
}
}
else if(c == '^')
openedString = true;
else if(c == ';') // a ';' that is in command line (and not in string) ends the command -> throw away rest
{
line.erase(i+!verm, line.length() - i - !verm); //leave ';' at the end only at ERM commands
break;
}
}
else if(c == '^')
openedString = false;
}
if(verm && !openedBraces && i < line.length())
{
line.erase(i, line.length() - i);
}
if(wholeCommand.size()) //separate lines with a space
wholeCommand += " ";
wholeCommand += line;
if(!openedBraces && !openedString)
return wholeCommand;
//loop end
}
if(openedBraces || openedString)
logGlobal->errorStream() << "Ill-formed file: " << fname;
return "";
}
void CERMPreprocessor::getline(std::string &ret)
{
lineNo++;
std::getline(file, ret);
boost::trim(ret); //get rid of wspace
}
ERMParser::ERMParser(std::string file)
:srcFile(file)
{}
std::vector<LineInfo> ERMParser::parseFile()
{
CERMPreprocessor preproc(srcFile);
std::vector<LineInfo> ret;
try
{
while(1)
{
std::string command = preproc.retreiveCommandLine();
if(command.length() == 0)
break;
repairEncoding(command);
LineInfo li;
li.realLineNum = preproc.getCurLineNo();
li.tl = parseLine(command, li.realLineNum);
ret.push_back(li);
}
}
catch (ParseErrorException & e)
{
logGlobal->errorStream() << "stopped parsing file";
}
return ret;
}
BOOST_FUSION_ADAPT_STRUCT(
ERM::TStringConstant,
(std::string, str)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TMacroUsage,
(std::string, macro)
)
// BOOST_FUSION_ADAPT_STRUCT(
// ERM::TQMacroUsage,
// (std::string, qmacro)
// )
BOOST_FUSION_ADAPT_STRUCT(
ERM::TMacroDef,
(std::string, macro)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TVarExpNotMacro,
(boost::optional<char>, questionMark)
(std::string, varsym)
(ERM::TVarExpNotMacro::Tval, val)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TArithmeticOp,
(ERM::TIexp, lhs)
(char, opcode)
(ERM::TIexp, rhs)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TVarpExp,
(ERM::TVarExp, var)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TVRLogic,
(char, opcode)
(ERM::TIexp, var)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TVRArithmetic,
(char, opcode)
(ERM::TIexp, rhs)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TNormalBodyOption,
(char, optionCode)
(ERM::TNormalBodyOptionList, params)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::Ttrigger,
(ERM::TCmdName, name)
(boost::optional<ERM::Tidentifier>, identifier)
(boost::optional<ERM::Tcondition>, condition)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TComparison,
(ERM::TIexp, lhs)
(std::string, compSign)
(ERM::TIexp, rhs)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TSemiCompare,
(std::string, compSign)
(ERM::TIexp, rhs)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TCurriedString,
(ERM::TIexp, iexp)
(ERM::TStringConstant, string)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TVarConcatString,
(ERM::TVarExp, var)
(ERM::TStringConstant, string)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::Tcondition,
(char, ctype)
(ERM::Tcondition::Tcond, cond)
(ERM::TconditionNode, rhs)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::Tinstruction,
(ERM::TCmdName, name)
(boost::optional<ERM::Tidentifier>, identifier)
(boost::optional<ERM::Tcondition>, condition)
(ERM::Tbody, body)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::Treceiver,
(ERM::TCmdName, name)
(boost::optional<ERM::Tidentifier>, identifier)
(boost::optional<ERM::Tcondition>, condition)
(boost::optional<ERM::Tbody>, body)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TPostTrigger,
(ERM::TCmdName, name)
(boost::optional<ERM::Tidentifier>, identifier)
(boost::optional<ERM::Tcondition>, condition)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::Tcommand,
(ERM::Tcommand::Tcmd, cmd)
(std::string, comment)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TVExp,
(std::vector<ERM::TVModifier>, modifier)
(std::vector<ERM::TVOption>, children)
)
BOOST_FUSION_ADAPT_STRUCT(
ERM::TSymbol,
(std::vector<ERM::TVModifier>, symModifier)
(std::string, sym)
)
namespace ERM
{
template<typename Iterator>
struct ERM_grammar : qi::grammar<Iterator, TLine(), ascii::space_type>
{
ERM_grammar() : ERM_grammar::base_type(vline, "VERM script line")
{
//do not build too complicated expressions, e.g. (a >> b) | c, qi has problems with them
ERMmacroUsage %= qi::lexeme[qi::lit('$') >> *(qi::char_ - '$') >> qi::lit('$')];
ERMmacroDef %= qi::lexeme[qi::lit('@') >> *(qi::char_ - '@') >> qi::lit('@')];
varExpNotMacro %= -qi::char_("?") >> (+(qi::char_("a-z") - 'u')) >> -qi::int_;
//TODO: mixed var/macro expressions like in !!HE-1&407:Id$cost$; [script 13]
/*qERMMacroUsage %= qi::lexeme[qi::lit("?$") >> *(qi::char_ - '$') >> qi::lit('$')];*/
varExp %= varExpNotMacro | ERMmacroUsage;
iexp %= varExp | qi::int_;
varp %= qi::lit("?") >> varExp;
comment %= *qi::char_;
commentLine %= (~qi::char_("!") >> comment | (qi::char_('!') >> (~qi::char_("?!$#[")) >> comment ));
cmdName %= qi::lexeme[qi::repeat(2)[qi::char_]];
arithmeticOp %= iexp >> qi::char_ >> iexp;
//identifier is usually a vector of i-expressions but VR receiver performs arithmetic operations on it
identifier %= (iexp | arithmeticOp) % qi::lit('/');
comparison %= iexp >> (*qi::char_("<=>")) >> iexp;
condition %= qi::char_("&|X/") >> (comparison | qi::int_) >> -condition;
trigger %= cmdName >> -identifier >> -condition > qi::lit(";"); /////
string %= qi::lexeme['^' >> *(qi::char_ - '^') >> '^'];
VRLogic %= qi::char_("&|X") >> iexp;
VRarithmetic %= qi::char_("+*:/%-") >> iexp;
semiCompare %= +qi::char_("<=>") >> iexp;
curStr %= iexp >> string;
varConcatString %= varExp >> qi::lit("+") >> string;
bodyOptionItem %= varConcatString | curStr | string | semiCompare | ERMmacroDef | varp | iexp | qi::eps;
exactBodyOptionList %= (bodyOptionItem % qi::lit("/"));
normalBodyOption = qi::char_("A-Z+") > exactBodyOptionList;
bodyOption %= VRLogic | VRarithmetic | normalBodyOption;
body %= qi::lit(":") >> +(bodyOption) > qi::lit(";");
instruction %= cmdName >> -identifier >> -condition >> body;
receiver %= cmdName >> -identifier >> -condition >> -body; //receiver without body exists... change needed
postTrigger %= cmdName >> -identifier >> -condition > qi::lit(";");
command %= (qi::lit("!") >>
(
(qi::lit("?") >> trigger) |
(qi::lit("!") >> receiver) |
(qi::lit("#") >> instruction) |
(qi::lit("$") >> postTrigger)
) >> comment
);
rline %=
(
command | commentLine | spirit::eps
);
vmod %= qi::string("`") | qi::string(",!") | qi::string(",") | qi::string("#'") | qi::string("'");
vsym %= *vmod >> qi::lexeme[+qi::char_("+*/$%&_=<>~a-zA-Z0-9-")];
qi::real_parser<double, qi::strict_real_policies<double> > strict_double;
vopt %= qi::lexeme[(qi::lit("!") >> qi::char_ >> qi::lit("!"))] | qi::lexeme[strict_double] | qi::lexeme[qi::int_] | command | vexp | string | vsym;
vexp %= *vmod >> qi::lit("[") >> *(vopt) >> qi::lit("]");
vline %= (( qi::lit("!") >>vexp) | rline ) > spirit::eoi;
//error handling
string.name("string constant");
ERMmacroUsage.name("macro usage");
/*qERMMacroUsage.name("macro usage with ?");*/
ERMmacroDef.name("macro definition");
varExpNotMacro.name("variable expression (not macro)");
varExp.name("variable expression");
iexp.name("i-expression");
comment.name("comment");
commentLine.name("comment line");
cmdName.name("name of a command");
identifier.name("identifier");
condition.name("condition");
trigger.name("trigger");
body.name("body");
instruction.name("instruction");
receiver.name("receiver");
postTrigger.name("post trigger");
command.name("command");
rline.name("ERM script line");
vsym.name("V symbol");
vopt.name("V option");
vexp.name("V expression");
vline.name("VERM line");
qi::on_error<qi::fail>
(
vline
, std::cout //or phoenix::ref(std::count), is there any difference?
<< phoenix::val("Error! Expecting ")
<< qi::_4 // what failed?
<< phoenix::val(" here: \"")
<< phoenix::construct<std::string>(qi::_3, qi::_2) // iterators to error-pos, end
<< phoenix::val("\"")
);
}
qi::rule<Iterator, TStringConstant(), ascii::space_type> string;
qi::rule<Iterator, TMacroUsage(), ascii::space_type> ERMmacroUsage;
/*qi::rule<Iterator, TQMacroUsage(), ascii::space_type> qERMMacroUsage;*/
qi::rule<Iterator, TMacroDef(), ascii::space_type> ERMmacroDef;
qi::rule<Iterator, TVarExpNotMacro(), ascii::space_type> varExpNotMacro;
qi::rule<Iterator, TVarExp(), ascii::space_type> varExp;
qi::rule<Iterator, TIexp(), ascii::space_type> iexp;
qi::rule<Iterator, TVarpExp(), ascii::space_type> varp;
qi::rule<Iterator, TArithmeticOp(), ascii::space_type> arithmeticOp;
qi::rule<Iterator, std::string(), ascii::space_type> comment;
qi::rule<Iterator, std::string(), ascii::space_type> commentLine;
qi::rule<Iterator, TCmdName(), ascii::space_type> cmdName;
qi::rule<Iterator, Tidentifier(), ascii::space_type> identifier;
qi::rule<Iterator, TComparison(), ascii::space_type> comparison;
qi::rule<Iterator, Tcondition(), ascii::space_type> condition;
qi::rule<Iterator, TVRLogic(), ascii::space_type> VRLogic;
qi::rule<Iterator, TVRArithmetic(), ascii::space_type> VRarithmetic;
qi::rule<Iterator, TSemiCompare(), ascii::space_type> semiCompare;
qi::rule<Iterator, TCurriedString(), ascii::space_type> curStr;
qi::rule<Iterator, TVarConcatString(), ascii::space_type> varConcatString;
qi::rule<Iterator, TBodyOptionItem(), ascii::space_type> bodyOptionItem;
qi::rule<Iterator, TNormalBodyOptionList(), ascii::space_type> exactBodyOptionList;
qi::rule<Iterator, TNormalBodyOption(), ascii::space_type> normalBodyOption;
qi::rule<Iterator, TBodyOption(), ascii::space_type> bodyOption;
qi::rule<Iterator, Ttrigger(), ascii::space_type> trigger;
qi::rule<Iterator, Tbody(), ascii::space_type> body;
qi::rule<Iterator, Tinstruction(), ascii::space_type> instruction;
qi::rule<Iterator, Treceiver(), ascii::space_type> receiver;
qi::rule<Iterator, TPostTrigger(), ascii::space_type> postTrigger;
qi::rule<Iterator, Tcommand(), ascii::space_type> command;
qi::rule<Iterator, TERMline(), ascii::space_type> rline;
qi::rule<Iterator, TSymbol(), ascii::space_type> vsym;
qi::rule<Iterator, TVModifier(), ascii::space_type> vmod;
qi::rule<Iterator, TVOption(), ascii::space_type> vopt;
qi::rule<Iterator, TVExp(), ascii::space_type> vexp;
qi::rule<Iterator, TLine(), ascii::space_type> vline;
};
};
ERM::TLine ERMParser::parseLine( const std::string & line, int realLineNo )
{
try
{
return parseLine(line);
}
catch(...)
{
logGlobal->errorStream() << "Parse error occurred in file " << srcFile << " (line " << realLineNo << ") :" << line;
throw;
}
}
ERM::TLine ERMParser::parseLine(const std::string & line)
{
std::string::const_iterator beg = line.begin(),
end = line.end();
ERM::ERM_grammar<std::string::const_iterator> ERMgrammar;
ERM::TLine AST;
bool r = qi::phrase_parse(beg, end, ERMgrammar, ascii::space, AST);
if(!r || beg != end)
{
logGlobal->errorStream() << "Parse error: cannot parse: " << std::string(beg, end);
throw ParseErrorException();
}
return AST;
}
ERMParser::ELineType ERMParser::classifyLine( const std::string & line, bool inString ) const
{
ERMParser::ELineType ret;
if(line[0] == '!')
{
if(countHatsBeforeSemicolon(line) % 2 == 1)
ret = ERMParser::UNFINISHED;
else
ret = ERMParser::COMMAND_FULL;
}
else
{
if(inString)
{
if(countHatsBeforeSemicolon(line) % 2 == 1)
ret = ERMParser::END_OF;
else
ret = ERMParser::UNFINISHED;
}
else
{
ret = ERMParser::COMMENT;
}
}
return ret;
}
int ERMParser::countHatsBeforeSemicolon( const std::string & line ) const
{
//CHECK: omit macros? or anything else?
int numOfHats = 0; //num of '^' before ';'
//check for unmatched ^
BOOST_FOREACH(char c, line)
{
if(c == ';')
break;
if(c == '^')
++numOfHats;
}
return numOfHats;
}
void ERMParser::repairEncoding( std::string & str ) const
{
for(int g=0; g<str.size(); ++g)
if(str[g] & 0x80)
str[g] = '|';
}
void ERMParser::repairEncoding( char * str, int len ) const
{
for(int g=0; g<len; ++g)
if(str[g] & 0x80)
str[g] = '|';
}
#else
ERMParser::ERMParser(std::string file){}
std::vector<LineInfo> ERMParser::parseFile() {std::vector<LineInfo> dummy; return dummy;} //compile fix
#endif

279
scripting/erm/ERMParser.h Normal file
View File

@@ -0,0 +1,279 @@
#pragma once
#include <boost/spirit/home/support/unused.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
namespace spirit = boost::spirit;
namespace qi = boost::spirit::qi;
namespace ascii = spirit::ascii;
namespace phoenix = boost::phoenix;
/*
* ERMParser.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
*
*/
class CERMPreprocessor
{
std::string fname;
std::ifstream file;
int lineNo;
enum {INVALID, ERM, VERM} version;
void getline(std::string &ret);
public:
CERMPreprocessor(const std::string &Fname);
std::string retreiveCommandLine();
int getCurLineNo() const
{
return lineNo;
}
};
//various classes that represent ERM/VERM AST
namespace ERM
{
struct TStringConstant
{
std::string str;
};
struct TMacroUsage
{
std::string macro;
};
// //macro with '?', for write only
// struct TQMacroUsage
// {
// std::string qmacro;
// };
//definition of a macro
struct TMacroDef
{
std::string macro;
};
typedef std::string TCmdName;
struct TVarExpNotMacro
{
typedef boost::optional<int> Tval;
boost::optional<char> questionMark;
std::string varsym;
Tval val;
};
typedef boost::variant<TVarExpNotMacro, TMacroUsage> TVarExp;
//write-only variable expression
struct TVarpExp
{
TVarExp var;
};
//i-expression (identifier expression) - an integral constant, variable symbol or array symbol
typedef boost::variant<TVarExp, int> TIexp;
struct TArithmeticOp
{
TIexp lhs, rhs;
char opcode;
};
struct TVRLogic
{
char opcode;
TIexp var;
};
struct TVRArithmetic
{
char opcode;
TIexp rhs;
};
struct TSemiCompare
{
std::string compSign;
TIexp rhs;
};
struct TCurriedString
{
TIexp iexp;
TStringConstant string;
};
struct TVarConcatString
{
TVarExp var;
TStringConstant string;
};
typedef boost::variant<TVarConcatString, TStringConstant, TCurriedString, TSemiCompare, TMacroDef, TIexp, TVarpExp, boost::spirit::unused_type> TBodyOptionItem;
typedef std::vector<TBodyOptionItem> TNormalBodyOptionList;
struct TNormalBodyOption
{
char optionCode;
TNormalBodyOptionList params;
};
typedef boost::variant<TVRLogic, TVRArithmetic, TNormalBodyOption> TBodyOption;
typedef boost::variant<TIexp, TArithmeticOp > TIdentifierInternal;
typedef std::vector< TIdentifierInternal > Tidentifier;
struct TComparison
{
std::string compSign;
TIexp lhs, rhs;
};
struct Tcondition;
typedef
boost::optional<
boost::recursive_wrapper<Tcondition>
>
TconditionNode;
struct Tcondition
{
typedef boost::variant<
TComparison,
int>
Tcond; //comparison or condition flag
char ctype;
Tcond cond;
TconditionNode rhs;
};
struct TTriggerBase
{
bool pre; //if false it's !$ post-trigger, elsewise it's !# (pre)trigger
TCmdName name;
boost::optional<Tidentifier> identifier;
boost::optional<Tcondition> condition;
};
struct Ttrigger : TTriggerBase
{
Ttrigger()
{
pre = true;
}
};
struct TPostTrigger : TTriggerBase
{
TPostTrigger()
{
pre = false;
}
};
//a dirty workaround for preprocessor magic that prevents the use types with comma in it in BOOST_FUSION_ADAPT_STRUCT
//see http://comments.gmane.org/gmane.comp.lib.boost.user/62501 for some info
//
//moreover, I encountered a quite serious bug in boost: http://boost.2283326.n4.nabble.com/container-hpp-111-error-C2039-value-type-is-not-a-member-of-td3352328.html
//not sure how serious it is...
//typedef boost::variant<char, TStringConstant, TMacroUsage, TMacroDef> bodyItem;
typedef std::vector<TBodyOption> Tbody;
struct Tinstruction
{
TCmdName name;
boost::optional<Tidentifier> identifier;
boost::optional<Tcondition> condition;
Tbody body;
};
struct Treceiver
{
TCmdName name;
boost::optional<Tidentifier> identifier;
boost::optional<Tcondition> condition;
boost::optional<Tbody> body;
};
struct Tcommand
{
typedef boost::variant<
Ttrigger,
Tinstruction,
Treceiver,
TPostTrigger
>
Tcmd;
Tcmd cmd;
std::string comment;
};
//vector expression
typedef boost::variant<Tcommand, std::string, boost::spirit::unused_type> TERMline;
typedef std::string TVModifier; //'`', ',', ',@', '#''
struct TSymbol
{
std::vector<TVModifier> symModifier;
std::string sym;
};
//for #'symbol expression
enum EVOtions{VEXP, SYMBOL, CHAR, DOUBLE, INT, TCMD, STRINGC};
struct TVExp;
typedef boost::variant<boost::recursive_wrapper<TVExp>, TSymbol, char, double, int, Tcommand, TStringConstant > TVOption; //options in v-expression
//v-expression
struct TVExp
{
std::vector<TVModifier> modifier;
std::vector<TVOption> children;
};
//script line
typedef boost::variant<TVExp, TERMline> TLine;
}
struct LineInfo
{
ERM::TLine tl;
int realLineNum;
};
class ERMParser
{
private:
std::string srcFile;
void repairEncoding(char * str, int len) const; //removes nonstandard ascii characters from string
void repairEncoding(std::string & str) const; //removes nonstandard ascii characters from string
enum ELineType{COMMAND_FULL, COMMENT, UNFINISHED, END_OF};
int countHatsBeforeSemicolon(const std::string & line) const;
ELineType classifyLine(const std::string & line, bool inString) const;
ERM::TLine parseLine(const std::string & line, int realLineNo);
public:
ERMParser(std::string file);
std::vector<LineInfo> parseFile();
static ERM::TLine parseLine(const std::string & line);
};

View File

@@ -0,0 +1,34 @@
#include "StdInc.h"
#include "ERMScriptModule.h"
#include "ERMInterpreter.h"
/*
* ERMScriptingModule.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
*
*/
IGameEventRealizer *acb;
CPrivilagedInfoCallback *icb;
#ifdef __GNUC__
#define strcpy_s(a, b, c) strncpy(a, c, b)
#endif
const char *g_cszAiName = "(V)ERM interpreter";
extern "C" DLL_EXPORT void GetAiName(char* name)
{
strcpy_s(name, strlen(g_cszAiName) + 1, g_cszAiName);
}
extern "C" DLL_EXPORT CScriptingModule* GetNewModule()
{
return new ERMInterpreter();
}

View File

@@ -0,0 +1,17 @@
#pragma once
#include "../../lib/CScriptingModule.h"
/*
* ERMScriptingModule.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
*
*/
extern IGameEventRealizer *acb;
extern CPrivilagedInfoCallback *icb;

2
scripting/erm/StdInc.cpp Normal file
View File

@@ -0,0 +1,2 @@
// Creates the precompiled header
#include "StdInc.h"

10
scripting/erm/StdInc.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
#include "../../Global.h"
// This header should be treated as a pre compiled header file(PCH) in the compiler building settings.
// Here you can add specific libraries and macros which are specific to this project.
#include <boost/variant.hpp>
#include <boost/optional.hpp>