mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-03-03 22:19:40 +00:00
added basic message box formatting
This commit is contained in:
parent
aeb41105c4
commit
804aed6298
8 changed files with 271 additions and 21 deletions
|
@ -4,11 +4,13 @@
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <stack>
|
||||||
|
|
||||||
#include "generator.hpp"
|
#include "generator.hpp"
|
||||||
#include "scanner.hpp"
|
#include "scanner.hpp"
|
||||||
#include "errorhandler.hpp"
|
#include "errorhandler.hpp"
|
||||||
#include "locals.hpp"
|
#include "locals.hpp"
|
||||||
|
#include "stringparser.hpp"
|
||||||
|
|
||||||
namespace Compiler
|
namespace Compiler
|
||||||
{
|
{
|
||||||
|
@ -187,21 +189,7 @@ namespace Compiler
|
||||||
|
|
||||||
void ExprParser::parseArguments (const std::string& arguments, Scanner& scanner)
|
void ExprParser::parseArguments (const std::string& arguments, Scanner& scanner)
|
||||||
{
|
{
|
||||||
ExprParser parser (getErrorHandler(), getContext(), mLocals, mLiterals, true);
|
parseArguments (arguments, scanner, mCode);
|
||||||
|
|
||||||
for (std::string::const_iterator iter (arguments.begin()); iter!=arguments.end();
|
|
||||||
++iter)
|
|
||||||
{
|
|
||||||
parser.reset();
|
|
||||||
scanner.scan (parser);
|
|
||||||
|
|
||||||
char type = parser.append (mCode);
|
|
||||||
|
|
||||||
if (type!=*iter)
|
|
||||||
Generator::convert (mCode, type, *iter);
|
|
||||||
|
|
||||||
mOperands.push_back (*iter);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ExprParser::ExprParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
|
ExprParser::ExprParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
|
||||||
|
@ -285,6 +273,7 @@ namespace Compiler
|
||||||
parseArguments ("f", scanner);
|
parseArguments ("f", scanner);
|
||||||
|
|
||||||
Generator::squareRoot (mCode);
|
Generator::squareRoot (mCode);
|
||||||
|
mOperands.push_back ('f');
|
||||||
|
|
||||||
mNextOperand = false;
|
mNextOperand = false;
|
||||||
return true;
|
return true;
|
||||||
|
@ -433,5 +422,60 @@ namespace Compiler
|
||||||
assert (mOperands.size()==1);
|
assert (mOperands.size()==1);
|
||||||
return mOperands[0];
|
return mOperands[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ExprParser::parseArguments (const std::string& arguments, Scanner& scanner,
|
||||||
|
std::vector<Interpreter::Type_Code>& code, bool invert)
|
||||||
|
{
|
||||||
|
ExprParser parser (getErrorHandler(), getContext(), mLocals, mLiterals, true);
|
||||||
|
StringParser stringParser (getErrorHandler(), getContext(), mLiterals);
|
||||||
|
|
||||||
|
std::stack<std::vector<Interpreter::Type_Code> > stack;
|
||||||
|
|
||||||
|
for (std::string::const_iterator iter (arguments.begin()); iter!=arguments.end();
|
||||||
|
++iter)
|
||||||
|
{
|
||||||
|
if (*iter=='S')
|
||||||
|
{
|
||||||
|
stringParser.reset();
|
||||||
|
scanner.scan (stringParser);
|
||||||
|
|
||||||
|
if (invert)
|
||||||
|
{
|
||||||
|
std::vector<Interpreter::Type_Code> tmp;
|
||||||
|
stringParser.append (tmp);
|
||||||
|
|
||||||
|
stack.push (tmp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
stringParser.append (code);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
parser.reset();
|
||||||
|
scanner.scan (parser);
|
||||||
|
|
||||||
|
std::vector<Interpreter::Type_Code> tmp;
|
||||||
|
|
||||||
|
char type = parser.append (tmp);
|
||||||
|
|
||||||
|
if (type!=*iter)
|
||||||
|
Generator::convert (tmp, type, *iter);
|
||||||
|
|
||||||
|
if (invert)
|
||||||
|
stack.push (tmp);
|
||||||
|
else
|
||||||
|
std::copy (tmp.begin(), tmp.end(), std::back_inserter (code));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!stack.empty())
|
||||||
|
{
|
||||||
|
std::vector<Interpreter::Type_Code>& tmp = stack.top();
|
||||||
|
|
||||||
|
std::copy (tmp.begin(), tmp.end(), std::back_inserter (code));
|
||||||
|
|
||||||
|
stack.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -89,6 +89,13 @@ namespace Compiler
|
||||||
char append (std::vector<Interpreter::Type_Code>& code);
|
char append (std::vector<Interpreter::Type_Code>& code);
|
||||||
///< Generate code for parsed expression.
|
///< Generate code for parsed expression.
|
||||||
/// \return Type ('l': integer, 'f': float)
|
/// \return Type ('l': integer, 'f': float)
|
||||||
|
|
||||||
|
void parseArguments (const std::string& arguments, Scanner& scanner,
|
||||||
|
std::vector<Interpreter::Type_Code>& code, bool invert = false);
|
||||||
|
///< Parse sequence of arguments specified by \a arguments.
|
||||||
|
/// \param arguments Each character represents one arguments ('l': integer,
|
||||||
|
/// 'f': float, 'S': string)
|
||||||
|
/// \param invert Store arguments in reverted order.
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -275,6 +275,12 @@ namespace Compiler
|
||||||
opFetchFloatLiteral (code);
|
opFetchFloatLiteral (code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void pushString (CodeContainer& code, Literals& literals, const std::string& value)
|
||||||
|
{
|
||||||
|
int index = literals.addString (value);
|
||||||
|
opPushInt (code, index);
|
||||||
|
}
|
||||||
|
|
||||||
void assignToLocal (CodeContainer& code, char localType,
|
void assignToLocal (CodeContainer& code, char localType,
|
||||||
int localIndex, const CodeContainer& value, char valueType)
|
int localIndex, const CodeContainer& value, char valueType)
|
||||||
{
|
{
|
||||||
|
|
|
@ -18,6 +18,8 @@ namespace Compiler
|
||||||
|
|
||||||
void pushFloat (CodeContainer& code, Literals& literals, float value);
|
void pushFloat (CodeContainer& code, Literals& literals, float value);
|
||||||
|
|
||||||
|
void pushString (CodeContainer& code, Literals& literals, const std::string& value);
|
||||||
|
|
||||||
void assignToLocal (CodeContainer& code, char localType,
|
void assignToLocal (CodeContainer& code, char localType,
|
||||||
int localIndex, const CodeContainer& value, char valueType);
|
int localIndex, const CodeContainer& value, char valueType);
|
||||||
|
|
||||||
|
|
|
@ -78,6 +78,37 @@ namespace Compiler
|
||||||
|
|
||||||
if (mState==MessageState || mState==MessageCommaState)
|
if (mState==MessageState || mState==MessageCommaState)
|
||||||
{
|
{
|
||||||
|
std::string arguments;
|
||||||
|
|
||||||
|
for (std::size_t i=0; i<name.size(); ++i)
|
||||||
|
{
|
||||||
|
if (name[i]=='%')
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
if (i<name.size())
|
||||||
|
{
|
||||||
|
if (name[i]=='G' || name[i]=='g')
|
||||||
|
{
|
||||||
|
arguments += "l";
|
||||||
|
}
|
||||||
|
else if (name[i]=='S' || name[i]=='s')
|
||||||
|
{
|
||||||
|
arguments += 'S';
|
||||||
|
}
|
||||||
|
else if (name[i]=='.' || name[i]=='f')
|
||||||
|
{
|
||||||
|
arguments += 'f';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!arguments.empty())
|
||||||
|
{
|
||||||
|
mExprParser.reset();
|
||||||
|
mExprParser.parseArguments (arguments, scanner, mCode, true);
|
||||||
|
}
|
||||||
|
|
||||||
Generator::message (mCode, mLiterals, name, 0);
|
Generator::message (mCode, mLiterals, name, 0);
|
||||||
mState = EndState;
|
mState = EndState;
|
||||||
return false;
|
return false;
|
||||||
|
|
52
components/compiler/stringparser.cpp
Normal file
52
components/compiler/stringparser.cpp
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
|
||||||
|
#include "stringparser.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
|
#include "scanner.hpp"
|
||||||
|
#include "generator.hpp"
|
||||||
|
|
||||||
|
namespace Compiler
|
||||||
|
{
|
||||||
|
StringParser::StringParser (ErrorHandler& errorHandler, Context& context, Literals& literals)
|
||||||
|
: Parser (errorHandler, context), mLiterals (literals), mState (StartState)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StringParser::parseName (const std::string& name, const TokenLoc& loc,
|
||||||
|
Scanner& scanner)
|
||||||
|
{
|
||||||
|
if (mState==StartState || mState==CommaState)
|
||||||
|
{
|
||||||
|
Generator::pushString (mCode, mLiterals, name);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Parser::parseName (name, loc, scanner);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StringParser::parseSpecial (int code, const TokenLoc& loc, Scanner& scanner)
|
||||||
|
{
|
||||||
|
if (code==Scanner::S_comma && mState==StartState)
|
||||||
|
{
|
||||||
|
mState = CommaState;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Parser::parseSpecial (code, loc, scanner);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringParser::append (std::vector<Interpreter::Type_Code>& code)
|
||||||
|
{
|
||||||
|
std::copy (mCode.begin(), mCode.end(), std::back_inserter (code));
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringParser::reset()
|
||||||
|
{
|
||||||
|
mState = StartState;
|
||||||
|
mCode.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
46
components/compiler/stringparser.hpp
Normal file
46
components/compiler/stringparser.hpp
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
#ifndef COMPILER_STRINGPARSER_H_INCLUDED
|
||||||
|
#define COMPILER_STRINGPARSER_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <components/interpreter/types.hpp>
|
||||||
|
|
||||||
|
#include "parser.hpp"
|
||||||
|
|
||||||
|
namespace Compiler
|
||||||
|
{
|
||||||
|
class Literals;
|
||||||
|
|
||||||
|
class StringParser : public Parser
|
||||||
|
{
|
||||||
|
enum State
|
||||||
|
{
|
||||||
|
StartState, CommaState
|
||||||
|
};
|
||||||
|
|
||||||
|
Literals& mLiterals;
|
||||||
|
State mState;
|
||||||
|
std::vector<Interpreter::Type_Code> mCode;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
StringParser (ErrorHandler& errorHandler, Context& context, Literals& literals);
|
||||||
|
|
||||||
|
virtual bool parseName (const std::string& name, const TokenLoc& loc,
|
||||||
|
Scanner& scanner);
|
||||||
|
///< Handle a name token.
|
||||||
|
/// \return fetch another token?
|
||||||
|
|
||||||
|
virtual bool parseSpecial (int code, const TokenLoc& loc, Scanner& scanner);
|
||||||
|
///< Handle a special character token.
|
||||||
|
/// \return fetch another token?
|
||||||
|
|
||||||
|
void append (std::vector<Interpreter::Type_Code>& code);
|
||||||
|
///< Append code for parsed string.
|
||||||
|
|
||||||
|
void reset();
|
||||||
|
///< Reset parser to clean state.
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
|
@ -4,6 +4,7 @@
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include "opcodes.hpp"
|
#include "opcodes.hpp"
|
||||||
#include "runtime.hpp"
|
#include "runtime.hpp"
|
||||||
|
@ -19,10 +20,71 @@ namespace Interpreter
|
||||||
if (arg0!=0)
|
if (arg0!=0)
|
||||||
throw std::logic_error ("message box buttons not implemented yet");
|
throw std::logic_error ("message box buttons not implemented yet");
|
||||||
|
|
||||||
|
// message
|
||||||
int index = runtime[0];
|
int index = runtime[0];
|
||||||
runtime.pop();
|
runtime.pop();
|
||||||
|
std::string message = runtime.getStringLiteral (index);
|
||||||
|
|
||||||
|
// additional parameters
|
||||||
|
std::string formattedMessage;
|
||||||
|
|
||||||
|
for (std::size_t i=0; i<message.size(); ++i)
|
||||||
|
{
|
||||||
|
char c = message[i];
|
||||||
|
|
||||||
|
if (c!='%')
|
||||||
|
formattedMessage += c;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
if (i<message.size())
|
||||||
|
{
|
||||||
|
c = message[i];
|
||||||
|
|
||||||
|
if (c=='S' || c=='s')
|
||||||
|
{
|
||||||
|
int index = runtime[0];
|
||||||
|
runtime.pop();
|
||||||
|
formattedMessage += runtime.getStringLiteral (index);
|
||||||
|
}
|
||||||
|
else if (c=='g' || c=='G')
|
||||||
|
{
|
||||||
|
int value = *reinterpret_cast<const int *> (&runtime[0]);
|
||||||
|
runtime.pop();
|
||||||
|
|
||||||
|
std::ostringstream out;
|
||||||
|
out << value;
|
||||||
|
formattedMessage += out.str();
|
||||||
|
}
|
||||||
|
else if (c=='f' || c=='F' || c=='.')
|
||||||
|
{
|
||||||
|
while (c!='f' && i<message.size())
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
float value = *reinterpret_cast<const float *> (&runtime[0]);
|
||||||
|
runtime.pop();
|
||||||
|
|
||||||
|
std::ostringstream out;
|
||||||
|
out << value;
|
||||||
|
formattedMessage += out.str();
|
||||||
|
}
|
||||||
|
else if (c=='%')
|
||||||
|
formattedMessage += "%";
|
||||||
|
else
|
||||||
|
{
|
||||||
|
formattedMessage += "%";
|
||||||
|
formattedMessage += c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// buttons (not implemented)
|
||||||
std::vector<std::string> buttons;
|
std::vector<std::string> buttons;
|
||||||
runtime.getContext().messageBox (runtime.getStringLiteral (index), buttons);
|
|
||||||
|
runtime.getContext().messageBox (formattedMessage, buttons);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue