diff options
| author | hachem <im@hachem.wtf> | 2026-09-19 12:38:43 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-09-19 12:38:43 +0200 |
| commit | a79c97e04562b171780a24cba2cb1a71acfa4bbb (patch) | |
| tree | 412642f948a30eb220512c304b34d60356f23b42 /eval.c | |
| parent | 74a8a2b10a0513f5346ec47aa9b463e30f2b972e (diff) | |
types: array, extent, tuple_size, integral_constant
Diffstat (limited to 'eval.c')
| -rw-r--r-- | eval.c | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -4,6 +4,7 @@ #include "eval.h" +static int evaluate_type(const struct Node* node, struct Type* out); static int evaluate_value(const struct Node* node, int64_t* out); static int complain(const struct Node* node, const char* message) @@ -24,6 +25,7 @@ static int evaluate_value(const struct Node* node, int64_t* out) { int64_t left = 0; int64_t right = 0; + struct Type type; switch (node->kind) { case VALUE_DIFFERENCE: @@ -50,6 +52,15 @@ static int evaluate_value(const struct Node* node, int64_t* out) *out = left / right; return 0; + case VALUE_EXTENT: + case VALUE_TUPLE_SIZE: + if (evaluate_type(node->first, &type)) + return 1; + + // a non-array has no extent, so it gets zero, exactly like the real thing + *out = type.kind == KIND_ARRAY ? type.number : 0; + return 0; + case VALUE_INTEGRAL_CONSTANT: return evaluate_value(node->first, out); default: break; } @@ -61,6 +72,7 @@ static int evaluate_type(const struct Node* node, struct Type* out) { out->kind = KIND_VOID; out->number = 0; + struct Type element; switch (node->kind) { case TYPE_VOID: return 0; @@ -68,6 +80,13 @@ static int evaluate_type(const struct Node* node, struct Type* out) case TYPE_BYTE: out->kind = KIND_BYTE; return 0; case TYPE_SIZE: out->kind = KIND_SIZE; return 0; case TYPE_REMOVE_CV: return evaluate_type(node->first, out); + case TYPE_ARRAY: + // the element type is evaluated for its side of the bargain, then forgotten + if (evaluate_type(node->first, &element) || evaluate_value(node->second, &out->number)) + return 1; + + out->kind = KIND_ARRAY; + return 0; case TYPE_DECLTYPE: if (evaluate_value(node->first, &out->number)) return 1; @@ -92,6 +111,7 @@ static enum RunStatus emit(const struct Node* node) case KIND_INT32: (void)printf("int32_t\n"); break; case KIND_BYTE: (void)printf("byte\n"); break; case KIND_SIZE: (void)printf("size_t\n"); break; + case KIND_ARRAY: (void)printf("array[%" PRId64 "]\n", type.number); break; case KIND_VALUE: (void)printf("%" PRId64 "\n", type.number); break; } |
