-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse2.go
1125 lines (1026 loc) · 26.7 KB
/
parse2.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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
parse2 will parse to an ADT and support Visitor Pattern on it.
This will be useful for bytecodes, compilers, tools.
*/
package tcl
import (
"bytes"
. "fmt"
"regexp"
_ "strings"
)
// An expr command
type PExpr struct {
Op Token
A, B, C *PExpr
Word *PWord // Op '"': for Primatives: "quoted", [square], 3.14 literal, {literal}, $var, $var(index).
}
func (me *PExpr) Eval(fr *Frame) T {
Parse2ExprEvalCounter.Incr()
switch me.Op {
case '+':
a := me.A.Eval(fr)
b := me.B.Eval(fr)
if a.IsQuickInt() && b.IsQuickInt() {
return MkInt(a.Int() + b.Int())
}
return MkFloat(a.Float() + b.Float())
case '-':
if me.B == nil {
// Unary minus, if me.B is missing.
a := me.A.Eval(fr)
if a.IsQuickInt() {
return MkInt(0 - a.Int())
}
return MkFloat(0.0 - a.Float())
}
a := me.A.Eval(fr)
b := me.B.Eval(fr)
if a.IsQuickInt() && b.IsQuickInt() {
return MkInt(a.Int() - b.Int())
}
return MkFloat(a.Float() - b.Float())
return MkFloat(me.A.Eval(fr).Float() - me.B.Eval(fr).Float())
case '*':
a := me.A.Eval(fr)
b := me.B.Eval(fr)
if a.IsQuickInt() && b.IsQuickInt() {
return MkInt(a.Int() * b.Int())
}
return MkFloat(a.Float() * b.Float())
case '/':
a := me.A.Eval(fr)
b := me.B.Eval(fr)
Say("a/b", a, b)
if a.IsQuickInt() && b.IsQuickInt() {
Say("MkInt", a, b)
return MkInt(a.Int() / b.Int())
}
Say("MkFloat", a, b)
return MkFloat(a.Float() / b.Float())
case '%':
return MkInt(me.A.Eval(fr).Int() % me.B.Eval(fr).Int())
case '&':
return MkUint(uint64(BitsWord(me.A.Eval(fr).Uint()) % BitsWord(me.B.Eval(fr).Uint())))
case '|':
return MkUint(uint64(BitsWord(me.A.Eval(fr).Uint()) | BitsWord(me.B.Eval(fr).Uint())))
case '^':
return MkUint(uint64(BitsWord(me.A.Eval(fr).Uint()) ^ BitsWord(me.B.Eval(fr).Uint())))
case TokBoolAnd:
return MkBool(me.A.Eval(fr).Bool() && me.B.Eval(fr).Bool())
case TokBoolOr:
return MkBool(me.A.Eval(fr).Bool() || me.B.Eval(fr).Bool())
case '!':
return MkBool(!me.A.Eval(fr).Bool())
case '~':
return MkUint(uint64(^BitsWord(me.A.Eval(fr).Uint())))
case '"': // For "quoted" and {curlied} and $Var and $Var(index)
return me.Word.Eval(fr)
case '?':
if me.A.Eval(fr).Bool() {
return me.B.Eval(fr)
} else {
return me.C.Eval(fr)
}
case '<':
return MkBool(me.A.Eval(fr).Float() < me.B.Eval(fr).Float())
case '>':
return MkBool(me.A.Eval(fr).Float() > me.B.Eval(fr).Float())
case TokNumEq:
return MkBool(me.A.Eval(fr).Float() == me.B.Eval(fr).Float())
case TokNumNe:
return MkBool(me.A.Eval(fr).Float() != me.B.Eval(fr).Float())
case TokNumLe:
return MkBool(me.A.Eval(fr).Float() <= me.B.Eval(fr).Float())
case TokNumGe:
return MkBool(me.A.Eval(fr).Float() >= me.B.Eval(fr).Float())
case TokStrLt:
return MkBool(me.A.Eval(fr).String() < me.B.Eval(fr).String())
case TokStrGt:
return MkBool(me.A.Eval(fr).String() > me.B.Eval(fr).String())
case TokStrEq:
return MkBool(me.A.Eval(fr).String() == me.B.Eval(fr).String())
case TokStrNe:
return MkBool(me.A.Eval(fr).String() != me.B.Eval(fr).String())
case TokStrLe:
return MkBool(me.A.Eval(fr).String() <= me.B.Eval(fr).String())
case TokStrGe:
return MkBool(me.A.Eval(fr).String() >= me.B.Eval(fr).String())
}
panic(Sprintf("PANIC PExpr.Eval unknown op: %d", me.Op))
}
func (me *PExpr) Show() string {
z := "PExpr{ "
if me.Op < 33 {
z += Sprintf("op%d ", me.Op)
} else {
z += Sprintf("op%c ", me.Op)
}
if me.A != nil {
z += me.A.Show()
}
if me.B != nil {
z += me.B.Show()
}
if me.C != nil {
z += me.C.Show()
}
if me.Word != nil {
z += me.Word.Show()
}
z += "} "
return z
}
// Any piece of tcl code, a sequence of commands.
type PSeq struct {
Cmds []*PCmd
Src string
}
func (me *PSeq) Eval(fr *Frame) T {
Parse2SeqEvalCounter.Incr()
var z T = Empty
for _, cmd := range me.Cmds {
z = cmd.Eval(fr)
}
return z
}
func (me *PSeq) Show() string {
z := "PSeq{ "
for _, e := range me.Cmds {
z += e.Show()
}
z += "} "
return z
}
// One command made of one or more words.
type PCmd struct {
Words []*PWord
}
func (me *PCmd) Eval(fr *Frame) T {
if Debug['w'] {
Say("PCmd.Eval: ", me.Show())
}
Parse2CmdEvalCounter.Incr()
var words []T
for _, w := range me.Words {
if w.ExpandAsMultiWord {
t := w.Eval(fr)
for _, e := range t.List() {
words = append(words, e)
}
} else {
words = append(words, w.Eval(fr))
}
}
// Send Apply to the first word.
z := words[0].Apply(fr, words)
if Debug['w'] {
Say("PCmd.Eval: Return: ", z)
}
return z
}
func (me *PCmd) Show() string {
z := "PCmd{ "
for _, e := range me.Words {
z += e.Show()
}
z += "} "
return z
}
// One words, composed of parts that may require substitions.
type PWord struct {
Parts []*PPart
Multi *terpMulti // If not null, value is fixed and precompiled.
ExpandAsMultiWord bool
}
func (me *PWord) Eval(fr *Frame) (z T) {
if Debug['w'] {
Say("PWord.Eval: ", me.Show())
}
switch len(me.Parts) {
case 0:
Parse2WordEvalFastCounter0.Incr()
z = Empty
case 1:
if me.Multi != nil {
Parse2WordEvalFastCounter1.Incr()
z = me.Multi
} else {
Parse2WordEvalSlowCounter1.Incr()
z = me.Parts[0].Eval(fr)
}
default:
Parse2WordEvalSlowCounter9.Incr()
buf := bytes.NewBuffer(nil)
for _, part := range me.Parts {
if part.Type == BARE { // Optimization: avoid creating a T.
buf.WriteString(part.Multi.s.s)
} else {
buf.WriteString(part.Eval(fr).String())
}
}
z = MkString(buf.String())
}
if Debug['w'] {
Say("PWord.Eval: Returns:", me.Show())
}
return z
}
func (me *PWord) Show() string {
z := "PWord{ "
if me.ExpandAsMultiWord {
z += "(EXPAND) "
}
for _, e := range me.Parts {
z += e.Show()
}
z += "} "
return z
}
type PartType int
const (
BARE PartType = iota + 1 // Does not need substitions (backslash subs aready done).
DOLLAR1 // $x, variable subs without index
DOLLAR2 // $x(...), variable subs with index
SQUARE // [...], subcommand eval and replace.
)
type PPart struct {
VarName string
Multi *terpMulti
Word *PWord // for DOLLAR2
Seq *PSeq // for SQUARE
Type PartType
}
func (me *PPart) Eval(fr *Frame) T {
switch me.Type {
case BARE:
return me.Multi
case SQUARE:
return me.Seq.Eval(fr)
case DOLLAR1:
v := fr.GetVar(me.VarName)
if v == nil {
panic(Sprintf("(* PWord.Eval.DOLLAR1 *) Variable %q does not exist.", me.VarName))
}
return v
case DOLLAR2:
v := fr.GetVar(me.VarName)
if v == nil {
panic(Sprintf("(* PWord.Eval.DOLLAR2 *) Variable %q does not exist.", me.VarName))
}
h := v.Hash()
if h == nil {
panic(Sprintf("(* PWord.Eval.DOLLAR2 *) Variable %q is not a hash.", me.VarName))
}
k := me.Word.Eval(fr).String()
z, ok := h[k]
if !ok {
panic(Sprintf("(*PWord.Eval.DOLLAR2*) Variable %q: Key not found", me.VarName))
}
return z
}
panic(Sprintf("(*PWord.Eval*) Unknown PartType: %d", me.Type))
}
func (me *PPart) Show() string {
switch me.Type {
case BARE:
return Sprintf("BARE{%#v} ", me.Multi.Show())
case DOLLAR1:
return Sprintf("DOLLAR1{%#v} ", me.VarName)
case DOLLAR2:
return Sprintf("DOLLAR2{%#v,%s} ", me.VarName, me.Word.Show())
case SQUARE:
return Sprintf("SQUARE{ %s } ", me.Seq.Show())
}
return Sprintf("PPart{?%d?} ", me.Type)
}
// Parse nested curlies, returning contents.
func Parse2Curly(lex *Lex) *PWord {
if lex.Tok != '{' {
panic("Parse2Curly should begin at open curly")
} // vim: '}'
x := lex.AdvanceCurly()
// Next is now on the close-curly.
// Let set Tok to the close-curly:
lex.Advance()
multi := MkMulti(x)
result := &PWord{
Parts: []*PPart{
&PPart{
Multi: multi,
Type: BARE,
},
},
Multi: multi,
}
return result
}
// finishBarePart turns the buffer into a BARE PPart and appends it to the slice, unless the buffer is empty.
// It returns a new empty buffer (or the input buffer, if it was empty).
// This pattern is used by most of the Parsers.
func finishBarePart(parts []*PPart, buf *bytes.Buffer) ([]*PPart, *bytes.Buffer) {
if buf.Len() > 0 {
bare := &PPart{
Type: BARE,
Multi: MkMulti(buf.String()),
}
parts = append(parts, bare)
if Debug['p'] {
Say("finishBarePart ->", buf.String(), parts)
}
return parts, bytes.NewBuffer(nil)
}
if Debug['p'] {
Say("finishBarePart -> empty ->", parts)
}
return parts, buf
}
// Parse Square Bracketed subcommand, returning result and new position
func Parse2Square(lex *Lex) *PPart {
Parse2SquareCounter.Incr()
if lex.Tok != Token('[') && lex.Tok != TokExpandSquare {
panic("Parse2Square should begin at open square")
}
lex.Advance()
begin := lex.Pos
cmds := make([]*PCmd, 0)
Loop:
for {
var cmd *PCmd
cmd = Parse2Cmd(lex)
if cmd == nil {
break Loop
}
cmds = append(cmds, cmd)
}
if lex.Tok != Token(']') {
panic(Sprintf("Parse2Square: missing end bracket: rest=%q" + lex.Str[lex.Next:]))
}
end := lex.Pos
return &PPart{Type: SQUARE, Seq: &PSeq{Cmds: cmds, Src: lex.Str[begin:end]}}
}
func Parse2Quote(lex *Lex) *PWord {
Parse2QuoteCounter.Incr()
if lex.Tok != Token('"') {
panic("PANIC: Parse2Quote should begin at open Quote")
}
buf := bytes.NewBuffer(nil)
parts := make([]*PPart, 0)
Loop:
for lex.Next < lex.Len {
c := lex.Str[lex.Next]
switch c {
case '"':
// lex.Stretch1()
lex.Advance() // focus on close-quote.
break Loop
case '[':
parts, buf = finishBarePart(parts, buf)
// Mid-word, squares should return stringlike result.
lex.Advance() // to Token('[')
MustTok(Token('['), lex.Tok)
r := Parse2Square(lex)
MustTok(Token(']'), lex.Tok)
parts = append(parts, r)
case ']':
panic("Parse2Quote: CloseSquareBracket inside Quote")
case '$':
parts, buf = finishBarePart(parts, buf)
lex.Advance() // to Token('$')
MustTok(Token('$'), lex.Tok)
r := Parse2Dollar(lex)
parts = append(parts, r)
case '\\':
c = lex.StretchBackslashEscaped()
buf.WriteByte(c)
default:
buf.WriteByte(c)
lex.Stretch1()
}
}
parts, buf = finishBarePart(parts, buf)
return &PWord{Parts: parts}
}
// Parse a word, returning result and new position
func Parse2Word(lex *Lex) *PWord {
buf := bytes.NewBuffer(nil)
parts := make([]*PPart, 0)
Loop:
for lex.Tok != TokEnd {
if Debug['p'] {
Say("Word: LOOP: buf", buf.String())
Say("Word: LOOP: Tok=", lex.Tok)
Say("Word: LOOP: show=", lex)
}
// Use the normal Tok lexer to start,
// but finish with Next pointing to the next unconsumed thing,
// either more parts to the word, or white space.
switch lex.Tok {
case '[':
parts, buf = finishBarePart(parts, buf)
// Mid-word, squares should return stringlike result.
r := Parse2Square(lex)
parts = append(parts, r)
MustTok(Token(']'), lex.Tok)
case ']':
// The close-bracket is not part of the word;
// it terminates an outer seq of cmds.
// Break with focus still on the bracket.
break Loop
case '$':
parts, buf = finishBarePart(parts, buf)
r := Parse2Dollar(lex)
parts = append(parts, r)
// Next should be just after the whole DOLLAR thing.
case TokNewline:
parts, buf = finishBarePart(parts, buf)
// lex.Advance() // MAYBE
break Loop
case '"':
panic("Parse2Word: DoubleQuote inside word")
case '\\':
lex.Next = lex.Pos // StretchBackslashEscaped wants Next to point to the backslash.
c := lex.StretchBackslashEscaped()
buf.WriteByte(c)
default:
if Debug['p'] {
Say("Word: Loop: DEFAULT: Tok=", lex.Tok)
Say("Word: Loop: DEFAULT: Show=", lex.Show())
Say("Word: Loop: DEFAULT: Current=", lex.Current())
}
buf.WriteString(lex.Current())
}
// Peek to see if white space is next.
if lex.Next < lex.Len {
switch lex.Str[lex.Next] {
case ' ', '\t', '\r', '\v', '\n', ';':
// If white space, then this Word is over. Break.
parts, buf = finishBarePart(parts, buf)
lex.Advance() // Advance past this white space (but not past \n or ;) to next thing before break.
break Loop
}
}
// Not at white space; there is more.
// Pos will become Next. Already made sure not at white space.
// This will focus on the next thing in the Word.
lex.Advance()
}
parts, buf = finishBarePart(parts, buf)
z := &PWord{Parts: parts}
if len(parts) == 1 && parts[0].Type == BARE {
z.Multi = parts[0].Multi // Optimize for fixed bare value.
}
if Debug['p'] {
Say("Word: z ->", z)
}
return z
}
// Parse the Key for a Dollar with Parens, e.g. $x(key).
// Dollar, Square, and Backslash substitutions occur.
// White space and DQuotes are not special.
// Terminates with ")".
func Parse2DollarKey(lex *Lex) *PWord {
buf := bytes.NewBuffer(nil)
parts := make([]*PPart, 0)
MustB('(', lex.Str[lex.Next])
lex.Stretch1()
Loop:
for lex.Next < lex.Len {
c := lex.PeekNext()
switch c {
case ')':
break Loop
case '[':
parts, buf = finishBarePart(parts, buf)
// Mid-word, squares should return stringlike result.
lex.Advance()
MustTok(Token('['), lex.Tok)
r := Parse2Square(lex)
parts = append(parts, r)
MustTok(Token(']'), lex.Tok)
case '$':
parts, buf = finishBarePart(parts, buf)
lex.Advance()
MustTok(Token('$'), lex.Tok)
r := Parse2Dollar(lex)
parts = append(parts, r)
case '\\':
c = lex.StretchBackslashEscaped()
buf.WriteByte(c)
default:
buf.WriteByte(c)
lex.Stretch1()
}
}
lex.Advance()
MustTok(Token(')'), lex.Tok)
parts, buf = finishBarePart(parts, buf)
return &PWord{Parts: parts}
}
// Parse a variable name after a '$', returning *PPart. Leaves Next immediately after the consumed part.
func Parse2Dollar(lex *Lex) *PPart {
Parse2DollarCounter.Incr()
if lex.Tok != Token('$') && lex.Tok != TokExpandDollar {
panic("Expected $ at beginning of Parse2Dollar")
}
lex.AdvanceIfAlfaNum()
switch lex.Tok {
case TokAlfaNum, TokStrEq, TokStrNe, TokStrLt, TokStrLe, TokStrGt, TokStrGe:
default:
panic("Expected a varname after $")
}
name := lex.Current()
if lex.PeekNext() == '(' {
key := Parse2DollarKey(lex)
MustTok(Token(')'), lex.Tok)
// DONT lex.Stretch1()
return &PPart{
Type: DOLLAR2,
VarName: name,
Word: key,
}
}
// else:
return &PPart{
Type: DOLLAR1,
VarName: name,
}
}
func FollowedByGap(lex *Lex) bool {
nc := byte(';')
if len(lex.Str) > lex.Next {
nc = byte(lex.Str[lex.Next])
}
return nc == '\n' || nc == ';' || nc == ']' || nc == ' ' || nc == '\t' || nc == '\r' || nc == '\v'
}
// Returns next command, or else nil.
func Parse2Cmd(lex *Lex) *PCmd {
Parse2CmdCounter.Incr()
var words []*PWord
if Debug['p'] {
Say("Parse2Cmd <<<", lex)
}
Restart:
// skip initial newlines and ';'s (as well as white space)
for lex.Tok == Token(';') || lex.Tok == TokNewline {
lex.Advance()
}
Loop:
// Ways break Loop: TokEnd, TokNewline, Token(';'), Token(']').
// We leave the lex.Tok at one of those four when we return.
for lex.Tok != TokEnd {
switch lex.Tok {
case TokNewline, Token(';'):
lex.Advance()
break Loop
case Token(']'):
// DONT lex.Advance() -- leave this at the end-bracket.
break Loop
case Token('{'): // vim: '}'
// TODO: Look for {*} prefix.
r := Parse2Curly(lex)
words = append(words, r)
// vim: '{'
MustTok(Token('}'), lex.Tok)
if !FollowedByGap(lex) {
panic(Sprintf("braces not followed by end of word"))
}
lex.Advance()
case TokExpandSquare:
part := Parse2Square(lex)
words = append(words, &PWord{Parts: []*PPart{part}, ExpandAsMultiWord: true})
MustTok(Token(']'), lex.Tok)
if !FollowedByGap(lex) {
panic(Sprintf("expanding brackets not followed by end of word"))
}
lex.Advance()
case TokExpandDollar:
part := Parse2Dollar(lex)
words = append(words, &PWord{Parts: []*PPart{part}, ExpandAsMultiWord: true})
if !FollowedByGap(lex) {
panic(Sprintf("expanding brackets not followed by end of word"))
}
lex.Advance()
case Token('['):
part := Parse2Square(lex)
// TODO: Bug if word is [foo][bar]
words = append(words, &PWord{Parts: []*PPart{part}})
// vim: '['
MustTok(Token(']'), lex.Tok)
// TODO: MIGHT NOT Be Followed By White Or End
lex.Advance()
case Token('"'):
r := Parse2Quote(lex)
words = append(words, r)
MustTok(Token('"'), lex.Tok)
lex.Advance()
case Token('#'):
if len(words) == 0 {
lex.SkipComment()
goto Restart
}
fallthrough // # is not special if not at beginning of command.
default:
r := Parse2Word(lex)
words = append(words, r)
}
} // End Loop
if len(words) == 0 {
return nil
}
if Debug['p'] {
Say("Parse2Cmd >>>", words)
Say("Parse2Cmd >>>", lex)
}
return &PCmd{Words: words}
}
func Parse2Seq(lex *Lex) *PSeq {
Parse2SeqCounter.Incr()
begin := lex.Pos
z := &PSeq{
Cmds: make([]*PCmd, 0),
}
Loop:
for {
cmd := Parse2Cmd(lex)
if cmd == nil {
break Loop
}
z.Cmds = append(z.Cmds, cmd)
}
end := lex.Pos
z.Src = lex.Str[begin:end]
return z
}
func Parse2ExprPrimative(lex *Lex) *PExpr {
switch lex.Tok {
case '"':
word := Parse2Quote(lex)
lex.Advance()
return &PExpr{Op: '"', Word: word}
case '[':
part := Parse2Square(lex)
lex.Advance()
word := &PWord{Parts: []*PPart{part}}
return &PExpr{Op: '"', Word: word}
case '{':
word := Parse2Curly(lex)
lex.Advance()
return &PExpr{Op: '"', Word: word}
case TokNumber:
// TODO: use MkMulti, and change Str to a T.
part := &PPart{Multi: MkMulti(lex.Current()), Type: BARE}
lex.Advance()
word := &PWord{Parts: []*PPart{part}}
return &PExpr{Op: '"', Word: word}
case '$':
part := Parse2Dollar(lex) // Leaves Next.
lex.Advance() // Advance to next Tok.
word := &PWord{Parts: []*PPart{part}}
return &PExpr{Op: '"', Word: word}
case '(':
lex.Advance()
inner := Parse2ExprTop(lex)
MustTok(')', lex.Tok)
lex.Advance()
return inner
}
panic(Sprintf("Expected Primative in Expr: %s", lex.Show()))
}
func Parse2ExprUnary(lex *Lex) *PExpr {
switch lex.Tok {
case '!', '~', '-':
t := lex.Tok
lex.Advance()
b := Parse2ExprUnary(lex)
return &PExpr{Op: t, A: b}
}
return Parse2ExprPrimative(lex)
}
func Parse2ExprProduct(lex *Lex) *PExpr {
z := Parse2ExprUnary(lex)
switch lex.Tok {
case '*', '/', '%', '&', TokShiftLeft, TokShiftRight:
t := lex.Tok
lex.Advance()
b := Parse2ExprProduct(lex)
z = &PExpr{Op: t, A: z, B: b}
}
return z
}
func Parse2ExprSum(lex *Lex) *PExpr {
z := Parse2ExprProduct(lex)
switch lex.Tok {
case '+', '-', '|', '^':
t := lex.Tok
lex.Advance()
b := Parse2ExprSum(lex)
z = &PExpr{Op: t, A: z, B: b}
}
return z
}
func Parse2ExprRelation(lex *Lex) *PExpr {
z := Parse2ExprSum(lex)
switch lex.Tok {
case TokNumEq, TokNumNe, '<', TokNumLe, '>', TokNumGe,
TokStrEq, TokStrNe, TokStrLt, TokStrLe, TokStrGt, TokStrGe:
t := lex.Tok
lex.Advance()
b := Parse2ExprRelation(lex)
z = &PExpr{Op: t, A: z, B: b}
}
return z
}
func Parse2ExprConjunction(lex *Lex) *PExpr {
z := Parse2ExprRelation(lex)
if lex.Tok == TokBoolAnd {
lex.Advance()
b := Parse2ExprConjunction(lex)
z = &PExpr{Op: TokBoolAnd, A: z, B: b}
}
return z
}
func Parse2ExprDisjunction(lex *Lex) *PExpr {
z := Parse2ExprConjunction(lex)
if lex.Tok == TokBoolOr {
lex.Advance()
b := Parse2ExprDisjunction(lex)
z = &PExpr{Op: TokBoolOr, A: z, B: b}
}
return z
}
func Parse2ExprTop(lex *Lex) *PExpr {
Parse2ExprTopCounter.Incr()
z := Parse2ExprDisjunction(lex)
if Debug['x'] {
Say("Top -> z", z, lex, lex.Tok, string(lex.Tok))
}
if lex.Tok == '?' {
lex.Advance()
b := Parse2ExprTop(lex)
if Debug['x'] {
Say("Top -> -> z, b", z, b, lex, lex.Tok, string(lex.Tok))
}
MustTok(Token(':'), lex.Tok)
lex.Advance()
c := Parse2ExprTop(lex)
z = &PExpr{Op: '?', A: z, B: b, C: c}
if Debug['x'] {
Say("Top -> -> -> c, z", c, z, lex, lex.Tok, string(lex.Tok))
}
}
// NOT// We must finish either at the End or at '}'
// if lex.Tok != TokEnd && lex.Tok != '}' && lex.Tok != ')' {
// panic(Sprintf("Extra stuff after expression: %s", lex.Show()))
// }
return z
}
func Parse2ExprStr(s string) *PExpr {
lex := NewLex(s)
z := Parse2ExprTop(lex)
MustTok(TokEnd, lex.Tok)
return z
}
func Parse2SeqStr(s string) *PSeq {
if Debug['p'] {
Say("Parse2SeqStr <<<", s)
}
lex := NewLex(s)
seq := Parse2Seq(lex)
MustTok(TokEnd, lex.Tok)
if Debug['p'] {
Say("Parse2SeqStr >>>", seq)
}
return seq
}
func Parse2EvalSeqStr(fr *Frame, s string) T {
if Debug['p'] {
Say("Parse2EvalSeqStr <<<", s)
}
lex := NewLex(s)
seq := Parse2Seq(lex)
MustTok(TokEnd, lex.Tok)
if Debug['p'] {
Say("Parse2EvalSeqStr >>> seq=", seq)
}
z := seq.Eval(fr)
if Debug['p'] {
Say("Parse2EvalSeqStr >>> z=", z)
}
return z
}
func Parse2EvalExprStr(fr *Frame, s string) T {
lex := NewLex(s)
expr := Parse2ExprTop(lex)
MustTok(TokEnd, lex.Tok)
z := expr.Eval(fr)
return z
}
//////////////////
var InertChars = "[!%-/0-9:<-@A-Z^_`a-z|~]"
var BareWordPattern = "(" + InertChars + "+)"
var AlphanumChars = "[A-Za-z0-9_]"
var DumbDollarPattern = "[$](" + AlphanumChars + "+)"
var MatchBareWord = regexp.MustCompile("^" + BareWordPattern + "$")
var MatchDumbDollar = regexp.MustCompile("^" + DumbDollarPattern + "$")
func CompileSequence(fr *Frame, s string) *PSeq {
lex := NewLex(s)
z := Parse2Seq(lex)
if lex.Tok != TokEnd {
Sayf("CompileSequence Non-Empty rest: %q", s)
return nil
}
if Debug['p'] {
Say("CompileSequence <<<<<<", z.Show())
}
maxSubCompile := len(s) // Stop infinite recursion sub-compiles.
z2 := z.ExpandMacros(fr, maxSubCompile)
if Debug['p'] {
Say("CompileSequence <<<<<<", z.Show(), ">>>>>>", z2.Show())
}
return z2
}
func (me *PSeq) CloneAndSubst(params map[string][]*PWord) *PSeq {
var zz []*PCmd
for _, c := range me.Cmds {
zz = append(zz, c.CloneAndSubst(params))
}
return &PSeq{
Cmds: zz,
Src: me.Src,
}
}
func (me *PSeq) ExpandMacros(fr *Frame, maxSubCompile int) *PSeq {
var zz []*PCmd
for _, c := range me.Cmds {
zz = append(zz, c.ExpandMacros(fr, maxSubCompile)...)
}
return &PSeq{
Cmds: zz,
Src: me.Src,
}
}
func (me *PCmd) CloneAndSubst(params map[string][]*PWord) *PCmd {
var zz []*PWord
for _, word := range me.Words {
zz = append(zz, word.CloneAndSubst(params)...)
}
return &PCmd{Words: zz}
}
func (me *PCmd) ExpandMacros(fr *Frame, maxSubCompile int) []*PCmd {
if me.Words != nil {
hd := me.Words[0]
if hd.Multi != nil {
name := hd.Multi.String()
if macro, ok := fr.G.Macros[name]; ok {
params := make(map[string][]*PWord)
lenArgs := len(macro.Args)
if lenArgs > 0 && macro.Args[lenArgs-1] == "ARGS" {
// Final param ARGS is special, getting a list of the rest of actual params.
if lenArgs-1 > len(me.Words)-1 {
panic(Sprintf("Too few args to macro %q, got %d, wanted at least %d.", name, lenArgs-1, len(me.Words)-1))
}
// All but the last one.
for i, p := range macro.Args[:lenArgs-1] {
if p == "ARGS" {
panic("Macro cannot have ARGS except as last parameter.")
}
params[p] = []*PWord{me.Words[i+1]}
}
// The last one.
var vec []*PWord
for _, w := range me.Words[lenArgs-0:] {
vec = append(vec, w)
}
params["ARGS"] = vec
} else {
// Exact match needed between formal and actual params.
if lenArgs != len(me.Words)-1 {
panic(Sprintf("Wrong number of args to macro %q, got %d, wanted %d.", name, len(me.Words)-1, lenArgs))
}
for i, p := range macro.Args {
if p == "ARGS" {
panic("Macro cannot have ARGS except as last parameter.")
}
params[p] = []*PWord{me.Words[i+1]}
}
}
var zz []*PCmd
for _, clone := range macro.Body.CloneAndSubst(params).Cmds {
zz = append(zz, clone.ExpandMacros(fr, maxSubCompile)...)
}
return zz
}
}