harec

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

01-arrays.ha (3614B)


      1 use rt::{compile, exited, EXIT_SUCCESS, EXIT_FAILURE};
      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 	assert(compile("
     22 		export fn main() void = {
     23 			let a = [1, 2, 3];
     24 			a[3];
     25 		};
     26 	") as exited != EXIT_SUCCESS);
     27 };
     28 
     29 fn measurements() void = {
     30 	let x = [1, 2, 3];
     31 	assert(len(x) == 3);
     32 	assert(size([3]int) == size(int) * 3);
     33 
     34 	static assert(len([1, 2, 3]) == 3);
     35 
     36 	assert(align([_]i8) == 1);
     37 	assert(align([_]i16) == 2);
     38 	assert(align([_]i32) == 4);
     39 	assert(align([_]i64) == 8);
     40 	assert(align([*]i8) == 1);
     41 	assert(align([*]i16) == 2);
     42 	assert(align([*]i32) == 4);
     43 	assert(align([*]i64) == 8);
     44 	assert(align([2]i8) == 1);
     45 	assert(align([2]i16) == 2);
     46 	assert(align([2]i32) == 4);
     47 	assert(align([2]i64) == 8);
     48 };
     49 
     50 fn storage() void = {
     51 	let x = [1, 2, 3];
     52 	let y = &x: uintptr;
     53 	assert(*((y + (size(int) * 0): uintptr): *int) == 1);
     54 	assert(*((y + (size(int) * 1): uintptr): *int) == 2);
     55 	assert(*((y + (size(int) * 2): uintptr): *int) == 3);
     56 };
     57 
     58 fn assignment() void = {
     59 	let x = [1, 2, 3];
     60 	let y = x;
     61 	let z = [0, 0, 0];
     62 	z = y;
     63 	let w = [1, 0];
     64 	w = [2, w[0]];
     65 	assert(y[0] == 1 && y[1] == 2 && y[2] == 3);
     66 	assert(z[0] == 1 && z[1] == 2 && z[2] == 3);
     67 	assert(w[0] == 2 && w[1] == 1);
     68 
     69 	assert(compile("
     70 		export fn main() void = {
     71 			let a: [3]uint = [1u,2u,3u];
     72 			let b: uint = 0;
     73 
     74 			let ptr: *[3]uint = &a;
     75 			ptr = &b;
     76 		};
     77 	") as exited != EXIT_SUCCESS);
     78 
     79 	assert(compile(`
     80 		export fn main() void = {
     81 			let a: *[*]uint = &[1u,2u,3u];
     82 			let b: [3]str = ["a", "b", "c"];
     83 			a = &b;
     84 		};
     85 	`) as exited != EXIT_SUCCESS);
     86 };
     87 
     88 fn param(x: [3]int) void = {
     89 	assert(len(x) == 3);
     90 	assert(x[0] == 1);
     91 	assert(x[1] == 2);
     92 	assert(x[2] == 3);
     93 };
     94 
     95 fn nested() void = {
     96 	let x = [[1, 2], [3, 4]];
     97 	assert(x[0][0] == 1 && x[0][1] == 2);
     98 	assert(x[1][0] == 3 && x[1][1] == 4);
     99 	assert(len(x[0]) == 2);
    100 	x[1] = [5, 6];
    101 	assert(x[1][0] == 5 && x[1][1] == 6);
    102 };
    103 
    104 fn expanded() void = {
    105 	let a: [5]int = [1337...];
    106 	for (let i = 0z; i < len(a); i += 1) {
    107 		assert(a[i] == 1337);
    108 	};
    109 
    110 	let b: [5]struct { x: int, y: int } = [struct {
    111 		x: int = 10,
    112 		y: int = 20,
    113 	}...];
    114 	for (let i = 0z; i < len(b); i += 1) {
    115 		assert(b[i].x == 10 && b[i].y == 20);
    116 	};
    117 
    118 	let c: [5]int = [1, 2, 3...];
    119 	let expected = [1, 2, 3, 3, 3];
    120 	for (let i = 0z; i < len(c); i += 1) {
    121 		assert(c[i] == expected[i]);
    122 	};
    123 
    124 	let q: [65535]int = [1, 2, 3...];
    125 	let expected = [1, 2, 3];
    126 	for (let i = 0z; i < len(expected); i += 1) {
    127 		assert(q[i] == expected[i]);
    128 	};
    129 	for (let i = 3z; i < len(q); i += 1) {
    130 		assert(q[i] == 3);
    131 	};
    132 };
    133 
    134 fn extype() void = {
    135 	let x: [5]u8 = [42...];
    136 	for (let i = 0z; i < len(x); i += 1) {
    137 		assert(x[i] == 42);
    138 	};
    139 	x[0] = 24;
    140 	assert(x[0] == 24);
    141 	assert(x[1] == 42);
    142 };
    143 
    144 fn eval_array() void = {
    145 	static let a = [1, 2];
    146 };
    147 
    148 fn eval_access() void = {
    149 	static assert([1, 2][0] == 1 && [1, 2][1] == 2);
    150 };
    151 
    152 fn reject() void = {
    153 	// unbounded arrays of values of undefined size
    154 	assert(compile("fn f() void = { let x = null: *[*][*]int; };")
    155 		as exited == EXIT_FAILURE);
    156 	assert(compile("fn f() void = { let x = null: *[*]fn ()int; };")
    157 		as exited == EXIT_FAILURE);
    158 };
    159 
    160 export fn main() void = {
    161 	indexing();
    162 	measurements();
    163 	storage();
    164 	assignment();
    165 	param([1, 2, 3]);
    166 	nested();
    167 	expanded();
    168 	extype();
    169 	eval_array();
    170 	eval_access();
    171 	reject();
    172 };