-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.go
96 lines (83 loc) · 1.75 KB
/
ast.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
package astjson
// NodeType represents an AST node type
//
//go:generate stringer -type=NodeType
type NodeType uint
const (
Number NodeType = iota
Null
String
Bool
Object
Array
)
// Value is the concrete AST representation
type Value struct {
NodeType
// AstValue stores the real value of an AST Value
// the literal type(Number, String, Bool and Null) stores them by value
// the Object and Array stores them by pointer
AstValue interface{}
}
//go:generate stringer -type=numberType
type NumberType uint
const (
floatNumber NumberType = iota
// unsignedInteger refers we could store the value inside an uint64
unsignedInteger
// integer refers we could store the value inside an int64
integer
)
type NumberAst struct {
Nt NumberType
f float64
u uint64
i int64
}
// GetInt64 returns the KvMap inside a NumberAst, it's possible to
// lose precise for float64 or overflow for uint64
func (n NumberAst) GetInt64() int64 {
switch n.Nt {
case integer:
return n.i
case unsignedInteger:
return int64(n.u)
case floatNumber:
// precise is acceptable because users need us to cast it.
return int64(n.f)
}
panic("")
}
func (n NumberAst) GetUint64() uint64 {
switch n.Nt {
case integer:
return uint64(n.i)
case unsignedInteger:
return n.u
case floatNumber:
// precise is acceptable because users need us to cast it.
return uint64(n.f)
}
panic("")
}
func (n NumberAst) GetFloat64() float64 {
switch n.Nt {
case integer:
return float64(n.i)
case unsignedInteger:
return float64(n.u)
case floatNumber:
// todo: check further whether this logic is correct
return n.f
}
panic("")
}
type NullAst struct{}
type BoolAst bool
type StringAst string
type ObjectAst struct {
KvMap map[string]Value
}
type ArrayAst struct {
Values []Value
}