harec

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

07-aliases.ha (3565B)


      1 type my_int = int;
      2 
      3 fn alias_builtin() void = {
      4 	let i: my_int = 1234;
      5 	assert(i == 1234, "built-in alias");
      6 };
      7 
      8 fn unwrap() void = {
      9 	let i: ...my_int = 1234;
     10 	assert(i == 1234);
     11 };
     12 
     13 type my_array = [3]int;
     14 type my_array_ptr = *my_array;
     15 type my_array_ptr_ptr = *my_array_ptr;
     16 type my_slice = []int;
     17 
     18 fn alias_array() void = {
     19 	let a: my_array = [1, 2, 3];
     20 	let i: my_int = 0;
     21 	let b: my_array_ptr = &a;
     22 	let c: my_array_ptr_ptr = &b;
     23 	let d: my_slice = c[..];
     24 	assert(a[i] == 1, "array alias");
     25 	assert(a[1] == 2, "array alias");
     26 	assert(a[2] == 3, "array alias");
     27 	assert(b[i] == 1, "array ptr alias");
     28 	assert(b[1] == 2, "array ptr alias");
     29 	assert(b[2] == 3, "array ptr alias");
     30 	assert(c[i] == 1, "array ptr ptr alias");
     31 	assert(c[1] == 2, "array ptr ptr alias");
     32 	assert(c[2] == 3, "array ptr ptr alias");
     33 	assert(d[i] == 1, "array ptr ptr slice alias");
     34 	assert(d[1] == 2, "array ptr ptr slice alias");
     35 	assert(d[2] == 3, "array ptr ptr slice alias");
     36 };
     37 
     38 type my_fn = *const fn(_: int) int;
     39 type my_fn_ptr = *my_fn;
     40 type my_fn_ptr_ptr = *my_fn_ptr;
     41 
     42 fn foo(n: int) int = (n + 1) * 2;
     43 
     44 fn alias_fn() void = {
     45 	let f: my_fn = &foo;
     46 	let g: my_fn_ptr = &f;
     47 	let h: my_fn_ptr_ptr = &g;
     48 	assert(f(0) == foo(0), "fn alias");
     49 	assert(g(0) == foo(0), "fn ptr alias");
     50 	assert(h(0) == foo(0), "fn ptr ptr alias");
     51 };
     52 
     53 type my_struct = struct { x: int, y: int };
     54 type my_struct_ptr = *my_struct;
     55 type my_struct_ptr_ptr = *my_struct_ptr;
     56 type my_other_struct = struct { x: my_struct_ptr_ptr };
     57 
     58 fn alias_struct() void = {
     59 	let s: my_struct = struct {
     60 		x: int = 42,
     61 		y: int = 69,
     62 	};
     63 	let t: my_struct_ptr = &s;
     64 	let u: my_struct_ptr_ptr = &t;
     65 	let v: my_other_struct = struct { x: my_struct_ptr_ptr = u };
     66 	assert(s.x == 42, "struct alias");
     67 	assert(s.y == 69, "struct alias");
     68 	assert(t.x == 42, "struct alias ptr");
     69 	assert(t.y == 69, "struct alias ptr");
     70 	assert(u.x == 42, "struct alias ptr ptr");
     71 	assert(u.y == 69, "struct alias ptr ptr");
     72 	assert(v.x.x == 42, "struct alias ptr ptr alias");
     73 	assert(v.x.y == 69, "struct alias ptr ptr alias");
     74 };
     75 
     76 type my_tagged = (int | void);
     77 
     78 fn alias_tagged() void = {
     79 	let a: my_tagged = 42;
     80 	assert(a is int, "tag");
     81 	assert(a as int == 42, "value");
     82 };
     83 
     84 type my_my_array = my_array;
     85 type my_my_int = my_int;
     86 
     87 fn alias_alias() void = {
     88 	let a: my_my_array = [1, 2, 3];
     89 	let i: my_my_int = 0;
     90 	assert(a[i] == 1, "alias alias");
     91 	assert(a[1] == 2, "alias alias");
     92 	assert(a[2] == 3, "alias alias");
     93 };
     94 
     95 type recur = struct {
     96 	self: *recur,
     97 };
     98 
     99 fn recursive() void = {
    100 	let x: recur = struct {
    101 		self: *recur = null: *recur,
    102 	};
    103 	x.self = &x;
    104 	assert(x.self == x.self.self);
    105 };
    106 
    107 fn measurement() void = {
    108 	assert(size(my_int) == size(int) && size(my_my_int) == size(int));
    109 	assert(size(my_array) == size([3]int));
    110 	assert(size(my_array_ptr) == size(*[3]int));
    111 	assert(size(my_slice) == size([]int));
    112 	assert(size(my_struct) == size(struct { x: int, y: int }));
    113 	assert(size(my_struct_ptr) == size(*struct { x: int, y: int }));
    114 	assert(size(my_tagged) == size((int | void)));
    115 
    116 	assert(align(my_int) == align(int) && align(my_my_int) == align(int));
    117 	assert(align(my_array) == align([3]int));
    118 	assert(align(my_array_ptr) == align(*[3]int));
    119 	assert(align(my_slice) == align([]int));
    120 	assert(align(my_struct) == align(struct { x: int, y: int }));
    121 	assert(align(my_struct_ptr) == align(*struct { x: int, y: int }));
    122 	assert(align(my_tagged) == align((int | void)));
    123 };
    124 
    125 export fn main() void = {
    126 	alias_builtin();
    127 	alias_array();
    128 	alias_fn();
    129 	alias_struct();
    130 	alias_tagged();
    131 	alias_alias();
    132 	recursive();
    133 	measurement();
    134 };