diff --git a/examples/hello_world.l b/examples/hello_world.l index 317b29e..ec4071b 100644 --- a/examples/hello_world.l +++ b/examples/hello_world.l @@ -3,3 +3,8 @@ struct b { u32 test, u32 test1, } + +u32 test(b hello) +{ + +} diff --git a/sema.c b/sema.c index 128ecfa..ea4cf10 100644 --- a/sema.c +++ b/sema.c @@ -16,6 +16,8 @@ typedef struct { u8 flags; char *name; } type_key; static struct { char *key; pair *value; } *types; static struct { char *key; type *value; } *type_reg; +static struct { char *key; prototype *value; } *prototypes; + /* Print the error message and sync the parser. */ 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); } -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 **ordered = NULL; 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 *s = arena_alloc(a, sizeof(sema)); diff --git a/sema.h b/sema.h index 6b4ce46..9c27332 100644 --- a/sema.h +++ b/sema.h @@ -54,7 +54,7 @@ typedef struct _type { typedef struct { char *name; type *type; - type *parameters + type **parameters; } prototype; typedef struct {