From 63c942c4bdf7d90f47e7671a2b88ec7b8fd81e21 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Fri, 22 Nov 2019 10:08:40 -0500 Subject: fixed type parameters --- cgen.c | 19 ++++++++----------- main.c | 7 +++---- test.toc | 3 +++ typedefs_cgen.c | 4 ++-- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/cgen.c b/cgen.c index eed4f2e..76dbd1b 100644 --- a/cgen.c +++ b/cgen.c @@ -28,6 +28,8 @@ static bool cgen_defs_decl(CGenerator *g, Declaration *d); #define cgen_recurse_subexprs_fn_simple(fn, decl_f, block_f) \ if (!fn_enter(fn, 0)) return false; \ + FnExpr *prev_fn = g->f##n; \ + g->f##n = fn; \ arr_foreach(fn->params, Declaration, param) \ if (!decl_f(g, param)) \ return false; \ @@ -36,7 +38,8 @@ static bool cgen_defs_decl(CGenerator *g, Declaration *d); return false; \ if (!block_f(g, &fn->body)) \ return false; \ - fn_exit(fn); + fn_exit(fn); \ + g->f##n = prev_fn; /* calls f on every sub-expression of e, block_f on every sub-block, and decl_f on every sub-declaration. */ #define cgen_recurse_subexprs(g, e, f, block_f, decl_f) \ @@ -306,7 +309,7 @@ static bool cgen_type_pre(CGenerator *g, Type *t, Location where) { case TYPE_STRUCT: cgen_write(g, "struct {"); g->indent_lvl++; - cgen_nl(g); + cgen_nl(g); arr_foreach(t->struc.fields, Field, f) { if (!cgen_type_pre(g, f->type, where)) return false; cgen_write(g, " "); @@ -1370,8 +1373,8 @@ static bool cgen_expr(CGenerator *g, Expression *e) { case EXPR_CAST: { Type *from = &e->cast.expr->type; Type *to = &e->cast.type; - if (from->kind == TYPE_USER || to->kind == TYPE_USER) { - /* don't need to cast; they're the same type in C */ + if (to->kind == TYPE_ARR) { + /* can't cast to array type */ if (!cgen_expr(g, e->cast.expr)) return false; } else { @@ -1503,13 +1506,7 @@ static bool cgen_fn(CGenerator *g, FnExpr *f, Location where, U64 instance, Valu : ¶m->type; Value arg = compile_time_args[carg_idx]; if (type->kind == TYPE_TYPE) { - cgen_write(g, "typedef "); - if (!cgen_type_pre(g, arg.type, where)) - return false; - cgen_write(g, " "); - cgen_ident(g, *ident); - if (!cgen_type_post(g, arg.type, where)) - return false; + /* don't need to do anything; we'll just use the type's id */ } else { if (!cgen_val_pre(g, arg, type, where)) return false; diff --git a/main.c b/main.c index fb2122b..fb6a41a 100644 --- a/main.c +++ b/main.c @@ -1,10 +1,9 @@ /* TODO: -fix -alias type, instead of creating a new type. -make sure fn(t @ Type, x : t = 1598, y @ t = 9832) works -don't cgen decls etc. in parameter initializers +get fn(t @ Type, x : t = 1598, y @ t = 9832) t to work +don't cgen decls etc. in parameter initializers (hey we have a parameter flag now) check for leaks +switch to enums for flags struct parameters don't allow while {3; 5} (once break is added) diff --git a/test.toc b/test.toc index b23b62c..c7be16f 100644 --- a/test.toc +++ b/test.toc @@ -12,7 +12,10 @@ puti @= fn(x: int) { main @= fn() { puti(g(int)); puti(g()); + puti(g(i8)); puti(g(u8)); + puti(g(i16)); + puti(g(u16)); }; g @= fn(t @= int) int { diff --git a/typedefs_cgen.c b/typedefs_cgen.c index be2785d..422e6cd 100644 --- a/typedefs_cgen.c +++ b/typedefs_cgen.c @@ -36,7 +36,7 @@ static bool typedefs_decl(CGenerator *g, Declaration *d) { d->c.ids = allocr_calloc(g->allocr, arr_len(d->idents), sizeof *d->c.ids); /* generate typedef */ IdentID id = 0; - if (g->block != NULL) id = d->c.ids[idx] = g->ident_counter++; + if (g->block != NULL || g->fn != NULL) id = d->c.ids[idx] = g->ident_counter++; if (val->type->kind == TYPE_STRUCT) { /* we'll actually define the struct later; here we can just declare it */ cgen_write(g, "struct "); @@ -54,7 +54,7 @@ static bool typedefs_decl(CGenerator *g, Declaration *d) { cgen_write(g, "typedef "); if (!cgen_type_pre(g, val->type, d->where)) return false; cgen_write(g, " "); - if (g->block == NULL) { + if (g->block == NULL && g->fn == NULL) { /* we can refer to this by its name */ cgen_ident(g, i); } else { -- cgit v1.2.3