-
Notifications
You must be signed in to change notification settings - Fork 30
/
snek-error.c
135 lines (121 loc) · 2.53 KB
/
snek-error.c
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
/*
* Copyright © 2018 Keith Packard <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include "snek.h"
bool snek_abort;
static void
puts_clean(char *s)
{
unsigned char c;
while ((c = (unsigned char) *s++)) {
if (c < ' ') {
putc('\\', stderr);
switch (c) {
case '\n':
putc('n', stderr);
break;
default:
fprintf(stderr, "x%02x", c);
break;
}
} else
putc(c, stderr);
}
}
snek_poly_t
snek_error_name(CONST char *format, ...)
{
va_list args;
char c;
if (snek_abort)
return SNEK_NULL;
snek_abort = true;
va_start(args, format);
#ifdef SNEK_NO_FILE
fprintf(stderr, "<stdin>:%d ", snek_line);
#else
fprintf(stderr, "%s:%d ", snek_file, snek_line);
#endif
while ((c = *format++)) {
if (c == '%') {
switch ((c = *format++)) {
case 'd':
fprintf(stderr, "%d", va_arg(args, int));
break;
case 's':
puts_clean(va_arg(args, char *));
break;
case 'p':
snek_poly_print(stderr, va_arg(args, snek_poly_t), 'r');
break;
#if SNEK_DEBUG
default:
snek_panic("bad snek_error format");
break;
#endif
}
} else
putc(c, stderr);
}
putc('\n', stderr);
va_end(args);
return SNEK_NULL;
}
snek_poly_t
snek_error_0_name(CONST char *string)
{
return snek_error_name(string);
}
snek_poly_t
snek_error_value(snek_poly_t p)
{
return snek_error("invalid value: %p", p);
}
snek_poly_t
snek_error_type_2(snek_poly_t a, snek_poly_t b)
{
return snek_error("type mismatch: %p %p", a, b);
}
snek_poly_t
snek_error_type_1(snek_poly_t a)
{
return snek_error("invalid type: %p", a);
}
snek_poly_t
snek_error_step(void)
{
return snek_error_0("zero step");
}
snek_poly_t
snek_error_args(snek_soffset_t want, snek_soffset_t got)
{
return snek_error("bad args. wanted %d got %d", want, got);
}
snek_poly_t
snek_error_arg(snek_id_t bad)
{
return snek_error("bad arg \"%s\"", snek_name_string(bad));
}
snek_poly_t
snek_error_syntax(char *where)
{
return snek_error("Syntax error at \"%s\".", where);
}
#if SNEK_DEBUG || defined(DEBUG_MEMORY)
void
snek_panic(const char *message)
{
snek_error("%s", message);
abort();
}
#endif