-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathbytecode.h
More file actions
363 lines (316 loc) · 6.49 KB
/
Copy pathbytecode.h
File metadata and controls
363 lines (316 loc) · 6.49 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#ifndef cpp_bytecode_h
#define cpp_bytecode_h
#include "common.h"
namespace Bytecode
{
namespace Interpreter
{
//^expression
class Expression
{
public:
virtual ~Expression() {}
virtual double evaluate() = 0;
};
//^expression
//^number
class NumberExpression : public Expression
{
public:
NumberExpression(double value)
: value_(value)
{}
virtual double evaluate()
{
return value_;
}
private:
double value_;
};
//^number
//^addition
class AdditionExpression : public Expression
{
public:
AdditionExpression(Expression* left, Expression* right)
: left_(left),
right_(right)
{}
virtual double evaluate()
{
// 计算操作数
double left = left_->evaluate();
double right = right_->evaluate();
// 把它们加起来
return left + right;
}
private:
Expression* left_;
Expression* right_;
};
//^addition
}
//^magic-api
void setHealth(int wizard, int amount);
void setWisdom(int wizard, int amount);
void setAgility(int wizard, int amount);
//^magic-api
//^magic-api-fx
void playSound(int soundId);
void spawnParticles(int particleType);
//^magic-api-fx
void setHealth(int wizard, int amount) {}
void setWisdom(int wizard, int amount) {}
void setAgility(int wizard, int amount) {}
void playSound(int soundId) {}
void spawnParticles(int particleType) {}
int getHealth(int wizard) { return 0; }
int getAgility(int wizard) { return 0; }
int getWisdom(int wizard) { return 0; }
void increaseHealth()
{
//^increase-health
setHealth(0, getHealth(0) +
(getAgility(0) + getWisdom(0)) / 2);
//^increase-health
}
//^instruction-enum
enum Instruction
{
INST_SET_HEALTH = 0x00,
INST_SET_WISDOM = 0x01,
INST_SET_AGILITY = 0x02,
INST_PLAY_SOUND = 0x03,
INST_SPAWN_PARTICLES = 0x04
//^omit
,INST_LITERAL,
INST_GET_HEALTH,
INST_GET_WISDOM,
INST_GET_AGILITY,
INST_ADD,
//^omit
};
//^instruction-enum
static const int SOUND_BANG = 1;
static const int PARTICLE_FLAME = 1;
void interpretInstruction()
{
Instruction instruction = INST_SET_AGILITY;
//^interpret-instruction
switch (instruction)
{
case INST_SET_HEALTH:
setHealth(0, 100);
break;
case INST_SET_WISDOM:
setWisdom(0, 100);
break;
case INST_SET_AGILITY:
setAgility(0, 100);
break;
case INST_PLAY_SOUND:
playSound(SOUND_BANG);
break;
case INST_SPAWN_PARTICLES:
spawnParticles(PARTICLE_FLAME);
break;
//^omit
case INST_LITERAL:
case INST_GET_HEALTH:
case INST_GET_WISDOM:
case INST_GET_AGILITY:
case INST_ADD:
break;
//^omit
}
//^interpret-instruction
}
namespace NoParams
{
//^vm
class VM
{
public:
void interpret(char bytecode[], int size)
{
for (int i = 0; i < size; i++)
{
char instruction = bytecode[i];
switch (instruction)
{
// 每条指令的跳转分支……
//^omit
case INST_SPAWN_PARTICLES:
break;
default:
break;
//^omit
}
}
}
};
//^vm
}
namespace Stack
{
//^stack
class VM
{
public:
VM()
: stackSize_(0)
{}
// 其他代码……
private:
static const int MAX_STACK = 128;
int stackSize_;
int stack_[MAX_STACK];
};
//^stack
}
namespace PushPop
{
//^push-pop
class VM
{
private:
void push(int value)
{
// 检查栈溢出
assert(stackSize_ < MAX_STACK);
stack_[stackSize_++] = value;
}
int pop()
{
// 保证栈不是空的
assert(stackSize_ > 0);
return stack_[--stackSize_];
}
// 其余的代码
//^omit
void interpret();
static const int MAX_STACK = 128;
int stackSize_;
int stack_[MAX_STACK];
//^omit
};
//^push-pop
void VM::interpret()
{
int instruction = INST_SET_AGILITY;
//^pop-instructions
switch (instruction)
{
case INST_SET_HEALTH:
{
int amount = pop();
int wizard = pop();
setHealth(wizard, amount);
break;
}
case INST_SET_WISDOM:
case INST_SET_AGILITY:
// 像上面一样……
case INST_PLAY_SOUND:
playSound(pop());
break;
case INST_SPAWN_PARTICLES:
spawnParticles(pop());
break;
}
//^pop-instructions
char bytecode[123];
int i = 0;
switch (INST_LITERAL)
{
//^interpret-literal
case INST_LITERAL:
{
// 从字节码中读取下一个字节
int value = bytecode[++i];
push(value);
break;
}
//^interpret-literal
//^read-stats
case INST_GET_HEALTH:
{
int wizard = pop();
push(getHealth(wizard));
break;
}
case INST_GET_WISDOM:
case INST_GET_AGILITY:
// 你知道思路了吧……
//^read-stats
//^add
case INST_ADD:
{
int b = pop();
int a = pop();
push(a + b);
break;
}
//^add
}
}
}
namespace TaggedValue
{
//^tagged-value
enum ValueType
{
TYPE_INT,
TYPE_DOUBLE,
TYPE_STRING
};
struct Value
{
ValueType type;
union
{
int intValue;
double doubleValue;
char* stringValue;
};
};
//^tagged-value
}
namespace ValueOop
{
enum ValueType
{
TYPE_INT,
TYPE_DOUBLE,
TYPE_STRING
};
//^value-interface
class Value
{
public:
virtual ~Value() {}
virtual ValueType type() = 0;
virtual int asInt() {
// 只能在int上调用
assert(false);
return 0;
}
// 其他转换方法……
};
//^value-interface
//^int-value
class IntValue : public Value
{
public:
IntValue(int value)
: value_(value)
{}
virtual ValueType type() { return TYPE_INT; }
virtual int asInt() { return value_; }
private:
int value_;
};
//^int-value
}
}
#endif