-
Notifications
You must be signed in to change notification settings - Fork 0
/
susan.c
396 lines (382 loc) · 8.7 KB
/
susan.c
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
/*
* [2015 April 5] Invoke the surreal engine, assert divine inspiration.
* I, shafted by the enemy camping, and my team feeding, refuse this reality!
*
* The enemy has camped my lane,
* My team does not but feed.
* I've stacked my Q, for many gains,
* But they still have the lead.
*
* Diana ends our ADC,
* Soraka can't support.
* Our mid is ganked by Master Yi,
* The river void of wards.
*
* Despite my stats, so base at start,
* I diligently farmed.
* Though Teemo spams his blinding darts,
* I push up unalarmed.
*
* The Zed again begins to rant,
* When I am sprung upon.
* This cruel reality I can't,
* Accept or carry on.
*
* "When four of them are top for me,
* You guys apply some press're.
* I played my part so flawlessly,
* I could have done no better."
*
* I leave their tower to recall,
* Return to base - defend!
* To Diana my teammates quickly fall,
* A fight breaks out and ends.
*
* I find myself with half my health,
* In-the centre of their team.
* I fight outscored, but kill the four,
* By ult and proccing Sheen.
*
* Diana faces me once more,
* She bursts another half.
* Reality at me abhors,
* And she begins to laugh.
*
* The fatigue is gone from Nasus' eyes, all teeth are clenched in hate.
* The world -was- destined to be this way, but he will change his fate.
* And now his Q is raised, and now his lets it go,
* And now the air is shattered by the force of Nasus' blow.
*
* Oh, somewhere in the fields of justice the sun is shining bright,
* DJ Sona is playing somewhere, and somewhere hearts are light.
* And somewhere Zeds are laughing, and somewhere Teemos shout.
* But there is no joy for Nasus for his destiny played out!
* - One of dfr
*
*/
#include "os.h"
static void susan_eval(char**, int);
static int susan_eval_line(char*, int, int*, int*);
static void susan_print_bad_command(char*);
static int susan_parse_register(char*);
static int susan_parse_hex(char*);
void susan(Vechs* v) {
int n = os_vechs_size(v);
int max_lines = 1;
for (int i = 0; i < n; i++) {
if (os_vechs_get(v, i) == '\n') {
max_lines++;
}
}
char* lines[max_lines];
int lineno = 0;
Vechs* buf = os_vechs_new(32);
for (int i = 0; i < n; i++) {
char ch = os_vechs_get(v, i);
if (ch == '\n') {
if (os_vechs_size(buf) > 0) {
lines[lineno++] = str_from_vechs(buf);
os_vechs_clear(buf);
}
} else {
os_vechs_push(buf, ch);
}
}
if (os_vechs_size(buf) > 0) {
lines[lineno++] = str_from_vechs(buf);
}
os_vechs_delete(buf);
bdel_printstr("Executing program: ");
bdel_printhex(lineno);
bdel_printstr(" lines\n");
susan_eval(lines, lineno);
for (int i = 0; i < lineno; i++) {
os_free(lines[i]);
}
}
void susan_eval(char** lines, int num_lines) {
int registers[8];
char* memory = os_malloc(1024);
int pc = 0;
while (true) {
int next_pc = susan_eval_line(lines[pc], pc, registers, (int*) memory);
if (next_pc < 0 || next_pc >= num_lines) {
break;
}
pc = next_pc;
}
os_free(memory);
}
int susan_eval_line(char* line, int pc, int* registers, int* memory) {
char** parts = 0;
int num_parts = 0;
skye_parse(line, &parts, &num_parts);
int next_pc = pc + 1;
if (num_parts == 0) {
return next_pc;
}
char* cmd = parts[0];
if (streq(cmd, "literal")) {
if (num_parts < 3) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
int x = susan_parse_hex(parts[2]);
registers[r] = x;
} else if (streq(cmd, "add")) {
if (num_parts < 4) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
int r2 = susan_parse_register(parts[2]);
if (r2 < 0) {
susan_print_bad_command(cmd);
return -1;
}
int r3 = susan_parse_register(parts[3]);
if (r3 < 0) {
susan_print_bad_command(cmd);
return -1;
}
registers[r] = registers[r2] + registers[r3];
} else if (streq(cmd, "mul")) {
if (num_parts < 4) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
int r2 = susan_parse_register(parts[2]);
if (r2 < 0) {
susan_print_bad_command(cmd);
return -1;
}
int r3 = susan_parse_register(parts[3]);
if (r3 < 0) {
susan_print_bad_command(cmd);
return -1;
}
registers[r] = registers[r2] * registers[r3];
} else if (streq(cmd, "printhex")) {
if (num_parts < 2) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
bdel_printhex(registers[r]);
} else if (streq(cmd, "readhex")) {
if (num_parts < 2) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
registers[r] = bdel_readhex();
} else if (streq(cmd, "jmp")) {
if (num_parts < 2) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
next_pc = registers[r];
} else if (streq(cmd, "blt")) {
if (num_parts < 4) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
int r2 = susan_parse_register(parts[2]);
if (r2 < 0) {
susan_print_bad_command(cmd);
return -1;
}
if (registers[r] < registers[r2]) {
next_pc = susan_parse_hex(parts[3]);
}
} else if (streq(cmd, "beq")) {
if (num_parts < 4) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
int r2 = susan_parse_register(parts[2]);
if (r2 = 0) {
susan_print_bad_command(cmd);
return -1;
}
if (registers[r] == registers[r2]) {
next_pc = susan_parse_hex(parts[3]);
}
} else if (streq(cmd, "bge")) {
if (num_parts < 4) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
int r2 = susan_parse_register(parts[2]);
if (r2 < 0) {
susan_print_bad_command(cmd);
return -1;
}
if (registers[r] >= registers[r2]) {
next_pc = susan_parse_hex(parts[3]);
}
} else if (streq(cmd, "echo")) {
if (num_parts < 2) {
susan_print_bad_command(cmd);
return -1;
}
for (int i = 1; i < num_parts - 1; i++) {
bdel_printstr(parts[i]);
bdel_putchar(' ');
}
bdel_printstr(parts[num_parts - 1]);
} else if (streq(cmd, "load")) {
if (num_parts < 3) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
int r2 = susan_parse_register(parts[2]);
if (r2 < 0) {
susan_print_bad_command(cmd);
return -1;
}
registers[r] = memory[registers[r2]];
} else if (streq(cmd, "store")) {
if (num_parts < 3) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
int r2 = susan_parse_register(parts[2]);
if (r2 < 0) {
susan_print_bad_command(cmd);
return -1;
}
memory[registers[r]] = registers[r2];
} else if (streq(cmd, "putchar")) {
if (num_parts < 2) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
bdel_putchar(registers[r]);
} else if (streq(cmd, "sleep")) {
if (num_parts < 2) {
susan_print_bad_command(cmd);
return -1;
}
int r = susan_parse_register(parts[1]);
if (r < 0) {
susan_print_bad_command(cmd);
return -1;
}
os_sleep(registers[r]);
} else {
bdel_printstr("Syntax error: unknown ");
bdel_printstr(cmd);
bdel_putchar('\n');
return -1;
}
for (int i = 0; i < num_parts; i++) {
os_free(parts[i]);
}
if (parts != 0) {
os_free(parts);
}
return next_pc;
}
void susan_print_bad_command(char* cmd) {
bdel_printstr("Invalid use of ");
bdel_printstr(cmd);
bdel_putchar('\n');
}
int susan_parse_register(char* str) {
if (str[0] == '\0') {
return -1;
}
if ('0' <= str[1] && str[1] <= '7') {
return str[1] - '0';
}
return -1;
}
int susan_parse_hex(char* str) {
bool neg = false;
int i = 0;
if (str[i] == '-') {
neg = true;
i = 1;
}
if (str[i] != '0' || str[i + 1] != 'x') {
return 0;
}
i += 2;
int x = 0;
while (true) {
char ch = str[i];
if (ch == '\0') {
break;
}
int y = -1;
if ('0' <= ch && ch <= '9') {
y = ch - '0';
}
if ('a' <= ch && ch <= 'f') {
y = ch - 'a' + 10;
}
if (y >= 0) {
x = (x << 4) | y;
}
i++;
}
return x * (neg ? -1 : 1);
}