-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.go
71 lines (67 loc) · 1.3 KB
/
printer.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
package main
import (
"fmt"
"io"
"strconv"
)
type Writer interface {
io.StringWriter
io.ByteWriter
}
func exprWriteTo(w Writer, expr Expr, srcLike bool) {
print_list := func(lst []Expr, opening byte, closing byte) {
w.WriteByte(opening)
for i, it := range lst {
if i > 0 {
w.WriteByte(' ')
}
exprWriteTo(w, it, srcLike)
}
w.WriteByte(closing)
}
switch it := expr.(type) {
case ExprList:
print_list(it, '(', ')')
case ExprVec:
print_list(it, '[', ']')
case ExprHashMap:
w.WriteByte('{')
for k, v := range it {
w.WriteByte(' ')
exprWriteTo(w, exprStrOrKeyword(k), srcLike)
w.WriteByte(' ')
exprWriteTo(w, v, srcLike)
}
w.WriteString(" }")
case ExprIdent:
w.WriteString(string(it))
case ExprKeyword:
w.WriteString(string(it))
case ExprStr:
if srcLike {
w.WriteString(strconv.Quote(string(it)))
} else {
w.WriteString(string(it))
}
case ExprNum:
w.WriteString(strconv.Itoa(int(it)))
case ExprErr:
if srcLike {
w.WriteString("(error ")
w.WriteString(it.Error())
w.WriteByte(')')
} else {
w.WriteString(it.Error())
}
case *ExprAtom:
if srcLike {
w.WriteString("(atomFrom ")
exprWriteTo(w, it.Ref, true)
w.WriteByte(')')
} else {
exprWriteTo(w, it.Ref, false)
}
default:
w.WriteString(fmt.Sprintf("%#v", it))
}
}