harec

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

28-insert.ha (2030B)


      1 use rt::{compile, exited, EXIT_SUCCESS};
      2 
      3 fn basics() void = {
      4 	let x: []int = alloc([1, 2, 5]);
      5 	let y = &x;
      6 	insert(x[2], 4);
      7 	insert(x[2], 3);
      8 	assert(len(x) == 5);
      9 	for (let i = 0z; i < len(x); i += 1) {
     10 		assert(x[i] == i: int + 1);
     11 	};
     12 	insert(y[0], 42);
     13 	assert(len(x) == 6 && x[0] == 42);
     14 	free(x);
     15 };
     16 
     17 fn multi() void = {
     18 	let x: []int = alloc([1, 2, 5]);
     19 	insert(x[2], [3, 4]...);
     20 	assert(len(x) == 5);
     21 	for (let i = 0z; i < len(x); i += 1) {
     22 		assert(x[i] == i: int + 1);
     23 	};
     24 	free(x);
     25 
     26 	let x: []int = alloc([1, 2, 5]);
     27 	let y: []int = [3, 4];
     28 	insert(x[2], y...);
     29 	assert(len(x) == 5);
     30 	for (let i = 0z; i < len(x); i += 1) {
     31 		assert(x[i] == i: int + 1);
     32 	};
     33 	free(x);
     34 };
     35 
     36 fn _static() void = {
     37 	let buf: [32]int = [1, 2, 5, 42...];
     38 	let x = buf[..3];
     39 	static insert(x[2], 4);
     40 	static insert(x[2], 3);
     41 	assert(len(x) == 5);
     42 	for (let i = 0z; i < len(x); i += 1) {
     43 		assert(x[i] == i: int + 1);
     44 		assert(buf[i] == i: int + 1);
     45 	};
     46 	let z: []int = [1, 2, 3, 4];
     47 	static insert(x[2], [1, 2, 3, 4]...);
     48 	static insert(x[2], z...);
     49 	for (let i = len(x); i < len(buf); i += 1) {
     50 		assert(buf[i] == 42);
     51 	};
     52 };
     53 
     54 fn typehints() void = {
     55 	let x: []u8 = [];
     56 	insert(x[0], 42);
     57 	insert(x[1], [42]...);
     58 	assert(len(x) == 2);
     59 	assert(x[0] == 42u8);
     60 	assert(x[1] == 42u8);
     61 	free(x);
     62 };
     63 
     64 fn reject() void = {
     65 	assert(compile("
     66 		let x: []u8 = [0u8];
     67 		let y: int = 42;
     68 		insert(x[1], y);
     69 	") as exited != EXIT_SUCCESS); // object member type != value type
     70 	assert(compile("
     71 		let x: []u8 = [0u8];
     72 		let y = 42u8;
     73 		insert(x[1], y...);
     74 	") as exited != EXIT_SUCCESS); // value is not an array or a slice
     75 	assert(compile("
     76 		let x: []u8 = [0u8];
     77 		let y: []int = [42];
     78 		insert(x[1], y...);
     79 	") as exited != EXIT_SUCCESS); // object member type != value member type
     80 	assert(compile("
     81 		let x: []u8 = [0u8];
     82 		let y: []int = [42];
     83 		insert(x[1], [y...], 3);
     84 	") as exited != EXIT_SUCCESS); // insert expression doesn't have a length parameter
     85 };
     86 
     87 export fn main() void = {
     88 	basics();
     89 	multi();
     90 	_static();
     91 	typehints();
     92 	reject();
     93 };