-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.txt
357 lines (327 loc) · 5.2 KB
/
stdlib.txt
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
def io_draw(var charcode, var x, var y) {
asm {
#load 1 r0;
#load 2 r1;
#load 3 r2;
#draw r0 r1 r2;
};
}
var io_x = 0;
var io_y = 0;
def io_cr();
def io_wait();
def io_clear() {
var i = 0;
while (i < 52) {
var j = 0;
while (j < 28) {
asm {
#load 2 r0;
#load 1 r1;
#literal " " r2;
#draw r2 r0 r1;
};
j = j + 1;
}
i = i + 1;
}
io_x = 0;
io_y = 0;
}
def io_normalize(var n) {
if (io_x > 52 - n) {
io_x = 0;
io_y = io_y + 1;
}
if (io_y > 28 - 1) {
io_y = 26;
io_cr();
io_y = 25;
io_cr();
io_y = 24;
io_wait();
io_clear();
}
}
def io_putchar(var ch) {
io_normalize(1);
var x = io_x;
var y = io_y;
asm {
#load 3 r0;
#load 2 r1;
#load 1 r2;
#draw r0 r1 r2;
};
io_x = io_x + 1;
}
def io_readchar() {
io_normalize(1);
var x = io_x;
var y = io_y;
var z = asm {
#load 3 r0;
#load 2 r1;
#keyboard r0 r1 r13;
};
io_x = io_x + 1;
z;
}
def io_puthex(var ch) {
io_normalize(4);
var x = io_x;
var y = io_y;
asm {
#load 3 r0;
#load 2 r1;
#load 1 r2;
#printhex r0 r1 r2;
};
io_x = io_x + 4;
}
def io_readhex() {
io_normalize(4);
var x = io_x;
var y = io_y;
var z = asm {
#load 3 r0;
#load 2 r1;
#keyhex r0 r1 r13;
};
io_x = io_x + 4;
z;
}
def io_putdec(var ch) {
io_normalize(4);
var x = io_x;
var y = io_y;
asm {
#load 3 r0;
#load 2 r1;
#load 1 r2;
#printdec r0 r1 r2;
};
io_x = io_x + 4;
}
def io_readdec() {
io_normalize(4);
var x = io_x;
var y = io_y;
var z = asm {
#load 3 r0;
#load 2 r1;
#keydec r0 r1 r13;
};
io_x = io_x + 4;
z;
}
def io_cr() {
io_x = 0;
var i = 0;
while (i < 28) {
io_putchar(' ');
i = i + 1;
}
io_x = 0;
}
def io_endl() {
io_x = 255;
io_normalize(0);
}
def io_wait() {
io_endl();
io_endl();
echo "press any key to continue";
io_readchar();
}
def error(var code) {
asm {
#load 1 r0;
#output r0;
#die;
};
}
def heap_get(var address) {
asm {
#load 1 r0;
#unheap r0 r13;
};
}
def heap_set(var address, var value) {
asm {
#load 1 r0;
#load 2 r1;
#heap r1 r0;
};
value;
}
def malloc(var n) {
var i = asm {
#literal 0 r0;
#unheap r0 r13;
};
var ret = 0;
var break_i = 0;
while (!break_i && i < 1024) {
var x = heap_get(i);
if (x == 0) {
i = i + 2;
var j = 0;
var ok = 1;
var break_j = 0;
while (!break_j && j < n) {
if (heap_get(i + j) == 0) {
j = j + 1;
} else {
ok = 0;
break_j = 1;
}
}
if (ok == 1) {
heap_set(i - 2, 1);
heap_set(i - 1, n);
ret = i;
break_i = 1;
} else {
i = i + j;
}
} else {
i = i + 2 + heap_get(i + 1);
}
}
if (i > 1024 - 1) {
error(3);
}
ret;
}
def free(var address) {
var n = heap_get(address - 1);
var i = address - 2;
while (i < address + n) {
heap_set(i, 0);
i = i + 1;
}
}
def vector_new(var n) {
var pt = malloc(3);
var array = malloc(n);
heap_set(pt, array);
heap_set(pt + 1, n);
heap_set(pt + 2, 0);
pt;
}
def vector_delete(var pt) {
free(heap_get(pt));
free(pt);
}
def vector_get(var pt, var i) {
heap_get(heap_get(pt) + i);
}
def vector_set(var pt, var i, var x) {
heap_set(heap_get(pt) + i, x);
}
def vector_size(var pt) {
heap_get(pt + 2);
}
def vector_normalize(var pt) {
var alloc_prev = heap_get(pt + 1);
if (alloc_prev == vector_size(pt)) {
var size_prime = alloc_prev + 4;
var array_prime = malloc(size_prime);
var array = heap_get(pt);
var i = 0;
while (i < alloc_prev) {
heap_set(array_prime + i, heap_get(array + i));
i = i + 1;
}
free(array);
heap_set(pt, array_prime);
heap_set(pt + 1, size_prime);
}
}
def vector_push(var pt, var x) {
vector_normalize(pt);
var size = vector_size(pt);
vector_set(pt, size, x);
heap_set(pt + 2, size + 1);
}
def vector_pop(var pt) {
var next_size = vector_size(pt) - 1;
var x = vector_get(pt, next_size);
heap_set(pt + 2, next_size);
x;
}
def mergesort(var pt) {
var size = vector_size(pt);
if (size < 2) {
var cp = vector_new(2);
var i = 0;
while (i < size) {
vector_push(cp, vector_get(pt, i));
i = i + 1;
}
cp;
} else {
var left = vector_new(4);
var right = vector_new(4);
var mid = size / 2;
var i = 0;
while (i < mid) {
vector_push(left, vector_get(pt, i));
i = i + 1;
}
while (i < size) {
vector_push(right, vector_get(pt, i));
i = i + 1;
}
var left_sorted = mergesort(left);
vector_delete(left);
var right_sorted = mergesort(right);
vector_delete(right);
var sorted = vector_new(4);
i = 0;
var j = 0;
while (i < vector_size(left_sorted) && j < vector_size(right_sorted)) {
var a = vector_get(left_sorted, i);
var b = vector_get(right_sorted, j);
if (b > a) {
vector_push(sorted, a);
i = i + 1;
} else {
vector_push(sorted, b);
j = j + 1;
}
}
while (i < vector_size(left_sorted)) {
vector_push(sorted, vector_get(left_sorted, i));
i = i + 1;
}
while (j < vector_size(right_sorted)) {
vector_push(sorted, vector_get(right_sorted, j));
j = j + 1;
}
vector_delete(left_sorted);
vector_delete(right_sorted);
sorted;
}
}
def io_printstring(var vec) {
var i = 0;
while (i < vector_size(vec)) {
io_putchar(vector_get(vec, i));
i = i + 1;
}
vec;
}
def rand() {
asm {
#rand r13;
};
}
def abs(var x) {
if (x < 0) {
-x;
} else {
x;
}
}