harec

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

32-copy.ha (1136B)


      1 type coords = struct { x: i8, y: int, z: size };
      2 type anyint = struct { _8: i8, _16: i16, _32: i32, _64: i64 };
      3 
      4 export fn main() void = {
      5 	// Simple case
      6 	let x = 10;
      7 	let y = x;
      8 	assert(y == 10);
      9 
     10 	// With indirect target
     11 	let a = [1, 2, 3, 4];
     12 	let x = 2z;
     13 	assert(a[x] == 3);
     14 
     15 	// Aggregate types:
     16 	// arrays
     17 	let x = [1, 2, 3, 4];
     18 	let y = x;
     19 	assert(&x != &y);
     20 	assert(x[0] == y[0]);
     21 	assert(x[1] == y[1]);
     22 	assert(x[2] == y[2]);
     23 	assert(x[3] == y[3]);
     24 
     25 	// structs
     26 	let a = coords { x = 10, y = 20, z = 30 };
     27 	let b = a;
     28 	assert(&a != &b);
     29 	assert(a.x == b.x);
     30 	assert(a.y == b.y);
     31 	assert(a.z == b.z);
     32 
     33 	// unions
     34 	let a = anyint { _16 = 10, ... };
     35 	let b = a;
     36 	assert(&a != &b);
     37 	assert(a._16 == b._16);
     38 
     39 	// tuples
     40 	let a = (1, 2z, 3u8);
     41 	let b = a;
     42 	assert(a.0 == b.0);
     43 	assert(a.1 == b.1);
     44 	assert(a.2 == b.2);
     45 
     46 	let x = "hello world";
     47 	let y = x;
     48 	let px = &x: *struct {
     49 		data: *[*]u8,
     50 		length: size,
     51 		capacity: size,
     52 	};
     53 	let py = &y: *struct {
     54 		data: *[*]u8,
     55 		length: size,
     56 		capacity: size,
     57 	};
     58 	assert(px.length == py.length);
     59 	assert(px.capacity == py.capacity);
     60 	assert(px.data == py.data);
     61 
     62 	// TODO: Slices
     63 };