Skip to content

Commit

Permalink
redirecting output of expression evalutation to new report channel
Browse files Browse the repository at this point in the history
  • Loading branch information
zinnschlag committed Apr 26, 2011
1 parent 125319c commit 861dc6a
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 58 deletions.
13 changes: 13 additions & 0 deletions components/compiler/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ namespace
code.push_back (Compiler::Generator::segment3 (0, buttons));
}

void opReport (Compiler::Generator::CodeContainer& code)
{
code.push_back (Compiler::Generator::segment5 (58));
}

void opFetchLocalShort (Compiler::Generator::CodeContainer& code)
{
code.push_back (Compiler::Generator::segment5 (21));
Expand Down Expand Up @@ -516,6 +521,14 @@ namespace Compiler
opMessageBox (code, buttons);
}

void report (CodeContainer& code, Literals& literals, const std::string& message)
{
int index = literals.addString (message);

opPushInt (code, index);
opReport (code);
}

void fetchLocal (CodeContainer& code, char localType, int localIndex)
{
opPushInt (code, localIndex);
Expand Down
2 changes: 2 additions & 0 deletions components/compiler/generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ namespace Compiler
void message (CodeContainer& code, Literals& literals, const std::string& message,
int buttons);

void report (CodeContainer& code, Literals& literals, const std::string& message);

void fetchLocal (CodeContainer& code, char localType, int localIndex);

void jump (CodeContainer& code, int offset);
Expand Down
4 changes: 2 additions & 2 deletions components/compiler/lineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ namespace Compiler
{
case 'l':

Generator::message (mCode, mLiterals, "%g", 0);
Generator::report (mCode, mLiterals, "%g");
break;

case 'f':

Generator::message (mCode, mLiterals, "%f", 0);
Generator::report (mCode, mLiterals, "%f");
break;

default:
Expand Down
6 changes: 5 additions & 1 deletion components/interpreter/docs/vmformat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,9 @@ op 55: explicit reference = stack[0]; pop; disable explicit reference
op 56: explicit reference = stack[0]; pop; push 1, if explicit reference is disabled, 0 else
op 57: explicit reference = stack[0]; pop;
replace stack[0] with distance between explicit reference and a reference of ID stack[0]
opcodes 58-33554431 unused
op 58: report string literal index in stack[0];
additional arguments (if any) in stack[n]..stack[1];
n is determined according to the message string
all arguments are removed from stack
opcodes 59-33554431 unused
opcodes 33554432-67108863 reserved for extensions
2 changes: 1 addition & 1 deletion components/interpreter/installopcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ namespace Interpreter
interpreter.installSegment5 (54, new OpEnableExplicit);
interpreter.installSegment5 (55, new OpDisableExplicit);
interpreter.installSegment5 (56, new OpGetDisabledExplicit);
interpreter.installSegment5 (58, new OpReport);

// script control
interpreter.installSegment5 (46, new OpScriptRunning);
Expand All @@ -106,4 +107,3 @@ namespace Interpreter
interpreter.installSegment5 (57, new OpGetDistanceExplicit);
}
}

133 changes: 79 additions & 54 deletions components/interpreter/miscopcodes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,66 @@

namespace Interpreter
{
inline std::string formatMessage (const std::string& message, Runtime& runtime)
{
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].mInteger;
runtime.pop();
formattedMessage += runtime.getStringLiteral (index);
}
else if (c=='g' || c=='G')
{
Type_Integer value = runtime[0].mInteger;
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 = runtime[0].mFloat;
runtime.pop();

std::ostringstream out;
out << value;
formattedMessage += out.str();
}
else if (c=='%')
formattedMessage += "%";
else
{
formattedMessage += "%";
formattedMessage += c;
}
}
}
}

return formattedMessage;
}

class OpMessageBox : public Opcode1
{
public:
Expand All @@ -36,63 +96,28 @@ namespace Interpreter

std::reverse (buttons.begin(), buttons.end());

// additional parameters
std::string formattedMessage;
// handle additional parameters
std::string formattedMessage = formatMessage (message, runtime);

for (std::size_t i=0; i<message.size(); ++i)
{
char c = message[i];
runtime.getContext().messageBox (formattedMessage, buttons);
}
};

if (c!='%')
formattedMessage += c;
else
{
++i;
if (i<message.size())
{
c = message[i];

if (c=='S' || c=='s')
{
int index = runtime[0].mInteger;
runtime.pop();
formattedMessage += runtime.getStringLiteral (index);
}
else if (c=='g' || c=='G')
{
Type_Integer value = runtime[0].mInteger;
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 = runtime[0].mFloat;
runtime.pop();

std::ostringstream out;
out << value;
formattedMessage += out.str();
}
else if (c=='%')
formattedMessage += "%";
else
{
formattedMessage += "%";
formattedMessage += c;
}
}
}
}
class OpReport : public Opcode0
{
public:

runtime.getContext().messageBox (formattedMessage, buttons);
virtual void execute (Runtime& runtime)
{
// message
int index = runtime[0].mInteger;
runtime.pop();
std::string message = runtime.getStringLiteral (index);

// handle additional parameters
std::string formattedMessage = formatMessage (message, runtime);

runtime.getContext().report (formattedMessage);
}
};

Expand Down

0 comments on commit 861dc6a

Please sign in to comment.