summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c2
-rw-r--r--test.toc23
-rw-r--r--types.c16
3 files changed, 28 insertions, 13 deletions
diff --git a/main.c b/main.c
index 004a5a2..aa537ca 100644
--- a/main.c
+++ b/main.c
@@ -1,8 +1,8 @@
/*
TODO:
-test ArrInt ::= Arr(int);
make sure fn(t :: Type, x : t) t works
check fn(x :: int, y := x)
+check fn(x :: int) y := x
new version of copy_val for copying types??
there are probably places where we enter a function and never exit (in typing?) if there's an error
diff --git a/test.toc b/test.toc
index a6e8d36..a999512 100644
--- a/test.toc
+++ b/test.toc
@@ -2,11 +2,12 @@ puti ::= fn(x: int) {
#C("printf(\"%ld\\n\", (long)x);
");
};
-// putf ::= fn(x: float) {
-// #C("printf(\"%f\\n\", (double)x);
-// ");
-// };
+putf ::= fn(x: float) {
+ #C("printf(\"%f\\n\", (double)x);
+");
+};
+// it would be nice if Arr.data.len == Arr.len (: but this will require some C code...
Arr ::= fn (t :: Type) Type {
struct {
data : []t;
@@ -28,13 +29,23 @@ arr_add ::= fn(t :: Type, a : &Arr(t), x : t) {
};
+ArrInt ::= Arr(int);
+
+inc ::= fn(t :: Type, x : t) t {
+ x + 1
+};
main ::= fn() {
- arr : Arr(int);
+ arr : ArrInt;
+ farr : Arr(float);
each i := 1..100 {
- arr_add(int, &arr, i*i);
+ arr_add(int, &arr, inc(int, i*i));
+ arr_add(float, &farr, inc(float, (i*i) as float));
}
each i := 0..arr.len - 1 {
puti(arr.data[i]);
}
+ each i := 0..farr.len - 1 {
+ putf(farr.data[i]);
+ }
};
diff --git a/types.c b/types.c
index 26e259d..0797679 100644
--- a/types.c
+++ b/types.c
@@ -195,10 +195,12 @@ static bool type_of_fn(Typer *tr, FnExpr *f, Location where, Type *t, U16 flags)
bool entered_fn = false;
size_t param_idx;
FnExpr *prev_fn = tr->fn;
-
FnExpr fn_copy;
- if (!(flags & TYPE_OF_FN_IS_INSTANCE) && fn_has_any_const_params(f)) {
+ /* f has compile time params, but it's not an instance! */
+ bool generic = !(flags & TYPE_OF_FN_IS_INSTANCE) && fn_has_any_const_params(f);
+
+ if (generic) {
Copier cop = copier_create(tr->allocr, tr->block);
copy_fn_expr(&cop, &fn_copy, f, false);
f = &fn_copy;
@@ -215,7 +217,7 @@ static bool type_of_fn(Typer *tr, FnExpr *f, Location where, Type *t, U16 flags)
for (param_idx = 0; param_idx < nparams; param_idx++) {
Declaration *param = &f->params[param_idx];
U16 types_decl_flags = 0;
- if (!(flags & TYPE_OF_FN_IS_INSTANCE) && fn_has_any_const_params(f)) {
+ if (generic) {
types_decl_flags |= TYPES_DECL_DONT_RESOLVE;
}
if (!types_decl(tr, param, types_decl_flags)) {
@@ -296,9 +298,11 @@ static bool type_of_fn(Typer *tr, FnExpr *f, Location where, Type *t, U16 flags)
}
}
}
- if (!type_resolve(tr, &f->ret_type, where)) {
- success = false;
- goto ret;
+ if (!generic) {
+ if (!type_resolve(tr, &f->ret_type, where)) {
+ success = false;
+ goto ret;
+ }
}
*ret_type = f->ret_type;