-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.go
69 lines (61 loc) · 1.19 KB
/
compile.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
package json
import "strconv"
func (j Json) Compile() interface{} {
switch {
case j.Number != nil:
return j.Number.Compile()
case j.String != nil:
return j.String.Compile()
case j.Null != nil:
return j.Null.Compile()
case j.Bool != nil:
return j.Bool.Compile()
case j.Array != nil:
return j.Array.Compile()
case j.Dict != nil:
return j.Dict.Compile()
default:
panic("invalid json")
}
}
func (n Number) Compile() float64 {
x, err := strconv.ParseFloat(n.Value.Value(), 64)
if err != nil {
panic(err)
}
return x
}
func (s String) Compile() string {
cs, err := strconv.Unquote(s.Value.Value())
if err != nil {
panic(err)
}
return cs
}
func (n Null) Compile() interface{} {
return nil
}
func (b Bool) Compile() bool {
switch b.Value.Value() {
case "true":
return true
case "false":
return false
default:
panic("invalid boolean literal")
}
}
func (a Array) Compile() []interface{} {
var ca []interface{}
for _, item := range a.Items {
ca = append(ca, item.Compile())
}
return ca
}
func (d Dict) Compile() map[string]interface{} {
cd := map[string]interface{}{}
for _, item := range d.Items {
cd[item.Key.Compile()] = item.Value.Compile()
}
return cd
}