File: 31-postfix.ha

package info (click to toggle)
harec 0.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,480 kB
  • sloc: ansic: 20,054; asm: 335; makefile: 116; lisp: 80; sh: 45
file content (93 lines) | stat: -rw-r--r-- 1,771 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
type coords = struct { x: size, y: size };
type coords3 = struct { _2: coords, z: size };

fn foo() size = 2;
fn equal(x: int, y: int) bool = x == y;
fn aggregate(c: coords) coords = c;
fn not(x: bool) bool = x == false;

fn nested() void = {
	let c = coords3 {
		_2 = coords {
			x = 10,
			y = 20,
		},
		z = 30,
	};
	assert(c._2.x == 10);
	assert(c._2.y == 20);
	assert(c.z == 30);

	let x = coords { x = 10, y = 20 };
	let a = [x, x, x, x];
	assert(a[0].x == 10);
	assert(a[0].y == 20);
	assert(a[1].x == 10);
	assert(a[1].y == 20);
	assert(a[2].x == 10);
	assert(a[2].y == 20);
	assert(a[3].x == 10);
	assert(a[3].y == 20);
};

fn nonaccess() void = {
	let c = coords { x = 10, y = 20 };
	assert(aggregate(coords { x = 10, y = 20 }).x == 10);
	assert(aggregate(c).y == 20);
	assert(coords { x = 10, y = 20 }.x == 10);
	assert(coords { x = 10, y = 20 }.y == 20);
	assert([1, 2, 3, 4][2] == 3);
};

fn deref() void = {
	let a = coords { x = 10, y = 20 };
	let b = &a;
	let c = &b;
	assert(a.x == 10);
	assert(b.x == 10);
	assert(c.x == 10);

	let x = [1, 3, 3, 7];
	let y = &x;
	let z = &y;
	assert(x[2] == 3);
	assert(y[2] == 3);
	assert(z[2] == 3);

	let q = coords { x = 2, y = 2 };
	let o = &q;
	assert(x[q.x] == 3);
	assert(x[o.x] == 3);

	let f = ¬
	let g = &f;
	assert(not(true) == false);
	assert(f(true) == false);
	assert(g(true) == false);
};

fn calls() void = {
	// Indirect
	let x: size = foo();
	assert(x == 2);

	// Direct
	let x = [1, 2, 3];
	assert(x[foo()] == 3);

	// Direct & indirect params
	let x = 1234;
	assert(equal(x, 1234));

	// Aggregate params and return
	let x = coords { x = 1234, y = 4321 };
	let x = aggregate(x);
	assert(x.x == 1234 && x.y == 4321);
};

export fn main() void = {
	nested();
	nonaccess();
	deref();
	calls();
};