implemented cycling definition checking

This commit is contained in:
Lorenzo Torres 2025-12-04 22:41:18 +01:00
parent eed0f40516
commit 2f5dfe381e
2 changed files with 18 additions and 9 deletions

View file

@ -1,11 +1,11 @@
struct a {
b hello,
}
struct b { struct b {
a ciao, c ciao,
} }
struct c { struct c {
u32 test, b test,
}
struct a {
c hello,
} }

15
sema.c
View file

@ -20,7 +20,11 @@ static struct { char *key; type *value; } *type_reg;
/* Print the error message and sync the parser. */ /* Print the error message and sync the parser. */
static void error(ast_node *n, char *msg) static void error(ast_node *n, char *msg)
{ {
printf("\x1b[31m\x1b[1merror\x1b[0m\x1b[1m:%ld:%ld:\x1b[0m %s\n", n->position.row, n->position.column, msg); if (n) {
printf("\x1b[31m\x1b[1merror\x1b[0m\x1b[1m:%ld:%ld:\x1b[0m %s\n", n->position.row, n->position.column, msg);
} else {
printf("\x1b[31m\x1b[1merror\x1b[0m\x1b[1m:\x1b[0m %s\n", msg);
}
} }
static char *intern_string(sema *s, char *str, usize len) static char *intern_string(sema *s, char *str, usize len)
@ -180,7 +184,8 @@ end:
res_node **nodes = NULL; res_node **nodes = NULL;
res_node **ordered = NULL; res_node **ordered = NULL;
for (int i=0; i < shlen(types); i++) { usize node_count = shlen(types);
for (int i=0; i < node_count; i++) {
if (arrlen(types[i].value->node.in) == 0) { if (arrlen(types[i].value->node.in) == 0) {
arrput(nodes, &types[i].value->node); arrput(nodes, &types[i].value->node);
} }
@ -206,10 +211,14 @@ end:
} }
} }
if (arrlen(ordered) != node_count) {
error(NULL, "cycling struct definition.");
}
for (int i=0; i < arrlen(ordered); i++) { for (int i=0; i < arrlen(ordered); i++) {
type *t = ordered[i]->value; type *t = ordered[i]->value;
if (t && (t->tag == TYPE_STRUCT || t->tag == TYPE_UNION)) { if (t && (t->tag == TYPE_STRUCT || t->tag == TYPE_UNION)) {
char *name = intern_string(s, t->data.structure.name, t->data.structure.name_len); char *name = t->name;
printf("%s\n", name); printf("%s\n", name);
register_type(s, name, t); register_type(s, name, t);
} }