diff --git a/examples/frontend/typecheck-const-generics.sg b/examples/frontend/typecheck-const-generics.sg new file mode 100644 index 00000000..81d79352 --- /dev/null +++ b/examples/frontend/typecheck-const-generics.sg @@ -0,0 +1,113 @@ +struct Matrix { + arr: [[T * Cols] * Rows] +} + +impl Matrix { + fun new(x: T): Matrix { + return {arr=[[x] * Cols] * Rows}; + } + + fun get(&self, row: Int, col: Int): &T { + return &self.arr[row][col]; + } + + fun mul( + &self, + other: &Matrix, + zero: T, + add: fun(T, T) -> T, + mul: fun(T, T) -> T + ): Matrix { + let mut result = Matrix.new(zero); + for let mut j=0; j(&mut self, row: Int, col: Int) { + self.arr[row][col] = X; + } + + fun set_range(&mut self, row: Int) { + for let mut i=0; i(&mut self, row: Int) { + for let mut i=0; i(&mut self, col: Int) { + for let mut i=0; i(10); +let mut y = Matrix.new(5); +for let mut row=0; row < 4; row += 1; { + y.set_row<[1, 2, 3, 4]>(row); +} +y.set_range<4, [4, 3, 2, 1]>(0); + +x.set_col<[1, 2, 3, 4, 5]>(0); +x.set<5>(1, 1); + +x.print(); +println(); +y.print(); +println(); + +fun add_ints(a: Int, b: Int): Int = a + b; +fun mul_ints(a: Int, b: Int): Int = a * b; + +let z = y.mul<4>(&x, 0, add_ints, mul_ints); +z.print(); + +struct Test { + a: Matrix, + b: Matrix, + c: Matrix +} + +impl Test { + fun new(): Test { + {a=Matrix.new(10), b=Matrix.new(5), c=Matrix.new(0)} + } + + fun test(&mut self) { + let b = self.b; + self.c = b.mul<1>(&self.a, 0, add_ints, mul_ints); + } +} + +let mut t = Test.new(); +for let mut i=0; i<1; i+=1; { + t.test(); +} +t.c.print(); \ No newline at end of file diff --git a/examples/test-output/typecheck-const-generics.error.txt b/examples/test-output/typecheck-const-generics.error.txt new file mode 100644 index 00000000..331eb684 --- /dev/null +++ b/examples/test-output/typecheck-const-generics.error.txt @@ -0,0 +1 @@ +mismatched types: expected [Int * 4], found const param [1, 2, 3, 4, 5] in ((X: [Int * 4]) => (&mut (Matrix), Int) -> None) \ No newline at end of file