-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathysh-ify.sh
executable file
·657 lines (556 loc) · 8.14 KB
/
ysh-ify.sh
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
#!/usr/bin/env bash
#
# Test ysh-prettify transformations
#
# Usage:
# ./ysh-ify.sh <function name>
: ${LIB_OSH=stdlib/osh}
source $LIB_OSH/bash-strict.sh
source $LIB_OSH/task-five.sh
source test/common.sh # $OSH
readonly TEMP_DIR=_tmp
prettify-one() {
local file=$1
set +o errexit
$OSH --tool ysh-ify "$file"
local status=$?
set +o errexit
if test $status = 0; then
echo " (DONE $file)"
else
echo " FAIL: $file"
return 255 # xargs FAILURE
fi
}
smoke-test() {
### Run it against many of our files
find build benchmarks -name '*.sh' | xargs -n 1 -- $0 prettify-one
}
run-all() {
### For both CI and release.
# Note: might want to split these up.
run-test-funcs
smoke-test
}
soil-run() {
run-all
}
soil-run-cpp() {
### Not used yet, but it works
local osh=_bin/cxx-asan/osh
ninja $osh
#OSH=$osh run-test-funcs
OSH=$osh run-all
}
run-for-release() {
run-other-suite-for-release ysh-ify run-all
}
#
# Test Harness
#
check-osh2ysh() {
local osh_str=$1
local ysh_str=$2 # expected
local allow_invalid=${3:-}
# Make sure they are valid
bin/osh -n -c "$osh_str"
if test -z "$allow_invalid"; then
bin/ysh -n -c "$ysh_str"
fi
local tmp=$TEMP_DIR/actual.ysh
echo "$osh_str" | bin/osh --tool ysh-ify | tee $tmp
echo "$ysh_str" | diff -u $tmp -
echo 'OK'
# TODO: Also create a variant that tests equal STDOUT and STATUS!
# probably assert no stderr
#
# For backticks, etc.
}
#
# UNCHANGED
#
test-simple-command() {
### Unchanged
check-osh2ysh 'echo hi' 'echo hi'
}
test-line-breaks() {
### Unchanged
check-osh2ysh '
echo one \
two three \
four
' '
echo one \
two three \
four
'
}
test-and-or() {
check-osh2ysh \
'ls && echo "$@" || die "foo"' \
'ls && echo @ARGV || die "foo"'
}
#
# CHANGED WORD LANGUAGE
#
test-dollar-at() {
check-osh2ysh \
'echo one "$@" two' \
'echo one @ARGV two'
}
TODO-test-prefix-ops() {
check-osh2ysh \
'echo ${#s} ${#a[@]}' \
'echo $[len(s)] $[len(a)]'
}
test-unquote-subs-TODO() {
check-osh2ysh \
'echo "$1" "$foo"' \
'echo $1 $foo'
check-osh2ysh \
'echo "$(echo hi)"' \
'echo $(echo hi)'
return
# TODO: echo $foo
check-osh2ysh \
'echo "${foo}"' \
'echo $foo'
}
TODO-test-word-joining() {
local osh=$(cat <<EOF
echo 'foo " bar '"'"
EOF
)
# TODO: Use new YSTR syntax!
local ysh=$(cat <<EOF
echo y"foo \" bar '"
EOF
)
check-osh2ysh "$osh" "$ysh"
}
# Unchanged
test-command-sub() {
check-osh2ysh \
'echo $(echo hi)' \
'echo $(echo hi)'
check-osh2ysh \
'echo "__$(echo hi)__"' \
'echo "__$(echo hi)__"'
}
test-var-sub() {
# Unchanged
check-osh2ysh \
'echo $foo' \
'echo $foo'
# Could just be $bar
check-osh2ysh \
'echo $foo ${bar} "__${bar}__"' \
'echo $foo ${bar} "__${bar}__"'
return
# We could make this $[foo ? 'default'], but meh, let's not introduce more
# operators
#
# Better is getvar('foo', 'default')
check-osh2ysh \
'echo ${foo:-default}' \
"echo $[getvar('foo', 'default')]"
}
# Downgraded to one_pass_parse. This means \" will be wrong, but meh.
# Here the WordParser makes another pass with CommandParser.
#
# We could also translate it to:
# echo $[compat backticks 'echo hi']
# But that might be overly pedantic. This will work most of the time.
test-backticks-TODO() {
check-osh2ysh \
'echo `echo hi ${var}`' \
'echo $(echo hi ${var})'
check-osh2ysh \
'echo $({ echo hi; })' \
'echo $({ echo hi; })'
# TODO: Fix this
check-osh2ysh \
'echo `{ echo hi; }`' \
'echo $(do { echo hi)' \
INVALID
}
#
# CHANGED BUILTIN LANGUAGE
#
test-bracket-builtin() {
check-osh2ysh \
'[ ! -z "$foo" ] || die' \
'test ! -z $foo || die'
# Don't touch this invalid code?
check-osh2ysh \
'[ ] || die' \
'[ ] || die'
check-osh2ysh '
if [ "$foo" -eq 3 ]; then
echo yes
fi' \
'
if test $foo -eq 3 {
echo yes
}'
}
test-source-builtin() {
check-osh2ysh \
'. lib.sh' \
'source lib.sh'
check-osh2ysh \
'[ -f lib.sh ] && . lib.sh' \
'test -f lib.sh && source lib.sh'
}
TODO-test-set-builtin() {
# Not as important now that we have 'setvar'
check-osh2ysh \
'set -o errexit' \
'shopt --set errexit'
}
#
# CHANGED COMMAND LANGUAGE
#
test-here-doc() {
check-osh2ysh '
cat <<EOF
hi
EOF
' '
cat <<< """
hi
"""
'
check-osh2ysh "
cat <<'EOF'
hi
EOF
" "
cat <<< '''
hi
'''
"
}
test-bare-assign-TODO() {
check-osh2ysh "
a=
" "
setvar a = ''
"
check-osh2ysh "
a=b
" "
setvar a = 'b'
"
# TODO: Make it quoted
if false; then
check-osh2ysh '
a="$x"
' '
setvar a = "$x"
'
fi
check-osh2ysh '
a=$(hostname)
' '
setvar a = $(hostname)
'
check-osh2ysh '
a=${PATH:-}
' '
setvar a = ${PATH:-}
'
return
check-osh2ysh '
a=$x
' '
setvar a = "$x"
'
}
TODO-test-assign-builtins() {
check-osh2ysh "
local a=
" "
var a = ''
"
check-osh2ysh "
local a=b
" "
var a = 'b'
"
# TODO: more test cases
check-osh2ysh "
readonly a=b
" "
const a = 'b'
"
}
test-while-loop() {
check-osh2ysh '
while read line; do
echo $line
done' \
'
while read line {
echo $line
}'
check-osh2ysh '
while read \
line; do
echo $line
done' \
'
while read \
line {
echo $line
}'
}
test-if() {
check-osh2ysh '
if true; then
echo yes
fi' \
'
if true {
echo yes
}'
check-osh2ysh '
if true; then
echo yes
elif false; then
echo elif
elif spam; then
echo elif
else
echo no
fi' \
'
if true {
echo yes
} elif false {
echo elif
} elif spam {
echo elif
} else {
echo no
}'
# Redirect
check-osh2ysh '
if true; then
echo yes
fi > out' \
'
if true {
echo yes
} > out'
}
TODO-test-then-next-line() {
# TODO: Brace must be on same line
check-osh2ysh '
if true
then
echo yes
fi' \
'
if true {
echo yes
}'
}
test-posix-func() {
check-osh2ysh '
f() {
echo "hi"
}' '
proc f {
echo "hi"
}'
# The brace is moved
check-osh2ysh '
f()
{
echo "hi"
}' '
proc f {
echo "hi"
}'
return
# Nested functinos
check-osh2ysh '
func1() {
echo func1
func2()
{
echo func2
}
}' \
'
proc func1 {
echo func1
proc func2
{
echo func2
}
}'
return
# Non-brace function bodies
# TODO: Bail in this case
check-osh2ysh '
f() (
echo hi
)' \
'
proc f (
echo hi
)' \
INVALID
}
test-ksh-func() {
check-osh2ysh '
function func1 { # no parens
echo func1
}' '
proc func1 { # no parens
echo func1
}'
}
test-for-loop() {
check-osh2ysh '
for x in a b c \
d e f; do
echo $x
done
' '
for x in a b c \
d e f {
echo $x
}
'
check-osh2ysh '
for x in a b c \
d e f
do
echo $x
done
' '
for x in a b c \
d e f
{
echo $x
}
'
}
test-empty-for-loop() {
check-osh2ysh '
for x in
do
echo $x
done
' '
for x in
{
echo $x
}
'
}
test-args-for-loop() {
# Why are we missing a newline here?
check-osh2ysh '
for x; do
echo $x
done
' 'for x in @ARGV {
echo $x
}
'
# Change brace style
check-osh2ysh '
for x
do
echo $x
done
' 'for x in @ARGV {
echo $x
}
'
}
# TODO: translate to forkwait { proper spaces }
test-subshell() {
check-osh2ysh \
'(echo hi;)' \
'shell {echo hi;}' \
INVALID
check-osh2ysh \
'(echo hi)' \
'shell {echo hi}' \
INVALID
check-osh2ysh \
'(echo hi; echo bye)' \
'shell {echo hi; echo bye}' \
INVALID
check-osh2ysh \
'( (echo hi; echo bye ) )' \
'shell { shell {echo hi; echo bye } }' \
INVALID
}
test-brace-group() {
check-osh2ysh \
'{ echo hi; }' \
'do { echo hi; }' \
INVALID
check-osh2ysh \
'{ echo hi; echo bye; }' \
'do { echo hi; echo bye; }' \
INVALID
}
# TODO: New case syntax, which looks like
#
# case (myvar) {
# *.cc | *.h { echo 'C++' }
# }
# case (myvar) {
# *.cc | *.h {
# echo 'C++'
# }
# }
test-case() {
check-osh2ysh '
case $var in
foo|bar)
[ -f foo ] && echo file
;;
"")
echo empty
;;
*)
echo default
;;
esac
' '
case (var) {
foo|bar {
test -f foo && echo file
}
"" {
echo empty
}
* {
echo default
}
}
'
check-osh2ysh '
case "$var" in
*)
echo foo
echo bar # no dsemi
esac
' '
case (var) {
* {
echo foo
echo bar # no dsemi
}
}
'
}
task-five "$@"