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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
package shell
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"os/signal"
"slices"
"strconv"
"strings"
"syscall"
"unicode"
"github.com/jpappel/atlas/pkg/query"
)
type Interpreter struct {
State State
Scanner *bufio.Scanner
Workers uint
}
type ITokType int
const (
ITOK_INVALID ITokType = iota
ITOK_VAR_NAME
// values
ITOK_VAL_INT
ITOK_VAL_STR
ITOK_VAL_TOKENS
ITOK_VAL_CLAUSE
// commands
ITOK_CMD_HELP
ITOK_CMD_CLEAR
ITOK_CMD_LET
ITOK_CMD_DEL
ITOK_CMD_PRINT
ITOK_CMD_LEN
ITOK_CMD_SLICE
ITOK_CMD_REMATCH
ITOK_CMD_REPATTERN
ITOK_CMD_OPTIMIZE
ITOK_CMD_TOKENIZE
ITOK_CMD_PARSE
)
type IToken struct {
Type ITokType
Text string
}
func NewInterpreter(initialState State, inputSource io.Reader, workers uint) *Interpreter {
return &Interpreter{
initialState,
bufio.NewScanner(inputSource),
workers,
}
}
func (interpreter *Interpreter) Reset() {
interpreter.State = make(State)
}
func (interpreter *Interpreter) Eval(tokens []IToken) (bool, error) {
if len(tokens) == 0 {
return false, nil
}
if slices.ContainsFunc(tokens, func(token IToken) bool {
return token.Type == ITOK_INVALID
}) {
b := strings.Builder{}
b.WriteString("Unexpected token(s) at ")
for i, t := range tokens {
if t.Type == ITOK_INVALID {
b.WriteString(fmt.Sprint(i, ", "))
}
}
return false, errors.New(b.String())
}
var variableName string
var carryValue Value
var ok bool
for i := len(tokens) - 1; i >= 0; i-- {
t := tokens[i]
switch t.Type {
case ITOK_CMD_HELP:
printHelp()
break
case ITOK_CMD_CLEAR:
fmt.Println("\033[H\033[J")
break
case ITOK_CMD_LET:
if variableName != "" {
interpreter.State[variableName] = carryValue
carryValue.Type = VAL_INVALID
}
break
case ITOK_CMD_DEL:
if len(tokens) == 1 {
fmt.Println("Deleting all variables")
interpreter.State = make(State)
} else {
// HACK: variable name is not evaluated correctly so just look at the next token
delete(interpreter.State, tokens[i+1].Text)
}
carryValue.Type = VAL_INVALID
break
case ITOK_CMD_PRINT:
if len(tokens) == 1 {
fmt.Println("Variables:")
fmt.Println(interpreter.State)
} else {
carryValue, ok = interpreter.State[tokens[1].Text]
if !ok {
return false, fmt.Errorf("No variable %s", tokens[1].Text)
}
}
case ITOK_CMD_REMATCH:
if carryValue.Type != VAL_STRING {
return false, fmt.Errorf("Unable to march against argument of type: %s", carryValue.Type)
}
body, ok := carryValue.Val.(string)
if !ok {
return true, errors.New("Type corruption during rematch, expected string")
}
b := strings.Builder{}
matchGroupNames := query.LexRegex.SubexpNames()
for _, match := range query.LexRegex.FindAllStringSubmatch(body, -1) {
for i, part := range match {
b.WriteString(matchGroupNames[i])
fmt.Fprintf(&b, "[%d]", len(part))
b.WriteByte(':')
b.WriteString(part)
b.WriteByte('\n')
}
b.WriteByte('\n')
}
carryValue.Val = b.String()
case ITOK_CMD_REPATTERN:
fmt.Println(query.LexRegexPattern)
break
case ITOK_CMD_TOKENIZE:
if carryValue.Type != VAL_STRING {
return false, fmt.Errorf("Unable to tokenize argument of type: %s", carryValue.Type)
}
rawQuery, ok := carryValue.Val.(string)
if !ok {
return true, errors.New("Type corruption during tokenize, expected string")
}
carryValue.Type = VAL_TOKENS
carryValue.Val = query.Lex(rawQuery)
case ITOK_CMD_PARSE:
if carryValue.Type != VAL_TOKENS {
return false, fmt.Errorf("Unable to parse argument of type: %s", carryValue.Type)
}
queryTokens, ok := carryValue.Val.([]query.Token)
if !ok {
return true, errors.New("Type corruption during parse, expected []query.Tokens")
}
clause, err := query.Parse(queryTokens)
if err != nil {
return false, err
}
carryValue.Type = VAL_CLAUSE
carryValue.Val = clause
case ITOK_CMD_OPTIMIZE:
if carryValue.Type != VAL_CLAUSE {
return false, fmt.Errorf("Unable to flatten argument of type: %s", carryValue)
}
clause, ok := carryValue.Val.(*query.Clause)
if !ok {
return true, errors.New("Type corruption during flatten, expected *query.Clause")
}
o := query.NewOptimizer(clause, interpreter.Workers)
switch t.Text {
case "simplify":
o.Simplify()
case "tighten":
o.Tighten()
case "flatten":
o.Flatten()
case "sortStatements":
o.SortStatements()
case "tidy":
o.Tidy()
case "contradictions":
o.Contradictions()
case "compact":
o.Compact()
case "strictEq":
o.StrictEquality()
default:
return false, fmt.Errorf("Unrecognized optimization: %s", t.Text)
}
carryValue.Type = VAL_CLAUSE
carryValue.Val = clause
case ITOK_VAR_NAME:
// NOTE: very brittle, only allows expansion of a single variable
if i == len(tokens)-1 {
carryValue, ok = interpreter.State[t.Text]
if !ok {
return false, fmt.Errorf("No variable: %s", t.Text)
}
} else {
variableName = t.Text
}
case ITOK_VAL_STR:
carryValue.Type = VAL_STRING
carryValue.Val = t.Text
case ITOK_VAL_INT:
val, err := strconv.Atoi(t.Text)
if err != nil {
return false, fmt.Errorf("Unable to parse as integer: %v", err)
}
carryValue.Type = VAL_INT
carryValue.Val = val
case ITOK_CMD_LEN:
var length int
switch cType := carryValue.Type; cType {
case VAL_STRING:
s, ok := carryValue.Val.(string)
if !ok {
return true, fmt.Errorf("Type corruption during len, expected string")
}
length = len(s)
case VAL_TOKENS:
toks, ok := carryValue.Val.([]query.Token)
if !ok {
return true, fmt.Errorf("Type corruption during len, expected []query.Token")
}
length = len(toks)
default:
return false, fmt.Errorf("Unable to get length of argument with type: %s", carryValue.Type)
}
carryValue.Type = VAL_INT
carryValue.Val = length
case ITOK_CMD_SLICE:
// TODO: get start and end of range
switch cType := carryValue.Type; cType {
case VAL_STRING:
case VAL_TOKENS:
default:
return false, fmt.Errorf("Cannot slice argument: %v", cType)
}
fmt.Println("not implemented yet ;)")
break
}
}
if carryValue.Type != VAL_INVALID {
fmt.Println(carryValue)
interpreter.State["_"] = carryValue
}
return false, nil
}
func (interpreter Interpreter) Tokenize(line string) []IToken {
var prevType ITokType
tokens := make([]IToken, 0, 3)
for word := range strings.SplitSeq(line, " ") {
trimmedWord := strings.TrimSpace(word)
if trimmedWord == "" {
continue
}
if len(tokens) != 0 {
prevType = tokens[len(tokens)-1].Type
}
if trimmedWord == "help" {
tokens = append(tokens, IToken{Type: ITOK_CMD_HELP})
} else if trimmedWord == "clear" {
tokens = append(tokens, IToken{Type: ITOK_CMD_CLEAR})
} else if trimmedWord == "let" {
tokens = append(tokens, IToken{Type: ITOK_CMD_LET})
} else if trimmedWord == "del" {
tokens = append(tokens, IToken{Type: ITOK_CMD_DEL})
} else if trimmedWord == "print" {
tokens = append(tokens, IToken{Type: ITOK_CMD_PRINT})
} else if trimmedWord == "len" {
tokens = append(tokens, IToken{Type: ITOK_CMD_LEN})
} else if trimmedWord == "slice" {
tokens = append(tokens, IToken{Type: ITOK_CMD_SLICE})
} else if trimmedWord == "rematch" {
tokens = append(tokens, IToken{Type: ITOK_CMD_REMATCH})
} else if trimmedWord == "repattern" {
tokens = append(tokens, IToken{Type: ITOK_CMD_REPATTERN})
} else if trimmedWord == "tokenize" {
tokens = append(tokens, IToken{Type: ITOK_CMD_TOKENIZE})
} else if trimmedWord == "parse" {
tokens = append(tokens, IToken{Type: ITOK_CMD_PARSE})
} else if l := len("opt_"); len(trimmedWord) > l && trimmedWord[:l] == "opt_" {
tokens = append(tokens, IToken{ITOK_CMD_OPTIMIZE, trimmedWord[l:]})
} else if prevType == ITOK_CMD_LET {
tokens = append(tokens, IToken{ITOK_VAR_NAME, trimmedWord})
} else if prevType == ITOK_CMD_DEL {
tokens = append(tokens, IToken{ITOK_VAR_NAME, trimmedWord})
} else if prevType == ITOK_CMD_PRINT {
tokens = append(tokens, IToken{ITOK_VAR_NAME, trimmedWord})
} else if prevType == ITOK_CMD_LEN || prevType == ITOK_CMD_SLICE {
if trimmedWord[0] == '`' {
_, strLiteral, _ := strings.Cut(word, "`")
tokens = append(tokens, IToken{ITOK_VAL_STR, strLiteral})
} else {
tokens = append(tokens, IToken{ITOK_VAR_NAME, trimmedWord})
}
} else if prevType == ITOK_CMD_REMATCH || prevType == ITOK_CMD_TOKENIZE {
if trimmedWord[0] == '`' {
_, strLiteral, _ := strings.Cut(word, "`")
tokens = append(tokens, IToken{ITOK_VAL_STR, strLiteral})
} else {
tokens = append(tokens, IToken{ITOK_VAR_NAME, trimmedWord})
}
} else if prevType == ITOK_CMD_PARSE {
tokens = append(tokens, IToken{ITOK_VAR_NAME, trimmedWord})
} else if prevType == ITOK_CMD_OPTIMIZE {
tokens = append(tokens, IToken{ITOK_VAR_NAME, trimmedWord})
} else if prevType == ITOK_VAR_NAME && trimmedWord[0] == '`' {
_, strLiteral, _ := strings.Cut(word, "`")
tokens = append(tokens, IToken{ITOK_VAL_STR, strLiteral})
} else if prevType == ITOK_VAR_NAME && unicode.IsDigit(rune(trimmedWord[0])) {
tokens = append(tokens, IToken{ITOK_VAL_INT, trimmedWord})
} else if prevType == ITOK_VAL_STR {
tokens[len(tokens)-1].Text += " " + word
} else {
tokens = append(tokens, IToken{ITOK_INVALID, trimmedWord})
}
}
return tokens
}
func (interpreter Interpreter) Run() error {
signalCh := make(chan os.Signal, 1)
exitCh := make(chan error, 1)
lineCh := make(chan string)
defer close(signalCh)
defer close(lineCh)
defer close(exitCh)
signal.Notify(signalCh, syscall.SIGINT)
go func(output chan<- string, exitCh chan<- error) {
for {
if interpreter.Scanner.Scan() {
output <- interpreter.Scanner.Text()
} else if err := interpreter.Scanner.Err(); err != nil {
exitCh <- err
return
} else {
exitCh <- io.EOF
return
}
}
}(lineCh, exitCh)
for {
fmt.Print("atlasi> ")
select {
case <-signalCh:
fmt.Println("Recieved Ctrl-C, exitting")
return nil
case err := <-exitCh:
return err
case line := <-lineCh:
tokens := interpreter.Tokenize(line)
fatal, err := interpreter.Eval(tokens)
if fatal {
return err
} else if err != nil {
fmt.Println(err)
}
}
}
}
func printHelp() {
fmt.Println("Shitty debug shell for atlas")
fmt.Println("help - print this help")
fmt.Println("clear - clear the screen")
fmt.Println("let name (string|tokens|clause) - save value to a variable")
fmt.Println("del [name] - delete a variable or all variables")
fmt.Println("print [name] - print a variable or all variables")
fmt.Println("slice (string|tokens|name) start stop - slice a string or tokens from start to stop")
fmt.Println("len (string|tokens|name) - length of a string or token slice")
fmt.Println("rematch (string|name) - match against regex for querylang spec")
fmt.Println("repattern - print regex for querylang")
fmt.Println("tokenize (string|name) - tokenize a string")
fmt.Println(" ex. tokenize `author:me")
fmt.Println("parse (tokens|name) - parse tokens into a clause")
fmt.Println("opt_<subcommand> (clause|name) - optimize clause tree")
fmt.Println(" sortStatements - sort statements")
fmt.Println(" flatten - flatten clauses")
fmt.Println(" compact - compact equivalent statements")
fmt.Println(" tidy - remove zero statements and `AND` clauses containing any")
fmt.Println(" contradictions - zero contradicting statements and clauses")
fmt.Println(" strictEq - zero fuzzy/range statements when an eq is present")
fmt.Println(" tighten - zero redundant fuzzy/range statements when another mathes the same values")
fmt.Println("\nBare commands which return a value assign to an implicit variable _")
}
|