-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompiler.h
253 lines (211 loc) · 5.9 KB
/
Compiler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#pragma once
#ifndef COMPILER_H
#define COMPILER_H
#include <vector>
#include <string>
#include <bitset>
#include <unordered_map>
#include <tuple>
#include "vm/game.h"
using std::vector;
using std::string;
using std::bitset;
using std::unordered_map;
using std::tuple;
namespace Battler {
const int RUN_ERROR = -1;
const int RUN_FINISHED = 0;
const int RUN_WAITING_FOR_INTERACTION_RETURN = 1;
enum class OpcodeType
{
// Block headers
GAME_BLK_HEADER = 0,
CARD_BLK_HEADER,
SETUP_BLK_HEADER,
PHASE_BLK_HEADER,
TURN_BLK_HEADER,
IF_BLK_HEADER,
FOREACHPLAYER_BLK_HEADER,
FOREACHPLAYER_BLK_END,
BLK_END,
// Math & Logic
ADD,
SUBTRACT,
MULTIPLY,
DIVIDE,
PLAYERS_L_VALUE,
L_VALUE_DOT_SEPERATED_REF_CHAIN,
PLAYERS_R_VALUE,
L_VALUE,
R_VALUE_REF,
R_VALUE_DOT_SEPERATED_REF_CHAIN,
DOT_SEPERATED_REF_CHAIN_END,
R_VALUE,
COMPARE,
// Stack operations
STACK_SOURCE_RANDOM_CARD_TYPE,
STACK_SOURCE_TOP,
STACK_SOURCE_BOTTOM,
STACK_SOURCE_CHOOSE,
STACK_SOURCE_CHOICE_GATHER,
STACK_DEST_TOP,
STACK_DEST_BOTTOM,
// Stack cut operations,
STACK_CUT_SOURCE_CHOOSE,
STACK_CUT_SOURCE,
STACK_CUT_DEST_TOP,
STACK_CUT_DEST_BOTTOM,
STACK_CUT_SOURCE_TOP,
STACK_CUT_SOURCE_BOTTOM,
STACK_CUT_SOURCE_CHOICE_GATHER,
// other declarations
ATTR_DECL,
ATTR_ASSIGN,
ATTR_DATA_TYPE,
ATTR_DATA,
DO_DECL,
WINNER_DECL,
// just in case we need it
NO_OP,
};
enum class PROC_MODE
{
GAME,
CARD,
PHASE,
SETUP,
TURN,
FOREACH,
IF,
};
#define TYPE_CODE_T uint16_t
#define DATA_IX_T uint32_t
#define OPCODE_CONV_T uint64_t
#define TYPE_CODE_T_MASK uint16_t(0xFF)
// data type codes
const TYPE_CODE_T STRING_TC = 0x01;
const TYPE_CODE_T INT_TC = 0x02;
const TYPE_CODE_T BOOL_TC = 0x03;
const TYPE_CODE_T FLOAT_TC = 0x04;
const TYPE_CODE_T VISIBLE_STACK_TC = 0x05;
const TYPE_CODE_T HIDDEN_STACK_TC = 0x06;
const TYPE_CODE_T PRIVATE_STACK_TC = 0x07;
const TYPE_CODE_T FLAT_VISIBLE_STACK_TC = 0x08;
const TYPE_CODE_T FLAT_HIDDEN_STACK_TC = 0x09;
const TYPE_CODE_T FLAT_PRIVATE_STACK_TC = 0x0A;
const TYPE_CODE_T CARD_TC = 0x0B;
const TYPE_CODE_T STACK_REF = 0x0C;
const TYPE_CODE_T PLAYER_REF_TC = 0x0D;
typedef void(stack_move_callback_fun)(
int from,
int to,
bool from_top,
bool to_top,
const int* cardUUIDs,
int nCards,
void* data
);
class Opcode
{
public:
Opcode() : data(0), blk_size(0) {};
OpcodeType type;
int blk_size;
uint64_t data;
};
enum class InputOperationType {
MOVE,
CUT
};
class CardInputWait
{
public:
int srcStackID;
int dstStackID;
int nExpected{0};
bool dstTop;
InputOperationType type;
bool fixedDest{true};
bool fixedSrc{true};
int cutPoint;
std::vector<Card> cardsToMove;
};
class Program
{
public:
Program() : m_depth(0), m_current_opcode_index(0), m_stack_move_callback(nullptr) {};
void _Parse(vector<string>);
void Compile(vector<string>);
int Run(bool load = false);
int RunSetup();
int RunTurn(bool resume=false);
vector<AttrCont>& locale_stack();
bool m_waitingForUserInteraction{false};
CardInputWait m_card_input_wait;
void SetStackMoveCallbackFun(stack_move_callback_fun* fun, void* data);
bool AddCardToWaitingInput(Card c);
vector<Opcode> opcodes();
Game& game();
inline vector<Token> _Tokens() {return m_tokens;}
inline Expression _GetRootExpression() {return m_rootExpression;}
private:
//compiled data
vector<Opcode> m_opcodes;
vector<Token> m_tokens;
Expression m_rootExpression;
vector<string> m_strings;
vector<int> m_ints;
vector<bool> m_bools;
int m_setup_index;
int m_turn_index;
int m_depth;
int m_depth_store;
unordered_map<string, int> m_phase_indexes;
//runtime data
Game m_game;
int m_current_opcode_index;
vector<AttrCont> m_locale_stack;
vector<PROC_MODE> m_proc_mode_stack;
vector<string> m_block_name_stack;
stack_move_callback_fun* m_stack_move_callback;
void* m_stack_callback_data;
void factor_expression(Expression);
void compile_name(vector<Token>, bool lvalue);
int run(Opcode code, bool load = false);
static AttributeType s_type_code_to_attribute_type(TYPE_CODE_T);
void ignore_block();
void compile_expression(Expression);
//copied from run.h
AttrCont* GetObjectAttrContPtrFromIdentifier(vector<string>::iterator namesBegin, vector<string>::iterator namesEnd);
AttrCont* GetGlobalObjectAttrContPtr(AttrCont& cont, string name);
int resolve_number_expression();
bool resolve_bool_expression();
string resolve_string_expression();
float resolve_float_expression();
Attr resolve_expression_to_attr();
void read_name(vector<string>& names, OpcodeType nameType);
Attr* get_attr_ptr(vector<string>& names);
Attr get_attr_rvalue(vector<string>& names);
string get_card_parent_name(string nameSequence);
string get_card_name(string nameSequence);
Stack* get_stack_ptr(vector<string>& names);
bool compare_attrs(Attr a, Attr b);
Attr subtract_attrs(Attr a, Attr b);
Attr add_attrs(Attr a, Attr b);
Attr multiply_attrs(Attr a, Attr b);
Attr divide_attrs(Attr a, Attr b);
void call_stack_move_callback(int from, int to, bool fromTop, bool toTop, const int* cardIds, int nCards);
};
class CompileError {
public:
Token t;
string reason;
CompileError(string reason, Token t) : reason{ reason }, t{ t } {}
};
class VMError {
public:
string reason;
VMError(string reason) : reason{ reason } {}
};
}
#endif // !COMPILER_H