diff options
| author | Mustafa Quraish <[email protected]> | 2022-02-03 03:06:58 -0500 |
|---|---|---|
| committer | Mustafa Quraish <[email protected]> | 2022-02-03 03:08:55 -0500 |
| commit | 202fe666d4b5b69131a09a3911cf8c3828b720d8 (patch) | |
| tree | 00d6e4165192b6f684e9f3dfad1fcdbd6cf13f80 /src/types.c | |
| parent | Move all common utilities to `std/common.cup` (diff) | |
| download | cup-202fe666d4b5b69131a09a3911cf8c3828b720d8.tar.xz cup-202fe666d4b5b69131a09a3911cf8c3828b720d8.zip | |
Allow implicitly converting between integer-like-types
This design should also be useful for structs down the road.
Diffstat (limited to 'src/types.c')
| -rw-r--r-- | src/types.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/types.c b/src/types.c index 24458ff..31edc4b 100644 --- a/src/types.c +++ b/src/types.c @@ -17,6 +17,17 @@ bool type_equals(Type *a, Type *b) return a->type == b->type && type_equals(a->ptr, b->ptr); } +bool is_convertible(Type *from, Type *to) +{ + if (from->type == TYPE_ANY || to->type == TYPE_ANY) + return true; + // TODO: Should we rause a warning if the target type is narrower + // than the source type? + if (is_int_type(from) && is_int_type(to)) + return true; + return type_equals(from, to); +} + i64 size_for_type(Type *type) { switch (type->type) @@ -55,6 +66,18 @@ bool is_string_type(Type *type) && type->ptr->type == TYPE_CHAR; } +bool is_int_type(Type *type) +{ + switch (type->type) + { + case TYPE_INT: + case TYPE_CHAR: + return true; + default: + return false; + } +} + static char *data_type_to_str(DataType type) { switch (type) |