diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-11-30 19:38:28 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-11-30 19:38:28 -0500 |
commit | d24409b9bf4783be63109f59d8558cef884069e6 (patch) | |
tree | a1886005cc1e65bed5d03f493149874d8e4398b6 | |
parent | e8cd3b66a9bf20e5a68147a9882685299d40f495 (diff) |
fixed some bugs involving returning types
-rw-r--r-- | cgen.c | 4 | ||||
-rw-r--r-- | decls_cgen.c | 18 | ||||
-rw-r--r-- | eval.c | 13 | ||||
-rw-r--r-- | main.c | 10 | ||||
-rwxr-xr-x | runv | 8 | ||||
-rw-r--r-- | test.toc | 10 | ||||
-rw-r--r-- | types.c | 1 | ||||
-rw-r--r-- | types.h | 5 |
8 files changed, 43 insertions, 26 deletions
@@ -29,6 +29,7 @@ static bool cgen_defs_block(CGenerator *g, Block *b); static bool cgen_defs_decl(CGenerator *g, Declaration *d); #define cgen_recurse_subexprs_fn_simple(fn, decl_f, block_f) \ + if (cgen_should_gen_fn(fn)) { \ if (!fn_enter(fn, 0)) return false; \ FnExpr *prev_fn = g->f##n; \ g->f##n = fn; \ @@ -41,7 +42,8 @@ static bool cgen_defs_decl(CGenerator *g, Declaration *d); if (!block_f(g, &fn->body)) \ return false; \ fn_exit(fn); \ - g->f##n = prev_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) \ diff --git a/decls_cgen.c b/decls_cgen.c index 593bf5d..91ad40a 100644 --- a/decls_cgen.c +++ b/decls_cgen.c @@ -5,20 +5,20 @@ static bool cgen_decls_decl(CGenerator *g, Declaration *d); static bool cgen_decls_fn_instances(CGenerator *g, Expression *e) { assert(e->kind == EXPR_FN); FnExpr *f = &e->fn; - if (!cgen_should_gen_fn(f)) - return true; FnType *type = &e->type.fn; assert(type->constness); Instance **data = f->instances.data; for (U64 i = 0; i < f->instances.cap; i++) { if (f->instances.occupied[i]) { - (*data)->fn.c.name = f->c.name; - (*data)->fn.c.id = f->c.id; - - if (!cgen_fn_header(g, &(*data)->fn, e->where, (*data)->c.id, (*data)->val.tuple[0].u64)) - return false; - cgen_write(g, ";"); - cgen_nl(g); + if (cgen_should_gen_fn(&(*data)->fn)) { + (*data)->fn.c.name = f->c.name; + (*data)->fn.c.id = f->c.id; + + if (!cgen_fn_header(g, &(*data)->fn, e->where, (*data)->c.id, (*data)->val.tuple[0].u64)) + return false; + cgen_write(g, ";"); + cgen_nl(g); + } } data++; } @@ -1388,10 +1388,15 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { } break; case EXPR_CALL: { - Value fnv; - if (!eval_expr(ev, e->call.fn, &fnv)) - return false; - FnExpr *fn = fnv.fn; + FnExpr *fn; + if (e->call.instance) { + fn = &e->call.instance->fn; + } else { + Value fnv; + if (!eval_expr(ev, e->call.fn, &fnv)) + return false; + fn = fnv.fn; + } /* make sure function body is typed before calling it */ if (!types_block(ev->typer, &fn->body)) return false; @@ -17,12 +17,18 @@ int main(int argc, char **argv) { #ifdef TOC_DEBUG test_all(); #endif + + const char *in_filename; if (argc < 2) { +#ifdef TOC_DEBUG + in_filename = "test.toc"; +#else fprintf(stderr, "Please specify an input file.\n"); return EXIT_FAILURE; +#endif + } else { + in_filename = argv[1]; } - - const char *in_filename = argv[1]; const char *out_filename = "out.c"; for (int i = 2; i < argc-1; i++) { @@ -4,7 +4,13 @@ if [ "$2" = "" ]; then else tocf="$2" fi -valgrind -q --track-origins=yes --error-exitcode=1 --malloc-fill=0xcd --free-fill=0xef --num-callers=100 ./toc $tocf || exit 1 +if [ "$1" = "noq" ]; then + FLAGS= +else + FLAGS="-q" +fi + +valgrind $FLAGS --track-origins=yes --error-exitcode=1 --malloc-fill=0xcd --free-fill=0xef --num-callers=100 ./toc $tocf || exit 1 if [ "$1" = "c" ]; then gcc out.c && ./a.out elif [ "$1" = "pc" ]; then @@ -15,11 +15,13 @@ -f @= fn() Type { -int +f @= fn(s @ Type) Type { +t @= int; +t }; main @= fn() { -x : f(); - +x : f(int); +y : f(int); +z : f(float); };
\ No newline at end of file @@ -1229,7 +1229,6 @@ static bool types_expr(Typer *tr, Expression *e) { ret_type = f->type.fn.types; param_types = ret_type + 1; c->instance->c.id = original_fn->instances.n; /* let's help cgen out and assign an ID to this */ - /* type this instance */ if (!types_fn(tr, fn, &f->type, e->where, c->instance)) return false; @@ -526,10 +526,7 @@ typedef struct FnExpr { typedef struct Instance { Value val; /* key into hash table */ - union { - FnExpr fn; /* the typed function */ - Type type; /* the type, resolved */ - }; + FnExpr fn; /* the typed function */ struct { U64 id; } c; |