-
Notifications
You must be signed in to change notification settings - Fork 1
/
SchemeMachine.cpp
395 lines (352 loc) · 9.01 KB
/
SchemeMachine.cpp
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* SchemeMachine.cpp
*
* Created on: 17.04.2013
* Author: urandon
*/
#include "SchemeMachine.h"
#include "Task.h"
#include "Errors.h"
#include <cstdlib>
#include <cstdio>
SchemeMachine::SchemeMachine(const char * ip, int port, struct timeval default_timeval):
local_names(NULL), cycle(1), last_line(0)
{
client = new GameClient(ip, port, default_timeval);
}
SchemeMachine::~SchemeMachine() {
delete client;
}
/* SchemeMachine::task_stack_t */
const int SchemeMachine::task_stack_t::DEFAULT = 256;
SchemeMachine::task_stack_t::task_stack_t(){
size = 0;
allocated = DEFAULT;
data = (typeof(data)) malloc(allocated * sizeof(* data));
}
SchemeMachine::task_stack_t::~task_stack_t(){
delete [] data;
}
void SchemeMachine::task_stack_t::push(Task * task){
const int DISCOUNT_FACTOR = 16;
int newsize = size + 1;
if(newsize >= allocated){
allocated *= 2;
data = (typeof(data)) realloc(data, allocated * sizeof(* data));
}
if(allocated > DEFAULT && newsize > 0 && newsize * DISCOUNT_FACTOR < allocated){
allocated /= DISCOUNT_FACTOR / 4;
data = (typeof(data)) realloc(data, allocated * sizeof(* data));
}
data[size] = task;
size = newsize;
}
Task * SchemeMachine::task_stack_t::pop(int last_line){
if(size <= 0){
throw TaskStackUnderflowError(last_line);
}
return data[--size];
}
bool SchemeMachine::task_stack_t::isEmpty(){
return size == 0;
}
void SchemeMachine::task_stack_t::print(){
for(int i = size-1; i >= 0; --i){
printf("task#%d ", i);
data[i]->print();
printf("\n");
}
}
/* SchemeMachine::result_stack_t */
const int SchemeMachine::result_stack_t::DEFAULT = 64;
SchemeMachine::result_stack_t::result_stack_t():size(0),actual_top(0){
allocated = DEFAULT;
data = (typeof(data)) malloc(allocated * sizeof(* data));
}
SchemeMachine::result_stack_t::~result_stack_t(){
delete [] data;
}
void SchemeMachine::result_stack_t::push(SchObjectRef ref){
int newsize = size + 1;
const int DISCOUNT_FACTOR = 16;
if(newsize >= allocated){
allocated *= 2;
data = (typeof(data)) realloc(data, allocated * sizeof(* data));
}
if(actual_top > size){ // collect garbage
for(int i = size; i < actual_top; ++i){
delete data[i];
}
actual_top = size;
}
if(allocated > DEFAULT && newsize > 0 && actual_top * DISCOUNT_FACTOR < allocated){
allocated /= DISCOUNT_FACTOR / 4;
data = (typeof(data)) realloc(data, allocated * sizeof(* data));
}
data[size] = new SchObjectRef(ref);
actual_top = size = newsize;
}
SchObjectRef ** SchemeMachine::result_stack_t::pop(int N, int last_line){
if(size < N){
throw ResultStackUnderflowError(last_line);
}
if(actual_top > size){ // collect garbage
for(int i = size; i < actual_top; ++i){
delete data[i];
}
actual_top = size;
}
size -= N;
return data + size;
}
void SchemeMachine::result_stack_t::print(){
for(int i = size-1; i >= 0; --i){
printf("res#%d ", i);
(*data[i])->print();
printf("\n");
}
}
/* global names */
const int SchemeMachine::global_names_t::DEFAULT = 64;
SchemeMachine::global_names_t::global_names_t(){
allocated = DEFAULT;
size = 0;
ids = (int *) malloc(allocated * sizeof(int));
refs = (typeof(refs)) malloc(allocated * sizeof(*refs));
}
SchemeMachine::global_names_t::~global_names_t(){
for(int i = 0; i < size; ++i){
delete refs[i];
}
delete ids;
delete refs;
}
SchObjectRef * SchemeMachine::global_names_t::searchRef(int id){
for(int i = 0; i < size; ++i){
if(ids[i] == id){
return refs[i];
}
}
return NULL;
}
void SchemeMachine::global_names_t::push(int id, const SchObjectRef & ref){
bool found = false;
for(int i = 0; i < size && !found; ++i){
if(ids[i] == id){
delete refs[i];
refs[i] = new SchObjectRef(ref);
found = true;
}
}
if(!found){
int newsize = size+1;
if(newsize >= allocated){
allocated *= 2;
ids = (int *) realloc(ids, allocated * sizeof(* ids));
refs = (typeof(refs)) realloc(refs, allocated * sizeof(* refs));
}
ids[size] = id;
refs[size] = new SchObjectRef(ref);
size = newsize;
}
}
/* SchemeMachine public methods */
SchObjectRef ** SchemeMachine::popResult(int N){ return (N > 0) ? results.pop(N, last_line) : NULL; }
void SchemeMachine::pushResult(SchObjectRef ref){ results.push(ref); }
void SchemeMachine::addTask(Task * task){
tasks.push(task);
}
void SchemeMachine::launch(bool verbose){
while(!tasks.isEmpty()){
performTask();
if(verbose){
printf("\n\n\t\tCycle ## %d\n", cycle++);
printf("\tTask stack::\n");
tasks.print();
printf("\tResult stack::\n");
results.print();
if(local_names != NULL){
int level = 0;
printf("\tLocal names::");
for(local_names_t * curr = local_names; curr != NULL; curr = curr->prev){
printf("\nlevel#%d ", level++);
curr->current->print();
}
printf("\n");
}
if(global_names.size != 0){
printf("\tGlobal names::");
for(int i = 0; i < global_names.size; ++i){
printf(" [%d ", global_names.ids[i]);
(*global_names.refs[i])->print();
printf("]\n");
// printf(" [%d]", global_names.ids[i]);
}
printf("\n");
}
}
}
}
SchObjectRef SchemeMachine::getRef(int id){
SchObjectRef * res;
for(local_names_t * local = local_names; local != NULL; local = local->prev){
if( (res = local->current->searchRef(id)) != NULL ) {
return *res;
}
}
if( (res = global_names.searchRef(id)) != NULL ){
return *res;
}
throw UndefinedSymbolError(last_line, id);
}
void SchemeMachine::performTask(){
Task * task = tasks.pop(last_line);
int n;
switch(task->getType()){
case Task::EVALUATE :
{
EvalTask * evtask;
SchObjectRef * ref;
int step;
evtask = static_cast<EvalTask *>(task);
ref = new SchObjectRef(*evtask->ref);
last_line = (*ref)->getLine();
step = evtask->step;
delete task;
(*ref)->evaluate(*this, step);
delete ref;
}
break;
case Task::CALL :
{
// tail recursion optimisation. if CALL and argument haz been evaluated --> del
CallTask * ctask;
ctask = static_cast<CallTask *>(task);
int step = ctask->step;
if(!tasks.isEmpty() && tasks.data[tasks.size-1]->getType() == Task::DEL_NAMES &&
((*ctask->ref)->getType() != SchObject::SPECIAL || static_cast<SchSpecial *>(&**ctask->ref)->prepareArgs()))
{
// swap tasks
Task * tmp = tasks.data[tasks.size-1]; // DEL_NAMES
tasks.push(task);
tasks.data[tasks.size-2] = tasks.data[tasks.size-1]; // DEL <- CALL
tasks.data[tasks.size-1] = tmp; // CALL <- tmp
} else {
SchObjectRef * ref;
ref = new SchObjectRef(*ctask->ref);
last_line = (*ref)->getLine();
n = ctask->n;
delete task;
(*ref)->call(*this, n, step);
delete ref;
}
}
break;
case Task::EXECUTE :
{
ExecTask * extask;
int step;
bool must_swap = false;
extask = static_cast<ExecTask *>(task);
if(!tasks.isEmpty() && tasks.data[tasks.size-1]->getType() == Task::DEL_NAMES){
if((*extask->ref)->getType() == SchObject::LIST){ // TODO
must_swap = false;
} else
if((*extask->ref)->getType() != SchObject::SPECIAL){
must_swap = true;
} else
if(static_cast<SchSpecial *>(&**extask->ref)->prepareArgs()){
must_swap = true;
}
}
if(must_swap) {
// swap tasks
Task * tmp = tasks.data[tasks.size-1]; // DEL_NAMES
tasks.push(task);
tasks.data[tasks.size-2] = tasks.data[tasks.size-1]; // DEL <- CALL
tasks.data[tasks.size-1] = tmp; // CALL <- tmp
} else {
SchObjectRef * ref;
ref = new SchObjectRef(*extask->ref);
last_line = (*ref)->getLine();
step = extask->step;
n = extask->n;
delete task;
(*ref)->execute(*this, n, step);
delete ref;
}
}
break;
case Task::ALIAS :
{
AliasTask * altask;
altask = static_cast<AliasTask *>(task);
global_names.push(altask->id, *(altask->ref));
delete task;
}
break;
case Task::ADD_NAMES :
{
AddNamesTask * atask;
local_names_t * tmp;
atask = static_cast<AddNamesTask *>(task);
tmp = (typeof(tmp)) malloc (sizeof(*tmp));
tmp->current = atask->map;
tmp->prev = local_names;
local_names = tmp;
delete task;
}
break;
case Task::DEL_NAMES :
{
delete task;
if(local_names == NULL){
throw LocalNamespaceUnderflowError(last_line);
} else {
local_names_t * tmp = local_names->prev;
delete local_names->current;
delete local_names;
local_names = tmp;
}
}
break;
case Task::PRINT :
{
int N;
SchObjectRef ** refs;
N = static_cast<PrintTask *>(task)->N;
refs = results.pop(N, last_line);
for(int i = N-1; i >= 0; --i){
(*refs[i])->print();
}
printf("\n");
delete task;
}
break;
case Task::PUSH_RESULT :
{
SchObjectRef * ref;
ref = new SchObjectRef( * static_cast<PushResultTask *>(task)->ref );
delete task;
pushResult(*ref);
delete ref;
}
break;
case Task::NETWORK :
{
NetworkTask * ntask = static_cast<NetworkTask *>(task);
int quantity = ntask->quantity, price = ntask->price, pid=ntask->player_id;
if(ntask->gtype == NetworkTask::PERFORM){
client->perform(ntask->subtype, quantity, price);
} else {
int res = client->getInfo(ntask->gtype, ntask->subtype, pid, quantity, price);
pushResult(SchObjectRef(new SchInteger(last_line, res)));
}
delete task;
break;
}
default:
throw RuntimeError(last_line);
break;
}
}