summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cgen.c4
-rw-r--r--decls_cgen.c18
-rw-r--r--eval.c13
-rw-r--r--main.c10
-rwxr-xr-xrunv8
-rw-r--r--test.toc10
-rw-r--r--types.c1
-rw-r--r--types.h5
8 files changed, 43 insertions, 26 deletions
diff --git a/cgen.c b/cgen.c
index 2e227bf..64b3fe8 100644
--- a/cgen.c
+++ b/cgen.c
@@ -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++;
}
diff --git a/eval.c b/eval.c
index 4150527..bdb90db 100644
--- a/eval.c
+++ b/eval.c
@@ -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;
diff --git a/main.c b/main.c
index 8f1b25e..9e388a3 100644
--- a/main.c
+++ b/main.c
@@ -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++) {
diff --git a/runv b/runv
index 614eda3..08a52f8 100755
--- a/runv
+++ b/runv
@@ -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
diff --git a/test.toc b/test.toc
index 75c471f..473e442 100644
--- a/test.toc
+++ b/test.toc
@@ -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
diff --git a/types.c b/types.c
index ab186d1..e7bd2ec 100644
--- a/types.c
+++ b/types.c
@@ -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;
diff --git a/types.h b/types.h
index c855702..1faf498 100644
--- a/types.h
+++ b/types.h
@@ -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;