-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
248 lines (227 loc) · 4.88 KB
/
utils.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package utils
import (
"bytes"
"encoding/binary"
"math"
"strings"
)
const (
stringLiterals = "_@QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"
sciLiterals = "0123456789.eE-+_"
decLiterals = "0123456789._"
hexLiterals = "0123456789ABCDEFabcdef_"
binLiterals = "01_"
controlLiterals = "()"
operatorLiterals = ";#?:=-+*/%^!&|~<>,"
)
type WordType int
// Word types
const (
// Not word.
W_NONE WordType = iota
// Number in scientific notation.
W_NUM_SCI
// Number in decimal representation.
W_NUM_DEC
// Number in hexadecimal representation.
W_NUM_HEX
// Number in binary representation.
W_NUM_BIN
// Some variable, constant or function.
W_UNIT
// Operator.
W_OP
// Flow control (brackets).
W_CTL
// Detected function call.
W_FUNC
// String.
W_STR
)
type Word struct {
Type WordType
Literal string
}
// Is two words equal.
func WordsEqual(a, b []Word) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v.Literal != b[i].Literal {
return false
}
}
return true
}
// This functions splits provided string into words and determines its basic types.
func ParsePrompt(str string) []Word {
words := make([]Word, 0)
wordType := W_NUM_DEC
wordDone := false
wordBegin := -1
for i, c := range str {
if wordBegin > -1 {
switch wordType {
case W_UNIT:
if !(strings.Contains(stringLiterals, string(c)) || strings.Contains(decLiterals, string(c))) {
wordDone = true
}
case W_NUM_DEC, W_NUM_HEX, W_NUM_BIN, W_NUM_SCI:
if (c == 'x' || c == 'b') && i-wordBegin == 1 && wordType == W_NUM_DEC {
if c == 'x' {
wordType = W_NUM_HEX
} else {
wordType = W_NUM_BIN
}
wordBegin += 2
} else if (c == 'e' || c == 'E') && wordType == W_NUM_DEC {
wordType = W_NUM_SCI
} else {
switch wordType {
case W_NUM_SCI:
if !strings.Contains(sciLiterals, string(c)) {
wordDone = true
}
case W_NUM_DEC:
if !strings.Contains(decLiterals, string(c)) {
wordDone = true
}
case W_NUM_HEX:
if !strings.Contains(hexLiterals, string(c)) {
wordDone = true
}
case W_NUM_BIN:
if !strings.Contains(binLiterals, string(c)) {
wordDone = true
}
}
}
case W_STR:
if c == '"' {
wordDone = true
words = append(words, Word{wordType, str[wordBegin:i]})
wordBegin = -1
continue
}
case W_CTL:
wordDone = true
case W_OP:
if !strings.Contains(operatorLiterals, string(c)) {
wordDone = true
}
}
if wordDone && wordType != W_NONE {
words = append(words, Word{wordType, str[wordBegin:i]})
wordBegin = -1
}
}
if wordBegin < 0 {
wordBegin = i
wordDone = false
if c == '"' {
wordType = W_STR
wordBegin++
} else if strings.Contains(stringLiterals, string(c)) {
wordType = W_UNIT
} else if strings.Contains(decLiterals, string(c)) {
wordType = W_NUM_DEC
} else if strings.Contains(controlLiterals, string(c)) {
wordType = W_CTL
} else if strings.Contains(operatorLiterals, string(c)) {
wordType = W_OP
} else {
wordBegin = -1
wordType = W_NONE
}
}
}
if wordBegin > -1 && wordType != W_NONE {
words = append(words, Word{wordType, str[wordBegin:]})
}
return words
}
type number interface {
int64 | uint64 | float64
}
// Try to convert any variable to number T (int64 | uint64 | float64).
//
// It doesn't convert slices, arrays and structs.
func ToNumber[T number](i interface{}) T {
switch v := i.(type) {
case bool:
if v {
return T(1)
} else {
return T(0)
}
case string:
arr := []byte(v)
var val uint64
for i := 0; i < int(math.Min(float64(len(arr)), 8)); i++ {
val |= (uint64(arr[i]) << (i * 8))
}
return T(val)
case byte:
return T(v)
case int:
return T(v)
case uint:
return T(v)
case int64:
return T(v)
case uint64:
return T(v)
case float32:
if float64(v)-math.Floor(float64(v)) > 0 {
b := make([]byte, 4, 8)
b = append(b, ToByteArray(v)...)
return FromByteArray[T](b)
}
return T(v)
case float64:
if v-math.Floor(v) > 0 {
b := ToByteArray(v)
return FromByteArray[T](b)
}
return T(v)
}
return T(0)
}
// Convert any variable to bool.
//
// In most cases it returns true if i > 0.
func ToBool(i interface{}) bool {
switch v := i.(type) {
case bool:
return v
case string:
return len(v) > 0
case byte:
return v > 0
case int:
return v > 0
case uint:
return v > 0
case int64:
return v > 0
case uint64:
return v > 0
case float64:
return v > 0
}
return false
}
// Helper function that converts byte slice to type T.
func FromByteArray[T any](b []byte) (s T) {
buf := bytes.NewReader(b)
binary.Read(buf, binary.LittleEndian, &s)
return
}
// Helper function that converts type T to byte slice.
func ToByteArray[T any](s T) (b []byte) {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, s)
b = buf.Bytes()
return
}