From 202fe666d4b5b69131a09a3911cf8c3828b720d8 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Thu, 3 Feb 2022 03:06:58 -0500 Subject: Allow implicitly converting between integer-like-types This design should also be useful for structs down the road. --- src/types.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/types.c') 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) -- cgit v1.2.3