added local script compiling
parent
2064c43d89
commit
076b01559f
@ -0,0 +1,15 @@
|
||||
#ifndef GAME_SCRIPT_COMPILERCONTEXT_H
|
||||
#define GAME_SCRIPT_COMPILERCONTEXT_H
|
||||
|
||||
#include <components/compiler/context.hpp>
|
||||
|
||||
namespace MWScript
|
||||
{
|
||||
class CompilerContext : public Compiler::Context
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -0,0 +1,22 @@
|
||||
#ifndef GAME_SCRIPT_COMPILERCONTEXTSCRIPT_H
|
||||
#define GAME_SCRIPT_COMPILERCONTEXTSCRIPT_H
|
||||
|
||||
#include "compilercontext.hpp"
|
||||
|
||||
namespace MWScript
|
||||
{
|
||||
/// Context for local scripts, global scripts and targetted scripts
|
||||
|
||||
class CompilerContextScript : public CompilerContext
|
||||
{
|
||||
public:
|
||||
|
||||
// Is the compiler allowed to declare local variables?
|
||||
virtual bool canDeclareLocals() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -1,19 +1,98 @@
|
||||
|
||||
#include "scriptmanager.hpp"
|
||||
|
||||
#include <components/compiler/scanner.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <exception>
|
||||
|
||||
#include <components/esm/loadscpt.hpp>
|
||||
#include <components/esm_store/store.hpp>
|
||||
|
||||
namespace MWScript
|
||||
{
|
||||
ScriptManager::ScriptManager (const ESMS::ESMStore& store, bool verbose)
|
||||
: mStore (store), mVerbose (verbose)
|
||||
ScriptManager::ScriptManager (const ESMS::ESMStore& store, bool verbose,
|
||||
Compiler::Context& compilerContext)
|
||||
: mErrorHandler (std::cerr), mStore (store), mVerbose (verbose),
|
||||
mCompilerContext (compilerContext), mParser (mErrorHandler, mCompilerContext)
|
||||
{}
|
||||
|
||||
void ScriptManager::run (const std::string& name/*, Compiler::Context& compilerContext,
|
||||
Interpreter::Context& interpreterContext, Locals& locals*/)
|
||||
bool ScriptManager::compile (const std::string& name)
|
||||
{
|
||||
mParser.reset();
|
||||
mErrorHandler.reset();
|
||||
|
||||
bool Success = true;
|
||||
|
||||
if (const ESM::Script *script = mStore.scripts.find (name))
|
||||
{
|
||||
if (mVerbose)
|
||||
std::cout << "compiling script: " << name << std::endl;
|
||||
|
||||
try
|
||||
{
|
||||
std::istringstream input (script->scriptText);
|
||||
|
||||
Compiler::Scanner scanner (mErrorHandler, input);
|
||||
|
||||
scanner.scan (mParser);
|
||||
|
||||
if (!mErrorHandler.isGood())
|
||||
Success = false;
|
||||
}
|
||||
catch (const std::exception& error)
|
||||
{
|
||||
Success = false;
|
||||
}
|
||||
|
||||
if (!Success && mVerbose)
|
||||
{
|
||||
std::cerr
|
||||
<< "compiling failed: " << name << std::endl
|
||||
<< script->scriptText
|
||||
<< std::endl << std::endl;
|
||||
}
|
||||
|
||||
if (Success)
|
||||
{
|
||||
std::vector<Interpreter::Type_Code> code;
|
||||
mParser.getCode (code);
|
||||
mScripts.insert (std::make_pair (name, code));
|
||||
|
||||
// TODO sanity check on generated locals
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ScriptManager::run (const std::string& name/*,
|
||||
Interpreter::Context& interpreterContext*/, Locals& locals)
|
||||
{
|
||||
std::cout << "script request: " << name << std::endl;
|
||||
// compile script
|
||||
std::map<std::string, std::vector<Interpreter::Type_Code> >::iterator iter =
|
||||
mScripts.find (name);
|
||||
|
||||
if (iter==mScripts.end())
|
||||
{
|
||||
if (!compile (name))
|
||||
{
|
||||
// failed -> ignore script from now on.
|
||||
std::vector<Interpreter::Type_Code> empty;
|
||||
mScripts.insert (std::make_pair (name, empty));
|
||||
return;
|
||||
}
|
||||
|
||||
iter = mScripts.find (name);
|
||||
assert (iter!=mScripts.end());
|
||||
}
|
||||
|
||||
// execute script
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue