-
Notifications
You must be signed in to change notification settings - Fork 0
/
sep.go
720 lines (658 loc) · 19.1 KB
/
sep.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
package sep
//import "fmt"
import "strings"
import "strconv"
import "unicode"
import "errors"
type Datastore interface {
VarExists(name string) bool
SetVar(name, value string) error
GetVar(name string) (string, bool)
DeleteVar(name string) error
}
type ArbitraryOptions struct {
Comments bool
Multiline bool
//EnableStoreVarRead bool
//EnableStoreVarWrite bool
}
type ArbitraryBlock struct {
Name string
Contents string
Type int // 0: Unknown, 1: int, 2: string, 3: list, 4: map, 5: variable, 6: function, 7: operator, 8: literal, 9: comment
Extra interface{}
}
func HandleArbitraryCommands(command string, ds Datastore, extra_data ...interface{}) (out string, lastIndex int, err error) {
return parseArbitraryBlock(command, ds, ArbitraryOptions{Comments:true,Multiline:true}, 0, extra_data...)
}
func parseArbitraryBlock(command string, ds Datastore, options ArbitraryOptions, n int, extra_data ...interface{}) (out string, lastIndex int, err error) {
if n > 5 {
return "", -1, errors.New("too many nested calls x.x")
}
lastIndex = -1
var currentBlock ArbitraryBlock
var blocks []ArbitraryBlock
var ntype, brace_count int
var last_if int8 // Ternary value. -1, 0, 1. Needed to tell an else node whether it should eat it's block or not
CharLoop:
for i:=0;i < len(command);i++ {
char := command[i]
switch(ntype) {
case 0: // Unknown node
// Sniff the type of the next node
switch {
case '0' <= char && char <= '9':
currentBlock = ArbitraryBlock{Contents:string(char),Type:1}
ntype = 1
case ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z'):
currentBlock = ArbitraryBlock{Contents:string(char),Type:8}
ntype = 8
case char == '"':
currentBlock = ArbitraryBlock{Type:2}
ntype = 2
case char == '[':
currentBlock = ArbitraryBlock{Contents:string(char),Type:2}
ntype = 3
case char == '{':
currentBlock = ArbitraryBlock{Contents:string(char),Type:2}
ntype = 4
case char == '*':
currentBlock = ArbitraryBlock{Type:5}
ntype = 5
case char == '+' || char == '-' || char == '=' || char == '!' || char == '&' || char == '|':
currentBlock = ArbitraryBlock{Contents:string(char),Type:7}
ntype = 7
case char == '#' || char == '/':
if options.Comments {
if !options.Multiline {
break CharLoop
}
currentBlock = ArbitraryBlock{Contents:string(char),Type:9}
ntype = 9
}
case !unicode.IsSpace(rune(char)) && char != '`' && char != ':':
return "", i, errors.New("Illegal character '" + string(char) + "' in arbitrary expression")
}
case 1: // Integer
if !('0' <= char && char <= '9') {
blocks = append(blocks, currentBlock)
ntype = 0
} else {
currentBlock.Contents += string(char)
}
case 2: // String
if char == '"' {
blocks = append(blocks, currentBlock)
ntype = 0
} else {
currentBlock.Contents += string(char)
}
case 3: // List
currentBlock.Contents += string(char)
if char == ']' {
blocks = append(blocks, currentBlock)
ntype = 0
}
case 4: // Map
currentBlock.Contents += string(char)
if char == '}' {
blocks = append(blocks, currentBlock)
ntype = 0
}
case 5: // Variable
if char == ' ' {
currentBlock.Name = currentBlock.Contents
currentBlock.Contents, err = ResolveVariable(currentBlock.Name,ds)
if err != nil {
return "", i, err
}
blocks = append(blocks, currentBlock)
ntype = 0
} else {
currentBlock.Contents += string(char)
}
case 6: // Functions
if char == '(' {
brace_count++
}
if char == ')' {
if brace_count == 0 {
// Is this a control structure...?
if currentBlock.Name == "if" {
res, _, err := parseArbitraryBlock(currentBlock.Contents, ds, options, n + 1, extra_data...)
if err != nil {
return "", i, err
}
norm, ok := NormalizeBool(res)
if !ok {
return "", i, errors.New("if statements only accept boolean values")
}
if norm == "false" {
if len(command) > (i+1) {
// Eat everything upto the next line
if command[i+1] == ':' {
for ; i < len(command);i++ {
if command[i] == 10 {
break
}
}
} else { // Eat an entire block
// Jump to the next block
for ; (len(command) > i) && command[i] != '{' ;i++ {}
if len(command) > (i+1) {
i++
i = skipCurrentBlock(command,i)
}
}
}
last_if = -1
} else {
if len(command) > (i+1) {
if command[i+1] != ':' {
// Jump to the next block
for ; (len(command) > i) && command[i] != '{' ;i++ {}
if len(command) > (i+1) {
i++
var startIndex int = i
i = skipCurrentBlock(command,i)
res, _, err := parseArbitraryBlock(command[startIndex:i], ds, options, n + 1, extra_data...)
if err != nil {
return "", i, err
}
currentBlock.Contents = res
currentBlock.Type = 2
blocks = append(blocks, currentBlock)
}
}
}
last_if = 1
}
ntype = 0
continue
} else if currentBlock.Name == "switch" {
switch_text, _, err := parseArbitraryBlock(currentBlock.Contents, ds, options, n + 1, extra_data...)
if err != nil {
return "", i, err
}
var brace_count int = -1
var inner_text, default_text string
for i++; i < len(command);i++ {
char = command[i]
if char == '{' {
brace_count++
} else if char == '}' {
if brace_count == 0 {
i = skipCurrentBlock(command,i)
break
}
brace_count--
} else if char == ':' && inner_text != "" {
inner_text = strings.TrimSpace(inner_text)
if len(command) > (i+1) {
i++
}
startIndex := i
if inner_text != "default" {
res, _, err := parseArbitraryBlock(inner_text, ds, options, n + 1, extra_data...)
if err != nil {
return "", i, err
}
i = skipUntilChar(command,i,',')
inner_text = command[startIndex:i]
if res == switch_text {
i = skipCurrentBlock(command,i)
break
}
} else {
i = skipUntilChar(command,i,',')
default_text = command[startIndex:i]
}
inner_text = ""
} else if char != ':' {
inner_text += string(char)
}
}
if brace_count < 0 {
return "", i, errors.New("You missed an opening brace for or within a switch block")
}
if inner_text == "" && default_text != "" {
inner_text = default_text
}
if inner_text == "" {
ntype = 0
continue
}
res, _, err := parseArbitraryBlock(inner_text, ds, options, n + 1, extra_data...)
if err != nil {
return "", i, err
}
currentBlock.Contents = res
currentBlock.Type = 2
blocks = append(blocks, currentBlock)
ntype = 0
continue
}
res, err := ResolveArbitraryFunction(currentBlock.Name,currentBlock.Contents,ds,n,extra_data...)
if err != nil {
return "", i, err
}
currentBlock.Contents = res
currentBlock.Type = 2
blocks = append(blocks, currentBlock)
ntype = 0
} else {
brace_count--
}
}
currentBlock.Contents += string(char)
case 7: // Operators
if char != '+' && char != '-' && char != '=' && char != '/' && char != '!' && char != '&' && char != '|' {
blocks = append(blocks, currentBlock)
ntype = 0
} else {
currentBlock.Contents += string(char)
}
case 8: // Literals
if !('a' <= char && char <= 'z') && !('A' <= char && char <= 'Z') && char != '(' {
currentBlock.Contents = strings.ToLower(currentBlock.Contents)
if currentBlock.Contents == "true" || currentBlock.Contents == "false" {
currentBlock.Type = 2
}
if currentBlock.Contents == "as" {
currentBlock.Type = 7
}
if currentBlock.Contents == "else" {
if last_if == 0 {
return "", i, errors.New("There's no if statement to match this else to!")
}
if len(command) > (i+1) {
if last_if == 1 {
// Eat everything upto the next line
if command[i] == ':' {
i = skipCurrentLine(command,i)
} else { // Eat an entire block
// Jump to the next block
for ; (len(command) > i) && command[i] != '{' ;i++ {}
if len(command) > (i+1) {
i++
i = skipCurrentBlock(command,i)
}
}
} else if command[i] != ':' {
// Jump to the next block
for ; (len(command) > i) && command[i] != '{' ;i++ {}
if len(command) > (i+1) {
i++
var startIndex int = i
i = skipCurrentBlock(command,i)
res, _, err := parseArbitraryBlock(command[startIndex:i], ds, options, n + 1, extra_data...)
if err != nil {
return "", i, err
}
currentBlock.Contents = res
currentBlock.Type = 2
blocks = append(blocks, currentBlock)
}
} else {
var startIndex int = i
i = skipCurrentLine(command,i)
res, _, err := parseArbitraryBlock(command[startIndex:i], ds, options, n + 1, extra_data...)
if err != nil {
return "", i, err
}
currentBlock.Contents = res
currentBlock.Type = 2
blocks = append(blocks, currentBlock)
}
}
ntype = 0
last_if = 0
continue
}
blocks = append(blocks, currentBlock)
ntype = 0
}
if char == '(' {
currentBlock.Name = currentBlock.Contents
currentBlock.Contents = ""
currentBlock.Type = 6
ntype = 6
} else {
currentBlock.Contents += string(char)
}
case 9: // Comments
switch(currentBlock.Contents) {
case "/":
if char == '/' {
currentBlock.Contents = "//"
} else if char == '*' {
currentBlock.Contents = "/*"
} else {
i--
currentBlock.Type = 7
ntype = 7
}
case "#","//":
if char == 10 { // Newline character
ntype = 0
}
case "/*":
if char == '*' && ((i + 1) < len(command)) && command[i + 1] == '/' {
ntype = 0
i++
}
}
}
}
if ntype != 0 {
if ntype == 6 {
return "", lastIndex, errors.New("there's an unclosed function call x.x")
}
if ntype == 5 {
var err error
currentBlock.Name = currentBlock.Contents
currentBlock.Contents, err = ResolveVariable(currentBlock.Name,ds)
if err != nil {
return "", lastIndex, err
}
}
if ntype == 8 {
currentBlock.Contents = strings.ToLower(currentBlock.Contents)
if currentBlock.Contents == "true" || currentBlock.Contents == "false" {
currentBlock.Type = 2
}
if currentBlock.Contents == "as" {
currentBlock.Type = 7
}
}
if ntype != 9 {
blocks = append(blocks, currentBlock)
}
}
var outbuf_cursor int // = 0
var outbuf map[int]string = make(map[int]string)
var boolInvert bool
blockCount := len(blocks)
for index := 0;index < blockCount;index++ {
block := blocks[index]
if block.Type == 1 || block.Type == 2 || block.Type == 5 {
if index > 0 {
if boolInvert {
block.Contents = strings.ToLower(block.Contents)
if block.Contents == "true" || block.Contents == "1" || block.Contents == "yes" {
block.Contents = "false"
} else if block.Contents == "false" || block.Contents == "0" || block.Contents == "no" {
block.Contents = "true"
}
}
prevtype := blocks[index - 1].Type
if prevtype == 2 || prevtype == 1 { // Append to the previous string or int
outbuf[outbuf_cursor] = outbuf[outbuf_cursor] + block.Contents
} else {
outbuf_cursor++
outbuf[outbuf_cursor] = block.Contents
}
} else {
outbuf[0] = block.Contents
}
} else if block.Type == 7 {
if (blockCount - 1) <= index {
return "", lastIndex, errors.New("Missing a right operand in the arbitrary expression")
}
if block.Contents == "!" {
continue
}
if index == 0 {
return "", lastIndex, errors.New("Missing a left operand in the arbitrary expression")
}
prevtype := blocks[index - 1].Type
if prevtype == 7 {
return "", lastIndex, errors.New("You cannot have an operator next to another operator in an arbitrary expression")
}
switch(block.Contents) {
case "+": return "", lastIndex, errors.New("+ not implemented")
case "-": return "", lastIndex, errors.New("- not implemented")
case "=": return "", lastIndex, errors.New("= not implemented")
case "as": return "", lastIndex, errors.New("as not implemented")
case "++": return "", lastIndex, errors.New("++ not implemented")
case "--": return "", lastIndex, errors.New("-- not implemented")
case "+=": return "", lastIndex, errors.New("+= not implemented")
case "-=": return "", lastIndex, errors.New("-= not implemented")
case "/": return "", lastIndex, errors.New("/ not implemented")
case "==": return "", lastIndex, errors.New("== not implemented")
case "&&":
previtem := blocks[index - 1]
nextitem := blocks[index + 1]
previtem_s, success := NormalizeBool(previtem.Contents)
if !success {
return "", lastIndex, errors.New("cannot coerce to bool")
}
nextitem_s, success := NormalizeBool(nextitem.Contents)
if !success {
return "", lastIndex, errors.New("cannot coerce to bool")
}
if previtem_s == "true" && nextitem_s == "true" {
outbuf[outbuf_cursor] = "true"
} else {
outbuf[outbuf_cursor] = "false"
}
index++
case "||":
previtem := blocks[index - 1]
nextitem := blocks[index + 1]
previtem_s, success := NormalizeBool(previtem.Contents)
if !success {
return "", lastIndex, errors.New("cannot coerce string to bool")
}
nextitem_s, success := NormalizeBool(nextitem.Contents)
if !success {
return "", lastIndex, errors.New("cannot coerce string to bool")
}
if previtem_s == "true" || nextitem_s == "true" {
outbuf[outbuf_cursor] = "true"
} else {
outbuf[outbuf_cursor] = "false"
}
index++
default:
return "", lastIndex, errors.New("Invalid operator")
}
} else {
//fmt.Println(ntype)
//fmt.Println(block)
return "", lastIndex, errors.New("Unable to reduce to string")
}
}
for _, item := range outbuf {
out += item
}
return out, lastIndex, nil
}
func ResolveVariable(data string, ds Datastore) (result string, err error) {
if len(data) < 3 {
return "", errors.New("variable name too short x.x")
}
if data[0] == '*' {
data = data[1:]
}
// Validate the variable and break it up into chunks...
var parts []string
var buffer string
var in_brackets bool
for _, char := range data {
if !('a' <= char && char <= 'z') && !('A' <= char && char <= 'Z') && !('0' <= char && char <= '9') && char != '[' && char != ']' && char != '.' && char != '_' && char != '-' {
return "", errors.New("invalid character in variable name")
}
if in_brackets {
if char == ']' {
if buffer != "" {
parts = append(parts,buffer)
buffer = ""
}
in_brackets = false
continue
}
}
if char == '[' {
if buffer != "" {
parts = append(parts,buffer)
buffer = ""
}
in_brackets = true
} else if char == '.' {
if buffer != "" {
parts = append(parts,buffer)
buffer = ""
}
} else {
buffer += string(char)
}
}
if buffer != "" {
parts = append(parts,buffer)
}
partCount := len(parts)
data_value, ok := ds.GetVar(parts[0])
if !ok {
return "", errors.New("this variable doesn't exist o.o")
}
data_type := DetectType(data_value)
if data_type == "string" && partCount == 2 {
conv_data, err := strconv.Atoi(parts[1])
if err != nil {
return "", errors.New("indices can only be integers x.x")
}
if len(data_value) <= conv_data {
return "", errors.New("index out of range")
}
return string(data_value[conv_data]), nil
}
if partCount > 1 && data_type != "map" && data_type != "list" && data_type != "variable" {
return "", errors.New("type " + data_type + " cannot have subelements")
} else {
return data_value, nil
}
var n int
for _, part := range parts {
if n > 5 {
return "", errors.New("too many nested subelements")
}
switch(data_type) {
case "map":
pmap, err := MapParser(data_value)
if err != nil {
return "", errors.New("invalid map x.x")
}
pdata, ok := pmap[part]
if !ok {
return "", errors.New("field does not exist in map")
}
data_value = pdata
case "list":
plist, err := ListParser(data_value)
if err != nil {
return "", errors.New("invalid list x.x")
}
list_index, err := strconv.Atoi(part)
if err != nil {
return "", errors.New("list indices must be integers o.o")
}
if len(plist) <= list_index {
return "", errors.New("index out of range o.o")
}
data_value = plist[list_index]
case "variable":
data_value, ok := ds.GetVar(parts[0])
if !ok {
return "", errors.New("subvariable doesn't exist o.o")
}
data_type = DetectType(data_value)
}
n++
}
return data_value, nil
}
func skipBlock(data string, index int) int {
var brace_count int
for ;index < len(data);index++{
char := data[index]
if char == '{' {
brace_count++
} else if char == '}' {
if brace_count == 0 {
return index
}
brace_count--
}
}
return index
}
func skipFunc(data string, index int) int {
var brace_count int
for ;index < len(data);index++{
char := data[index]
if char == '(' {
brace_count++
} else if char == ')' {
if brace_count == 0 {
return index
}
brace_count--
}
}
return index
}
func skipList(data string, index int) int {
var brace_count int
for ;index < len(data);index++{
char := data[index]
if char == '[' {
brace_count++
} else if char == ']' {
if brace_count == 0 {
return index
}
brace_count--
}
}
return index
}
func skipUntilChar(data string, index int, char byte) int {
SwitchLoop:
for ; index < len(data);index++ {
switch(data[index]) {
case '{': index = skipBlock(data,index)
case '[': index = skipList(data,index)
case '(': index = skipFunc(data,index)
case '}':
if index != 0 {
index--
}
break SwitchLoop
case char: break SwitchLoop
}
}
return index
}
func skipCurrentBlock(data string, index int) int {
SwitchLoop:
for ; index < len(data);index++ {
switch(data[index]) {
case '{': index = skipBlock(data,index)
case '[': index = skipList(data,index)
case '(': index = skipFunc(data,index)
case '}': break SwitchLoop
}
}
return index
}
func skipCurrentLine(data string, index int) int {
for ; index < len(data);index++ {
if data[index] == 10 {
return index
}
}
return index
}
/*func getBlockLastIndex() (index int) {
}*/