Skip to content

Commit

Permalink
Added const generics example to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-mcdaniel committed Sep 16, 2024
1 parent 636d48f commit 48f27fa
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 31 deletions.
66 changes: 66 additions & 0 deletions examples/frontend/const-generics-array.sg
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
fun main() {
// Create an array struct
let mut a = Array.new<Int, 5>([1, 2, 3, 4, 5]);
// Print it out
a.println();
// Set a value!
a.set(1, 1000);
// Print the changed value
println(*a.get(1));
// Print the whole changed array
a.println();
println(a);
a[0] = 555;
a.println();
}

// An array with a constant parameter length
type Array<T, const N: Int> = [T * N];

impl Array<T, N> {
// Create a new Array container
fun new(arr: [T * N]): Array<T, N> {
return arr;
}

// Get a value from the array
fun get(&self, n: Int): &T {
if n < N {
return &self.data()[n];
} else {
return Null;
}
}

fun data_mut(&mut self): &mut T {
return self as &mut T;
}

fun data(&self): &T {
return self as &T;
}

// Set a value in the array
fun set(&mut self, n: Int, val: T) {
self.data_mut()[n] = val;
}

// Print the array
fun print(&self) {
print("[");
for let mut i=0; i<N; i+=1; {
print(self.data()[i]);
if i != N - 1 {
print(", ")
}
}
print("]");
}

fun println(&self) {
self.print();
println();
}
}

main();
2 changes: 1 addition & 1 deletion examples/frontend/mandelbrot.sg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fun main() {
let width = 60;
let height = 30;

let max_iter = 1000;
let max_iter = 250;
let zoom = 1.25;

let bounds = get_bounds(x_center, y_center, zoom);
Expand Down
5 changes: 5 additions & 0 deletions examples/test-output/const-generics-array.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[1, 2, 3, 4, 5]
1000
[1, 1000, 3, 4, 5]
[1, 1000, 3, 4, 5]
[555, 1000, 3, 4, 5]
60 changes: 30 additions & 30 deletions examples/test-output/mandelbrot.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
............................................ ..........
............................................ .........
............................................. ..........
...................................... .. # .......
................................ .. ..
.................................+
...............................
............................*
............................
...........................
......... ... . .......
......... ....
.......# ..
......# .
..
..
......# .
.......# ..
......... ....
......... ... . .......
...........................
............................
............................*
...............................
.................................+
................................ .. ..
...................................... .. # .......
............................................. ..........
............................................ .........
............................................ ..........
...........................................* **........
...........................................* **.......
............................................+ *.........
...............................*...... *. ++.+...
................................ .* +.
................................*
..........................**...
............................#
..........................**
........**.....*.........*.
........+ .+* * ......*
........* *...
....... ..
.*.*.. *
**
**
.*.*.. *
....... ..
........* *...
........+ .+* * ......*
........**.....*.........*.
..........................**
............................#
..........................**...
................................*
................................ .* +.
...............................*...... *. ++.+...
............................................+ *.........
...........................................* **.......
...........................................* **........

0 comments on commit 48f27fa

Please sign in to comment.