mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-03-03 21:49:41 +00:00
added literal container and reworked output container handling
This commit is contained in:
parent
ba847ae9d8
commit
5cf3264bd3
12 changed files with 284 additions and 16 deletions
|
@ -71,7 +71,8 @@ set(COMPILER components/compiler/errorhandler.cpp
|
||||||
components/compiler/lineparser.cpp components/compiler/skipparser.cpp
|
components/compiler/lineparser.cpp components/compiler/skipparser.cpp
|
||||||
components/compiler/parser.cpp components/compiler/scanner.cpp
|
components/compiler/parser.cpp components/compiler/scanner.cpp
|
||||||
components/compiler/streamerrorhandler.cpp
|
components/compiler/streamerrorhandler.cpp
|
||||||
components/compiler/locals.cpp)
|
components/compiler/locals.cpp components/compiler/literals.cpp
|
||||||
|
components/compiler/output.cpp)
|
||||||
file(GLOB COMPILER_HEADER components/compiler/*.hpp)
|
file(GLOB COMPILER_HEADER components/compiler/*.hpp)
|
||||||
source_group(compiler FILES ${COMPILER} ${COMPILER_HEADER})
|
source_group(compiler FILES ${COMPILER} ${COMPILER_HEADER})
|
||||||
|
|
||||||
|
|
|
@ -98,7 +98,6 @@ namespace Compiler
|
||||||
mState = BeginState;
|
mState = BeginState;
|
||||||
mName.clear();
|
mName.clear();
|
||||||
mScriptParser.reset();
|
mScriptParser.reset();
|
||||||
mLocals.clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include "parser.hpp"
|
#include "parser.hpp"
|
||||||
#include "scriptparser.hpp"
|
#include "scriptparser.hpp"
|
||||||
#include "locals.hpp"
|
#include "locals.hpp"
|
||||||
|
#include "literals.hpp"
|
||||||
|
|
||||||
namespace Compiler
|
namespace Compiler
|
||||||
{
|
{
|
||||||
|
|
|
@ -10,8 +10,9 @@
|
||||||
namespace Compiler
|
namespace Compiler
|
||||||
{
|
{
|
||||||
LineParser::LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
|
LineParser::LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
|
||||||
std::vector<Interpreter::Type_Code>& code)
|
Literals& literals, std::vector<Interpreter::Type_Code>& code)
|
||||||
: Parser (errorHandler, context), mLocals (locals), mCode (code), mState (BeginState)
|
: Parser (errorHandler, context), mLocals (locals), mLiterals (literals), mCode (code),
|
||||||
|
mState (BeginState)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
bool LineParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner)
|
bool LineParser::parseInt (int value, const TokenLoc& loc, Scanner& scanner)
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
namespace Compiler
|
namespace Compiler
|
||||||
{
|
{
|
||||||
class Locals;
|
class Locals;
|
||||||
|
class Literals;
|
||||||
|
|
||||||
/// \brief Line parser, to be used in console scripts and as part of ScriptParser
|
/// \brief Line parser, to be used in console scripts and as part of ScriptParser
|
||||||
|
|
||||||
|
@ -23,13 +24,14 @@ namespace Compiler
|
||||||
};
|
};
|
||||||
|
|
||||||
Locals& mLocals;
|
Locals& mLocals;
|
||||||
|
Literals& mLiterals;
|
||||||
std::vector<Interpreter::Type_Code>& mCode;
|
std::vector<Interpreter::Type_Code>& mCode;
|
||||||
State mState;
|
State mState;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
|
LineParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
|
||||||
std::vector<Interpreter::Type_Code>& code);
|
Literals& literals, std::vector<Interpreter::Type_Code>& code);
|
||||||
|
|
||||||
virtual bool parseInt (int value, const TokenLoc& loc, Scanner& scanner);
|
virtual bool parseInt (int value, const TokenLoc& loc, Scanner& scanner);
|
||||||
///< Handle an int token.
|
///< Handle an int token.
|
||||||
|
|
94
components/compiler/literals.cpp
Normal file
94
components/compiler/literals.cpp
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
|
||||||
|
#include "literals.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace Compiler
|
||||||
|
{
|
||||||
|
int Literals::getIntegerSize() const
|
||||||
|
{
|
||||||
|
return mIntegers.size() * sizeof (Interpreter::Type_Integer);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Literals::getFloatSize() const
|
||||||
|
{
|
||||||
|
return mFloats.size() * sizeof (Interpreter::Type_Float);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Literals::getStringSize() const
|
||||||
|
{
|
||||||
|
int size = 0;
|
||||||
|
|
||||||
|
for (std::vector<std::string>::const_iterator iter (mStrings.begin());
|
||||||
|
iter!=mStrings.end(); ++iter)
|
||||||
|
size += static_cast<int> (iter->size()) + 1;
|
||||||
|
|
||||||
|
if (size % 4) // padding
|
||||||
|
size += 4 - size % 4;
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Literals::append (std::vector<Interpreter::Type_Code>& code) const
|
||||||
|
{
|
||||||
|
for (std::vector<Interpreter::Type_Integer>::const_iterator iter (mIntegers.begin());
|
||||||
|
iter!=mIntegers.end(); ++iter)
|
||||||
|
code.push_back (*reinterpret_cast<const Interpreter::Type_Code *> (&*iter));
|
||||||
|
|
||||||
|
for (std::vector<Interpreter::Type_Float>::const_iterator iter (mFloats.begin());
|
||||||
|
iter!=mFloats.end(); ++iter)
|
||||||
|
code.push_back (*reinterpret_cast<const Interpreter::Type_Code *> (&*iter));
|
||||||
|
|
||||||
|
int stringBlockSize = getStringSize();
|
||||||
|
int size = static_cast<int> (code.size());
|
||||||
|
|
||||||
|
code.resize (size+stringBlockSize/4);
|
||||||
|
|
||||||
|
int offset = 0;
|
||||||
|
|
||||||
|
for (std::vector<std::string>::const_iterator iter (mStrings.begin());
|
||||||
|
iter!=mStrings.end(); ++iter)
|
||||||
|
{
|
||||||
|
int stringSize = iter->size()+1;
|
||||||
|
|
||||||
|
std::copy (iter->c_str(), iter->c_str()+stringSize,
|
||||||
|
reinterpret_cast<char *> (&code[size]) + offset);
|
||||||
|
offset += stringSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Literals::addInteger (Interpreter::Type_Integer value)
|
||||||
|
{
|
||||||
|
int index = static_cast<int> (mIntegers.size());
|
||||||
|
|
||||||
|
mIntegers.push_back (value);
|
||||||
|
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Literals::addFloat (Interpreter::Type_Float value)
|
||||||
|
{
|
||||||
|
int index = static_cast<int> (mFloats.size());
|
||||||
|
|
||||||
|
mFloats.push_back (value);
|
||||||
|
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Literals::addString (const std::string& value)
|
||||||
|
{
|
||||||
|
int index = static_cast<int> (mStrings.size());
|
||||||
|
|
||||||
|
mStrings.push_back (value);
|
||||||
|
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Literals::clear()
|
||||||
|
{
|
||||||
|
mIntegers.clear();
|
||||||
|
mFloats.clear();
|
||||||
|
mStrings.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
49
components/compiler/literals.hpp
Normal file
49
components/compiler/literals.hpp
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
#ifndef COMPILER_LITERALS_H_INCLUDED
|
||||||
|
#define COMPILER_LITERALS_H_INCLUDED
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <components/interpreter/types.hpp>
|
||||||
|
|
||||||
|
namespace Compiler
|
||||||
|
{
|
||||||
|
/// \brief Literal values.
|
||||||
|
|
||||||
|
class Literals
|
||||||
|
{
|
||||||
|
std::vector<Interpreter::Type_Integer> mIntegers;
|
||||||
|
std::vector<Interpreter::Type_Float> mFloats;
|
||||||
|
std::vector<std::string> mStrings;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
int getIntegerSize() const;
|
||||||
|
///< Return size of integer block (in bytes).
|
||||||
|
|
||||||
|
int getFloatSize() const;
|
||||||
|
///< Return size of float block (in bytes).
|
||||||
|
|
||||||
|
int getStringSize() const;
|
||||||
|
///< Return size of string block (in bytes).
|
||||||
|
|
||||||
|
void append (std::vector<Interpreter::Type_Code>& code) const;
|
||||||
|
///< Apepnd literal blocks to code.
|
||||||
|
/// \note code blocks will be padded for 32-bit alignment.
|
||||||
|
|
||||||
|
int addInteger (Interpreter::Type_Integer value);
|
||||||
|
///< add integer liternal and return index.
|
||||||
|
|
||||||
|
int addFloat (Interpreter::Type_Float value);
|
||||||
|
///< add float literal and return value.
|
||||||
|
|
||||||
|
int addString (const std::string& value);
|
||||||
|
///< add string literal and return value.
|
||||||
|
|
||||||
|
void clear();
|
||||||
|
///< remove all literals.
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
74
components/compiler/output.cpp
Normal file
74
components/compiler/output.cpp
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
|
||||||
|
#include "output.hpp"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
|
#include "locals.hpp"
|
||||||
|
|
||||||
|
namespace Compiler
|
||||||
|
{
|
||||||
|
Output::Output (Locals& locals) : mLocals (locals) {}
|
||||||
|
|
||||||
|
void Output::getCode (std::vector<Interpreter::Type_Code>& code) const
|
||||||
|
{
|
||||||
|
code.clear();
|
||||||
|
|
||||||
|
// header
|
||||||
|
code.push_back (static_cast<Interpreter::Type_Code> (mCode.size()));
|
||||||
|
|
||||||
|
assert (mLiterals.getIntegerSize()%4==0);
|
||||||
|
code.push_back (static_cast<Interpreter::Type_Code> (mLiterals.getIntegerSize()/4));
|
||||||
|
|
||||||
|
assert (mLiterals.getFloatSize()%4==0);
|
||||||
|
code.push_back (static_cast<Interpreter::Type_Code> (mLiterals.getFloatSize()/4));
|
||||||
|
|
||||||
|
assert (mLiterals.getStringSize()%4==0);
|
||||||
|
code.push_back (static_cast<Interpreter::Type_Code> (mLiterals.getStringSize()/4));
|
||||||
|
|
||||||
|
// code
|
||||||
|
std::copy (mCode.begin(), mCode.end(), std::back_inserter (code));
|
||||||
|
|
||||||
|
// literals
|
||||||
|
mLiterals.append (code);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Literals& Output::getLiterals() const
|
||||||
|
{
|
||||||
|
return mLiterals;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<Interpreter::Type_Code>& Output::getCode() const
|
||||||
|
{
|
||||||
|
return mCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Locals& Output::getLocals() const
|
||||||
|
{
|
||||||
|
return mLocals;
|
||||||
|
}
|
||||||
|
|
||||||
|
Literals& Output::getLiterals()
|
||||||
|
{
|
||||||
|
return mLiterals;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Interpreter::Type_Code>& Output::getCode()
|
||||||
|
{
|
||||||
|
return mCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
Locals& Output::getLocals()
|
||||||
|
{
|
||||||
|
return mLocals;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Output::clear()
|
||||||
|
{
|
||||||
|
mLiterals.clear();
|
||||||
|
mCode.clear();
|
||||||
|
mLocals.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
44
components/compiler/output.hpp
Normal file
44
components/compiler/output.hpp
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
#ifndef COMPILER_OUTPUT_H_INCLUDED
|
||||||
|
#define COMPILER_OUTPUT_H_INCLUDED
|
||||||
|
|
||||||
|
#include "literals.hpp"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <components/interpreter/types.hpp>
|
||||||
|
|
||||||
|
namespace Compiler
|
||||||
|
{
|
||||||
|
class Locals;
|
||||||
|
|
||||||
|
class Output
|
||||||
|
{
|
||||||
|
Literals mLiterals;
|
||||||
|
std::vector<Interpreter::Type_Code> mCode;
|
||||||
|
Locals& mLocals;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
Output (Locals& locals);
|
||||||
|
|
||||||
|
void getCode (std::vector<Interpreter::Type_Code>& code) const;
|
||||||
|
///< store generated code in \æ code.
|
||||||
|
|
||||||
|
const Literals& getLiterals() const;
|
||||||
|
|
||||||
|
const Locals& getLocals() const;
|
||||||
|
|
||||||
|
const std::vector<Interpreter::Type_Code>& getCode() const;
|
||||||
|
|
||||||
|
Literals& getLiterals();
|
||||||
|
|
||||||
|
std::vector<Interpreter::Type_Code>& getCode();
|
||||||
|
|
||||||
|
Locals& getLocals();
|
||||||
|
|
||||||
|
void clear();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -7,13 +7,14 @@ namespace Compiler
|
||||||
{
|
{
|
||||||
ScriptParser::ScriptParser (ErrorHandler& errorHandler, Context& context,
|
ScriptParser::ScriptParser (ErrorHandler& errorHandler, Context& context,
|
||||||
Locals& locals, bool end)
|
Locals& locals, bool end)
|
||||||
: Parser (errorHandler, context), mLineParser (errorHandler, context, locals, mCode),
|
: Parser (errorHandler, context), mOutput (locals),
|
||||||
mLocals (locals), mEnd (end)
|
mLineParser (errorHandler, context, locals, mOutput.getLiterals(), mOutput.getCode()),
|
||||||
|
mEnd (end)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void ScriptParser::getCode (std::vector<Interpreter::Type_Code>& code) const
|
void ScriptParser::getCode (std::vector<Interpreter::Type_Code>& code) const
|
||||||
{
|
{
|
||||||
code = mCode;
|
mOutput.getCode (code);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ScriptParser::parseName (const std::string& name, const TokenLoc& loc,
|
bool ScriptParser::parseName (const std::string& name, const TokenLoc& loc,
|
||||||
|
@ -61,7 +62,7 @@ namespace Compiler
|
||||||
void ScriptParser::reset()
|
void ScriptParser::reset()
|
||||||
{
|
{
|
||||||
mLineParser.reset();
|
mLineParser.reset();
|
||||||
mCode.clear();
|
mOutput.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
#ifndef COMPILER_SCRIPTPARSER_H_INCLUDED
|
#ifndef COMPILER_SCRIPTPARSER_H_INCLUDED
|
||||||
#define COMPILER_SCRIPTPARSER_H_INCLUDED
|
#define COMPILER_SCRIPTPARSER_H_INCLUDED
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <components/interpreter/types.hpp>
|
|
||||||
|
|
||||||
#include "parser.hpp"
|
#include "parser.hpp"
|
||||||
#include "lineparser.hpp"
|
#include "lineparser.hpp"
|
||||||
|
#include "output.hpp"
|
||||||
|
|
||||||
namespace Compiler
|
namespace Compiler
|
||||||
{
|
{
|
||||||
|
@ -16,10 +14,9 @@ namespace Compiler
|
||||||
|
|
||||||
class ScriptParser : public Parser
|
class ScriptParser : public Parser
|
||||||
{
|
{
|
||||||
|
Output mOutput;
|
||||||
LineParser mLineParser;
|
LineParser mLineParser;
|
||||||
Locals& mLocals;
|
|
||||||
bool mEnd;
|
bool mEnd;
|
||||||
std::vector<Interpreter::Type_Code> mCode;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,11 @@ namespace Interpreter
|
||||||
{
|
{
|
||||||
typedef unsigned int Type_Code; // 32 bit
|
typedef unsigned int Type_Code; // 32 bit
|
||||||
|
|
||||||
|
typedef unsigned int Type_Data; // 32 bit
|
||||||
|
|
||||||
|
typedef unsigned int Type_Integer; // 32 bit
|
||||||
|
|
||||||
|
typedef unsigned int Type_Float; // 32 bit
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue