File: abort.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 (53 lines) | stat: -rw-r--r-- 1,182 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
fn write_stderr(s: str) size = write(2, *(&s: **opaque), len(s));

export @symbol("rt.abort") fn _abort(
	path: *str,
	line: u64,
	col: u64,
	msg: str,
) void = {
	write_stderr("Abort: ");
	write_stderr(*path);
	write_stderr(":");
	write_stderr(u64tos(line));
	write_stderr(":");
	write_stderr(u64tos(col));
	write_stderr(": ");
	write_stderr(msg);
	write_stderr("\n");
	kill(getpid(), SIGABRT);
};

// See harec:include/expr.h enum fixed_aborts
const reasons: [_]str = [
	"slice or array access out of bounds",	// 0
	"type assertion failed",		// 1
	"unreachable code",			// 2
	"slice allocation capacity smaller than initializer",	// 3
	"assertion failed",			// 4
	"error occurred",			// 5
];

export fn abort_fixed(path: *str, line: u64, col: u64, i: u64) void = {
	_abort(path, line, col, reasons[i]);
};

fn u64tos(u: u64) str = {
	static let buf: [20]u8 = [0...]; // len("18446744073709551615")
	let sl = buf[..0];
	if (u == 0) {
		static append(sl, '0')!;
	};
	for (u > 0) {
		static append(sl, (u % 10): u8 + '0')!;
		u /= 10;
	};
	for (let s = 0z, e = len(sl) - 1; s < e) {
		let tmp = sl[s];
		sl[s] = sl[e];
		sl[e] = tmp;
		s += 1;
		e -= 1;
	};
	return *(&sl: *str);
};