-
Notifications
You must be signed in to change notification settings - Fork 0
/
nyan.c
234 lines (209 loc) · 6.33 KB
/
nyan.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
#include <malloc.h>
#include <stdio.h>
#include <wchar.h>
#include <fcntl.h>
#include <stdint.h>
#include "nyan.h"
#include "mouse.h"
#include "util.h"
#define MEMSIZE 1024
struct jump_pair {
int jz;
int jnz;
};
void set_jump_pair(struct nyan_s *nyan, struct jump_pair *jump_pairs, int *jplen) {
int stack[nyan->len];
int stack_idx = 0;
for (int i = 0; i < nyan->len; i++) {
switch (nyan->commands[i]) {
case JUMP_ZERO:
stack[stack_idx] = i;
stack_idx++;
break;
case JUMP_NON_ZERO:
stack_idx--;
jump_pairs[*jplen].jz = stack[stack_idx];
jump_pairs[*jplen].jnz = i;
(*jplen)++;
break;
default:
// do nothing
break;
}
}
if (stack_idx > 0) {
printf("Error: unmatched [ found\n");
}
else if (stack_idx < 0) {
printf("Error: unmatched ] found\n");
}
}
int run_nyan(struct nyan_s nyan, int * rv) {
int *memory = malloc(sizeof(int) * MEMSIZE);
for (int i = 0; i < MEMSIZE; i++) {
memory[i] = 0;
}
int pointer = 0;
int module_pointer = 0;
if (rv == NULL) {
memory[pointer] = 0;
} else {
memory[pointer] = *rv;
}
uintptr_t max_gone = 0;
uintptr_t min_gone = 0;
struct jump_pair *jump_pairs = malloc(sizeof(struct jump_pair) * nyan.len);
int jplen = 0;
set_jump_pair(&nyan, jump_pairs, &jplen);
for (int i = 0; i < nyan.len; i++) {
switch (nyan.commands[i]) {
case POINTER_ADD:
pointer++;
break;
case POINTER_SUB:
pointer--;
break;
case VALUE_ADD:
memory[pointer]++;
break;
case VALUE_SUB:
memory[pointer]--;
break;
case DEBUG_PRINT:
printf("{%d}", memory[pointer]);
break;
case PRINT:
printf("%c", memory[pointer]);
break;
case JUMP_ZERO:
if (memory[pointer] == 0) {
// loop jump_pairs and find jz=i
for (int j = 0; j < jplen; j++) {
if (jump_pairs[j].jz == i) {
i = jump_pairs[j].jnz;
break;
}
}
}
break;
case JUMP_NON_ZERO:
if (memory[pointer] != 0) {
// loop jump_pairs and find jnz=i
for (int j = 0; j < jplen; j++) {
if (jump_pairs[j].jnz == i) {
i = jump_pairs[j].jz;
break;
}
}
}
break;
case MODULE_POINTER_ADD:
module_pointer++;
break;
case MODULE_POINTER_SUB:
module_pointer--;
break;
case MODULE_RETREIVE:
{
struct mouse* mice = nyan.mice;
for (int j = 0; j < nyan.mice_len; j++) {
if (mice[j].x == module_pointer) {
// call child mice
struct nyan_s child_nyan = parse_nyan(mice[j].f);
int ret = run_nyan(child_nyan, &memory[pointer]);
memory[pointer] = ret;
break;
}
}
}
break;
case MODULE_RETURN:
// return to parent module
return memory[pointer];
break;
}
}
return -1;
}
// create a function that returns nyan struct
struct nyan_s parse_nyan(char* filename) {
wchar_t* code = NULL;
size_t len = 0;
wread_file(&code, &len, filename);
char* mbuffer = NULL;
size_t msize;
char* mousename = remove_nyan_ext(filename);
strcat(mousename, ".mouse");
read_file(&mbuffer, &msize, mousename);
int mlen;
struct mouse* mice = parse_mouse(mbuffer, &mlen);
struct nyan_s nyan;
nyan.commands = calloc(len, sizeof(unsigned int*));
for (int i = 0; i < len; i++) {
nyan.commands[i] = IGNORE;
}
nyan.len = len;
nyan.mice = mice;
nyan.mice_len = mlen;
// for (int i = 0; i < nyan.mice_len; i++) {
// int x;
// int y;
// memcpy(&x, &nyan.mice[i]->x, sizeof(int));
// memcpy(&y, &nyan.mice[i]->y, sizeof(int));
// printf("NYMouse %d: %d, %d, %s\n", i, x, y, nyan.mice[i]->f);
// }
for (int i = 0; i < len; i++) {
switch (code[i]) {
case L'?':
nyan.commands[i] = POINTER_ADD;
break;
case L'!':
nyan.commands[i] = POINTER_SUB;
break;
case L'냥':
nyan.commands[i] = VALUE_ADD;
break;
case L'냐':
nyan.commands[i] = VALUE_SUB;
break;
case L'뀨':
nyan.commands[i] = DEBUG_PRINT;
break;
case L'.':
nyan.commands[i] = PRINT;
break;
case L'~':
nyan.commands[i] = JUMP_ZERO;
break;
case L'-':
nyan.commands[i] = JUMP_NON_ZERO;
break;
case L'먕':
nyan.commands[i] = MODULE_POINTER_ADD;
break;
case L'먀':
nyan.commands[i] = MODULE_POINTER_SUB;
break;
case L':':
nyan.commands[i] = MODULE_RETREIVE;
break;
case L';':
nyan.commands[i] = MODULE_RETURN;
break;
case L' ':
nyan.commands[i] = IGNORE;
// ignore whitespace
break;
case L'\n':
nyan.commands[i] = IGNORE;
// ignore newlines
break;
default:
wprintf(L"Unknown command: %lc\n", code[i]);
}
}
free(mbuffer);
free(mousename);
free(code);
return nyan;
}