implemented function prototype registration.

This commit is contained in:
Lorenzo Torres 2025-12-05 17:10:56 +01:00
parent 3ca2c2f009
commit c8e88a6a98
3 changed files with 53 additions and 8 deletions

View file

@ -3,3 +3,8 @@ struct b {
u32 test, u32 test,
u32 test1, u32 test1,
} }
u32 test(b hello)
{
}

54
sema.c
View file

@ -16,6 +16,8 @@ typedef struct { u8 flags; char *name; } type_key;
static struct { char *key; pair *value; } *types; static struct { char *key; pair *value; } *types;
static struct { char *key; type *value; } *type_reg; static struct { char *key; type *value; } *type_reg;
static struct { char *key; prototype *value; } *prototypes;
/* 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)
{ {
@ -249,14 +251,8 @@ static void register_type(sema *s, char *name, type *t)
shput(type_reg, name, t); shput(type_reg, name, t);
} }
static void analyze_unit(sema *s, ast_node *node) static void create_types(sema *s)
{ {
ast_node *current = node;
while (current && current->type == NODE_UNIT) {
order_type(s, current->expr.unit_node.expr);
current = current->expr.unit_node.next;
}
res_node **nodes = NULL; res_node **nodes = NULL;
res_node **ordered = NULL; res_node **ordered = NULL;
usize node_count = shlen(types); usize node_count = shlen(types);
@ -299,6 +295,50 @@ static void analyze_unit(sema *s, ast_node *node)
} }
} }
static void create_prototype(sema *s, ast_node *node)
{
prototype *p = arena_alloc(s->allocator, sizeof(prototype));
p->name = intern_string(s, node->expr.function.name, node->expr.function.name_len);
member *m = node->expr.function.parameters;
while (m) {
type *t = get_type(s, m->type);
if (!t) {
error(m->type, "unknown type.");
return;
}
arrput(p->parameters, t);
m = m->next;
}
p->type = get_type(s, node->expr.function.type);
shput(prototypes, p->name, p);
}
static void analyze_unit(sema *s, ast_node *node)
{
ast_node *current = node;
while (current && current->type == NODE_UNIT) {
order_type(s, current->expr.unit_node.expr);
current = current->expr.unit_node.next;
}
create_types(s);
current = node;
while (current && current->type == NODE_UNIT) {
if (current->expr.unit_node.expr->type == NODE_FUNCTION) {
create_prototype(s, current->expr.unit_node.expr);
}
current = current->expr.unit_node.next;
}
for (int i=0; i < shlen(prototypes); i++) {
printf("f: %s\n", prototypes[i].key);
}
}
sema *sema_init(parser *p, arena *a) sema *sema_init(parser *p, arena *a)
{ {
sema *s = arena_alloc(a, sizeof(sema)); sema *s = arena_alloc(a, sizeof(sema));

2
sema.h
View file

@ -54,7 +54,7 @@ typedef struct _type {
typedef struct { typedef struct {
char *name; char *name;
type *type; type *type;
type *parameters type **parameters;
} prototype; } prototype;
typedef struct { typedef struct {