From 0e0c41af58a0f4ec5a39ce77822de71e5523fcba Mon Sep 17 00:00:00 2001 From: Kimplul Date: Wed, 7 May 2025 21:22:38 +0300 Subject: implement enough type analysis for vector example + Big commit, scary + Some details still a bit up in the air, mainly about move checking structure member access ('register' types are freely copied I guess, same as in rust? How about user types?) --- src/debug.c | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'src/debug.c') diff --git a/src/debug.c b/src/debug.c index f3540f0..8ef4293 100644 --- a/src/debug.c +++ b/src/debug.c @@ -134,14 +134,27 @@ void semantic_error(struct scope *scope, struct ast *node, va_end(args); } +void type_error(struct scope *scope, struct type *type, + const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + struct src_issue issue; + issue.level = SRC_ERROR; + issue.loc = type->loc; + issue.fctx = scope->fctx; + _issue(issue, fmt, args); + va_end(args); +} + void type_mismatch(struct scope *scope, struct ast *node, struct type *l, struct type *r) { - const char *ls = type_str(l); - const char *rs = type_str(r); + char *ls = type_str(l); + char *rs = type_str(r); semantic_error(scope, node, "type mismatch: %s vs %s\n", ls, rs); - free((void *)ls); - free((void *)rs); + free(ls); + free(rs); } void move_error(struct ast *new_use, struct ast *prev_use) @@ -213,6 +226,16 @@ static void _type_str(FILE *f, struct type *type) fprintf(f, "void"); break; + case TYPE_NIL: + fprintf(f, "nil"); + break; + + case TYPE_ARR: + /** @todo length? */ + fprintf(f, "[]"); + _type_list_str(f, tarr_base(type)); + break; + case TYPE_PTR: fprintf(f, "*"); _type_list_str(f, tptr_base(type)); @@ -244,16 +267,10 @@ static void _type_str(FILE *f, struct type *type) _type_list_str(f, type->t0); fprintf(f, ")"); break; - - case TYPE_CONSTRUCT: - fprintf(f, "%s![", type->id); - _type_list_str(f, type->t0); - fprintf(f, "]"); - break; } } -const char *type_str(struct type *t) +char *type_str(struct type *t) { if (!t) return strdup("NULL"); -- cgit v1.2.3