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
108
109
110
111
112
113
114
115
116
117
118
119
|
package walk
import (
"math"
"fmt"
)
type AtomType int64
const (
AtomNull AtomType = iota
AtomBool
AtomNumber
AtomTerminal
AtomStringTerminal
AtomStringRune
)
type AtomOLD struct {
Typ AtomType
data uint64
}
func NewAtomNull() AtomOLD {
return AtomOLD {
Typ: AtomNull,
data: 0,
}
}
func NewAtomBool(v bool) AtomOLD {
if v {
return AtomOLD {
Typ: AtomBool,
data: 1,
}
} else {
return AtomOLD {
Typ: AtomBool,
data: 0,
}
}
}
func (v AtomOLD) Bool() bool {
if v.Typ != AtomBool {
panic("Tried to use non-bool as bool")
}
return v.data == 1
}
func NewAtomNumber(v float64) AtomOLD {
return AtomOLD {
Typ: AtomNumber,
data: math.Float64bits(v),
}
}
func (v AtomOLD) Number() float64 {
if v.Typ != AtomNumber {
panic("Tried to use non-number as number")
}
return math.Float64frombits(v.data)
}
func NewAtomTerminal(v ValueTerminal) AtomOLD {
return AtomOLD {
Typ: AtomTerminal,
data: uint64(v),
}
}
func (v AtomOLD) Terminal() ValueTerminal {
if v.Typ != AtomTerminal {
panic("Tried to use non-terminal as terminal")
}
return ValueTerminal(v.data)
}
func NewAtomStringTerminal() AtomOLD {
return AtomOLD {
Typ: AtomStringTerminal,
data: 0,
}
}
func NewAtomStringRune(v rune) AtomOLD {
return AtomOLD {
Typ: AtomStringRune,
data: uint64(v),
}
}
func (v AtomOLD) StringRune() rune {
if v.Typ != AtomStringRune {
panic("Tried to use non-stringrune as stringrune")
}
return rune(v.data)
}
func (v AtomOLD) String() string {
switch v.Typ {
case AtomNull:
return "null"
case AtomBool:
if v.data == 0 {
return "false"
}
return "true"
case AtomNumber:
return fmt.Sprintf("%v", math.Float64frombits(v.data))
case AtomTerminal:
switch ValueTerminal(v.data) {
case MapBegin:
return "{"
case MapEnd:
return "}"
case ArrayBegin:
return "["
case ArrayEnd:
return "]"
default:
panic("Invalid terminal atom")
}
case AtomStringTerminal:
return "\""
case AtomStringRune:
return string(rune(v.data))
default:
panic("Invalid atom type")
}
}
|