summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c2
-rw-r--r--test.toc4
-rw-r--r--tests/arr2/.gitignore1
-rw-r--r--tests/arr2/arr2.toc63
-rw-r--r--tests/arr2/expected200
-rwxr-xr-xtests/arr2/test.sh3
-rwxr-xr-xtests/test.sh1
-rw-r--r--types.c4
8 files changed, 272 insertions, 6 deletions
diff --git a/main.c b/main.c
index dd99a02..5d755cc 100644
--- a/main.c
+++ b/main.c
@@ -19,9 +19,7 @@
/*
TODO:
-inferred const params
make sure all parameters to a function which returns a type are constant
-check that it still works when you specify the inferred param
---
get ArrInt to work with inference
packages
diff --git a/test.toc b/test.toc
index 18e8ef6..ea9ea79 100644
--- a/test.toc
+++ b/test.toc
@@ -31,7 +31,7 @@ arr_add ::= fn(t ::=, a : &Arr(t), x : t) {
square ::= fn(t ::=, x : t) t {
a : Arr(t);
each i := 1,2..2*x-1 {
- arr_add(t, &a, i);
+ arr_add(&a, i);
};
sum := 0 as t;
each i := 0..a.len-1 {
@@ -50,7 +50,7 @@ inc ::= fn(t ::=, x : t) t {
main ::= fn() {
arr : Arr(int);
farr : Arr(float);
- each i := 1..100 {
+ each i := 1..100 {
arr_add(&arr, inc(square(i)));
arr_add(&farr, inc(square(i as float)));
}
diff --git a/tests/arr2/.gitignore b/tests/arr2/.gitignore
new file mode 100644
index 0000000..a0ae6ee
--- /dev/null
+++ b/tests/arr2/.gitignore
@@ -0,0 +1 @@
+got \ No newline at end of file
diff --git a/tests/arr2/arr2.toc b/tests/arr2/arr2.toc
new file mode 100644
index 0000000..ea9ea79
--- /dev/null
+++ b/tests/arr2/arr2.toc
@@ -0,0 +1,63 @@
+puti ::= fn(x: int) {
+ #C("printf(\"%ld\\n\", (long)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;
+ len, cap : int;
+ }
+};
+
+arr_add ::= fn(t ::=, a : &Arr(t), x : t) {
+ if a.len >= a.cap {
+ a.cap = a.cap * 2 + 2;
+ new_data := new(t, a.cap);
+ each i := 0..a.len-1 {
+ new_data[i] = a.data[i];
+ }
+ a.data = new_data;
+ }
+ a.data[a.len] = x;
+ a.len += 1;
+};
+
+square ::= fn(t ::=, x : t) t {
+ a : Arr(t);
+ each i := 1,2..2*x-1 {
+ arr_add(&a, i);
+ };
+ sum := 0 as t;
+ each i := 0..a.len-1 {
+ sum += a.data[i];
+ };
+ sum
+};
+
+
+// ArrInt ::= Arr(int);
+
+inc ::= fn(t ::=, x : t) t {
+ x + 1
+};
+
+main ::= fn() {
+ arr : Arr(int);
+ farr : Arr(float);
+ each i := 1..100 {
+ arr_add(&arr, inc(square(i)));
+ arr_add(&farr, inc(square(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/tests/arr2/expected b/tests/arr2/expected
new file mode 100644
index 0000000..c0a3e45
--- /dev/null
+++ b/tests/arr2/expected
@@ -0,0 +1,200 @@
+2
+5
+10
+17
+26
+37
+50
+65
+82
+101
+122
+145
+170
+197
+226
+257
+290
+325
+362
+401
+442
+485
+530
+577
+626
+677
+730
+785
+842
+901
+962
+1025
+1090
+1157
+1226
+1297
+1370
+1445
+1522
+1601
+1682
+1765
+1850
+1937
+2026
+2117
+2210
+2305
+2402
+2501
+2602
+2705
+2810
+2917
+3026
+3137
+3250
+3365
+3482
+3601
+3722
+3845
+3970
+4097
+4226
+4357
+4490
+4625
+4762
+4901
+5042
+5185
+5330
+5477
+5626
+5777
+5930
+6085
+6242
+6401
+6562
+6725
+6890
+7057
+7226
+7397
+7570
+7745
+7922
+8101
+8282
+8465
+8650
+8837
+9026
+9217
+9410
+9605
+9802
+10001
+2.000000
+5.000000
+10.000000
+17.000000
+26.000000
+37.000000
+50.000000
+65.000000
+82.000000
+101.000000
+122.000000
+145.000000
+170.000000
+197.000000
+226.000000
+257.000000
+290.000000
+325.000000
+362.000000
+401.000000
+442.000000
+485.000000
+530.000000
+577.000000
+626.000000
+677.000000
+730.000000
+785.000000
+842.000000
+901.000000
+962.000000
+1025.000000
+1090.000000
+1157.000000
+1226.000000
+1297.000000
+1370.000000
+1445.000000
+1522.000000
+1601.000000
+1682.000000
+1765.000000
+1850.000000
+1937.000000
+2026.000000
+2117.000000
+2210.000000
+2305.000000
+2402.000000
+2501.000000
+2602.000000
+2705.000000
+2810.000000
+2917.000000
+3026.000000
+3137.000000
+3250.000000
+3365.000000
+3482.000000
+3601.000000
+3722.000000
+3845.000000
+3970.000000
+4097.000000
+4226.000000
+4357.000000
+4490.000000
+4625.000000
+4762.000000
+4901.000000
+5042.000000
+5185.000000
+5330.000000
+5477.000000
+5626.000000
+5777.000000
+5930.000000
+6085.000000
+6242.000000
+6401.000000
+6562.000000
+6725.000000
+6890.000000
+7057.000000
+7226.000000
+7397.000000
+7570.000000
+7745.000000
+7922.000000
+8101.000000
+8282.000000
+8465.000000
+8650.000000
+8837.000000
+9026.000000
+9217.000000
+9410.000000
+9605.000000
+9802.000000
+10001.000000
diff --git a/tests/arr2/test.sh b/tests/arr2/test.sh
new file mode 100755
index 0000000..c6ffb01
--- /dev/null
+++ b/tests/arr2/test.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+./arr2.bin > got || exit 1
+diff got expected > /dev/null || exit 1
diff --git a/tests/test.sh b/tests/test.sh
index 988bce6..88318c6 100755
--- a/tests/test.sh
+++ b/tests/test.sh
@@ -32,3 +32,4 @@ STARTPWD="$(pwd)"
do_tests bf
do_tests arr
+do_tests arr2
diff --git a/types.c b/types.c
index c57cc71..47b0145 100644
--- a/types.c
+++ b/types.c
@@ -902,7 +902,7 @@ static bool types_expr(Typer *tr, Expression *e) {
if ((ea->type.flags & TYPE_IS_FLEXIBLE) && ea->range.to)
ea->type = ea->range.to->type;
-
+ ea->type.flags &= (TypeFlags)~(TypeFlags)TYPE_IS_FLEXIBLE;
} else {
if (!types_expr(tr, ea->of))
return false;
@@ -1405,7 +1405,7 @@ static bool types_expr(Typer *tr, Expression *e) {
if (!type_eq(expected, got)) {
char *estr = type_to_str(expected);
char *gstr = type_to_str(got);
- err_print(arg->where, "Expected type %s as %lu%s argument to function, but got %s.", estr, 1+(unsigned long)p, ordinals(1+p), gstr);
+ err_print(arg->where, "Expected type %s as argument to function, but got %s.", estr, gstr);
return false;
}
}