From 1cceedc5ca053b018bc98e96a7dd77d17dde1ada Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sat, 25 Apr 2020 17:43:13 -0400 Subject: consistently handling indexing pointer to array/slice --- tests/arrs_slices.toc | 47 ++++++++++++++++++++++++++++++++++++++++++++++ tests/arrs_slices_expected | 2 ++ tests/test.sh | 1 + 3 files changed, 50 insertions(+) create mode 100644 tests/arrs_slices.toc create mode 100644 tests/arrs_slices_expected (limited to 'tests') diff --git a/tests/arrs_slices.toc b/tests/arrs_slices.toc new file mode 100644 index 0000000..dc7526d --- /dev/null +++ b/tests/arrs_slices.toc @@ -0,0 +1,47 @@ +#include "std/mem.toc", mem; +#include "std/io.toc", io; +use mem; + +fill_array ::= fn(n ::=, a : &[n]int) { + for i := 0..n-1 { + a[i] = i*i; + } + a[0] = 17; +} + +sum_array ::= fn(n ::=, a : &[n]int) total := 0 { + for i := 0..n-1 { + total += a[i]; + } +} + +fill_slice ::= fn(a: &[]int) { + *a = mem.news(int, 3); + a[0] = 1; + a[1] = 48; + a[2] = 136; +} + +sum_slice ::= fn(a : &[]int) total := 0 { + for i := 0..a.len-1 { + total += a[i]; + } +} + +thing ::= fn() int { + foo : [5]int; + fill_array(&foo); + a := sum_array(&foo); + bar : []int; + defer dels(bar); + fill_slice(&bar); + b := sum_slice(&bar); + a+b +} + +main ::= fn() { + a := thing(); + b ::= thing(); + io.puti(a); + io.puti(b); +} diff --git a/tests/arrs_slices_expected b/tests/arrs_slices_expected new file mode 100644 index 0000000..d41c8a7 --- /dev/null +++ b/tests/arrs_slices_expected @@ -0,0 +1,2 @@ +232 +232 diff --git a/tests/test.sh b/tests/test.sh index 1168e5b..75697ef 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -3,6 +3,7 @@ tests='bf control_flow types +arrs_slices defer sizeof new -- cgit v1.2.3