harec

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

21-tuples.ha (3059B)


      1 use rt;
      2 
      3 fn storage() void = {
      4 	let x: (int, size) = (42, 1337);
      5 	assert(size((int, size)) == size(size) * 2);
      6 	let ptr = &x: *struct { i: int, z: size };
      7 	assert(ptr.i == 42 && ptr.z == 1337);
      8 };
      9 
     10 fn indexing() void = {
     11 	let x: ((int, uint), size) = ((42, 69), 1337);
     12 	assert(x.0.0 == 42);
     13 	assert(x.0.1 == 69);
     14 	assert(x.1 == 1337);
     15 	assert(x.1z == 1337);
     16 	assert(x.0x1 == 1337);
     17 	assert(x.1e+0 == 1337);
     18 };
     19 
     20 fn func(in: (int, size)) (int, size) = (in.0 + 1, in.1 + 1);
     21 fn eval_expr_access() void = {
     22 	static assert((42, 0).0 == 42 && (42, 0).1 == 0);
     23 };
     24 
     25 fn eval_expr_tuple() void = {
     26 	static let t = (42, 8);
     27 };
     28 
     29 fn funcs() void = {
     30 	let x = func((41, 1336));
     31 	assert(x.0 == 42 && x.1 == 1337);
     32 };
     33 
     34 fn unpacking_static() int = {
     35 	static let (a, b) = (0, 0);
     36 	a += 1;
     37 	b += 1;
     38 	return a;
     39 };
     40 
     41 fn unpacking_demo() (int, int) = {
     42 	return (10, 20);
     43 };
     44 
     45 fn unpacking_eval() (int, int) = {
     46 	static let i = 0;
     47 	const res = (10 + i, 20 + i);
     48 	i += 1;
     49 	return res;
     50 };
     51 
     52 let unpacking_global: int = 0i;
     53 
     54 fn unpacking_addone() int = {
     55 	unpacking_global += 1;
     56 	return unpacking_global;
     57 };
     58 
     59 type tuple_alias = (int, int);
     60 fn unpacking() void = {
     61 	const (a, b, c) = (42, 8, 12);
     62 	assert(a == 42);
     63 	assert(b == 8);
     64 	assert(c == 12);
     65 
     66 	const (a, b): (i64, u64) = (2i, 4z);
     67 	assert(a == 2i64);
     68 	assert(b == 4u64);
     69 
     70 	const (a, b, c): (i64, str, f64) = (2i, "hello", 1.0);
     71 	assert(a == 2i64);
     72 	assert(b == "hello");
     73 	assert(c == 1.0f64);
     74 
     75 	let (a, b): (i64, u64) = (1i, 3z);
     76 	a += 1;
     77 	b += 1;
     78 	assert(a == 2i64);
     79 	assert(b == 4u64);
     80 
     81 	const (_, b, c) = (1, 2, 3);
     82 	assert(b == 2);
     83 	assert(c == 3);
     84 
     85 	const (a, _, c) = (1, 2, 3);
     86 	assert(a == 1);
     87 	assert(c == 3);
     88 
     89 	const (a, b, _) = (1, 2, 3);
     90 	assert(a == 1);
     91 	assert(b == 2);
     92 
     93 	const t: tuple_alias = (1, 2);
     94 	const (a, b) = t;
     95 	assert(a == 1);
     96 	assert(b == 2);
     97 
     98 	unpacking_static();
     99 	unpacking_static();
    100 	const a = unpacking_static();
    101 	assert(a == 3);
    102 
    103 	const (a, b) = unpacking_demo();
    104 	assert(a == 10);
    105 	assert(b == 20);
    106 
    107 	const (a, b) = unpacking_eval();
    108 	assert(a == 10);
    109 	assert(b == 20);
    110 
    111 	let (a, b, _, d) = (unpacking_addone(), unpacking_addone(),
    112 		unpacking_addone(), unpacking_addone());
    113 	assert(a == 1 && b == 2 && d == 4);
    114 
    115 	assert(rt::compile("
    116 		export fn main() void = {
    117 			let (_, _) = (1, 2);
    118 		};
    119 	") != 0);
    120 	assert(rt::compile("
    121 		export fn main() void = {
    122 			let (x, y) = (1, 2, 3);
    123 		};
    124 	") != 0);
    125 	assert(rt::compile("
    126 		export fn main() void = {
    127 			let (x, y) = 5;
    128 		};
    129 	") != 0);
    130 	assert(rt::compile("
    131 		fn getval() int = 5;
    132 		export fn main() void = {
    133 			static let (a, b) = (2, getval());
    134 		};
    135 	") != 0);
    136 	assert(rt::compile("
    137 		fn getval() int = 5;
    138 		export fn main() void = {
    139 			static let (a, _) = (2, getval());
    140 		};
    141 	") != 0);
    142 	assert(rt::compile("
    143 		export fn main() void = {
    144 			let (a, b): int = (2, 3);
    145 		};
    146 	") != 0);
    147 };
    148 
    149 // Regression tests for miscellaneous compiler bugs
    150 fn regression() void = {
    151 	let a: (((int | void), int) | void) = (void, 0);
    152 };
    153 
    154 export fn main() void = {
    155 	storage();
    156 	indexing();
    157 	funcs();
    158 	eval_expr_tuple();
    159 	eval_expr_access();
    160 	unpacking();
    161 	regression();
    162 };