harec

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

03-pointers.ha (2812B)


      1 use rt::{compile, exited, EXIT_SUCCESS};
      2 
      3 type intp = *int;
      4 
      5 fn basics() void = {
      6 	let x = 42;
      7 	let y: intp = &x;
      8 	assert(*y == 42);
      9 	*y = 1337;
     10 	assert(x == 1337);
     11 
     12 	assert(size(*int) == size(uintptr));
     13 	assert(align(*int) == align(uintptr));
     14 };
     15 
     16 fn _nullable() void = {
     17 	let x: nullable *int = null;
     18 	assert(x == null);
     19 	let y = 42;
     20 	x = &y;
     21 	assert(*(x: *int) == 42);
     22 
     23 	assert(compile(
     24 		"fn test() void = { let x: nullable *int = null; let z = *x; };",
     25 	) as exited != EXIT_SUCCESS);
     26 };
     27 
     28 fn casts() void = {
     29 	let a: *uint = &4u;
     30 	let b = a: *void;
     31 	let c = b: *uint;
     32 	assert(a == c && *c == 4);
     33 
     34 	let a: nullable *uint = &7u;
     35 	let b = a: *uint;
     36 	assert(b == a && *b == 7);
     37 
     38 	let a: nullable *uint = &10u;
     39 	let b = a as *uint;
     40 	assert(b == a && *b == 10);
     41 
     42 	let a: nullable *int = &4;
     43 	assert(a is *int);
     44 
     45 	let a: nullable *int = null;
     46 	assert(a is null);
     47 	assert((a as null): nullable *void == null);
     48 
     49 	let a: nullable *int = &4;
     50 	assert(a is *int);
     51 };
     52 
     53 fn reject() void = {
     54 	assert(compile("
     55 		type s = null;
     56 		fn test() void = {
     57 			void;
     58 		};
     59 	") as exited != EXIT_SUCCESS);
     60 	assert(compile("
     61 		type s = *null;
     62 		fn test() void = {
     63 			void;
     64 		};
     65 	") as exited != EXIT_SUCCESS);
     66 	assert(compile("
     67 		fn test() void = {
     68 			let a = &null;
     69 		};
     70 	") as exited != EXIT_SUCCESS);
     71 	assert(compile("
     72 		fn test() void = {
     73 			let a = &3: null;
     74 		};
     75 	") as exited != EXIT_SUCCESS);
     76 	assert(compile("
     77 		fn test() void = {
     78 			let b: nullable *int = null;
     79 			let a = b as null;
     80 		};
     81 	") as exited != EXIT_SUCCESS);
     82 	assert(compile("
     83 		fn test() void = {
     84 			let a = (null, 3);
     85 		};
     86 	") as exited != EXIT_SUCCESS);
     87 	assert(compile("
     88 		fn test() void = {
     89 			let a: []null = [null];
     90 		};
     91 	") as exited != EXIT_SUCCESS);
     92 	assert(compile("
     93 		fn test() void = {
     94 			let a = [null];
     95 		};
     96 	") as exited != EXIT_SUCCESS);
     97 	assert(compile("
     98 		fn test() void = {
     99 			let a: [_]null = [null];
    100 		};
    101 	") as exited != EXIT_SUCCESS);
    102 	assert(compile("
    103 		fn test() void = {
    104 			let a = null;
    105 		};
    106 	") as exited != EXIT_SUCCESS);
    107 	assert(compile("
    108 		fn test() void = {
    109 			let a: nullable *int = &4;
    110 			a as int;
    111 		};
    112 	") as exited != EXIT_SUCCESS);
    113 	assert(compile("
    114 		fn test() void = {
    115 			let a: nullable *int = &4;
    116 			a as *str;
    117 		};
    118 	") as exited != EXIT_SUCCESS);
    119 
    120 	// type assertions on non-nullable pointers are prohibited
    121 	assert(compile("
    122 		fn test() void = {
    123 			let a: *int = &4;
    124 			assert(a as *int);
    125 		};
    126 	") as exited != EXIT_SUCCESS);
    127 
    128 	// dereference expression not in translation-compatible subset
    129 	assert(compile("
    130 		let a: int = 0;
    131 		let b: *int = &a;
    132 		let c: int = *b;
    133 	") as exited != EXIT_SUCCESS);
    134 
    135 	// dereference of a zero-sized type
    136 	assert(compile("
    137 		fn test() void = {
    138 			let a: *void = &12;
    139 			*a;
    140 		};
    141 	") as exited != EXIT_SUCCESS);
    142 };
    143 
    144 export fn main() void = {
    145 	basics();
    146 	_nullable();
    147 	casts();
    148 	reject();
    149 };