-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhitespace.cpp
474 lines (418 loc) · 11.5 KB
/
whitespace.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
const std::string IMP_STACK = "\x20";
const std::string IMP_ARITHMETIC = "\x9\x20";
const std::string IMP_HEAP = "\x9\x9";
const std::string IMP_FLOWC = "\xA";
const std::string IMP_IO = "\x9\xA";
// Stack manipulation
const std::string STACK_PUSH = "\x20"; // one argument
const std::string STACK_DUPLICATE = "\xA\x20";
const std::string STACK_COPY = "\x9\x20"; // one argument
const std::string STACK_SWAP = "\xA\x9";
const std::string STACK_POP = "\xA\xA";
const std::string STACK_REMOVE = "\x9\xA"; // one argument
// Arithmetic
const std::string ARITHMETIC_ADD = "\x20\x20";
const std::string ARITHMETIC_SUB = "\x20\x9";
const std::string ARITHMETIC_MULTIPLICATION = "\x20\xA";
const std::string ARITHMETIC_DIVISION = "\x9\x20";
const std::string ARITHMETIC_MODULO = "\x9\x9";
// Heap access
const std::string HEAP_STORE = "\x20";
const std::string HEAP_RETRIEVE = "\x9";
// Flow control
const std::string FLOW_MARK = "\x20\x20";
const std::string FLOW_CALL_SUBROUTINE = "\x20\x9";
const std::string FLOW_JUMP = "\x20\xA";
const std::string FLOW_ZJUMP = "\x9\x20"; // jump if top of the stack is zero
const std::string FLOW_NJUMP = "\x9\x9"; // negative jump
const std::string FLOW_RETURN = "\x9\xA";
const std::string FLOW_ENDPROGRAM = "\xA\xA";
// IO
const std::string IO_OUTPUT_CHAR = "\x20\x20";
const std::string IO_OUTPUT_NUMBER = "\x20\x9";
const std::string IO_READ_CHAR = "\x9\x20";
const std::string IO_READ_NUMBER = "\x9\x9";
inline bool is_valid_ws(char cn){ return cn == '\x20' || cn == '\xA' || cn == '\x9'; }
typedef int (*command_execution)(void *);
struct command
{
command_execution exec;
int parameter_count;
command() {}
command(command_execution e, int p, const std::string &dename) : exec(e), parameter_count(p),
dname(dename) {}
const std::string &get_debug_name() { return dname; }
private:
std::string dname;
};
typedef std::map<std::string, command> instruction_set;
typedef std::map<std::string, instruction_set> imp_set;
typedef std::map<std::string, int> namesp;
typedef long long int WHSPARAM;
struct parameter
{
std::string raw;
WHSPARAM val;
};
struct token
{
instruction_set *iset;
command *exec;
std::vector<parameter> parameters;
};
std::vector<WHSPARAM> stack;
std::map<int, WHSPARAM> heap;
int stack_push(void *info)
{
WHSPARAM number = *(WHSPARAM *)info;
stack.push_back(number);
return 1;
}
int stack_duplicate(void *)
{
if(stack.empty()) return 0;
WHSPARAM last_item = stack.back();
stack.push_back(last_item);
return 1;
}
int stack_copy(void *v)
{
WHSPARAM slot = *(WHSPARAM *)v;
if(slot < 0 || slot >= stack.size()) return 0;
stack.push_back(stack[slot]);
return 1;
}
int stack_swap(void *)
{
if(stack.size() < 2) return 0;
WHSPARAM last = stack.back();
WHSPARAM lastmi = stack[stack.size() - 2];
WHSPARAM temp;
temp = last;
last = lastmi;
lastmi = temp;
stack.pop_back();
stack.pop_back();
stack.push_back(lastmi);
stack.push_back(last);
return 1;
}
int stack_pop(void *)
{
if(stack.empty()) return 0;
stack.pop_back();
return 1;
}
int stack_erase(void *w)
{
int count = *(int *)w;
if(stack.empty()) return 0;
WHSPARAM last = stack.back();
stack.pop_back();
if(stack.size() < count) return 0;
stack.erase(stack.begin(), stack.end() - count);
return 1;
}
int* deliver()
{
static int w[2];
w[1] = stack[stack.size() - 1];
w[0] = stack[stack.size() - 2];
stack.pop_back();
stack.pop_back();
return w;
}
int arithmetic_add(void *)
{
if(stack.size() < 2) return 0;
int *n = deliver();
stack.push_back(n[0] + n[1]);
return 1;
}
int arithmetic_sub(void *)
{
if(stack.size() < 2) return 0;
int *n = deliver();
stack.push_back(n[0] - n[1]);
return 1;
}
int arithmetic_multiplication(void *)
{
if(stack.size() < 2) return 0;
int *n = deliver();
stack.push_back(n[0] * n[1]);
return 1;
}
int arithmetic_division(void *)
{
if(stack.size() < 2) return 0;
int *n = deliver();
if(n[1] == 0) return 0;
stack.push_back(n[0] / n[1]);
return 1;
}
int arithmetic_modulo(void *)
{
if(stack.size() < 2) return 0;
int *n = deliver();
stack.push_back(n[0] % n[1]);
return 1;
}
int heap_store(void *)
{
if(stack.size() < 2) return 0;
WHSPARAM adr = stack[stack.size() - 2];
WHSPARAM val = stack.back();
heap[adr] = val;
stack.pop_back();
stack.pop_back();
return 1;
}
int heap_retrieve(void *)
{
if(stack.size() < 1) return 0;
WHSPARAM adr = stack.back();
stack.pop_back();
stack.push_back(heap[adr]);
return 1;
}
int dummy(void *)
{
return 0;
}
int io_output_char(void *)
{
WHSPARAM a = stack.back();
std::cout << (char)a;
stack.pop_back();
return 1;
}
int io_output_number(void *)
{
WHSPARAM a = stack.back();
std::cout << a;
stack.pop_back();
return 1;
}
int io_read_char(void *)
{
char c;
std::cin.get(c);
heap[stack.back()] = c;
stack.pop_back();
return 1;
}
int io_read_number(void *)
{
WHSPARAM numb;
std::cin >> numb;
heap[stack.back()] = numb;
stack.pop_back();
return 1;
}
template<class rtype, class wtype>
rtype *detect(std::ifstream &data, wtype &f)
{
std::string tf;
while(!data.eof())
{
char c;
data.read(&c, 1);
if(!is_valid_ws(c)) continue;
tf += c;
typename wtype::iterator wfs = f.find(tf);
if(wfs != f.end())
return &wfs->second;
}
return 0;
}
std::string read_parameter(std::ifstream &data)
{
std::string fg;
while(!data.eof())
{
char c;
data.read(&c, 1);
if(c == '\xA') break;
if(c == '\x20')
fg += '0';
else if(c == '\x9')
fg += '1';
}
return fg;
}
WHSPARAM generate_value(const std::string &fstr)
{
WHSPARAM val = 0;
for(int j = 0; j < fstr.length(); j++)
val |= fstr[fstr.length() - j - 1] == '1' ? (1 << j) : 0;
if(fstr[0] == '1') val = ~val + 1;
return val;
}
int jump(const namesp &space, std::string where)
{
namesp::const_iterator kc = space.find(where);
if(kc == space.end())
{
std::cout << "Jump to unknown space.";
return -1;
}
return kc->second;
}
typedef instruction_set *(*deimp)(std::ifstream &, imp_set &);
typedef command *(*decom)(std::ifstream &, instruction_set &);
deimp detect_imp = detect<instruction_set, imp_set>;
decom detect_command = detect<command, instruction_set>;
void run_code(const std::vector<token> &code, const namesp &space)
{
int current_instruction = 0;
bool running = true;
std::vector<int> function_stack;
static std::vector<std::string> fcj;
fcj.push_back("flow_zjump(label)");
fcj.push_back("flow_njump(label)");
while(running && current_instruction < code.size())
{
token w = code[current_instruction];
if(w.exec->get_debug_name() == "flow_endprogram()")
{
std::cout << "Finished executing script.";
running = false;
break;
}
if(w.exec->get_debug_name() == "flow_jump(label)")
if((current_instruction = jump(space, w.parameters[0].raw)) < 0)
return;
if(!stack.empty() && find(fcj.begin(), fcj.end(), w.exec->get_debug_name()) != fcj.end())
{
int stack_last;
stack_last = stack.back();
stack.pop_back();
if(w.exec->get_debug_name() == "flow_zjump(label)" )
if(stack_last == 0)
if((current_instruction = jump(space, w.parameters[0].raw)) < 0)
return;
if(w.exec->get_debug_name() == "flow_njump(label)")
if(stack_last < 0)
if((current_instruction = jump(space, w.parameters[0].raw)) < 0)
return;
}
if(w.exec->get_debug_name() == "flow_call_subroutine(label)")
{
function_stack.push_back(current_instruction + 1);
current_instruction = jump(space, w.parameters[0].raw);
if(current_instruction < 0) return;
}
if(w.exec->get_debug_name() == "flow_return()")
{
int where = function_stack.back();
function_stack.pop_back();
current_instruction = where;
continue;
}
void *k = (w.parameters.empty() ? 0 : (void *)&w.parameters[0].val);
w.exec->exec(k);
current_instruction += 1;
}
}
int main(int argc, char *argv[])
{
if(argc < 2)
{
std::cout << "Usage: <filename>";
return 1;
}
std::ifstream data(argv[1]);
if(data.fail())
{
std::cout << "Error while trying to read file " << argv[1] << std::endl;
return 2;
}
instruction_set stack_set;
stack_set[STACK_PUSH] = command(stack_push, 1, "stack_push(value)");
stack_set[STACK_COPY] = command(stack_copy, 1, "stack_copy(value)");
stack_set[STACK_SWAP] = command(stack_swap, 0, "stack_swap()");
stack_set[STACK_DUPLICATE] = command(stack_duplicate, 0, "stack_duplicate()");
stack_set[STACK_POP] = command(stack_pop, 0, "stack_pop()");
stack_set[STACK_REMOVE] = command(stack_erase, 1, "stack_remove(value)");
instruction_set arithmetic_set;
arithmetic_set[ARITHMETIC_ADD] = command(arithmetic_add, 0, "arithmetic_add()");
arithmetic_set[ARITHMETIC_SUB] = command(arithmetic_sub, 0, "arithmetic_sub()");
arithmetic_set[ARITHMETIC_MULTIPLICATION] = command(arithmetic_multiplication, 0, "arithmetic_multiplication()");
arithmetic_set[ARITHMETIC_DIVISION] = command(arithmetic_division, 0, "arithmetic_division()");
arithmetic_set[ARITHMETIC_MODULO] = command(arithmetic_modulo, 0, "arithmetic_modulo()");
instruction_set heap_set;
heap_set[HEAP_STORE] = command(heap_store, 0, "heap_store(where, value)");
heap_set[HEAP_RETRIEVE] = command(heap_retrieve, 0, "heap_retrieve(from)");
instruction_set flow_set;
flow_set[FLOW_MARK] = command(dummy, 1, "flow_mark(label)");
flow_set[FLOW_CALL_SUBROUTINE] = command(dummy, 1, "flow_call_subroutine(label)");
flow_set[FLOW_JUMP] = command(dummy, 1, "flow_jump(label)");
flow_set[FLOW_ZJUMP] = command(dummy, 1, "flow_zjump(label)");
flow_set[FLOW_NJUMP] = command(dummy, 1, "flow_njump(label)");
flow_set[FLOW_RETURN] = command(dummy, 0, "flow_return()");
flow_set[FLOW_ENDPROGRAM] = command(dummy, 0, "flow_endprogram()");
instruction_set io_set;
io_set[IO_OUTPUT_CHAR] = command(io_output_char, 0, "io_output_char()");
io_set[IO_OUTPUT_NUMBER] = command(io_output_number, 0, "io_output_number()");
io_set[IO_READ_CHAR] = command(io_read_char, 0, "io_read_char()");
io_set[IO_READ_NUMBER] = command(io_read_number, 0, "io_read_number");
imp_set is;
is[IMP_STACK] = stack_set;
is[IMP_ARITHMETIC] = arithmetic_set;
is[IMP_HEAP] = heap_set;
is[IMP_FLOWC] = flow_set;
is[IMP_IO] = io_set;
std::vector<token> code;
namesp global_namespace;
bool syntax_valid = true;
int instruction_index = 0;
while(syntax_valid && !data.eof())
{
char cn = data.peek();
if(!is_valid_ws(cn))
{
data.get(cn);
continue;
}
token cur_token;
instruction_set *w = detect_imp(data, is);
if(!w)
{
std::cout << "Error while detecting imp type. exiting!";
syntax_valid = false;
break;
}
command *wm = detect_command(data, *w);
if(!wm)
{
std::cout << "Command not recognized from this specified instruction set.";
syntax_valid = false;
break;
}
cur_token.iset = w;
cur_token.exec = wm;
std::string param;
for(int i = 0; i < wm->parameter_count; i++)
{
parameter p;
p.raw = (param = read_parameter(data));
p.val = generate_value(p.raw);
cur_token.parameters.push_back(p);
}
if(wm->get_debug_name() == "flow_mark(label)")
global_namespace[param] = instruction_index;
code.push_back(cur_token);
instruction_index++;
}
if(syntax_valid)
run_code(code, global_namespace);
data.close();
return 0;
}