-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathast_value.go
107 lines (89 loc) · 1.5 KB
/
ast_value.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
package spiker
import (
"sort"
"strconv"
)
// ValueList value list
type ValueList []interface{}
// ValueMap kv dict
type ValueMap map[string]interface{}
// NodeVariable variable node
type NodeVariable struct {
Ast
Value string
}
// Format .
func (nv NodeVariable) Format() string {
return nv.Value
}
// NodeString string node
type NodeString struct {
Ast
Value string
}
// Format .
func (str NodeString) Format() string {
return "\"" + str.Value + "\""
}
// NodeNumber number node
type NodeNumber struct {
Ast
Value float64
}
// Format .
func (num NodeNumber) Format() string {
return strconv.FormatFloat(num.Value, 'f', -1, 64)
}
// NodeBool bool node
type NodeBool struct {
Ast
Value bool
}
// Format .
func (num NodeBool) Format() string {
if num.Value {
return "true"
}
return "false"
}
// NodeList list node
type NodeList struct {
Ast
List []AstNode
}
// Format .
func (arr NodeList) Format() string {
f := "["
for idx, as := range arr.List {
if idx > 0 {
f += ", "
}
f += as.Format()
}
f += "]"
return f
}
// NodeMap map node
type NodeMap struct {
Ast
Map map[AstNode]AstNode
}
// Format .
func (nm NodeMap) Format() string {
sortedKeys := make([]string, 0)
sortMap := make(map[string]AstNode)
for kn, kv := range nm.Map {
sortedKeys = append(sortedKeys, kn.Format())
sortMap[kn.Format()] = kv
}
sort.Strings(sortedKeys)
f := "["
for idx, key := range sortedKeys {
if idx > 0 {
f += ", "
}
f += key + ": " + sortMap[key].Format()
}
f += "]"
return f
}