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
|
#include "vm.h"
#include <math.h>
#include <string.h>
#include "dyn_arr.h"
#include "us_debug.h"
#include "uscript.h"
struct vm vm;
void init_vm(void)
{
vm.objs = da_create(struct us_val, 128);
vm.cf = vm.cf_stack;
vm.stacktop = vm.stack;
}
void deinit_vm(void)
{
for (int i = 0; i < da_len(vm.objs); i++) {
free_val(vm.objs[i]);
}
da_clear(vm.objs); // not needed, but makes me feel better :)
da_free(vm.objs);
}
static
bool as_bool(struct us_val v)
{
if (v.type == VAL_ZILCH)
return false;
if (v.type == VAL_BOOL)
return get_bool(v);
return true;
}
static
struct us_str *concat(struct us_val a, struct us_val b)
{
int a_len;
char *a_str = val_to_str(a, &a_len);
int b_len;
char *b_str = val_to_str(b, &b_len);
int len = a_len + b_len;
char *chars = mem_alloc(sizeof(char) * (len + 1));
memcpy(chars, a_str, a_len);
memcpy(chars + a_len, b_str, b_len);
chars[len] = '\0';
mem_free(a_str);
mem_free(b_str);
return take_str(chars, len);
}
static
u16 read_short(struct us_proto *proto, int *i)
{
return (u16)(proto->bytecode[++*i] << 8) | proto->bytecode[++*i];
}
static
void close_upvals(struct us_val *to)
{
struct us_upval *upval = vm.open_upvals;
while (upval && upval->loc > to) {
upval->closed = *upval->loc;
upval->loc = &upval->closed;
upval = upval->next;
}
vm.open_upvals = upval;
}
void us_exec(struct us_func *func)
{
#define read_byte() (func->proto->bytecode[++i])
#define read_const() (func->proto->constants[read_byte()])
vm.cf++;
vm.cf->func = func;
vm.cf->stackbot = vm.stacktop - func->proto->argc;
for (int i = 0; i < da_len(func->proto->bytecode); i++) {
enum bytecode instruction = func->proto->bytecode[i];
// putc('>', stderr);
// for (struct us_val *val = vm.stack; val < vm.stacktop; val++) {
// char *val_str = val_to_str(*val, NULL);
// if (val == vm.cf->stackbot - 1)
// fprintf(stderr, " %s >", val_str);
// else
// fprintf(stderr, " %s |", val_str);
// mem_free(val_str);
// }
// putc('\n', stderr);
// putc('>', stderr);
// print_instruction(func->proto, i);
switch (instruction) {
case BC_LOAD:
vm_push(read_const());
break;
case BC_LOAD_FUNC: {
struct us_proto *proto = get_proto(read_const());
struct us_func *new_func = create_func(proto);
for (int j = 0; j < proto->upvalc; j++) {
u8 is_local = read_byte();
u8 index = read_byte();
if (is_local) {
struct us_upval *upval = create_upval(
vm.cf->stackbot + index
);
upval->next = vm.open_upvals;
vm.open_upvals = upval;
new_func->upvals[j] = upval;
} else {
new_func->upvals[j] =
func->upvals[index];
}
}
vm_push(wrap_func(new_func));
break;
}
case BC_SMALL_INT:
vm_push(create_num(read_byte()));
break;
case BC_FALSE: vm_push(create_bool(false)); break;
case BC_TRUE: vm_push(create_bool(true)); break;
case BC_ZILCH: vm_push(create_zilch()); break;
case BC_ARR: {
struct us_arr *arr = create_arr();
u8 arr_len = read_byte();
// elements are in reverse order, so we add them in the
// correct order, and pop them all off at the end
for (u8 i = 0; i < arr_len; i++) {
da_append(
struct us_val,
&arr->e,
*(vm.stacktop - arr_len + i)
);
}
vm.stacktop -= arr_len;
vm_push(wrap_arr(arr));
break;
}
case BC_SET_LOCAL:
vm.cf->stackbot[read_byte()] = vm_peek();
break;
case BC_GET_LOCAL:
vm_push(vm.cf->stackbot[read_byte()]);
break;
case BC_GET_UPVAL:
vm_push(*func->upvals[read_byte()]->loc);
break;
case BC_SET_UPVAL:
*func->upvals[read_byte()]->loc = vm_peek();
break;
case BC_SET_INDEX: {
struct us_val set_val = vm_pop();
struct us_val idx_val = vm_pop();
struct us_val arr_val = vm_pop();
if (idx_val.type != VAL_NUM)
log_fatal(1, "index type must be number");
if (arr_val.type != VAL_ARR)
log_fatal(1, "only arrays can be indexed");
int idx = (int)get_num(idx_val);
struct us_arr *arr = get_arr(arr_val);
if (idx < 0)
idx += da_len(arr->e);
if (idx < 0 || idx >= da_len(arr->e)) {
log_fatal(
1,
"index out of range (%d/%d)",
idx,
da_len(arr->e) - 1
);
}
arr->e[idx] = set_val;
vm_push(set_val);
break;
}
case BC_GET_INDEX: {
struct us_val idx_val = vm_pop();
struct us_val arr_val = vm_pop();
if (idx_val.type != VAL_NUM)
log_fatal(1, "index type must be number");
if (arr_val.type != VAL_ARR)
log_fatal(1, "only arrays can be indexed");
int idx = (int)get_num(idx_val);
struct us_arr *arr = get_arr(arr_val);
if (idx < 0)
idx += da_len(arr->e);
if (idx < 0 || idx >= da_len(arr->e)) {
log_fatal(
1,
"index out of range (%d/%d)",
idx,
da_len(arr->e) - 1
);
}
vm_push(arr->e[idx]);
break;
}
case BC_POP_UPVAL:
close_upvals(vm.stacktop - 1);
vm_pop();
break;
case BC_POP: vm_pop(); break;
case BC_ADD: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
if (b.type != VAL_NUM || a.type != VAL_NUM)
log_fatal(1, "Invalid operands");
vm_push(create_num(get_num(a) + get_num(b)));
break;
}
case BC_SUB: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
if (b.type != VAL_NUM || a.type != VAL_NUM)
log_fatal(1, "Invalid operands");
vm_push(create_num(get_num(a) - get_num(b)));
break;
}
case BC_MULT: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
if (b.type != VAL_NUM || a.type != VAL_NUM)
log_fatal(1, "Invalid operands");
vm_push(create_num(get_num(a) * get_num(b)));
break;
}
case BC_DIV: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
if (b.type != VAL_NUM || a.type != VAL_NUM)
log_fatal(1, "Invalid operands");
vm_push(create_num(get_num(a) / get_num(b)));
break;
}
case BC_MOD: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
if (b.type != VAL_NUM || a.type != VAL_NUM)
log_fatal(1, "Invalid operands");
vm_push(create_num(fmod(get_num(a), get_num(b))));
break;
}
case BC_GT: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
if (b.type != VAL_NUM || a.type != VAL_NUM)
log_fatal(1, "Invalid operands");
vm_push(create_bool(get_num(a) > get_num(b)));
break;
}
case BC_LT: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
if (b.type != VAL_NUM || a.type != VAL_NUM)
log_fatal(1, "Invalid operands");
vm_push(create_bool(get_num(a) < get_num(b)));
break;
}
case BC_GTE: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
if (b.type != VAL_NUM || a.type != VAL_NUM)
log_fatal(1, "Invalid operands");
vm_push(create_bool(get_num(a) >= get_num(b)));
break;
}
case BC_LTE: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
if (b.type != VAL_NUM || a.type != VAL_NUM)
log_fatal(1, "Invalid operands");
vm_push(create_bool(get_num(a) <= get_num(b)));
break;
}
case BC_NEG: {
struct us_val a = vm_pop();
if (a.type != VAL_NUM)
log_fatal(1, "Invalid operand");
vm_push(create_num(-get_num(a)));
break;
}
case BC_NOT: {
bool negated = !as_bool(vm_pop());
vm_push(create_bool(negated));
break;
}
case BC_CONCAT: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
vm_push(wrap_str(concat(a, b)));
break;
}
case BC_EQL: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
vm_push(create_bool(vals_eql(a, b)));
break;
}
case BC_NEQL: {
struct us_val b = vm_pop();
struct us_val a = vm_pop();
vm_push(create_bool(!vals_eql(a, b)));
break;
}
case BC_FALSEY_JMP: {
u16 jmp = read_short(func->proto, &i);
if (as_bool(vm_pop()))
break;
i += jmp;
break;
}
case BC_JMP:
i += read_short(func->proto, &i);
break;
case BC_LOOP:
i -= read_short(func->proto, &i);
break;
case BC_PRINT: {
char *str = val_to_str(vm_pop(), NULL);
olog(str);
mem_free(str);
break;
}
case BC_CALL: {
int argc = read_byte();
struct us_val callee = vm.stacktop[-argc - 1];
if (callee.type != VAL_FUNC)
log_fatal(1, "can only call functions");
struct us_func *func = get_func(callee);
if (argc != func->proto->argc) {
log_fatal(
1,
"wrong number of arguments to '%s()' (%d/%d)",
func->proto->name->chars,
argc,
func->proto->argc
);
}
us_exec(func);
break;
}
case BC_RET: {
struct us_val ret_val = vm_pop();
close_upvals(vm.cf->stackbot - 1);
vm.stacktop = vm.cf->stackbot - 1;
vm.cf--;
vm_push(ret_val);
return;
}
default:
log_fatal(1, "unhandled instruction %d", instruction);
break;
}
}
}
|