~qpfiffer/lair

lair/src/error.c -rw-r--r-- 1.1 KiB
825e6a4eQuinlan Pfiffer Don't need to check here, just use throw_exception. 5 months ago
                                                                                
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
// vim: noet ts=4 sw=4
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "error.h"
#include "lair.h"
#include "parse.h"

static inline const char *_friendly_err(const ERROR_TYPE err) {
	switch (err) {
		case ERR_PARSE:			return "ERR_PARSE";
		case ERR_RUNTIME:		return "ERR_RUNTIME";
		case ERR_SYNTAX:		return "ERR_SYNTAX";
		default:				return "ERROR";
	}
}

void check(
		struct _lair_runtime *r,
		const int cond,
		const ERROR_TYPE err_type, const char *msg) {
	if (!cond)
		throw_exception(r, err_type, msg);
}

void throw_exception(
		struct _lair_runtime *r,
		const ERROR_TYPE err_type,
		const char *msg) {
	r->exception_type = err_type;
	r->exception_msg = strdup(msg);

	longjmp(r->exception_buffer, 1);
}

void error_and_die(const ERROR_TYPE err_type, const char *msg) {
	print_error(err_type, msg);
	exit(1);
}

void print_error(const ERROR_TYPE err_type, const char *msg) {
	const char *friendly_err = _friendly_err(err_type);
	char buf[256] = {0};

	snprintf(buf, sizeof(buf), "%c[%dm%s%c[%dm", 0x1B, 31, friendly_err, 0x1B, 0x0);
	snprintf(buf + strlen(buf), sizeof(buf), ": %s\n", msg);
	printf("%s", buf);
}