forked from chrislusf/gelo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror.go
232 lines (190 loc) · 4.77 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
package gelo
import (
"bytes"
"fmt"
)
//hopefully one day we also have name, lineno, etc
type _error struct {
from uint32
msg string
}
//we do not let _errSystem become a gelo.Error but we do want to piggyback
//on the formatting. This is an unexported type because this kind of error is
//used for serious bugs that if allowed to continue would only wreak
//unimaginable havoc, so we want our SOS to escape even if we have to blow up
//the host application to do so. Should be caught by anything looking for
//os.Error and make it to a log if not the console.
type _errSystem struct {
_error
}
//This error type isn't meant to be caught by this program or the host program
//it signals a static misuse of Gelo that needs to be corrected. Such as trying
//to run a VM without a program or use a VM that has been killed
type _hostProgrammerError struct {
_error
}
type ErrSyntax struct {
_error
}
type ErrRuntime struct {
_error
}
//Predefined kinds of errors
func programmerError(vm *VM, s ...interface{}) {
panic(_hostProgrammerError{_make_error(vm, s)})
}
//note a system error is not a word and should only be called if the impossible
//to recover from occurs
func systemError(vm *VM, s ...interface{}) {
x := make([]interface{}, len(s)+1)
copy(x, s)
x[len(x)-1] = `
If you are reading this, you have discovered a bug in Gelo, a and not the
program that you are using.
Please see if this has been reported at:
http://github.com/Esgorhannoth/gelo/issues/list
and if not report the error there, so that we may fix it, before notifying the
owners of the application that you are using of the error.
Thank you for your time and the Gelo team deeply apologizes for the inconvience.
`
panic(_errSystem{_make_errorM(vm, x...)})
}
func SyntaxError(s ...interface{}) {
panic(&ErrSyntax{_make_error(nil, s)})
}
func VariableUndefined(vm *VM, name interface{}) {
panic(&ErrRuntime{_make_errorM(vm, "Undefined variable:", name)})
}
func RuntimeError(vm *VM, s ...interface{}) {
panic(&ErrRuntime{_make_error(vm, s)})
}
func TypeMismatch(vm *VM, exp, got interface{}) {
panic(&ErrRuntime{
_make_errorM(vm, "Type mismatch. Expected:", exp, "Got:", got),
})
}
//TODO get name from VM when it saves it
func ArgumentError(vm *VM, name, spec, args interface{}) {
if args == nil {
args = "no arguments"
}
panic(&ErrRuntime{
_make_errorM(vm, "Illegal arguments.", name, "expected:", spec, "Got:",
args),
})
}
func killed(vm *VM) Error {
return &ErrRuntime{_make_error(vm, []interface{}{"VM killed"})}
}
//common methods on error
func (e _error) From() uint32 {
return e.from
}
func (e _error) Error() string {
return e.msg
}
func (e _error) Message() string {
return e.msg
}
//syntax errors
func (self *ErrSyntax) _tag() {}
func (self *ErrSyntax) Ser() Symbol {
return Convert("Syntax error: " + self.Error()).(Symbol)
}
func (self *ErrSyntax) Equals(w Word) bool {
e, ok := w.(*ErrSyntax)
if !ok {
return false
}
return bytes.Equal([]byte(self.msg), []byte(e.msg))
}
func (self *ErrSyntax) Copy() Word {
return self
}
func (self *ErrSyntax) DeepCopy() Word {
return self
}
func (self *ErrSyntax) Type() Symbol {
return interns("*SYNTAX-ERROR*")
}
//Runtime Errors
func (self *ErrRuntime) _tag() {}
func (self *ErrRuntime) Ser() Symbol {
return StrToSym("Runtime error: " + self.Error())
}
func (self *ErrRuntime) Equals(w Word) bool {
e, ok := w.(*ErrRuntime)
if !ok {
return false
}
return bytes.Equal([]byte(self.msg), []byte(e.msg))
}
func (self *ErrRuntime) Copy() Word {
return self
}
func (self *ErrRuntime) DeepCopy() Word {
return self
}
func (self *ErrRuntime) Type() Symbol {
return interns("*RUNTIME-ERROR*")
}
//Implementation details
func _make_errorM(vm *VM, s ...interface{}) _error {
return _make_error(vm, s)
}
func _make_error(vm *VM, s []interface{}) _error {
var id uint32
if vm != nil {
id = vm.ProcID()
}
return _error{id, _format(s)}
}
func _format(all ...interface{}) string {
return _format_slice(all)
}
func _format_slice(all []interface{}) string {
buf := newBuf(0)
buf.Write(_format1(all[0]))
for _, v := range all[1:] {
buf.WriteString(" ")
buf.Write(_format1(v))
}
return buf.String()
}
func _format1(item interface{}) (ret []byte) {
switch t := item.(type) {
case nil:
ret = []byte("NIL")
case string:
if len(t) == 0 {
ret = []byte("\"\"")
} else {
ret = []byte(t)
}
case []byte:
if len(t) == 0 {
ret = []byte("\"\"")
} else {
ret = t
}
case *List:
buf := newBuf(0)
for ; t != nil; t = t.Next {
buf.Write(_format1(t.Value))
buf.WriteString(" ")
}
ret = buf.Bytes()
case Symbol:
ret = t.Bytes()
if len(ret) == 0 {
ret = []byte("<<the null string>>")
}
case Word:
ret = t.Ser().Bytes()
case []interface{}:
ret = []byte(_format_slice(t))
default:
ret = []byte(fmt.Sprint(t))
}
return
}