harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

01-arrays.ha (2903B)


      1 use rt;
      2 
      3 fn indexing() void = {
      4 	let x = [1, 2, 3];
      5 	let y = &x;
      6 	let z = &y;
      7 	assert(x[0] == 1 && x[1] == 2 && x[2] == 3);
      8 	assert(y[0] == 1 && y[1] == 2 && y[2] == 3);
      9 	assert(z[0] == 1 && z[1] == 2 && z[2] == 3);
     10 
     11 	x[0] = 5;
     12 	x[1] = 6;
     13 	x[2] = 7;
     14 	assert(x[0] == 5 && x[1] == 6 && x[2] == 7);
     15 	assert(y[0] == 5 && y[1] == 6 && y[2] == 7);
     16 
     17 	let q = &x[0];
     18 	*q = 1337;
     19 	assert(x[0] == 1337 && y[0] == 1337);
     20 };
     21 
     22 fn measurements() void = {
     23 	let x = [1, 2, 3];
     24 	assert(len(x) == 3);
     25 	assert(size([3]int) == size(int) * 3);
     26 
     27 	static assert(len([1, 2, 3]) == 3);
     28 };
     29 
     30 fn storage() void = {
     31 	let x = [1, 2, 3];
     32 	let y = &x: uintptr;
     33 	assert(*((y + (size(int) * 0): uintptr): *int) == 1);
     34 	assert(*((y + (size(int) * 1): uintptr): *int) == 2);
     35 	assert(*((y + (size(int) * 2): uintptr): *int) == 3);
     36 };
     37 
     38 fn alignment() void = {
     39 	let x: [_]i32 = [1, 2, 3];
     40 	assert(&x: uintptr: size % 4 == 0);
     41 	let y: [_]i64 = [1, 2, 3];
     42 	assert(&y: uintptr: size % 8 == 0);
     43 };
     44 
     45 fn assignment() void = {
     46 	let x = [1, 2, 3];
     47 	let y = x;
     48 	let z = [0, 0, 0];
     49 	z = y;
     50 	assert(y[0] == 1 && y[1] == 2 && y[2] == 3);
     51 	assert(z[0] == 1 && z[1] == 2 && z[2] == 3);
     52 
     53 	assert(rt::compile("
     54 		export fn main() void = {
     55 			let a: [3]uint = [1u,2u,3u];
     56 			let b: uint = 0;
     57 
     58 			let ptr: *[3]uint = &a;
     59 			ptr = &b;
     60 		};
     61 	") != 0);
     62 
     63 	assert(rt::compile(`
     64 		export fn main() void = {
     65 			let a: *[*]uint = &[1u,2u,3u];
     66 			let b: [3]str = ["a", "b", "c"];
     67 			a = &b;
     68 		};
     69 	`) != 0);
     70 };
     71 
     72 fn param(x: [3]int) void = {
     73 	assert(len(x) == 3);
     74 	assert(x[0] == 1);
     75 	assert(x[1] == 2);
     76 	assert(x[2] == 3);
     77 };
     78 
     79 fn nested() void = {
     80 	let x = [[1, 2], [3, 4]];
     81 	assert(x[0][0] == 1 && x[0][1] == 2);
     82 	assert(x[1][0] == 3 && x[1][1] == 4);
     83 	assert(len(x[0]) == 2);
     84 	x[1] = [5, 6];
     85 	assert(x[1][0] == 5 && x[1][1] == 6);
     86 };
     87 
     88 fn expanded() void = {
     89 	let a: [5]int = [1337...];
     90 	for (let i = 0z; i < len(a); i += 1) {
     91 		assert(a[i] == 1337);
     92 	};
     93 
     94 	let b: [5]struct { x: int, y: int } = [struct {
     95 		x: int = 10,
     96 		y: int = 20,
     97 	}...];
     98 	for (let i = 0z; i < len(b); i += 1) {
     99 		assert(b[i].x == 10 && b[i].y == 20);
    100 	};
    101 
    102 	let c: [5]int = [1, 2, 3...];
    103 	let expected = [1, 2, 3, 3, 3];
    104 	for (let i = 0z; i < len(c); i += 1) {
    105 		assert(c[i] == expected[i]);
    106 	};
    107 
    108 	let q: [65535]int = [1, 2, 3...];
    109 	let expected = [1, 2, 3];
    110 	for (let i = 0z; i < len(expected); i += 1) {
    111 		assert(q[i] == expected[i]);
    112 	};
    113 	for (let i = 3z; i < len(q); i += 1) {
    114 		assert(q[i] == 3);
    115 	};
    116 };
    117 
    118 fn extype() void = {
    119 	let x: [5]u8 = [42...];
    120 	for (let i = 0z; i < len(x); i += 1) {
    121 		assert(x[i] == 42);
    122 	};
    123 	x[0] = 24;
    124 	assert(x[0] == 24);
    125 	assert(x[1] == 42);
    126 };
    127 
    128 fn eval_array() void = {
    129 	static let a = [1, 2];
    130 };
    131 
    132 fn eval_access() void = {
    133 	static assert([1, 2][0] == 1 && [1, 2][1] == 2);
    134 };
    135 
    136 export fn main() void = {
    137 	indexing();
    138 	measurements();
    139 	storage();
    140 	alignment();
    141 	assignment();
    142 	param([1, 2, 3]);
    143 	nested();
    144 	expanded();
    145 	extype();
    146 	eval_array();
    147 	eval_access();
    148 };