added basic message box formatting

actorid
Marc Zinnschlag 15 years ago
parent aeb41105c4
commit 804aed6298

@ -4,11 +4,13 @@
#include <stdexcept>
#include <cassert>
#include <algorithm>
#include <stack>
#include "generator.hpp"
#include "scanner.hpp"
#include "errorhandler.hpp"
#include "locals.hpp"
#include "stringparser.hpp"
namespace Compiler
{
@ -184,24 +186,10 @@ namespace Compiler
popOperator();
}
void ExprParser::parseArguments (const std::string& arguments, Scanner& scanner)
{
ExprParser parser (getErrorHandler(), getContext(), mLocals, mLiterals, true);
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);
}
parseArguments (arguments, scanner, mCode);
}
ExprParser::ExprParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
@ -285,6 +273,7 @@ namespace Compiler
parseArguments ("f", scanner);
Generator::squareRoot (mCode);
mOperands.push_back ('f');
mNextOperand = false;
return true;
@ -433,5 +422,60 @@ namespace Compiler
assert (mOperands.size()==1);
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();
}
}
}

@ -48,9 +48,9 @@ namespace Compiler
void pushBinaryOperator (char c);
void close();
void parseArguments (const std::string& arguments, Scanner& scanner);
void parseArguments (const std::string& arguments, Scanner& scanner);
public:
ExprParser (ErrorHandler& errorHandler, Context& context, Locals& locals,
@ -89,6 +89,13 @@ namespace Compiler
char append (std::vector<Interpreter::Type_Code>& code);
///< Generate code for parsed expression.
/// \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);
}
void pushString (CodeContainer& code, Literals& literals, const std::string& value)
{
int index = literals.addString (value);
opPushInt (code, index);
}
void assignToLocal (CodeContainer& code, char localType,
int localIndex, const CodeContainer& value, char valueType)
{

@ -17,6 +17,8 @@ namespace Compiler
void pushInt (CodeContainer& code, Literals& literals, int 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,
int localIndex, const CodeContainer& value, char valueType);

@ -78,6 +78,37 @@ namespace Compiler
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);
mState = EndState;
return false;

@ -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();
}
}

@ -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 <vector>
#include <string>
#include <sstream>
#include "opcodes.hpp"
#include "runtime.hpp"
@ -18,11 +19,72 @@ namespace Interpreter
{
if (arg0!=0)
throw std::logic_error ("message box buttons not implemented yet");
// message
int index = runtime[0];
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;
runtime.getContext().messageBox (runtime.getStringLiteral (index), buttons);
runtime.getContext().messageBox (formattedMessage, buttons);
}
};
}

Loading…
Cancel
Save