-
Notifications
You must be signed in to change notification settings - Fork 0
/
aweexcept.c
140 lines (112 loc) · 3.96 KB
/
aweexcept.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
136
137
138
139
140
/* aweexcept.c -- the "Exceptional Condition" variables
and their predefined record class 'EXCEPTION', See section 8.5.
This file is part of Awe. Copyright 2012 Glyn Webster.
This file is free software: you can redistribute it and/or modify it
under the terms of the GNU Limited General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Awe 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.
You should have received a copy of the GNU Limited General Public
License along with Awe. If not, see <http://www.gnu.org/licenses/>.
*/
#include "awe.h"
#include <stdlib.h>
void *divzero;
void *intdivzero;
void *sqrterr;
void *experr;
void *lnlogerr;
void *sincoserr;
void *endfile;
struct exception { /* This is a RECORD structure. */
const char *_class;
int _number;
int xcpnoted;
int xcplimit;
int xcpaction;
int xcpmark;
unsigned char xcpmsg[64];
};
const char * const _awe_class_0_exception = "exception";
void *
exception ( _awe_loc loc,
int xcpnoted,
int xcplimit,
int xcpaction,
int xcpmark,
_awe_str xcpmsg )
{
struct exception *ref = (struct exception *)_awe_allocate_record(loc, sizeof(struct exception));
ref->_class = _awe_class_0_exception;
ref->_number = _awe_record_counter++;
ref->xcpnoted = xcpnoted;
ref->xcplimit = xcplimit;
ref->xcpaction = xcpaction;
ref->xcpmark = xcpmark;
_awe_str_cpy(ref->xcpmsg, 64, xcpmsg, 64);
return (void *)ref;
}
int *
xcpnoted (_awe_loc loc, void *ref)
{
_awe_ref_field_check(loc, ref, _awe_class_0_exception, "xcpnoted");
return &((struct exception *)ref)->xcpnoted;
}
int *
xcplimit (_awe_loc loc, void *ref)
{
_awe_ref_field_check(loc, ref, _awe_class_0_exception, "xcplimit");
return &((struct exception *)ref)->xcplimit;
}
int *
xcpaction (_awe_loc loc, void *ref)
{
_awe_ref_field_check(loc, ref, _awe_class_0_exception, "xcpaction");
return &((struct exception *)ref)->xcpaction;
}
int *
xcpmark (_awe_loc loc, void *ref)
{
_awe_ref_field_check(loc, ref, _awe_class_0_exception, "xcpmark");
return &((struct exception *)ref)->xcpmark;
}
_awe_str
xcpmsg (_awe_loc loc, void *ref)
{
_awe_ref_field_check(loc, ref, _awe_class_0_exception, "xcpmsg");
return ((struct exception *)ref)->xcpmsg;
}
void
_awe_process_exception (_awe_loc loc, void *condition)
{
if (condition) {
*xcpnoted(loc, condition) = 1;
(*xcplimit(loc, condition))--;
if (*xcplimit(loc, condition) < 0 || *xcpmark(loc, condition)) {
unsigned char msg[65];
_awe_str_cpy(msg, 64, xcpmsg(loc, condition), 64); msg[64] = '\0';
_awe_warning(loc, "%s", msg);
}
if (*xcplimit(loc, condition) < 0) {
_awe_finalize(loc);
exit(EXIT_FAILURE);
}
}
}
void
_awe_init_exceptions (_awe_loc loc)
{
_awe_record_counter = -8; /* Predeclared records will have negative record numbers. */
divzero = exception(loc, 0, 0, 0, 1, (_awe_str)"Floating-point division by zero. ");
intdivzero = exception(loc, 0, 0, 0, 1, (_awe_str)"Integer division by zero. ");
sqrterr = exception(loc, 0, 0, 0, 1, (_awe_str)"Negative argument for SQRT or LONGSQRT. ");
experr = exception(loc, 0, 0, 0, 1, (_awe_str)"Argument of EXP or LONGEXP out of domain. ");
lnlogerr = exception(loc, 0, 0, 0, 1, (_awe_str)"Argument of LN, LOG, LONGLN or LONGLOG out of domain. ");
sincoserr = exception(loc, 0, 0, 0, 1, (_awe_str)"Argument of SIN, COS, LONGSIN or LONGCOS out of domain. ");
endfile = exception(loc, 0, 0, 0, 1, (_awe_str)"Unexpected end of input. ");
_awe_record_counter = 0;
}
/* end */