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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
package walk
import (
"strings"
"math"
"unicode/utf8"
)
// int or string
type PathSegment interface {}
type Path []PathSegment
func (path Path) ToWalkValues() []Value {
var values []Value
for _, segment := range path {
switch s := segment.(type) {
case int:
values = append(values, ValueNumber(s))
case string:
values = append(values, ValueString(s))
default:
panic("Invalid PathSegment")
}
}
return values
}
func PathFromWalkValues(values []Value) Path {
var segments []PathSegment
for _, value := range values {
switch v := value.(type) {
case ValueNumber:
segments = append(segments, int(math.Round(float64(v))))
case ValueString:
segments = append(segments, string(v))
default:
panic("Invalid value in path")
}
}
return segments
}
type WalkItem struct {
Value []Atom
Path []Atom
}
func ConcatData(first []Atom, second []Atom) []Atom {
res := make([]Atom, 0, len(first) + len(second))
res = append(res, first...)
res = append(res, second...)
return res
}
func Atomise(in []Value) (out []Atom) {
numAtoms := 0
for _, value := range in {
switch v := value.(type) {
case ValueTerminal, ValueNull, ValueBool, ValueNumber:
numAtoms++
case ValueString:
numAtoms += utf8.RuneCountInString(string(v)) + 2
default:
panic("Invalid WalkValue")
}
}
out = make([]Atom, 0, numAtoms)
for _, value := range in {
out = value.Atomise(out)
}
return out
}
type CompoundError int
const (
CompoundRuneOutsideString CompoundError = iota
CompoundUnknownAtom
CompoundMissingEnd
CompoundInvalidStringAtom
)
func (err CompoundError) Error() string {
switch err {
case CompoundRuneOutsideString:
return "Compound Error: Rune Outside String"
case CompoundUnknownAtom:
return "Compound Error: Unknown Atom"
case CompoundMissingEnd:
return "Compound Error: Missing End"
case CompoundInvalidStringAtom:
return "Compound Error: Invalid String Atom"
default:
panic("Invalid CompoundError")
}
}
type CompoundResult struct {
value Value
error error
}
func Compound(in []Atom) (out []Value, error error) {
numValues := 0
i := 0
inString := false
for _, atom := range in {
switch atom.Typ {
case AtomNull, AtomBool, AtomNumber, AtomTerminal:
if !inString {
numValues++
}
case AtomStringTerminal:
if inString {
numValues++
}
inString = !inString
}
}
i = 0
out = make([]Value, 0, numValues)
for {
if i >= len(in) {
break
}
atom := in[i]
i++
switch atom.Typ {
case AtomNull:
out = append(out, ValueNull{})
continue
case AtomBool:
out = append(out, ValueBool(atom.data != 0))
continue
case AtomNumber:
out = append(out, ValueNumber(math.Float64frombits(atom.data)))
continue
case AtomTerminal:
out = append(out, ValueTerminal(atom.data))
continue
case AtomStringRune:
return nil, CompoundRuneOutsideString
case AtomStringTerminal:
default:
return nil, CompoundUnknownAtom
}
// Handle string start
var builder strings.Builder
for {
if i >= len(in) {
return nil, CompoundMissingEnd
}
atom := in[i]
i++
if atom.Typ == AtomStringTerminal {
break
}
builder.WriteString(atom.String())
}
out = append(out, ValueString(builder.String()))
}
return out, nil
}
|