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
475
476
477
478
479
|
static void eval_block(Evaluator *ev, Block *b, Value *v);
static void evalr_create(Evaluator *ev) {
allocr_create(&ev->allocr);
}
static void evalr_free(Evaluator *ev) {
allocr_free_all(&ev->allocr);
}
static inline void *evalr_malloc(Evaluator *ev, size_t bytes) {
return allocr_malloc(&ev->allocr, bytes);
}
static bool builtin_truthiness(Value *v, BuiltinType b) {
switch (b) {
case BUILTIN_I8: return v->i8 != 0;
case BUILTIN_I16: return v->i16 != 0;
case BUILTIN_I32: return v->i32 != 0;
case BUILTIN_I64: return v->i64 != 0;
case BUILTIN_U8: return v->u8 != 0;
case BUILTIN_U16: return v->u16 != 0;
case BUILTIN_U32: return v->u32 != 0;
case BUILTIN_U64: return v->u64 != 0;
case BUILTIN_F32: return v->f32 != 0;
case BUILTIN_F64: return v->f64 != 0;
case BUILTIN_BOOL: return v->boolv;
case BUILTIN_CHAR: return v->charv != 0;
case BUILTIN_TYPE_COUNT: break;
}
assert(0); return false;
}
static bool val_truthiness(Value *v, Type *t) {
switch (t->kind) {
case TYPE_VOID: return false;
case TYPE_UNKNOWN: assert(0); return false;
case TYPE_BUILTIN: return builtin_truthiness(v, t->builtin);
case TYPE_PTR: return v->ptr != NULL;
case TYPE_FN: return v->fn != NULL;
case TYPE_ARR: return t->arr.n > 0;
case TYPE_TUPLE: break;
}
assert(0);
return false;
}
static I64 val_to_i64(Value *v, BuiltinType v_type) {
switch (v_type) {
case BUILTIN_I8: return (I64)v->i8;
case BUILTIN_I16: return (I64)v->i16;
case BUILTIN_I32: return (I64)v->i32;
case BUILTIN_I64: return (I64)v->i64;
case BUILTIN_U8: return (I64)v->u8;
case BUILTIN_U16: return (I64)v->u16;
case BUILTIN_U32: return (I64)v->u32;
case BUILTIN_U64: return (I64)v->u64;
default: break;
}
assert(0);
return 0;
}
static U64 val_to_u64(Value *v, BuiltinType v_type) {
if (v_type == BUILTIN_U64) return v->u64;
return (U64)val_to_i64(v, v_type);
}
static void i64_to_val(Value *v, BuiltinType v_type, I64 x) {
switch (v_type) {
case BUILTIN_I8:
v->i8 = (I8)x; break;
case BUILTIN_I16:
v->i16 = (I16)x; break;
case BUILTIN_I32:
v->i32 = (I32)x; break;
case BUILTIN_I64:
v->i64 = (I64)x; break;
case BUILTIN_U8:
v->u8 = (U8)x; break;
case BUILTIN_U16:
v->u16 = (U16)x; break;
case BUILTIN_U32:
v->u32 = (U32)x; break;
case BUILTIN_U64:
v->u64 = (U64)x; break;
default: assert(0); break;
}
}
static void u64_to_val(Value *v, BuiltinType v_type, U64 x) {
if (v_type == BUILTIN_U64)
v->u64 = x;
else
i64_to_val(v, v_type, (I64)x);
}
#define builtin_casts_to_int(x) \
case BUILTIN_I8: \
vout->i8 = (I8)vin->x; break; \
case BUILTIN_I16: \
vout->i16 = (I16)vin->x; break; \
case BUILTIN_I32: \
vout->i32 = (I32)vin->x; break; \
case BUILTIN_I64: \
vout->i64 = (I64)vin->x; break; \
case BUILTIN_U8: \
vout->u8 = (U8)vin->x; break; \
case BUILTIN_U16: \
vout->u16 = (U16)vin->x; break; \
case BUILTIN_U32: \
vout->u32 = (U32)vin->x; break; \
case BUILTIN_U64: \
vout->u64 = (U64)vin->x; break
#define builtin_casts_to_num(x) \
builtin_casts_to_int(x); \
case BUILTIN_F32: \
vout->f32 = (F32)vin->x; break; \
case BUILTIN_F64: \
vout->f64 = (F64)vin->x; break
#define builtin_int_casts(low, up) \
case BUILTIN_##up: \
switch (to) { \
builtin_casts_to_num(low); \
case BUILTIN_CHAR: vout->charv = (char)vin->low; break; \
case BUILTIN_BOOL: vout->boolv = vin->low != 0; break; \
case BUILTIN_TYPE_COUNT: assert(0); break; \
} break
#define builtin_float_casts(low, up) \
case BUILTIN_##up: \
switch (to) { \
builtin_casts_to_num(low); \
case BUILTIN_BOOL: vout->boolv = vin->low != 0.0f; break; \
case BUILTIN_CHAR: \
case BUILTIN_TYPE_COUNT: assert(0); break; \
} break
static void val_builtin_cast(Value *vin, BuiltinType from, Value *vout, BuiltinType to) {
if (from == to) {
*vout = *vin;
return;
}
switch (from) {
builtin_int_casts(i8, I8);
builtin_int_casts(i16, I16);
builtin_int_casts(i32, I32);
builtin_int_casts(i64, I64);
builtin_int_casts(u8, U8);
builtin_int_casts(u16, U16);
builtin_int_casts(u32, U32);
builtin_int_casts(u64, U64);
builtin_float_casts(f32, F32);
builtin_float_casts(f64, F64);
case BUILTIN_BOOL: vout->boolv = builtin_truthiness(vin, from); break;
case BUILTIN_CHAR:
switch (to) {
builtin_casts_to_int(charv);
case BUILTIN_CHAR: /* handled at top of func */
case BUILTIN_F32:
case BUILTIN_F64:
case BUILTIN_BOOL:
case BUILTIN_TYPE_COUNT:
assert(0); break;
}
break;
case BUILTIN_TYPE_COUNT: assert(0); break;
}
}
static void val_cast(Value *vin, Type *from, Value *vout, Type *to) {
if (to->kind == TYPE_BUILTIN && to->builtin == BUILTIN_BOOL) {
vout->boolv = val_truthiness(vin, from);
return;
}
switch (from->kind) {
case TYPE_VOID: assert(0); break;
case TYPE_UNKNOWN: assert(0); break;
case TYPE_TUPLE: assert(0); break;
case TYPE_BUILTIN:
switch (to->kind) {
case TYPE_BUILTIN:
val_builtin_cast(vin, from->builtin, vout, to->builtin);
break;
case TYPE_PTR:
switch (from->builtin) {
case BUILTIN_I8: vout->ptr = (void *)(U64)vin->i8; break;
case BUILTIN_I16: vout->ptr = (void *)(U64)vin->i16; break;
case BUILTIN_I32: vout->ptr = (void *)(U64)vin->i32; break;
case BUILTIN_I64: vout->ptr = (void *)(U64)vin->i64; break;
case BUILTIN_U8: vout->ptr = (void *)(U64)vin->u8; break;
case BUILTIN_U16: vout->ptr = (void *)(U64)vin->u16; break;
case BUILTIN_U32: vout->ptr = (void *)(U64)vin->u32; break;
case BUILTIN_U64: vout->ptr = (void *)(U64)vin->u64; break;
default: assert(0); break;
}
break;
case TYPE_VOID:
case TYPE_UNKNOWN:
case TYPE_TUPLE:
case TYPE_FN:
case TYPE_ARR:
assert(0);
break;
}
break;
case TYPE_FN:
switch (to->kind) {
case TYPE_PTR:
vout->ptr = (void *)vin->fn;
break;
case TYPE_FN:
vout->fn = vin->fn;
break;
case TYPE_UNKNOWN:
case TYPE_TUPLE:
case TYPE_VOID:
case TYPE_ARR:
case TYPE_BUILTIN:
assert(0); break;
}
break;
case TYPE_PTR:
switch (to->kind) {
case TYPE_BUILTIN:
switch (to->builtin) {
builtin_casts_to_int(ptr);
case BUILTIN_BOOL:
case BUILTIN_CHAR:
case BUILTIN_F32:
case BUILTIN_F64:
case BUILTIN_TYPE_COUNT:
assert(0); break;
}
break;
case TYPE_ARR:
vout->arr = vin->ptr;
break;
case TYPE_PTR:
vout->ptr = vin->ptr;
break;
case TYPE_FN:
vout->fn = vin->ptr;
break;
case TYPE_UNKNOWN:
case TYPE_TUPLE:
case TYPE_VOID:
assert(0);
break;
}
break;
case TYPE_ARR:
switch (to->kind) {
case TYPE_PTR:
vout->ptr = vin->arr;
break;
case TYPE_ARR:
vout->arr = vin->arr;
break;
case TYPE_FN:
case TYPE_UNKNOWN:
case TYPE_TUPLE:
case TYPE_VOID:
case TYPE_BUILTIN:
assert(0); break;
}
break;
}
}
static void eval_expr(Evaluator *ev, Expression *e, Value *v) {
/* WARNING: macros ahead */
#define eval_unary_op_one(low, up, op) \
case BUILTIN_##up: \
v->low = op of.low; break
#define eval_unary_op_nums(builtin, op) \
eval_unary_op_one(i8, I8, op); \
eval_unary_op_one(i16, I16, op); \
eval_unary_op_one(i32, I32, op); \
eval_unary_op_one(i64, I64, op); \
eval_unary_op_one(u8, U8, op); \
eval_unary_op_one(u16, U16, op); \
eval_unary_op_one(u32, U32, op); \
eval_unary_op_one(u64, U64, op); \
eval_unary_op_one(f32, F32, op); \
eval_unary_op_one(f64, F64, op);
#define eval_unary_op_nums_only(op) \
switch (builtin) { \
eval_unary_op_nums(builtin, op); \
default: assert(0); break; \
}
#define eval_binary_op_one(low, up, op) \
case BUILTIN_##up: \
v->low = lhs.low op rhs.low; break
#define eval_binary_op_nums(builtin, op) \
eval_binary_op_one(i8, I8, op); \
eval_binary_op_one(i16, I16, op); \
eval_binary_op_one(i32, I32, op); \
eval_binary_op_one(i64, I64, op); \
eval_binary_op_one(u8, U8, op); \
eval_binary_op_one(u16, U16, op); \
eval_binary_op_one(u32, U32, op); \
eval_binary_op_one(u64, U64, op); \
eval_binary_op_one(f32, F32, op); \
eval_binary_op_one(f64, F64, op)
#define eval_binary_op_nums_only(op) \
switch (builtin) { \
eval_binary_op_nums(builtin, op); \
default: assert(0); break; \
}
#define eval_binary_bool_op_one(low, up, op) \
case BUILTIN_##up: \
v->boolv = lhs.low op rhs.low; break
#define eval_binary_bool_op_nums(builtin, op) \
eval_binary_bool_op_one(i8, I8, op); \
eval_binary_bool_op_one(i16, I16, op); \
eval_binary_bool_op_one(i32, I32, op); \
eval_binary_bool_op_one(i64, I64, op); \
eval_binary_bool_op_one(u8, U8, op); \
eval_binary_bool_op_one(u16, U16, op); \
eval_binary_bool_op_one(u32, U32, op); \
eval_binary_bool_op_one(u64, U64, op); \
eval_binary_bool_op_one(f32, F32, op); \
eval_binary_bool_op_one(f64, F64, op);
#define eval_binary_bool_op_nums_only(op) \
switch (builtin) { \
eval_binary_bool_op_nums(builtin, op); \
default: assert(0); break; \
}
switch (e->kind) {
case EXPR_UNARY_OP: {
Value of;
eval_expr(ev, e->unary.of, &of);
switch (e->unary.op) {
case UNARY_MINUS: {
BuiltinType builtin = e->type.builtin;
assert(e->type.kind == TYPE_BUILTIN);
eval_unary_op_nums_only(-);
} break;
case UNARY_NOT:
v->boolv = !val_truthiness(v, &e->unary.of->type);
break;
}
} break;
case EXPR_BINARY_OP: {
Value lhs, rhs;
/* TODO(eventually): short-circuiting */
eval_expr(ev, e->binary.lhs, &lhs);
eval_expr(ev, e->binary.rhs, &rhs);
/* OPTIM: this is not ideal, but 5+3.7 will be 5:int+3.7:f32 right now */
val_cast(&lhs, &e->binary.lhs->type, &lhs, &e->type);
val_cast(&rhs, &e->binary.rhs->type, &rhs, &e->type);
BuiltinType builtin = e->type.builtin;
assert(e->type.kind == TYPE_BUILTIN);
switch (e->binary.op) {
case BINARY_ADD:
eval_binary_op_nums_only(+); break;
case BINARY_SUB:
eval_binary_op_nums_only(-); break;
case BINARY_MUL:
eval_binary_op_nums_only(*); break;
case BINARY_DIV:
eval_binary_op_nums_only(/); break;
case BINARY_LT:
eval_binary_bool_op_nums_only(<); break;
case BINARY_LE:
eval_binary_bool_op_nums_only(<=); break;
case BINARY_GT:
eval_binary_bool_op_nums_only(>); break;
case BINARY_GE:
eval_binary_bool_op_nums_only(>=); break;
case BINARY_EQ:
eval_binary_bool_op_nums_only(==); break;
case BINARY_NE:
eval_binary_bool_op_nums_only(!=); break;
}
} break;
case EXPR_LITERAL_INT:
assert(e->type.kind == TYPE_BUILTIN);
u64_to_val(v, e->type.builtin, e->intl);
break;
case EXPR_LITERAL_FLOAT:
assert(e->type.kind == TYPE_BUILTIN);
if (e->type.builtin == BUILTIN_F32) {
v->f32 = (F32)e->floatl;
} else if (e->type.builtin == BUILTIN_F64) {
v->f64 = (F64)e->floatl;
} else {
assert(0);
}
break;
case EXPR_IF: {
IfExpr *i = &e->if_;
if (i->cond) {
Value cond;
eval_expr(ev, i->cond, &cond);
if (val_truthiness(&cond, &i->cond->type)) {
eval_block(ev, &i->body, v);
} else if (i->next_elif) {
eval_expr(ev, i->next_elif, v);
return;
}
} else {
eval_block(ev, &i->body, v);
}
} break;
case EXPR_WHILE: {
Value cond;
WhileExpr *w = &e->while_;
bool looped_once = false;
while (1) {
eval_expr(ev, w->cond, &cond);
if (!val_truthiness(&cond, &w->cond->type))
break;
eval_block(ev, &w->body, v);
}
} break;
case EXPR_BLOCK:
eval_block(ev, &e->block, v);
break;
case EXPR_LITERAL_BOOL:
v->boolv = e->booll;
break;
case EXPR_LITERAL_CHAR:
v->charv = e->charl;
break;
case EXPR_LITERAL_STR:
v->arr = e->strl.str;
break;
case EXPR_CAST: {
Value casted;
eval_expr(ev, e->cast.expr, &casted);
val_cast(&casted, &e->cast.expr->type, v, &e->cast.type);
} break;
}
}
static void eval_stmt(Evaluator *ev, Statement *stmt) {
switch (stmt->kind) {
case STMT_DECL:
/* TODO */
break;
case STMT_EXPR: {
Value unused;
eval_expr(ev, &stmt->expr, &unused);
} break;
case STMT_RET:
/* TODO */
break;
}
}
static void eval_block(Evaluator *ev, Block *b, Value *v) {
arr_foreach(b->stmts, Statement, stmt) {
eval_stmt(ev, stmt);
}
if (b->ret_expr) {
eval_expr(ev, b->ret_expr, v);
}
}
|