-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
313 lines (256 loc) · 6.9 KB
/
error.go
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package gmnlisp
import (
"context"
"errors"
"fmt"
)
type ProgramError struct {
err error
}
type ControlError struct {
err error
}
type DomainError struct {
Object Node
ExpectedClass Class
}
type ParseError struct {
str String
ExpectedClass Class
}
type StorageExhausted struct{}
type StreamError struct {
Stream Node
}
type EndOfStream struct {
Stream Node
}
var (
programErrorClass = registerNewAbstractClass[ProgramError]("<program-error>", errorClass)
domainErrorClass = registerNewAbstractClass[*DomainError]("<domain-error>", programErrorClass, errorClass)
controlErrorClass = registerNewAbstractClass[ControlError]("<control-error>", errorClass)
parseErrorClass = registerNewAbstractClass[*ParseError]("<parse-error>", errorClass)
storageExhaustedClass = registerNewAbstractClass[StorageExhausted]("<storage-exhausted>", errorClass)
streamErrorClass = registerNewAbstractClass[StreamError]("<stream-error>", errorClass)
endOfStreamClass = registerNewAbstractClass[EndOfStream]("<end-of-stream>", streamErrorClass, errorClass)
)
func (e ProgramError) ClassOf() Class {
return programErrorClass
}
func (e ProgramError) Equals(n Node, _ EqlMode) bool {
_n, ok := n.(ProgramError)
if !ok {
return false
}
return errors.Is(e.err, _n.err) || errors.Is(_n.err, e.err)
}
func (e ProgramError) String() string {
return e.err.Error()
}
func (e ProgramError) Error() string {
return e.err.Error()
}
func (e ProgramError) Unwrap() error {
return e.err
}
func (e ControlError) ClassOf() Class {
return controlErrorClass
}
func (e ControlError) Equals(n Node, _ EqlMode) bool {
_n, ok := n.(ControlError)
if !ok {
return false
}
return errors.Is(e.err, _n.err) || errors.Is(_n.err, e.err)
}
func (e ControlError) String() string {
return e.err.Error()
}
func (e ControlError) Error() string {
return e.err.Error()
}
func (e ControlError) Unwrap() error {
return e.err
}
func (e *DomainError) ClassOf() Class {
return domainErrorClass
}
func (e *DomainError) Equals(_other Node, mode EqlMode) bool {
other, ok := _other.(*DomainError)
if !ok {
return false
}
return e.Object.Equals(other.Object, mode) &&
e.ExpectedClass.Equals(other.ExpectedClass, mode)
}
func (e *DomainError) String() string {
return fmt.Sprintf("%#v: Expected %#v", e.Object.String(), e.ExpectedClass.Name().String())
}
func (e *DomainError) Error() string {
return e.String()
}
func funDomainErrorObject(ctx context.Context, w *World, arg Node) (Node, error) {
e, err := ExpectClass[*DomainError](ctx, w, arg)
if err != nil {
return nil, err
}
return e.Object, nil
}
func funDomainErrorExpectedClass(ctx context.Context, w *World, arg Node) (Node, error) {
e, err := ExpectClass[*DomainError](ctx, w, arg)
if err != nil {
return nil, err
}
return e.ExpectedClass, nil
}
type errorAndNode interface {
error
Node
}
type callHandlerLimitter struct{}
func callHandler[T Node](ctx context.Context, w *World, cont bool, condition errorAndNode) (T, error) {
var zero T
if w.handler != nil {
if v := ctx.Value(callHandlerLimitter{}); v != nil {
return zero, condition
}
ctx = context.WithValue(ctx, callHandlerLimitter{}, 1)
_, e := w.handler.Call(ctx, w, UnevalList(condition))
var ce *_ErrContinueCondition
if cont && errors.As(e, &ce) {
if v, ok := ce.Value.(T); ok {
return v, nil
}
}
}
return zero, condition
}
func ExpectInterface[T Node](ctx context.Context, w *World, v Node, class Class) (T, error) {
if _v, ok := v.(Uneval); ok {
v = _v.Node
}
value, ok := v.(T)
if ok {
return value, nil
}
condition := &DomainError{
Object: v,
ExpectedClass: class,
}
return callHandler[T](ctx, w, true, condition)
}
func ExpectClass[T Node](ctx context.Context, w *World, v Node) (T, error) {
var zero T
class := zero.ClassOf() // .ClassOf must be called even when value is nil
return ExpectInterface[T](ctx, w, v, class)
}
type _UndefinedEntity struct {
name Symbol
space Symbol
}
func (u *_UndefinedEntity) Error() string {
return fmt.Sprintf("undefined %s: %#v", u.space.String(), u.name.String())
}
func (u *_UndefinedEntity) String() string {
return u.Error()
}
func (u *_UndefinedEntity) Equals(other Node, mode EqlMode) bool {
o, ok := other.(*_UndefinedEntity)
if !ok {
return false
}
return u.space.Equals(o.space, mode) && u.name.Equals(o.name, mode)
}
var (
undefinedEntityClass = registerNewAbstractClass[*_UndefinedEntity]("<undefined-entity>", programErrorClass)
unboundVariableClass = registerNewAbstractClass[*_UndefinedEntity]("<unbound-variable>", undefinedEntityClass)
undefinedFunctionClass = registerNewAbstractClass[*_UndefinedEntity]("<undefined-function>", undefinedEntityClass)
)
func (u *_UndefinedEntity) ClassOf() Class {
if u == nil {
return undefinedEntityClass
}
if u.space.Equals(symVariable, STRICT) {
return unboundVariableClass
}
return undefinedFunctionClass
}
func funUndefinedEntityName(ctx context.Context, w *World, arg Node) (Node, error) {
entity, err := ExpectClass[*_UndefinedEntity](ctx, w, arg)
if err != nil {
return nil, err
}
return entity.name, nil
}
func funUndefinedEntityNamespace(ctx context.Context, w *World, arg Node) (Node, error) {
entity, err := ExpectClass[*_UndefinedEntity](ctx, w, arg)
if err != nil {
return nil, err
}
return entity.space, nil
}
func funParseErrorString(ctx context.Context, w *World, arg Node) (Node, error) {
entity, err := ExpectClass[*ParseError](ctx, w, arg)
if err != nil {
return nil, err
}
return String(entity.str), nil
}
func funParseErrorExpectedClass(ctx context.Context, w *World, arg Node) (Node, error) {
entity, err := ExpectClass[*ParseError](ctx, w, arg)
if err != nil {
return nil, err
}
return entity.ExpectedClass, nil
}
func (p *ParseError) ClassOf() Class {
return parseErrorClass
}
func (p *ParseError) String() string {
return fmt.Sprintf("ParseError: %s: %s", String(p.str), p.ExpectedClass.String())
}
func (p *ParseError) Equals(n Node, _ EqlMode) bool {
return false
}
func (p *ParseError) Error() string {
return p.String()
}
func (s StorageExhausted) ClassOf() Class {
return storageExhaustedClass
}
func (s StorageExhausted) Equals(n Node, _ EqlMode) bool {
_, ok := n.(StorageExhausted)
return ok
}
func (s StorageExhausted) String() string {
return "storage exhausted"
}
func (s StorageExhausted) Error() string {
return "storage exhausted"
}
func (s StreamError) ClassOf() Class {
return streamErrorClass
}
func (s StreamError) Equals(n Node, _ EqlMode) bool {
_, ok := n.(StreamError)
return ok
}
func (s StreamError) String() string {
return "stream error"
}
func (s StreamError) Error() string {
return "stream error"
}
func (e EndOfStream) ClassOf() Class {
return endOfStreamClass
}
func (e EndOfStream) Equals(n Node, _ EqlMode) bool {
_, ok := n.(EndOfStream)
return ok
}
func (e EndOfStream) String() string {
return "end of stream"
}
func (e EndOfStream) Error() string {
return "end of stream"
}