-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoverage.html
More file actions
2203 lines (2014 loc) · 100 KB
/
coverage.html
File metadata and controls
2203 lines (2014 loc) · 100 KB
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>internal: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/birchb1024/goyamp/internal/api.go (65.2%)</option>
<option value="file1">github.com/birchb1024/goyamp/internal/builtin.go (84.4%)</option>
<option value="file2">github.com/birchb1024/goyamp/internal/define.go (81.0%)</option>
<option value="file3">github.com/birchb1024/goyamp/internal/dotnotation.go (70.2%)</option>
<option value="file4">github.com/birchb1024/goyamp/internal/execute.go (87.1%)</option>
<option value="file5">github.com/birchb1024/goyamp/internal/exit.go (0.0%)</option>
<option value="file6">github.com/birchb1024/goyamp/internal/goyamp.go (81.9%)</option>
<option value="file7">github.com/birchb1024/goyamp/internal/include.go (72.1%)</option>
<option value="file8">github.com/birchb1024/goyamp/internal/lua-builtin.go (86.8%)</option>
<option value="file9">github.com/birchb1024/goyamp/internal/panic.go (0.0%)</option>
<option value="file10">github.com/birchb1024/goyamp/internal/repeat.go (88.2%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package internal
import (
"fmt"
"io"
"os"
"strings"
"github.com/pkg/errors"
)
func (engine *Expander) init(environment []string, argv []string) <span class="cov8" title="1">{
// """
// Construct a new environment of globals.
// :return: New global dict
// """
envMap := mapy{}
for _, pair := range environment </span><span class="cov8" title="1">{
kv := strings.SplitN(pair, "=", 2)
envMap[stringy(kv[0])] = stringy(kv[1])
}</span>
<span class="cov8" title="1">pwd, err := os.Getwd()
if err != nil </span><span class="cov0" title="0">{
pwd = "."
_, _ = fmt.Fprintf(os.Stderr, "%v", err)
}</span>
<span class="cov8" title="1">engine.globals = &env{
engine: engine,
parent: nil,
bind: map[string]yamly{
"argv": classify(argv),
"env": envMap,
"__VERSION__": stringy(engine.version),
"__DIR__": stringy(pwd),
},
}
addBuiltinsToEnv(engine.globals)</span>
}
// Syntax holds different output format syntaxes
type Syntax int
// Constants for YAML and JSON syntax
const (
YAML Syntax = iota + 0
JSON
LINES
)
// NewExpander creates a Goyamp macro expansion engine.
func NewExpander(commandArgs []string, environment []string, ow io.Writer, format Syntax, version string) Expander <span class="cov8" title="1">{
ex := Expander{
globals: nil,
output: ow,
outFormat: format,
version: version,
}
ex.init(environment, commandArgs)
return ex
}</span>
// ExpandStream reads a stream of YAML and expands it.
func (engine *Expander) ExpandStream(input io.Reader, filename string) (err error) <span class="cov0" title="0">{
defer func() </span><span class="cov0" title="0">{
if r := recover(); r != nil </span><span class="cov0" title="0">{
err = errors.New(fmt.Sprintf("%+v", r))
}</span>
}()
<span class="cov0" title="0">engine.globals.bind["__FILE__"] = stringy(filename)
return expandStream(input, filename, engine.globals)</span>
}
// ExpandFile reads a file of YAML given a path
func (engine *Expander) ExpandFile(filename string) (err error) <span class="cov8" title="1">{
defer func() </span><span class="cov8" title="1">{
if r := recover(); r != nil </span><span class="cov0" title="0">{
err = errors.New(fmt.Sprintf("%+v", r))
}</span>
}()
<span class="cov8" title="1">engine.globals.bind["__FILE__"] = stringy(filename)
return expandFile(filename, engine.globals)</span>
}
</pre>
<pre class="file" id="file1" style="display: none">package internal
import (
"fmt"
"log"
"math"
"reflect"
"sort"
"strconv"
)
// """
// :return: True or False depending if args are the same.
// """
func equalsBuiltin(tree mapy, args yamly, bindings *env) yamly <span class="cov8" title="1">{
log.Printf("== %v %v\n", tree, args)
argarray, ok := args.(seqy)
if !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("== builtin expected sequence, got %v", args))</span>
}
<span class="cov8" title="1">if len(argarray) < 2 </span><span class="cov0" title="0">{
panic(fmt.Sprintf("== builtin expected more than two in sequence, got %v", args))</span>
}
<span class="cov8" title="1">expected := argarray[0]
for _, item := range argarray </span><span class="cov8" title="1">{
if !reflect.DeepEqual(item, expected) </span><span class="cov8" title="1">{
return booly(false)
}</span>
}
<span class="cov8" title="1">return booly(true)</span>
}
// """
// :return: the sum of the arguments.
// """
func plusBuiltin(tree mapy, args yamly, bindings *env) yamly <span class="cov8" title="1">{
argarray, ok := args.(seqy)
if !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("+ builtin exepected sequence, got %v", args))</span>
}
<span class="cov8" title="1">if len(argarray) < 2 </span><span class="cov0" title="0">{
panic(fmt.Sprintf("+ builtin exepected more than two in sequence, got %v", args))</span>
}
<span class="cov8" title="1">var sum float64
for _, item := range argarray </span><span class="cov8" title="1">{
switch toadd := item.(type) </span>{
case stringy:<span class="cov8" title="1">
ind, err := strconv.Atoi(string(toadd))
if err != nil </span><span class="cov0" title="0">{
panic(fmt.Sprintf("+ builtin cannot add with %v", toadd))</span>
}
<span class="cov8" title="1">sum += float64(ind)</span>
case inty:<span class="cov8" title="1">
sum += float64(int(inty(toadd)))</span>
case float64y:<span class="cov8" title="1">
sum += float64(float64y(toadd))</span>
default:<span class="cov0" title="0">
panic(fmt.Sprintf("+ builtin cannot add with %v", toadd))</span>
}
}
<span class="cov8" title="1">if math.Round(sum) == sum </span><span class="cov8" title="1">{
return inty(int(sum))
}</span>
<span class="cov0" title="0">return float64y(sum)</span>
}
// """
// :return: a list from statement[0] to statement[1]
// """
func rangeBuiltin(tree mapy, args yamly, bindings *env) yamly <span class="cov8" title="1">{
switch args := args.(type) </span>{
case seqy:<span class="cov8" title="1">
if len(args) != 2 </span><span class="cov0" title="0">{
panic(fmt.Sprintf("range builtin needs sequence of 2, got %v", args))</span>
}
<span class="cov8" title="1">start := any2int(args[0], "range builtin needs integers, got %v")
end := any2int(args[1], "range builtin needs integers, got %v")
result := seqy{}
if start < end </span><span class="cov8" title="1">{
for i := start; i < end+1; i++ </span><span class="cov8" title="1">{
result = append(result, inty(i))
}</span>
} else<span class="cov8" title="1"> {
for i := start; i >= end; i-- </span><span class="cov8" title="1">{
result = append(result, inty(i))
}</span>
}
<span class="cov8" title="1">if len(result) == 0 </span><span class="cov0" title="0">{
panic(fmt.Sprintf("range empty range in %v ", tree))</span>
}
<span class="cov8" title="1">return result</span>
case mapy:<span class="cov8" title="1">
result := seqy{}
for key := range args </span><span class="cov8" title="1">{
result = append(result, key)
}</span>
<span class="cov8" title="1">sort.Sort(result)
return result</span>
default:<span class="cov0" title="0">
panic(fmt.Sprintf("range builtin needs sequence or map, got %v", args))</span>
}
}
// """
// expand and flatten a list.
// :param bindings:
// :return:
// """
func flattenList(any yamly, depth int) yamly <span class="cov8" title="1">{
log.Printf("flattenList: %v", any)
if depth == 0 </span><span class="cov8" title="1">{
return any
}</span>
<span class="cov8" title="1">switch listy := any.(type) </span>{
case seqy:<span class="cov8" title="1">
result := seqy{}
for _, item := range listy </span><span class="cov8" title="1">{
switch item := item.(type) </span>{
case seqy:<span class="cov8" title="1">
sub := flattenList(item, depth-1)
if sublist, ok := sub.(seqy); ok </span><span class="cov8" title="1">{
for _, v := range sublist </span><span class="cov8" title="1">{
result = append(result, v)
}</span>
} else<span class="cov0" title="0"> {
result = append(result, item)
}</span>
default:<span class="cov8" title="1">
result = append(result, item)</span>
}
}
<span class="cov8" title="1">return result</span>
default:<span class="cov0" title="0">
return any</span>
}
}
func flattenBuiltin(tree mapy, args yamly, bindings *env) yamly <span class="cov8" title="1">{
// """
// See flattenList
// """
return flattenList(args, math.MaxInt32)
}</span>
func flatoneBuiltin(tree mapy, args yamly, bindings *env) yamly <span class="cov8" title="1">{
// """
// See flatList
// """
return flattenList(args, 1)
}</span>
func mergeBuiltin(tree mapy, args yamly, bindings *env) yamly <span class="cov8" title="1">{
// """
// See mergeMaps
// """
// """
// Expand and combine multiple maps into one map. Not recursive. Later maps overwrite earlier.
// :param mappy: list of maps to be merged.
// :param bindings:
// :return: new map with merged content
// """
result := make(mapy)
switch args := args.(type) </span>{
case seqy:<span class="cov8" title="1">
for _, item := range args </span><span class="cov8" title="1">{
switch item := item.(type) </span>{
case mapy:<span class="cov8" title="1">
for k, v := range item </span><span class="cov8" title="1">{
result[k] = v // later overwrites earlier
}</span>
default:<span class="cov0" title="0">
panic(fmt.Sprintf("Error: non-map passed to merge '%v' from %v", item, tree))</span>
}
}
<span class="cov8" title="1">return result</span>
default:<span class="cov0" title="0">
return args</span>
}
}
func assertKeys(validKeys map[string]bool, tmap mapy) <span class="cov8" title="1">{
for x := range tmap </span><span class="cov8" title="1">{
x, ok := x.(stringy)
if !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("'%v' is not a valid key in %v", x, tmap))</span>
}
<span class="cov8" title="1">if _, ok := validKeys[string(x)]; !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("'%v' is not a valid key in %v", x, tmap))</span>
}
}
<span class="cov8" title="1">for k, mandatory := range validKeys </span><span class="cov8" title="1">{
if mandatory </span><span class="cov8" title="1">{
if _, ok := tmap[stringy(k)]; !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("missing key '%v' in %v", k, tmap))</span>
}
}
}
}
func ifBuiltin(tmap mapy, args yamly, bindings *env) yamly <span class="cov8" title="1">{
log.Printf("ifBuiltin %v\n", tmap)
// """
// Conditional expression
// :return: either the expansion of the 'then' or 'else' elements.
// """
assertKeys(map[string]bool{"if": true, "then": false, "else": false}, tmap)
thenClause, thok := tmap[stringy("then")]
elseClause, elok := tmap[stringy("else")]
if !thok && !elok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("Syntax error 'then' or 'else' missing in %v", tmap))</span>
}
<span class="cov8" title="1">condition := tmap[stringy("if")].expand(bindings)
var condBool bool
switch condition := condition.(type) </span>{
case empty:<span class="cov0" title="0">
condBool = false</span>
case nily:<span class="cov8" title="1">
condBool = false</span>
case booly:<span class="cov8" title="1">
condBool = bool(condition)</span>
default:<span class="cov0" title="0">
panic(fmt.Sprintf("If condition not 'true', 'false' or 'null'. Got: '%v' in %v", condition, tmap))</span>
}
<span class="cov8" title="1">log.Printf("ifBuiltin: condBool %v thok %v elok %v\n", condBool, thok, elok)
if condBool && thok </span><span class="cov8" title="1">{
expanded := thenClause.expand(bindings)
return expanded.expand(bindings)
}</span> else<span class="cov8" title="1"> if !condBool && elok </span><span class="cov8" title="1">{
expanded := elseClause.expand(bindings)
return expanded.expand(bindings)
}</span>
<span class="cov8" title="1">return empty{}</span>
}
func quoteBuiltin(tree mapy, args yamly, bindings *env) yamly <span class="cov8" title="1">{
// """
// :return: the args without expansion
// """
assertSingleKey(tree)
return args
}</span>
func addBuiltinsToEnv(env *env) <span class="cov8" title="1">{
// """
// Utility function to add all the builtins to an environment
// :env: environment to add to
// :return: The environment
// """
addNewBuiltin := func(name stringy, fn builtin, eager bool, quote bool) </span><span class="cov8" title="1">{
env.bind[string(name)] = compiledFunction{
fun: functionDef{
eager: eager,
quote: quote,
name: name,
parameters: seqy{stringy("varargs")},
varargs: true,
bindings: env,
},
compiled: fn,
}
}</span>
<span class="cov8" title="1">addNewBuiltin("define", defineBuiltin, false, false)
addNewBuiltin("include", includeBuiltin, true, false)
addNewBuiltin("defmacro", defmacroBuiltin, false, false)
addNewBuiltin("==", equalsBuiltin, true, false)
addNewBuiltin("+", plusBuiltin, true, false)
addNewBuiltin("if", ifBuiltin, false, false)
addNewBuiltin("range", rangeBuiltin, true, false)
addNewBuiltin("repeat", repeatBuiltin, false, false)
addNewBuiltin("quote", quoteBuiltin, false, true)
addNewBuiltin("undefine", undefineBuiltin, false, false)
addNewBuiltin("flatten", flattenBuiltin, true, false)
addNewBuiltin("flatone", flatoneBuiltin, true, false)
addNewBuiltin("merge", mergeBuiltin, true, false)
addNewBuiltin("load", loadBuiltin, true, false)
addNewBuiltin("execute", executeBuiltin, true, true)
addNewBuiltin("panic", panicBuiltin, true, false)
addNewBuiltin("exit", exitBuiltin, true, false)
addNewBuiltin("gopherlua", gopherluaBuiltin, true, true)</span>
}
</pre>
<pre class="file" id="file2" style="display: none">package internal
import (
"fmt"
"log"
)
func mapDefine(defs mapy, bindings *env) yamly <span class="cov8" title="1">{
log.Printf("mapDefine: %v\n", defs)
definitions := defs.expand(bindings)
defmap, ok := definitions.(mapy)
if !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("Syntax error '%v' not a map from %v", definitions, defs))</span>
}
<span class="cov8" title="1">for k, v := range defmap </span><span class="cov8" title="1">{
varname, ok := k.(stringy)
if !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("define variable name %v must be string in %v", k, defs))</span>
}
<span class="cov8" title="1">bindings.bind[string(varname)] = v</span>
}
<span class="cov8" title="1">return empty{}</span>
}
func defineBuiltin(tree mapy, args yamly, bindings *env) yamly <span class="cov8" title="1">{
log.Printf("defineBuiltin:\n %#v\n %#v\n", tree, args)
// """
// Define one or more variables in the current scope.
// :return: None
// """
assertSingleKey(tree)
todefine, aok := args.(mapy)
if !aok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("define expects a map in %v got %T", tree, args))</span>
}
<span class="cov8" title="1">name, nok := todefine[stringy("name")]
value, vok := todefine[stringy("value")]
log.Printf("defineBuiltin: nok vok %v %v\n", nok, vok)
if !nok && !vok </span><span class="cov8" title="1">{
return mapDefine(todefine, bindings)
}</span>
<span class="cov8" title="1">if !(nok && vok) </span><span class="cov0" title="0">{
panic(fmt.Sprintf("Syntax error '%v' missing keyword in %v", args, tree))</span>
}
<span class="cov8" title="1">namestr, ok := name.(stringy)
if !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("Syntax error '%v' not a string in %v", name, tree))</span>
}
<span class="cov8" title="1">newvalue := value.expand(bindings)
bindings.bind[string(namestr)] = newvalue
log.Printf("\ndefineBuiltin:\n new value for : %#v\n value: %#v\n", namestr, newvalue)
return empty{}</span>
}
func undefineBuiltin(tree mapy, args yamly, bindin *env) yamly <span class="cov8" title="1">{
log.Printf("undefineBuiltin:\n %#v\n %#v\n", tree, args)
// """
// Remove binding in the current environment only.
// :return: None
// """
assertSingleKey(tree)
if variable, ok := args.(stringy); ok </span><span class="cov8" title="1">{
delete(bindin.bind, string(variable))
return empty{}
}</span>
<span class="cov0" title="0">panic(fmt.Sprintf("Syntax error was expecting string in %v got %v", tree, args))</span>
}
func defmacroBuiltin(tree mapy, args yamly, bindin *env) yamly <span class="cov8" title="1">{
// """
// Define a new macro.
// :return: None
// """
if args == nil </span><span class="cov0" title="0">{
panic(fmt.Sprintf("Syntax error empty defmacro %v", tree))</span>
}
<span class="cov8" title="1">argst, ok := args.(mapy)
if !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("Syntax error defmacro expected a map, got %v", args))</span>
}
<span class="cov8" title="1">macroName, ok := argst[stringy("name")]
if !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("missing names in %v", tree))</span>
}
<span class="cov8" title="1">macroNameStr, ok := macroName.(stringy)
if !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("in macro %v is not a string", macroName))</span>
}
// Collate arguments
<span class="cov8" title="1">var macroParams = seqy{} // default is list if no args
varargs := false
macrop, ok := argst[stringy("args")]
if ok </span><span class="cov8" title="1">{
switch theparams := macrop.(type) </span>{
case nily:<span class="cov8" title="1"></span>
case stringy:<span class="cov8" title="1"> //varargs
macroParams = append(macroParams, theparams)
varargs = true</span>
case seqy:<span class="cov8" title="1">
for _, k := range theparams </span><span class="cov8" title="1">{
if paramName, ok := k.(stringy); ok </span><span class="cov8" title="1">{
macroParams = append(macroParams, paramName)
}</span> else<span class="cov0" title="0"> {
panic(fmt.Sprintf("in defmacro args '%#v' is not a string", k))</span>
}
}
default:<span class="cov0" title="0">
panic(fmt.Sprintf("in defmacro args '%#v' is not a string", macrop))</span>
}
}
<span class="cov8" title="1">macroBody, ok := argst[stringy("value")]
newMacro := macroFunction{
fun: functionDef{
name: macroNameStr,
eager: true,
quote: false,
parameters: macroParams,
varargs: varargs,
bindings: bindin,
},
body: macroBody,
}
bindin.bind[string(macroNameStr)] = newMacro
return empty{}</span>
}
</pre>
<pre class="file" id="file3" style="display: none">package internal
import (
"fmt"
"log"
"strconv"
)
func subvarLookup(original string, varsList []string, majorVariable yamly, bindings *env) yamly <span class="cov8" title="1">{
log.Printf("subvarLookup: %v %v %#v %v\n", original, varsList, majorVariable, bindings)
// """
// Parse and expand a 'dot notation' variable string. Recursively walk the tree of the main variable value,
// as given by the subvariable list. Return the last value if possible.
//
// :param original: The dot notation string - ie. 'b.1' - used for debug
// :param varsList: a list of 'sub' variables - ie ['1']
// :param majorVariable: the value of the major variable - ie. value of 'b' => ['x', 'y']
// :param bindings: the current environment
// :return: Example - Given 'b.1', ['b', '1' ] , {'b': ['x', 'y']} => returns 'y'
// """
if len(varsList) == 0 </span><span class="cov0" title="0">{
panic(fmt.Sprintf("Subvariable not found in %v %v", original, majorVariable))</span>
}
<span class="cov8" title="1">if majorVariable == nil </span><span class="cov0" title="0">{
panic(fmt.Sprintf("Subvariable %v not found in %v and %v", varsList, original, majorVariable))</span>
}
// If the subvar is a variable binding, use it
<span class="cov8" title="1">var first yamly
ftv, ok := bindings.lookup(stringy(varsList[0]))
log.Printf("subvarLookup ftv ok: %v %v", ftv, ok)
if ok </span><span class="cov8" title="1">{
first = ftv
}</span> else<span class="cov8" title="1"> {
first = stringy(varsList[0])
}</span>
<span class="cov8" title="1">firststr, sok := first.(stringy)
firstint, iok := first.(inty)
if sok && !iok </span><span class="cov8" title="1">{
// Look for a string containg an int
i, err := strconv.Atoi(firststr.String())
if err == nil </span><span class="cov8" title="1">{
iok = true
firstint = inty(i)
}</span>
}
<span class="cov8" title="1">if iok && !sok </span><span class="cov8" title="1">{
// got an int make a string as well
sok = true
firststr = stringy(strconv.Itoa(int(firstint)))
}</span>
<span class="cov8" title="1">if !sok && !iok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("Subvariable value %v for not string or int in %v", first, original))</span>
}
// What kind of variable data do we have?
<span class="cov8" title="1">switch majorVariableTyped := majorVariable.(type) </span>{
case mapy:<span class="cov8" title="1">
// A map of values
result, ok := majorVariableTyped[firststr] // Usually map keys are strings
if !ok </span><span class="cov0" title="0">{
result, ok = majorVariableTyped[firstint] // but majorVariable may have an integer key
if !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("Subvariable '%#v' not found in %v and %v", firstint, original, majorVariableTyped))</span>
}
}
<span class="cov8" title="1">if len(varsList) == 1 </span><span class="cov8" title="1">{
return result
}</span>
<span class="cov8" title="1">return subvarLookup(original, varsList[1:], result, bindings)</span>
case seqy:<span class="cov8" title="1">
// A Sequence needing an integer index
var index int
if iok </span><span class="cov8" title="1">{
index = int(firstint)
}</span> else<span class="cov0" title="0"> if sok </span><span class="cov0" title="0">{
ind, err := strconv.Atoi(string(firststr))
if err != nil </span><span class="cov0" title="0">{
panic(fmt.Sprintf("Subvariable List index not numeric: '%v' for %v %v", first, original, majorVariable))</span>
}
<span class="cov0" title="0">index = ind</span>
} else<span class="cov0" title="0"> {
panic(fmt.Sprintf("Subvariable List index not numeric: '%v' for %v %v", first, original, majorVariable))</span>
}
<span class="cov8" title="1">if len(majorVariableTyped) <= index || index < 0 </span><span class="cov0" title="0">{
panic(fmt.Sprintf("Subvariable List index out of bounds: %v for %v %v", index, original, majorVariableTyped))</span>
}
<span class="cov8" title="1">if len(varsList) == 1 </span><span class="cov8" title="1">{ // Last one
return majorVariableTyped[index]
}</span>
<span class="cov8" title="1">return subvarLookup(original, varsList[1:], majorVariableTyped[index], bindings)</span>
default:<span class="cov0" title="0">
panic(fmt.Sprintf("Subvariable data not indexable %#v in %v", majorVariable, original))</span>
}
}
</pre>
<pre class="file" id="file4" style="display: none">package internal
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"gopkg.in/yaml.v3"
)
func argString(tree yamly, args mapy, name string, defualt string) string <span class="cov8" title="1">{
a, ok := args[stringy(name)]
if !ok </span><span class="cov8" title="1">{
return defualt
}</span>
<span class="cov8" title="1">val, ok := a.(stringy)
if !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("'%#v' %v is not a string in '%v'", a, name, tree))</span>
}
<span class="cov8" title="1">return string(val)</span>
}
func executeBuiltin(tree mapy, args yamly, _ *env) yamly <span class="cov8" title="1">{
log.Printf("exec: %v", args)
validResponseType := map[string]bool{"lines": true, "yaml": true, "json": true, "string": true}
validRequestType := map[string]bool{"lines": true, "yaml": true, "json": true, "string": true}
assertSingleKey(tree)
var command, directory string
responseType := "lines"
requestType := "lines"
request := []byte{}
environment := os.Environ()
arguments := []string{}
switch args := args.(type) </span>{
case stringy:<span class="cov8" title="1">
str := string(args)
command = strings.Split(str, " ")[0]
arguments = strings.Split(str, " ")[1:]
responseType = "string"</span>
case mapy:<span class="cov8" title="1">
assertKeys(map[string]bool{"command": true, "args": false, "environment": false, "directory": false, "request": false, "request-type": false, "response-type": false}, args)
command = argString(tree, args, "command", "")
a, aok := args[stringy("args")]
if aok </span><span class="cov8" title="1">{
clst, ok := a.(seqy)
if !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("execute: '%v' is not a valid args sequence", a))</span>
} else<span class="cov8" title="1"> {
for _, v := range clst </span><span class="cov8" title="1">{
arguments = append(arguments, v.String())
log.Printf("execute: '%v' '%v' '%v' '%v' '%v' '%v' '%v'", command, arguments, directory, environment, requestType, responseType, string(request))
}</span>
}
}
<span class="cov8" title="1">log.Printf("execute: '%v' '%v' '%v' '%v' '%v' '%v' '%v'", command, arguments, directory, environment, requestType, responseType, string(request))
directory = argString(tree, args, "directory", "")
requestType = argString(tree, args, "request-type", "lines")
if _, ok := validRequestType[responseType]; !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("'%v' is not a valid request-type", requestType))</span>
}
<span class="cov8" title="1">responseType = argString(tree, args, "response-type", "lines")
if _, ok := validResponseType[responseType]; !ok </span><span class="cov0" title="0">{
panic(fmt.Sprintf("'%v' is not a valid response-type", responseType))</span>
}
<span class="cov8" title="1">if envi, eok := args[stringy("environment")]; eok </span><span class="cov8" title="1">{
switch envi.(type) </span>{
case mapy:<span class="cov8" title="1">
for k, v := range envi.(mapy) </span><span class="cov8" title="1">{
environment = append(environment, k.String()+"="+v.String())
}</span>
default:<span class="cov0" title="0">
panic(fmt.Sprintf("'%#v' is not a environment map", envi))</span>
}
}
<span class="cov8" title="1">log.Printf("execute: '%v' '%v' '%v' '%v' '%v' '%v' '%v'", command, arguments, directory, environment, requestType, responseType, string(request))
if req, eok := args[stringy("request")]; eok </span><span class="cov8" title="1">{
switch requestType </span>{
case "string":<span class="cov8" title="1">
request = []byte(req.String())</span>
case "lines":<span class="cov8" title="1">
switch req.(type) </span>{
case stringy:<span class="cov8" title="1">
request = []byte(req.String())</span>
case seqy:<span class="cov8" title="1">
var buf bytes.Buffer
for _, line := range req.(seqy) </span><span class="cov8" title="1">{
buf.WriteString(line.String())
buf.WriteString("\n")
}</span>
<span class="cov8" title="1">request = buf.Bytes()</span>
default:<span class="cov0" title="0">
panic(fmt.Sprintf("execute: '%v' is not a string or sequence for lines", req))</span>
}
case "json":<span class="cov8" title="1">
var buf bytes.Buffer
j := req.declassify(JSON)
jenc := json.NewEncoder(&buf)
jenc.SetIndent("", " ")
err := jenc.Encode(j)
if err != nil </span><span class="cov0" title="0">{
panic(fmt.Sprintf("execute: '%v' could not be encoded as JSON", req))</span>
}
<span class="cov8" title="1">request = buf.Bytes()</span>
case "yaml":<span class="cov8" title="1">
var buf bytes.Buffer
y := req.declassify()
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
err := enc.Encode(y)
if err != nil </span><span class="cov0" title="0">{
panic(fmt.Sprintf("execute: '%v' could not be encoded as YAML!", req))</span>
}
<span class="cov8" title="1">request = buf.Bytes()</span>
default:<span class="cov0" title="0">
panic(fmt.Sprintf("'%v' is not a valid request-type", requestType))</span>
}
}
default:<span class="cov0" title="0">
panic(fmt.Sprintf("execute args are not string or map %v", args))</span>
}
<span class="cov8" title="1">log.Printf("execute: '%v' '%#v' '%v' '%v' '%v' '%v' '%v'", command, arguments, directory, environment, requestType, responseType, string(request))
if command == "" </span><span class="cov0" title="0">{
panic(fmt.Sprintf("execute has no string command %v", args))</span>
}
<span class="cov8" title="1">cmd := exec.Command(command, arguments...)
cmd.Dir = directory
cmd.Env = environment
cmd.Stderr = os.Stderr // process stderr goes direct to user stderr
var responseBuffer bytes.Buffer
cmd.Stdout = &responseBuffer
if request != nil </span><span class="cov8" title="1">{
cmd.Stdin = bytes.NewReader(request)
}</span>
<span class="cov8" title="1">log.Printf("execute: start: '%#v'", cmd)
err := cmd.Run()
response := responseBuffer.Bytes()
log.Printf("execute: done.")
if err != nil </span><span class="cov0" title="0">{
panic(err)</span>
}
// Process the response from the sub-process
//
<span class="cov8" title="1">responsestr := string(response)
log.Printf("exec: response %#v", responsestr)
switch responseType </span>{
case "string":<span class="cov8" title="1">
return stringy(strings.TrimSpace(responsestr))</span>
case "lines":<span class="cov8" title="1">
lineslice := strings.Split(responsestr, "\n")
result := seqy{}
for i, l := range lineslice </span><span class="cov8" title="1">{
if i == len(lineslice)-1 && l == "" </span>{<span class="cov8" title="1">
}</span> else<span class="cov8" title="1"> {
result = append(result, stringy(l))
}</span>
}
<span class="cov8" title="1">return result</span>
case "json":<span class="cov8" title="1">
var doc interface{}
if len(response) == 0 </span><span class="cov8" title="1">{
return nily{} // JSON parser can't deal with ""
}</span>
<span class="cov8" title="1">err := json.Unmarshal(response, &doc)
if err != nil </span><span class="cov0" title="0">{
panic(fmt.Sprintf("execute response, '%s', was not JSON '%v'", response, err))</span>
}
<span class="cov8" title="1">return classify(intify(doc))</span> // convert floats to ints where possible, because JSON uses only float64 :-(
case "yaml":<span class="cov8" title="1">
decoder := yaml.NewDecoder(bytes.NewReader(response))
var doc interface{}
err = decoder.Decode(&doc)
if err != nil && err != io.EOF </span><span class="cov0" title="0">{
panic(fmt.Sprintf("execute response '%s', was not YAML '%v'", response, err))</span>
}
<span class="cov8" title="1">return classify(doc)</span>
default:<span class="cov0" title="0">
panic(fmt.Sprintf("execute unknown response type '%v'", responseType))</span>
}
}
</pre>
<pre class="file" id="file5" style="display: none">package internal
import (
"os"
"strconv"
)
//
// Exit the current processing and exit the process cleanly.
//
func exitBuiltin(tree mapy, args yamly, bindings *env) yamly <span class="cov0" title="0">{
result := 1
switch a := args.(type) </span>{
case empty:<span class="cov0" title="0">
result = 0</span>
case nily:<span class="cov0" title="0">
result = 0</span>
case inty:<span class="cov0" title="0">
result = int(a)</span>
case stringy:<span class="cov0" title="0">
if i, err := strconv.Atoi(string(a)); err == nil </span><span class="cov0" title="0">{
result = i
}</span>
}
<span class="cov0" title="0">os.Exit(result)
return nily{}</span>
}
</pre>
<pre class="file" id="file6" style="display: none">package internal
import (
"encoding/json"
"fmt"
lua "github.com/yuin/gopher-lua"
"gopkg.in/yaml.v3"
"io"
"log"
"os"
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
)
var documentCount int
type yamly interface {
expand(binding *env) yamly
declassify(params ...Syntax) interface{}
String() string
gopherluaify(L *lua.LState) lua.LValue
}
// Type nily stands instead of nil
// to avoid nil.expand()
type nily struct{}
type empty struct{}
type booly bool
type inty int
type stringy string
type float64y float64
type mapy map[yamly]yamly
type seqy []yamly
type unknowny struct {
x interface{}
}
func (x nily) expand(*env) yamly <span class="cov8" title="1">{ return x }</span>
func (x nily) String() string <span class="cov8" title="1">{ return "null" }</span>
func (e empty) expand(*env) yamly <span class="cov8" title="1">{ return e }</span>
func (e empty) String() string <span class="cov0" title="0">{ return "goyamp.EMPTY" }</span>
func (x inty) expand(*env) yamly <span class="cov8" title="1">{ return x }</span>
func (x inty) String() string <span class="cov8" title="1">{ return strconv.Itoa(int(x)) }</span>
func (x float64y) expand(*env) yamly <span class="cov8" title="1">{ return x }</span>
func (x float64y) String() string <span class="cov0" title="0">{ return strconv.FormatFloat(float64(x), 'G', -1, 32) }</span>
func (x booly) expand(*env) yamly <span class="cov8" title="1">{ return x }</span>
func (x booly) String() string <span class="cov0" title="0">{
if x </span><span class="cov0" title="0">{
return "true"
}</span>
<span class="cov0" title="0">return "false"</span>
}
func (x unknowny) expand(*env) yamly <span class="cov0" title="0">{ return x }</span>
func (x unknowny) String() string <span class="cov0" title="0">{ return fmt.Sprintf("unknown: %T %#v", x.x, x.x) }</span>
func (x stringy) String() string <span class="cov8" title="1">{ return string(x) }</span>
// - Engine internals...
type env struct {
bind map[string]yamly
parent *env
engine *Expander
}
// Expander holds the state of the goyamp engine.
// It is not a singleton type, have as many as you want.
type Expander struct {
globals *env
output io.Writer
outFormat Syntax
version string
}