File: compile.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 (110 lines) | stat: -rw-r--r-- 2,441 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
export type status = enum {
	SUCCESS,
	USER,
	LEX,
	PARSE,
	CHECK,
	_EXEC_FAILED,
	ABNORMAL = 255,
};

fn strstatus(status: int) str = {
	switch (status) {
	case status::SUCCESS =>
		return "success (0)";
	case status::USER =>
		return "user (1)";
	case status::LEX =>
		return "lex (2)";
	case status::PARSE =>
		return "parse (3)";
	case status::CHECK =>
		return "check (4)";
	case status::ABNORMAL =>
		return "abnormal (255)";
	case status::_EXEC_FAILED =>
		return "internal error: compiler execve failed";
	case =>
		return itos(status: int);
	};
};

export type error = !void;

// Runs the Hare compiler and returns the exit status.
export fn compile(
	expected: (status | void),
	src: str,
	flags: str...
) (void | error) = {
	let wstatus = 0;
	let pipefd = [-1, -1];
	assert(pipe2(&pipefd, 0) == 0);

	const child = fork();
	if (child == 0) {
		close(pipefd[1]);
		dup2(pipefd[0], 0);
		close(1);
		close(2);

		// FIXME use $BINOUT variable
		let harec = alloc_cstr("./.bin/harec");
		let argv: []nullable *u8 = alloc([harec]...)!;

		for (let i = 0z; i < len(flags); i += 1) {
			append(argv, alloc_cstr(flags[i]))!;
		};
		append(argv, [alloc_cstr("-o/dev/null"), alloc_cstr("-"), null]...)!;

		execve(harec, *(&argv: **[*]nullable *u8), envp);
		exit(status::_EXEC_FAILED);
	} else {
		assert(child != -1, "fork(2) failed");
		close(pipefd[0]);

		const buf = alloc_cstr(src): *const [*]u8;
		defer free(buf);
		for (let n = 0z; n < len(src)) {
			let m = write(pipefd[1], &buf[n], len(src) - n): size;
			assert(m > 0, "write(2) failed");
			n += m;
		};

		close(pipefd[1]);
		wait4(child, &wstatus, 0, null);
	};

	if (!wifexited(wstatus)) {
		assert(wifsignaled(wstatus));
		write_stderr("signaled ");
		write_stderr(itos(wtermsig(wstatus)));
		write_stderr("\n");
		return error;
	};

	match (expected) {
	case void =>
		const status = wexitstatus(wstatus);
		if (status == status::SUCCESS) {
			write_stderr("expected any failure, got success\n");
			return error;
		};
	case let expected: status =>
		const exit_status = wexitstatus(wstatus);
		if (exit_status == status::_EXEC_FAILED) {
			write_stderr(strstatus(exit_status));
			write_stderr("\n");
			return error;
		};
		if (exit_status != expected) {
			let s = "expected ";
			write_stderr("expected ");
			write_stderr(strstatus(expected));
			write_stderr(", got ");
			write_stderr(strstatus(exit_status));
			write_stderr("\n");
			return error;
		};
	};
};