summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-12-14 12:01:31 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2019-12-14 12:01:31 -0500
commiteab1148f8db6b9400f13e574dfe4e174d6f52fad (patch)
tree3bfa21591df9bde9ee0bfac4cb2c75b32f39118f
parent71408de2e2b2aace1982c5a24b1ffc61229866c1 (diff)
fixed problem with infer
-rw-r--r--cgen.c4
-rw-r--r--eval.c29
-rw-r--r--instance_table.c2
-rw-r--r--test.toc12
-rw-r--r--toc.c4
-rw-r--r--types.c5
6 files changed, 36 insertions, 20 deletions
diff --git a/cgen.c b/cgen.c
index 7e50024..a0bb8d7 100644
--- a/cgen.c
+++ b/cgen.c
@@ -1662,6 +1662,7 @@ static bool cgen_val_ptr(CGenerator *g, void *v, Type *t, Location where) {
switch (t->kind) {
case TYPE_TUPLE:
case TYPE_VOID:
+ case TYPE_EXPR:
case TYPE_TYPE:
assert(0);
return false;
@@ -1711,9 +1712,6 @@ static bool cgen_val_ptr(CGenerator *g, void *v, Type *t, Location where) {
case BUILTIN_BOOL: cgen_write(g, "%s", *(bool *)v ? "true" : "false"); break;
}
break;
- case TYPE_EXPR:
- assert(0);
- return false;
}
return true;
}
diff --git a/eval.c b/eval.c
index c35ca48..02f6327 100644
--- a/eval.c
+++ b/eval.c
@@ -247,12 +247,12 @@ static void *val_get_ptr(Value *v, Type *t) {
switch (t->kind) {
case TYPE_PTR:
case TYPE_BUILTIN:
- case TYPE_TUPLE:
case TYPE_VOID:
case TYPE_UNKNOWN:
case TYPE_FN:
case TYPE_SLICE:
case TYPE_TYPE:
+ case TYPE_TUPLE:
return v;
case TYPE_ARR:
return v->arr;
@@ -292,9 +292,15 @@ static void fprint_val_ptr(FILE *f, void *p, Type *t) {
case TYPE_FN:
fprintf(f, "<function @ %p>", (void *)*(FnExpr **)p);
break;
- case TYPE_TUPLE:
- fprintf(f, "<tuple>");
- break;
+ case TYPE_TUPLE: {
+ Value *tuple = *(Value **)p;
+ fprintf(f, "(");
+ for (size_t i = 0; i < arr_len(t->tuple); ++i) {
+ if (i) fprintf(f, ", ");
+ fprint_val(f, tuple[i], &t->tuple[i]);
+ }
+ fprintf(f, ")");
+ } break;
case TYPE_ARR: {
fprintf(f, "["); /* TODO: change? when array initializers are added */
size_t n = t->arr.n;
@@ -346,15 +352,12 @@ static void fprint_val_ptr(FILE *f, void *p, Type *t) {
}
static void fprint_val(FILE *f, Value v, Type *t) {
- if (t->kind == TYPE_TUPLE) {
- fprintf(f, "(");
- for (size_t i = 0; i < arr_len(t->tuple); ++i) {
- fprint_val(f, v.tuple[i], &t->tuple[i]);
- }
- fprintf(f, ")");
- } else {
- fprint_val_ptr(f, val_get_ptr(&v, t), t);
- }
+ fprint_val_ptr(f, val_get_ptr(&v, t), t);
+}
+
+static void print_val(Value v, Type *t) {
+ fprint_val(stdout, v, t);
+ printf("\n");
}
static void *val_ptr_to_free(Value *v, Type *t) {
diff --git a/instance_table.c b/instance_table.c
index 7f6c7e7..b96b31d 100644
--- a/instance_table.c
+++ b/instance_table.c
@@ -234,7 +234,7 @@ static bool val_ptr_eq(void *u, void *v, Type *t) {
case TYPE_TUPLE: {
Value *us = *(Value **)u;
Value *vs = *(Value **)v;
- for (size_t i = 0; i < arr_len(t->tuple); ++i) {
+ for (size_t i = 0, len = arr_len(t->tuple); i < len; ++i) {
if (!val_eq(us[i], vs[i], &t->tuple[i]))
return false;
}
diff --git a/test.toc b/test.toc
index 24b24b4..e6ff4bd 100644
--- a/test.toc
+++ b/test.toc
@@ -1,3 +1,10 @@
+puti ::= fn(x: int) {
+ #C("printf(\"%ld\\n\", x)");
+};
+putf ::= fn(x: float) {
+ #C("printf(\"%f\\n\", x)");
+};
+
f ::= fn(t::=, x :t) t {
x + 1
};
@@ -5,5 +12,8 @@ f ::= fn(t::=, x :t) t {
// test: fn(t::=int,u::=t,x:u)u
main ::= fn() {
- f(13);
+ puti(f(13));
+ puti(f(14));
+ puti(f(15));
+ putf(f(3.1));
}; \ No newline at end of file
diff --git a/toc.c b/toc.c
index 6d68950..1a6684c 100644
--- a/toc.c
+++ b/toc.c
@@ -17,6 +17,10 @@
#include <inttypes.h>
#include "types.h"
+
+/* forward declarations for debugging */
+static void print_val(Value v, Type *t);
+
#include "allocator.c"
#include "arr.c"
#include "location.c"
diff --git a/types.c b/types.c
index 2c6c654..3188692 100644
--- a/types.c
+++ b/types.c
@@ -1307,6 +1307,9 @@ static bool types_expr(Typer *tr, Expression *e) {
if (param->flags & DECL_INFER) {
Value *val = &inferred_vals[i];
Type *type = &inferred_types[i];
+ /* if we have an inferred type argument, it shouldn't be flexible */
+ if (type->kind == TYPE_TYPE)
+ val->type->flags &= (TypeFlags)~(TypeFlags)TYPE_IS_FLEXIBLE;
param->val = *val;
param->type = *type;
param->flags |= DECL_FOUND_VAL | DECL_FOUND_TYPE;
@@ -1365,8 +1368,6 @@ static bool types_expr(Typer *tr, Expression *e) {
if (fn_type->constness) {
bool 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) {
arr_cleara(&table_index_type.tuple, tr->allocr);
arr_cleara(&table_index.tuple, tr->allocr);