mirror of
https://github.com/vcmi/vcmi.git
synced 2025-04-23 12:08:45 +02:00
389 lines
8.6 KiB
C++
389 lines
8.6 KiB
C++
#include "ERMInterpreter.h"
|
|
|
|
#include <boost/filesystem.hpp>
|
|
#include <boost/algorithm/string.hpp>
|
|
#include <boost/foreach.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;
|
|
|
|
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());
|
|
scripts[name] = ep.parseFile();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void ERMInterpreter::printScripts( EPrintMode mode /*= EPrintMode::ALL*/ )
|
|
{
|
|
for(std::map<std::string, std::vector<ERM::TLine> >::const_iterator it = scripts.begin(); it != scripts.end(); ++it)
|
|
{
|
|
tlog2 << "----------------- script " << it->first << " ------------------\n";
|
|
for(int i=0; i<it->second.size(); ++i)
|
|
{
|
|
ERMPrinter::printAST(it->second[i]);
|
|
}
|
|
}
|
|
}
|