| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <list> |
| 4 |
#include <vector> |
| 5 |
#include <memory> |
| 6 |
|
| 7 |
#include "GuiExpression.h" |
| 8 |
|
| 9 |
namespace parser { class DefTokeniser; } |
| 10 |
|
| 11 |
namespace gui |
| 12 |
{ |
| 13 |
|
| 14 |
class Variable; |
| 15 |
typedef std::shared_ptr<Variable> VariablePtr; |
| 16 |
|
| 17 |
class GuiWindowDef; |
| 18 |
|
| 19 |
// A single script statement |
| 20 |
// Statement arguments remain in std::string form and are evaluated on the fly |
| 21 |
struct Statement |
| 22 |
{ |
| 23 |
// Possible statement types |
| 24 |
enum Type |
| 25 |
{ |
| 26 |
ST_NOP, |
| 27 |
ST_JMP, |
| 28 |
ST_SET, |
| 29 |
ST_TRANSITION, |
| 30 |
ST_IF, |
| 31 |
ST_SET_FOCUS, |
| 32 |
ST_ENDGAME, |
| 33 |
ST_RESET_TIME, |
| 34 |
ST_SHOW_CURSOR, |
| 35 |
ST_RESET_CINEMATICS, |
| 36 |
ST_LOCALSOUND, |
| 37 |
ST_RUNSCRIPT, |
| 38 |
ST_EVALREGS, |
| 39 |
}; |
| 40 |
|
| 41 |
// The statement type |
| 42 |
Type type; |
| 43 |
|
| 44 |
typedef std::vector<std::shared_ptr<IGuiExpression<std::string>>> Arguments; |
| 45 |
Arguments args; |
| 46 |
|
| 47 |
// Condition used by ST_IF |
| 48 |
std::shared_ptr<IGuiExpression<bool>> _condition; |
| 49 |
|
| 50 |
// The jump destination used by ST_IF and ST_JMP |
| 51 |
std::size_t jmpDest; |
| 52 |
|
| 53 |
Statement(Type type_) : |
| 54 |
type(type_), |
| 55 |
jmpDest(0) |
| 56 |
{} |
| 57 |
}; |
| 58 |
typedef std::shared_ptr<Statement> StatementPtr; |
| 59 |
|
| 60 |
class GuiScript |
| 61 |
{ |
| 62 |
private: |
| 63 |
// The owning windowDef |
| 64 |
GuiWindowDef& _owner; |
| 65 |
|
| 66 |
// The compiled "code" consists of a series of statements |
| 67 |
typedef std::vector<StatementPtr> StatementList; |
| 68 |
StatementList _statements; |
| 69 |
|
| 70 |
// The instruction pointer |
| 71 |
std::size_t _ip; |
| 72 |
|
| 73 |
// Current block level for parsing the code |
| 74 |
std::size_t _curLevel; |
| 75 |
|
| 76 |
public: |
| 77 |
GuiScript(GuiWindowDef& owner); |
| 78 |
|
| 79 |
// Construct this script from a token stream. |
| 80 |
// It expects the initial opening brace as first token. |
| 81 |
void constructFromTokens(parser::DefTokeniser& tokeniser); |
| 82 |
|
| 83 |
// Start or continue execution of this script |
| 84 |
void execute(); |
| 85 |
|
| 86 |
private: |
| 87 |
// Interprets the given string and returns the target object |
| 88 |
VariablePtr getVariableFromExpression(const std::shared_ptr<IGuiExpression<std::string>>& expr); |
| 89 |
std::string getValueFromExpression(const std::shared_ptr<IGuiExpression<std::string>>& expr); |
| 90 |
|
| 91 |
const Statement& getStatement(std::size_t index); |
| 92 |
|
| 93 |
// Adds a statement to the compiled list, returns the index of this statement |
| 94 |
std::size_t pushStatement(const StatementPtr& statement); |
| 95 |
|
| 96 |
// Returns the current position in the code |
| 97 |
std::size_t getCurPosition(); |
| 98 |
|
| 99 |
GuiExpressionPtr getExpression(parser::DefTokeniser& tokeniser); |
| 100 |
std::shared_ptr<IGuiExpression<bool>> getIfExpression(parser::DefTokeniser& tokeniser); |
| 101 |
|
| 102 |
// Reads a "statement", which can be single one or a group (surrounded by curly braces) |
| 103 |
void parseStatement(parser::DefTokeniser& tokeniser); |
| 104 |
|
| 105 |
// Contains a large if-else array to jump into the specialised methods below |
| 106 |
void switchOnToken(const std::string& token, parser::DefTokeniser& tokeniser); |
| 107 |
|
| 108 |
// Parse routines specialised on keywords |
| 109 |
void parseIfStatement(parser::DefTokeniser& tokeniser); |
| 110 |
void parseSetStatement(parser::DefTokeniser& tokeniser); |
| 111 |
void parseTransitionStatement(parser::DefTokeniser& tokeniser); |
| 112 |
void parseSetFocusStatement(parser::DefTokeniser& tokeniser); |
| 113 |
void parseEndGameStatement(parser::DefTokeniser& tokeniser); |
| 114 |
void parseResetTimeStatement(parser::DefTokeniser& tokeniser); |
| 115 |
void parseShowCursorStatement(parser::DefTokeniser& tokeniser); |
| 116 |
void parseResetCinematicStatement(parser::DefTokeniser& tokeniser); |
| 117 |
void parseLocalSoundStatement(parser::DefTokeniser& tokeniser); |
| 118 |
void parseRunScriptStatement(parser::DefTokeniser& tokeniser); |
| 119 |
void parseEvalRegsStatement(parser::DefTokeniser& tokeniser); |
| 120 |
}; |
| 121 |
typedef std::shared_ptr<GuiScript> GuiScriptPtr; |
| 122 |
|
| 123 |
} // namespace |