File: 28-insert.ha

package info (click to toggle)
harec 0.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,420 kB
  • sloc: ansic: 19,905; asm: 247; makefile: 116; lisp: 80; sh: 45
file content (168 lines) | stat: -rw-r--r-- 3,496 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use rt::{compile, status};

fn basics() void = {
	let x: []int = alloc([1, 2, 5])!;
	let y = &x;
	insert(x[2], 4)!;
	insert(x[2], 3)!;
	assert(len(x) == 5);
	for (let i = 0z; i < len(x); i += 1) {
		assert(x[i] == i: int + 1);
	};
	let res: (void | nomem) = insert(y[0], 42)!;
	assert(res is void);
	assert(len(x) == 6 && x[0] == 42);
	free(x);
};

fn multi() void = {
	let x: []int = alloc([1, 2, 5])!;
	insert(x[2], [3, 4]...)!;
	assert(len(x) == 5);
	for (let i = 0z; i < len(x); i += 1) {
		assert(x[i] == i: int + 1);
	};
	free(x);

	let x: []int = alloc([1, 2, 5])!;
	let y: []int = [3, 4];
	insert(x[2], y...)!;
	assert(len(x) == 5);
	for (let i = 0z; i < len(x); i += 1) {
		assert(x[i] == i: int + 1);
	};
	free(x);

	let x: []int = alloc([], 3)!;
	insert(x[0], [1, 2, 3]...)!;
	assert(len(x) == 3);
	free(x);
};

fn _static() void = {
	let buf: [32]int = [1, 2, 5, 42...];
	let x = buf[..3];
	static insert(x[2], 4)!;
	static insert(x[2], 3)!;
	assert(len(x) == 5);
	for (let i = 0z; i < len(x); i += 1) {
		assert(x[i] == i: int + 1);
		assert(buf[i] == i: int + 1);
	};
	let z: []int = [1, 2, 3, 4];
	static insert(x[2], [1, 2, 3, 4]...)!;
	static insert(x[2], z...)!;
	for (let i = len(x); i < len(buf); i += 1) {
		assert(buf[i] == 42);
	};

	let x = [1, 2, 3][..0];
	static insert(x[0], [1, 2, 3]...)!;
	assert(len(x) == 3);
};

fn withlength() void = {
	let x: []size = alloc([0, 0, 2, 2])!;
	insert(x[2], [1...], 2)!;

	assert(len(x) == 6);
	for (let i = 0z; i < len(x); i += 1) {
		assert(x[i] == i / 2);
	};

	free(x);
};

fn typehints() void = {
	let x: []u8 = [];
	insert(x[0], 42)!;
	insert(x[1], [42]...)!;
	assert(len(x) == 2);
	assert(x[0] == 42u8);
	assert(x[1] == 42u8);
	free(x);
};

fn reject() void = {
	compile(status::CHECK, "
		fn test() void = {
			let x: []u8 = [0u8];
			let y: int = 42;
			insert(x[1], y)!;
		};
	")!; // object member type != value type
	compile(status::CHECK, "
		fn test() void = {
			let x: []u8 = [0u8];
			let y = 42u8;
			insert(x[1], y...)!;
		};
	")!; // value is not an array or a slice
	compile(status::CHECK, "
		fn test() void = {
			let x: []u8 = [0u8];
			let y: []int = [42];
			insert(x[1], y...)!;
		};
	")!; // object member type != value member type
	compile(status::CHECK, "
		fn test() void = {
			let x: []u8 = [0u8];
			insert(x[1], [42i...], 3)!;
		};
	")!; // same as above, but for an expression with length
	compile(status::CHECK, "
		fn test() void = {
			let x: []u8 = [0u8];
			insert(x[1], [0u8...], 2i)!;
		};
	")!; // length expression is not assignable to size
	compile(status::CHECK, "
		fn test() void = {
			let x: []u8 = [0u8];
			insert(x[1], [42], 3)!;
		};
	")!; // must be an expandable array
	compile(status::CHECK, "
		fn test() void = {
			let x: []u8 = [0u8];
			let x: nullable *[]u8 = &x;
			insert(x[1], 42)!;
		};
	")!; // object member type is nullable pointer
	compile(status::PARSE, "
		fn test() void = {
			let x: []u8 = [0u8];
			insert(x[1], [0...]..., 3)!;
		};
	")!; // ellipsis with length
	compile(status::CHECK, "
		fn test() void = {
			let x: []u8 = [0u8];
			insert(x[1], [1]: [*]u8...)!;
		};
	")!; // unbounded array value
	compile(status::CHECK, "
		let y: opaque;
		fn test() void = {
			let x = []: []u8: []opaque;
			insert(x[0], y)!;
		};
	")!; // value has undefined size
};

fn _never() void = {
	let x: []int = [];
	insert(x[0], return)!;
	abort();
};

export fn main() void = {
	basics();
	multi();
	_static();
	withlength();
	typehints();
	reject();
	_never();
};