summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c2
-rw-r--r--test.toc16
-rw-r--r--types.c11
3 files changed, 15 insertions, 14 deletions
diff --git a/main.c b/main.c
index ffbe83a..73f0306 100644
--- a/main.c
+++ b/main.c
@@ -1,7 +1,7 @@
/*
TODO:
-get fn(t @ Type, x : t = 1598, y @ t = 9832) t to work
remove declarations , exit fn even on failure
+fix flexible types (example program should not need those casts)
get fn(x, y@ int) to work, you'll need to add back in tuple parameters (one decl multiple values)
can we get the instance before calling type_of_fn?
diff --git a/test.toc b/test.toc
index f2282a2..75a69aa 100644
--- a/test.toc
+++ b/test.toc
@@ -1,7 +1,7 @@
-// puti @= fn(x: int) {
-// #C("printf(\"%ld\\n\", (long)x);
-// ");
-// };
+puti @= fn(x: int) {
+ #C("printf(\"%ld\\n\", (long)x);
+");
+};
// putf @= fn(x: float) {
// #C("printf(\"%f\\n\", (double)x);
// ");
@@ -9,11 +9,11 @@
f @= fn(t @ Type, x: t) t {
- x + 1
+ x + 1 as t
};
main @= fn() {
- f(int, 10);
-};
-
+ puti(f(int, 3));
+ puti(f(u8, 255 as u8) as int);
+}; \ No newline at end of file
diff --git a/types.c b/types.c
index bebda9f..6b89ebe 100644
--- a/types.c
+++ b/types.c
@@ -193,7 +193,7 @@ static bool expr_must_lval(Expression *e) {
static bool type_of_fn(Typer *tr, FnExpr *f, Location where, Type *t, U16 flags) {
t->kind = TYPE_FN;
t->fn.types = NULL;
- t->fn.constness = NULL; /* OPTIM: constant doesn't need to be a dynamic array */
+ t->fn.constness = NULL; /* OPTIM: constness doesn't need to be a dynamic array */
FnExpr fn_copy;
if (!(flags & TYPE_OF_FN_NO_COPY_EVEN_IF_CONST) && fn_has_any_const_params(f)) {
@@ -1139,7 +1139,8 @@ static bool types_expr(Typer *tr, Expression *e) {
return false;
FnExpr *fn = fn_val.fn;
-
+ /* fn is the instance, original_fn is not */
+ FnExpr *original_fn = fn;
FnExpr fn_copy;
Copier cop = {.allocr = tr->allocr, .block = tr->block};
/* TODO: somehow don't do all of this if we've already generated this instance */
@@ -1170,7 +1171,7 @@ static bool types_expr(Typer *tr, Expression *e) {
Value *arg_val = typer_arr_add(tr, &table_index.tuple);
if (!eval_expr(tr->evalr, &new_args[i], arg_val)) {
if (tr->evalr->enabled) {
- info_print(new_args[i].where, "(error occured while trying to evaluate compile-time argument, argument #%lu)", (unsigned long)i);
+ info_print(new_args[i].where, "(error occured while trying to evaluate compile-time argument, argument #%lu)", 1+(unsigned long)i);
}
return false;
}
@@ -1205,7 +1206,7 @@ static bool types_expr(Typer *tr, Expression *e) {
}
bool instance_already_exists;
- c->instance = instance_table_adda(tr->allocr, &fn->instances, table_index, &table_index_type, &instance_already_exists);
+ c->instance = instance_table_adda(tr->allocr, &original_fn->instances, table_index, &table_index_type, &instance_already_exists);
if (!instance_already_exists) {
c->instance->fn = fn_copy;
/* type param declarations, etc */
@@ -1214,7 +1215,7 @@ static bool types_expr(Typer *tr, Expression *e) {
/* fix parameter and return types (they were kind of problematic before, because we didn't know about the instance) */
ret_type = f->type.fn.types;
param_types = ret_type + 1;
- c->instance->c.id = fn->instances.n; /* let's help cgen out and assign an ID to this */
+ 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))