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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
|
typedef enum {
VAL_VOID,
VAL_INT,
VAL_UINT,
VAL_CHAR,
VAL_FLOAT,
VAL_PTR,
VAL_FN,
VAL_BOOL,
VAL_ARR
} ValueKind;
#define VAL_FLAG_FN_NULL 0x01 /* is this function null? */
typedef double FloatVal;
typedef struct {
void *data;
UInteger n;
} ArrVal;
typedef struct Value {
ValueKind kind;
union {
ValueKind arr_kind;
ValueKind ptr_kind;
};
union {
Integer intv;
UInteger uintv;
FloatVal floatv;
bool boolv;
char charv;
void *ptr;
FnExpr *fn;
ArrVal arr;
};
} Value;
size_t sizeof_val_kind(ValueKind k) {
Value v;
switch (k) {
case VAL_VOID:
return 0;
case VAL_INT: return sizeof v.intv;
case VAL_UINT: return sizeof v.uintv;
case VAL_FLOAT: return sizeof v.floatv;
case VAL_BOOL: return sizeof v.boolv;
case VAL_CHAR: return sizeof v.charv;
case VAL_PTR: return sizeof v.ptr;
case VAL_FN: return sizeof v.fn;
case VAL_ARR: return sizeof v.arr;
}
assert(0);
return 0;
}
static ValueKind type_to_val_kind(Type *t) {
switch (t->kind) {
case TYPE_VOID: return VAL_VOID;
case TYPE_FN: return VAL_FN;
case TYPE_PTR: return VAL_PTR;
case TYPE_ARR: return VAL_ARR;
case TYPE_BUILTIN:
switch (t->builtin) {
case BUILTIN_I8:
case BUILTIN_I16:
case BUILTIN_I32:
case BUILTIN_I64:
return VAL_INT;
case BUILTIN_U8:
case BUILTIN_U16:
case BUILTIN_U32:
case BUILTIN_U64:
return VAL_UINT;
case BUILTIN_F32:
case BUILTIN_F64:
return VAL_FLOAT;
case BUILTIN_CHAR:
return VAL_CHAR;
case BUILTIN_BOOL:
return VAL_BOOL;
case BUILTIN_TYPE_COUNT: break;
}
break;
case TYPE_TUPLE: /* TODO */
case TYPE_UNKNOWN:
break;
}
assert(0);
return VAL_VOID;
}
static bool eval_truthiness(Value *v) {
switch (v->kind) {
case VAL_VOID:
assert(0);
return false;
case VAL_INT:
return v->intv != 0;
case VAL_UINT:
return v->uintv != 0;
case VAL_FLOAT:
return v->floatv != 0;
case VAL_PTR:
return v->ptr != NULL;
case VAL_FN:
return v->fn != NULL;
case VAL_BOOL:
return v->boolv;
case VAL_CHAR:
return v->charv != 0;
case VAL_ARR:
return v->arr.n != 0;
}
assert(0);
return false;
}
static inline bool val_eq(Value a, Value b) {
if (a.kind != b.kind) return false;
switch (a.kind) {
case VAL_UINT: return a.uintv == b.uintv;
case VAL_INT: return a.intv == b.intv;
case VAL_FLOAT: return a.floatv == b.floatv;
case VAL_BOOL: return a.boolv == b.boolv;
case VAL_FN: return a.fn == b.fn;
case VAL_PTR: return a.ptr == b.ptr;
case VAL_CHAR: return a.charv == b.charv;
case VAL_ARR:
case VAL_VOID: assert(0);
}
return false;
}
static inline bool val_lt(Value a, Value b) {
if (a.kind != b.kind) return false;
switch (a.kind) {
case VAL_UINT: return a.uintv < b.uintv;
case VAL_INT: return a.intv < b.intv;
case VAL_FLOAT: return a.floatv < b.floatv;
case VAL_BOOL: return a.boolv < b.boolv;
case VAL_FN: return a.fn < b.fn;
case VAL_PTR: return a.ptr < b.ptr;
case VAL_CHAR: return a.charv < b.charv;
case VAL_ARR:
case VAL_VOID: assert(0);
}
return false;
}
static bool val_cast(Location where, Value *out, Value cast, Type *type) {
/* TODO: errors at type checking */
switch (type->kind) {
case TYPE_VOID:
out->kind = VAL_VOID;
return true;
case TYPE_BUILTIN:
switch (type->builtin) {
case BUILTIN_U8:
case BUILTIN_U16:
case BUILTIN_U32:
case BUILTIN_U64:
out->kind = VAL_UINT;
switch (cast.kind) {
case VAL_UINT:
out->uintv = cast.uintv; break;
case VAL_INT:
out->uintv = (UInteger)cast.intv; break;
case VAL_FLOAT:
out->uintv = (UInteger)cast.floatv; break;
case VAL_BOOL:
out->uintv = cast.boolv ? 1 : 0; break;
case VAL_FN:
out->uintv = (UInteger)cast.fn; break;
case VAL_PTR:
out->uintv = (UInteger)cast.ptr; break;
case VAL_CHAR:
out->uintv = (UInteger)cast.charv; break;
case VAL_ARR:
case VAL_VOID: assert(0);
}
break;
case BUILTIN_I8:
case BUILTIN_I16:
case BUILTIN_I32:
case BUILTIN_I64:
out->kind = VAL_INT;
switch (cast.kind) {
case VAL_UINT:
out->intv = (Integer)cast.uintv; break;
case VAL_INT:
out->intv = cast.intv; break;
case VAL_FLOAT:
out->intv = (Integer)cast.floatv; break;
case VAL_BOOL:
out->intv = cast.boolv ? 1 : 0; break;
case VAL_FN:
out->intv = (Integer)cast.fn; break;
case VAL_PTR:
out->intv = (Integer)cast.ptr; break;
case VAL_CHAR:
out->intv = (Integer)cast.charv; break;
case VAL_ARR:
case VAL_VOID: assert(0);
}
break;
case BUILTIN_F32:
case BUILTIN_F64:
out->kind = VAL_FLOAT;
switch (cast.kind) {
case VAL_UINT:
out->floatv = (FloatVal)cast.uintv; break;
case VAL_INT:
out->floatv = (FloatVal)cast.intv; break;
case VAL_FLOAT:
out->floatv = cast.floatv; break;
case VAL_BOOL:
out->floatv = cast.boolv ? 1.0 : 0.0; break;
case VAL_CHAR:
out->floatv = (FloatVal)cast.charv; break;
case VAL_VOID:
case VAL_FN:
case VAL_ARR:
case VAL_PTR:
assert(0);
return false;
}
break;
case BUILTIN_BOOL:
out->kind = VAL_BOOL;
out->boolv = eval_truthiness(&cast);
break;
case BUILTIN_CHAR:
out->kind = VAL_CHAR;
switch (cast.kind) {
case VAL_UINT:
out->charv = (char)cast.uintv; break;
case VAL_INT:
out->charv = (char)cast.intv; break;
case VAL_BOOL:
out->charv = cast.boolv ? 1 : 0; break;
case VAL_CHAR:
out->charv = cast.charv; break;
case VAL_FLOAT:
case VAL_FN:
case VAL_PTR:
case VAL_ARR:
case VAL_VOID: assert(0);
}
break;
case BUILTIN_TYPE_COUNT: assert(0); return false;
}
break;
case TYPE_FN:
out->kind = VAL_FN;
switch (cast.kind) {
case VAL_UINT:
out->fn = (FnExpr *)cast.uintv; break;
case VAL_INT:
out->fn = (FnExpr *)cast.intv; break;
case VAL_FN:
out->fn = cast.fn; break;
case VAL_PTR:
out->fn = (FnExpr *)cast.ptr; break;
case VAL_CHAR:
case VAL_FLOAT:
case VAL_BOOL:
case VAL_ARR:
case VAL_VOID:
assert(0); return false;
}
break;
case TYPE_PTR:
out->kind = VAL_PTR;
out->ptr_kind = type_to_val_kind(type->ptr.of);
switch (cast.kind) {
case VAL_INT:
out->ptr = (void *)cast.intv; break;
case VAL_UINT:
out->ptr = (void *)cast.uintv; break;
case VAL_FN:
out->ptr = (void *)cast.fn; break;
case VAL_PTR:
out->ptr = cast.ptr; break;
case VAL_ARR:
out->ptr = cast.arr.data; break;
case VAL_CHAR:
case VAL_VOID:
case VAL_FLOAT:
case VAL_BOOL:
assert(0); return false;
}
return true;
case TYPE_ARR:
out->kind = VAL_ARR;
out->arr_kind = type_to_val_kind(type->arr.of);
out->arr.n = type->arr.n;
switch (cast.kind) {
case VAL_PTR:
out->arr.data = cast.ptr;
break;
case VAL_ARR:
out->arr.data = cast.arr.data;
break;
case VAL_FN:
case VAL_INT:
case VAL_UINT:
case VAL_CHAR:
case VAL_VOID:
case VAL_FLOAT:
case VAL_BOOL:
assert(0); return false;
}
return true;
case TYPE_TUPLE:
/* TODO: error at type checking */
assert(0);
return false;
case TYPE_UNKNOWN:
err_print(where, "Cannot cast to unknown type.");
return false;
}
return true;
}
static bool eval_block(Block *b, Value *v) {
v->kind = VAL_UINT;
v->uintv = (UInteger)b;
(void)b,(void)v;
return true;
}
static inline void val_promote_to_float(Value *v) {
switch (v->kind) {
case VAL_INT:
v->kind = VAL_FLOAT;
v->floatv = (FloatVal)v->intv;
break;
case VAL_UINT:
v->kind = VAL_FLOAT;
v->floatv = (FloatVal)v->uintv;
break;
case VAL_FLOAT: break;
default: assert(0); break;
}
}
/* NOTE: expr must be typed before it can be evaluated */
static bool eval_expr(Expression *e, Value *v) {
switch (e->kind) {
case EXPR_LITERAL_FLOAT:
v->kind = VAL_FLOAT;
v->floatv = (FloatVal)e->floatl;
return true;
case EXPR_LITERAL_INT:
if (e->intl > (UInteger)INTEGER_MAX) {
v->kind = VAL_UINT;
v->uintv = e->intl;
} else {
v->kind = VAL_INT;
v->intv = (Integer)e->intl;
}
break;
case EXPR_LITERAL_BOOL:
v->kind = VAL_BOOL;
v->boolv = e->booll;
break;
case EXPR_LITERAL_CHAR:
v->kind = VAL_CHAR;
v->charv = e->charl;
break;
case EXPR_LITERAL_STR:
v->kind = VAL_ARR;
v->arr_kind = VAL_CHAR;
v->arr.n = e->strl.len;
v->arr.data = e->strl.str;
break;
case EXPR_UNARY_OP: {
Expression *of_expr = e->unary.of;
Value of;
if (!eval_expr(of_expr, &of)) return false;
switch (e->unary.op) {
case UNARY_MINUS: {
assert(e->type.kind == TYPE_BUILTIN);
v->kind = of.kind;
if (v->kind == VAL_INT) {
v->intv = -of.intv;
} else if (v->kind == VAL_FLOAT) {
v->floatv = -of.floatv;
} else {
assert(0);
return false;
}
break;
}
case UNARY_NOT:
v->kind = VAL_BOOL;
v->boolv = !eval_truthiness(&of);
return true;
case UNARY_ADDRESS: {
v->kind = VAL_PTR;
get_ptr:
switch (of_expr->kind) {
case EXPR_UNARY_OP:
switch (of_expr->unary.op) {
case UNARY_DEREF:
/* &*x */
of_expr = of_expr->unary.of;
goto get_ptr;
default: assert(0); return false;
}
break;
case EXPR_IDENT:
err_print(e->where, "Cannot get address of identifiers at compile time yet.");
return false;
case EXPR_BINARY_OP:
switch (of_expr->binary.op) {
case BINARY_AT_INDEX: {
Expression *lhs_expr = of_expr->binary.lhs;
Expression *rhs_expr = of_expr->binary.rhs;
Value lhs, rhs;
if (!eval_expr(lhs_expr, &lhs)
|| !eval_expr(rhs_expr, &rhs))
return false;
assert(lhs.kind == VAL_ARR && (rhs.kind == VAL_INT || rhs.kind == VAL_UINT));
v->ptr = (char *)lhs.arr.data + (Integer)sizeof_val_kind(lhs.arr_kind)
* (rhs.kind == VAL_INT ? rhs.intv : (Integer)rhs.uintv);
} break;
default: assert(0); return false;
}
break;
default: assert(0); return false;
}
} break;
case UNARY_DEREF: {
v->kind = type_to_val_kind(&e->type);
switch (v->kind) {
case VAL_INT:
v->intv = *(Integer *)of.ptr; break;
case VAL_UINT:
v->uintv = *(UInteger *)of.ptr; break;
case VAL_FLOAT:
v->floatv = *(FloatVal *)of.ptr; break;
case VAL_BOOL:
v->boolv = *(bool *)of.ptr; break;
case VAL_CHAR:
v->charv = *(char *)of.ptr; break;
case VAL_ARR:
v->arr = *(ArrVal *)of.ptr; break;
case VAL_PTR:
v->ptr = *(void **)of.ptr; break;
case VAL_FN:
v->fn = *(FnExpr **)of.ptr; break;
case VAL_VOID:
assert(0); return false;
/* case VAL_INT: */
/* v->intv = *(Integer *)of.ptr; */
/* break; */
}
break;
} break;
case UNARY_DEL: /* TODO */
assert(0);
break;
}
} break;
case EXPR_BINARY_OP: {
Value lhs, rhs;
/* NOTE: this will need to change for short-circuiting */
if (!eval_expr(e->binary.lhs, &lhs)) return false;
if (!eval_expr(e->binary.rhs, &rhs)) return false;
if (e->type.kind != TYPE_BUILTIN) {
err_print(e->where, "Operators can only be applied to builtin types.");
return false;
}
bool is_uint = type_builtin_is_uint(e->type.builtin);
bool is_int = type_builtin_is_int(e->type.builtin);
bool is_float = type_builtin_is_float(e->type.builtin);
if (is_float) {
val_promote_to_float(&lhs);
val_promote_to_float(&rhs);
}
switch (e->binary.op) {
case BINARY_PLUS:
v->kind = lhs.kind;
if (is_uint) {
v->uintv = lhs.uintv + rhs.uintv;
} else if (is_int) {
v->intv = lhs.intv + rhs.intv;
} else if (is_float) {
v->floatv = lhs.floatv + rhs.floatv;
} else assert(0);
break;
case BINARY_MINUS:
v->kind = lhs.kind;
if (is_uint) {
v->uintv = lhs.uintv - rhs.uintv; /* TODO: will u64 - u64 => i64 be possible? */
} else if (is_int) {
v->intv = lhs.intv - rhs.intv;
} else if (is_float) {
v->floatv = lhs.floatv - rhs.floatv;
} else assert(0);
break;
case BINARY_MUL:
v->kind = lhs.kind;
if (is_uint) {
v->uintv = lhs.uintv * rhs.uintv;
} else if (is_int) {
v->intv = lhs.intv * rhs.intv;
} else if (is_float) {
v->floatv = lhs.floatv * rhs.floatv;
} else assert(0);
break;
case BINARY_DIV:
v->kind = lhs.kind;
/* TODO(eventually): check div by 0 */
if (is_uint) {
v->uintv = lhs.uintv / rhs.uintv;
} else if (is_int) {
v->intv = lhs.intv / rhs.intv;
} else if (is_float) {
v->floatv = lhs.floatv / rhs.floatv;
} else assert(0);
break;
case BINARY_EQ:
v->kind = VAL_BOOL;
v->boolv = val_eq(lhs, rhs);
break;
case BINARY_NE:
v->kind = VAL_BOOL;
v->boolv = !val_eq(lhs, rhs);
break;
case BINARY_GT:
v->kind = VAL_BOOL;
v->boolv = val_lt(rhs, lhs);
break;
case BINARY_GE:
v->kind = VAL_BOOL;
v->boolv = val_lt(rhs, lhs) || val_eq(lhs, rhs);
break;
case BINARY_LT:
v->kind = VAL_BOOL;
v->boolv = val_lt(lhs, rhs);
break;
case BINARY_LE:
v->boolv = val_lt(lhs, rhs) || val_eq(lhs, rhs);
break;
case BINARY_SET:
v->kind = VAL_VOID;
break;
case BINARY_COMMA:
err_print(e->where, "tuples not supported at compile time yet.");
return false;
case BINARY_AT_INDEX: {
assert(lhs.kind == VAL_ARR);
assert(rhs.kind == VAL_INT || rhs.kind == VAL_UINT);
/* TODO: optional bounds checking */
UInteger index = rhs.kind == VAL_INT ? (UInteger)rhs.intv : rhs.uintv;
void *arr = lhs.arr.data;
v->kind = lhs.arr_kind;
switch (lhs.arr_kind) {
case VAL_INT:
v->intv = ((Integer *)arr)[index]; break;
case VAL_UINT:
v->uintv = ((UInteger *)arr)[index]; break;
case VAL_FLOAT:
v->floatv = ((FloatVal *)arr)[index]; break;
case VAL_CHAR:
v->charv = ((char *)arr)[index]; break;
case VAL_BOOL:
v->boolv = ((bool *)arr)[index]; break;
case VAL_PTR:
v->ptr = ((Value **)arr)[index]; break;
case VAL_ARR:
v->arr = ((ArrVal *)arr)[index]; break;
case VAL_FN:
v->fn = ((FnExpr **)arr)[index]; break;
case VAL_VOID:
assert(0);
return false;
}
break;
}
}
} break;
case EXPR_IDENT: {
Identifier id = e->ident;
IdentDecl *id_decl = ident_decl(id);
if (!id_decl) {
char *id_str = ident_to_str(id);
err_print(e->where, "Undeclared identifier: %s", id_str);
free(id_str);
return false;
}
Declaration *d = id_decl->decl;
if (location_after(d->where, e->where)) {
err_print(e->where, "Use of constant before its declaration.");
info_print(d->where, "Declaration will be here.");
return false;
}
if (!(d->flags & DECL_FLAG_CONST)) {
err_print(e->where, "Use of non-constant identifier in a constant expression.");
info_print(d->where, "Declaration was here.");
return false;
}
if (!d->val) {
d->val = err_malloc(sizeof *d->val); /* OPTIM */
if (!eval_expr(&d->expr, d->val))
return false;
}
*v = *d->val;
break;
} break;
case EXPR_FN:
v->kind = VAL_FN;
v->fn = &e->fn;
break;
case EXPR_IF: {
IfExpr *i = &e->if_;
if (!i->cond) {
if (!eval_block(&i->body, v)) return false;
break;
}
Value cond;
if (!eval_expr(i->cond, &cond))
return false;
if (eval_truthiness(&cond)) {
if (!eval_block(&i->body, v)) return false;
} else if (i->next_elif) {
if (!eval_expr(i->next_elif, v)) return false;
} else {
v->kind = VAL_VOID;
}
break;
}
case EXPR_BLOCK:
return eval_block(&e->block, v);
case EXPR_CAST: {
Value cast_val;
if (!eval_expr(e->cast.expr, &cast_val)) return false;
if (!val_cast(e->where, v, cast_val, &e->cast.type)) return false;
break;
}
case EXPR_DIRECT:
switch (e->direct.which) {
case DIRECT_C:
err_print(e->where, "Can't run C code at compile time.");
return false;
case DIRECT_COUNT: assert(0); break;
}
break;
case EXPR_NEW:
v->kind = VAL_PTR;
v->ptr_kind = type_to_val_kind(&e->new.type);
v->ptr = err_calloc(1, sizeof_val_kind(v->ptr_kind)); /* TODO(eventually): get this to work even if NULL ptrs aren't 0 */
break;
case EXPR_WHILE:
case EXPR_CALL:
err_print(e->where, "Not implemented yet");
return false;
}
return true;
}
|