-
Notifications
You must be signed in to change notification settings - Fork 20
/
debug.go
75 lines (63 loc) · 1.39 KB
/
debug.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
package main
import (
"os"
)
func debugf(format string, v ...interface{}) {
if !debugMode {
return
}
var indents []byte
for i := 0; i < debugNest; i++ {
indents = append(indents, ' ')
indents = append(indents, ' ')
}
os.Stderr.Write(indents)
s2 := Sprintf(format, v...)
var b []byte = []byte(s2)
b = append(b, '\n')
os.Stderr.Write(b)
}
var debugNest int
// States "To Be Implemented"
func TBI(tok *Token, format string, v ...interface{}) {
format2 := "(To Be Implemented) " + format
errorft(tok, format2, v...)
}
// errorf with a position token
func errorft(tok *Token, format string, v ...interface{}) {
var tokString string
if tok != nil {
tokString = tok.String()
}
gs := format + "\n " + tokString
errorf(gs, v...)
}
func errorf(format string, v ...interface{}) {
s := Sprintf(format, v...)
b := []byte(s)
b = append(b, '\n')
os.Stderr.Write(b)
panic("")
}
func assert(cond bool, tok *Token, format string, v ...interface{}) {
if !cond {
s := Sprintf(string(format), v...)
var toks string
if tok != nil {
toks = tok.String()
}
msg := "assertion failed: " + s + toks
b := []byte(msg)
b = append(b, '\n')
os.Stderr.Write(b)
panic("")
}
}
func assertNotReached(tok *Token) {
msg := "assertNotReached " + string(tok.String())
os.Stderr.Write([]byte(msg))
panic("")
}
func assertNotNil(cond bool, tok *Token) {
assert(cond, tok, "should not be nil")
}