harec

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

31-postfix.ha (1771B)


      1 type coords = struct { x: size, y: size };
      2 type coords3 = struct { _2: coords, z: size };
      3 
      4 fn foo() size = 2;
      5 fn equal(x: int, y: int) bool = x == y;
      6 fn aggregate(c: coords) coords = c;
      7 fn not(x: bool) bool = x == false;
      8 
      9 fn nested() void = {
     10 	let c = coords3 {
     11 		_2 = coords {
     12 			x = 10,
     13 			y = 20,
     14 		},
     15 		z = 30,
     16 	};
     17 	assert(c._2.x == 10);
     18 	assert(c._2.y == 20);
     19 	assert(c.z == 30);
     20 
     21 	let x = coords { x = 10, y = 20 };
     22 	let a = [x, x, x, x];
     23 	assert(a[0].x == 10);
     24 	assert(a[0].y == 20);
     25 	assert(a[1].x == 10);
     26 	assert(a[1].y == 20);
     27 	assert(a[2].x == 10);
     28 	assert(a[2].y == 20);
     29 	assert(a[3].x == 10);
     30 	assert(a[3].y == 20);
     31 };
     32 
     33 fn nonaccess() void = {
     34 	let c = coords { x = 10, y = 20 };
     35 	assert(aggregate(coords { x = 10, y = 20 }).x == 10);
     36 	assert(aggregate(c).y == 20);
     37 	assert(coords { x = 10, y = 20 }.x == 10);
     38 	assert(coords { x = 10, y = 20 }.y == 20);
     39 	assert([1, 2, 3, 4][2] == 3);
     40 };
     41 
     42 fn deref() void = {
     43 	let a = coords { x = 10, y = 20 };
     44 	let b = &a;
     45 	let c = &b;
     46 	assert(a.x == 10);
     47 	assert(b.x == 10);
     48 	assert(c.x == 10);
     49 
     50 	let x = [1, 3, 3, 7];
     51 	let y = &x;
     52 	let z = &y;
     53 	assert(x[2] == 3);
     54 	assert(y[2] == 3);
     55 	assert(z[2] == 3);
     56 
     57 	let q = coords { x = 2, y = 2 };
     58 	let o = &q;
     59 	assert(x[q.x] == 3);
     60 	assert(x[o.x] == 3);
     61 
     62 	let f = ¬
     63 	let g = &f;
     64 	assert(not(true) == false);
     65 	assert(f(true) == false);
     66 	assert(g(true) == false);
     67 };
     68 
     69 fn calls() void = {
     70 	// Indirect
     71 	let x: size = foo();
     72 	assert(x == 2);
     73 
     74 	// Direct
     75 	let x = [1, 2, 3];
     76 	assert(x[foo()] == 3);
     77 
     78 	// Direct & indirect params
     79 	let x = 1234;
     80 	assert(equal(x, 1234));
     81 
     82 	// Aggregate params and return
     83 	let x = coords { x = 1234, y = 4321 };
     84 	let x = aggregate(x);
     85 	assert(x.x == 1234 && x.y == 4321);
     86 };
     87 
     88 export fn main() void = {
     89 	nested();
     90 	nonaccess();
     91 	deref();
     92 	calls();
     93 };