1 |
## oils_failures_allowed: 0
|
2 |
|
3 |
|
4 |
shopt --set parse_proc
|
5 |
|
6 |
proc f {
|
7 |
var x = 42
|
8 |
return $x
|
9 |
}
|
10 |
# this gets called with 3 args then?
|
11 |
f a b c
|
12 |
echo status=$?
|
13 |
## STDOUT:
|
14 |
status=42
|
15 |
## END
|
16 |
|
17 |
|
18 |
shopt --set parse_proc
|
19 |
|
20 |
proc f() {
|
21 |
return 42
|
22 |
}
|
23 |
f
|
24 |
echo status=$?
|
25 |
|
26 |
f a b # status 2
|
27 |
|
28 |
## status: 3
|
29 |
## STDOUT:
|
30 |
status=42
|
31 |
## END
|
32 |
|
33 |
|
34 |
shopt -s ysh:all
|
35 |
proc foo {
|
36 |
echo ARGV @ARGV
|
37 |
# do we care about this? I think we want to syntactically remove it from YSH
|
38 |
# but it can still be used for legacy
|
39 |
echo dollar-at "$@"
|
40 |
}
|
41 |
builtin set -- a b c
|
42 |
foo x y z
|
43 |
## STDOUT:
|
44 |
ARGV x y z
|
45 |
dollar-at a b c
|
46 |
## END
|
47 |
|
48 |
|
49 |
shopt -s ysh:all
|
50 |
|
51 |
proc foo(d, e, f) {
|
52 |
write params $d $e $f
|
53 |
argv.py dollar-at "$@"
|
54 |
argv.py ARGV @ARGV
|
55 |
}
|
56 |
builtin set -- a b c
|
57 |
foo x y z
|
58 |
## STDOUT:
|
59 |
params
|
60 |
x
|
61 |
y
|
62 |
z
|
63 |
['dollar-at', 'a', 'b', 'c']
|
64 |
['ARGV']
|
65 |
## END
|
66 |
|
67 |
|
68 |
shopt --set parse_proc
|
69 |
|
70 |
proc f(x='foo') {
|
71 |
echo x=$x
|
72 |
}
|
73 |
f
|
74 |
## STDOUT:
|
75 |
x=foo
|
76 |
## END
|
77 |
|
78 |
|
79 |
shopt --set parse_proc
|
80 |
|
81 |
# doesn't require ysh:all
|
82 |
proc f(x, y, z) {
|
83 |
echo $x $y $z
|
84 |
var ret = 42
|
85 |
return $ret
|
86 |
}
|
87 |
# this gets called with 3 args then?
|
88 |
f a b c
|
89 |
echo status=$?
|
90 |
## STDOUT:
|
91 |
a b c
|
92 |
status=42
|
93 |
## END
|
94 |
|
95 |
|
96 |
|
97 |
# TODO: opts goes with this
|
98 |
# var opt = grep_opts.parse(ARGV)
|
99 |
#
|
100 |
# func(**opt) # Assumes keyword args match?
|
101 |
# parse :grep_opts :opt @ARGV
|
102 |
|
103 |
shopt -s ysh:all
|
104 |
|
105 |
proc f(...names) {
|
106 |
write names: @names
|
107 |
}
|
108 |
# this gets called with 3 args then?
|
109 |
f a b c
|
110 |
echo status=$?
|
111 |
## STDOUT:
|
112 |
names:
|
113 |
a
|
114 |
b
|
115 |
c
|
116 |
status=0
|
117 |
## END
|
118 |
|
119 |
|
120 |
shopt --set ysh:all
|
121 |
|
122 |
proc f(first, ...rest) { # @ means "the rest of the arguments"
|
123 |
write --sep ' ' -- $first
|
124 |
write --sep ' ' -- @rest # @ means "splice this array"
|
125 |
}
|
126 |
f a b c
|
127 |
## STDOUT:
|
128 |
a
|
129 |
b c
|
130 |
## END
|
131 |
|
132 |
|
133 |
shopt --set ysh:upgrade
|
134 |
|
135 |
# TODO: duplicate param names aren't allowed
|
136 |
proc p (a; mylist, mydict; opt Int = 42) {
|
137 |
pp line (a)
|
138 |
pp line (mylist)
|
139 |
pp line (mydict)
|
140 |
#pp line (opt)
|
141 |
}
|
142 |
|
143 |
p WORD ([1,2,3], {name: 'bob'})
|
144 |
|
145 |
echo ---
|
146 |
|
147 |
p x (:| a b |, {bob: 42}, a = 5)
|
148 |
|
149 |
## STDOUT:
|
150 |
(Str) "WORD"
|
151 |
(List) [1,2,3]
|
152 |
(Dict) {"name":"bob"}
|
153 |
---
|
154 |
(Str) "x"
|
155 |
(List) ["a","b"]
|
156 |
(Dict) {"bob":42}
|
157 |
## END
|
158 |
|
159 |
|
160 |
shopt --set parse_proc parse_at
|
161 |
|
162 |
proc name-with-hyphen {
|
163 |
echo @ARGV
|
164 |
}
|
165 |
name-with-hyphen x y z
|
166 |
## STDOUT:
|
167 |
x y z
|
168 |
## END
|
169 |
|
170 |
|
171 |
shopt --set ysh:upgrade
|
172 |
|
173 |
# TODO: Test more of this
|
174 |
proc f(x, y ; ; ; block) {
|
175 |
echo f word $x $y
|
176 |
|
177 |
if (block) {
|
178 |
eval (block)
|
179 |
}
|
180 |
}
|
181 |
f a b { echo FFF }
|
182 |
|
183 |
# With varargs and block
|
184 |
shopt --set parse_proc
|
185 |
|
186 |
proc g(x, y, ...rest ; ; ; block) {
|
187 |
echo g word $x $y
|
188 |
echo g rest @rest
|
189 |
|
190 |
if (block) {
|
191 |
eval (block)
|
192 |
}
|
193 |
}
|
194 |
g a b c d {
|
195 |
echo GGG
|
196 |
}
|
197 |
|
198 |
## STDOUT:
|
199 |
f word a b
|
200 |
FFF
|
201 |
g word a b
|
202 |
g rest c d
|
203 |
GGG
|
204 |
## END
|
205 |
|
206 |
|
207 |
shopt --set parse_proc
|
208 |
|
209 |
# this should print an error message
|
210 |
proc f {
|
211 |
var a = %(one two)
|
212 |
return $a
|
213 |
}
|
214 |
f
|
215 |
## status: 3
|
216 |
## STDOUT:
|
217 |
## END
|
218 |
|
219 |
|
220 |
shopt --set parse_proc
|
221 |
|
222 |
# this should print an error message
|
223 |
proc f {
|
224 |
var s = 'not an integer status'
|
225 |
return $s
|
226 |
}
|
227 |
f
|
228 |
## status: 1
|
229 |
## STDOUT:
|
230 |
## END
|
231 |
|
232 |
|
233 |
proc p {
|
234 |
return 1 + 2
|
235 |
}
|
236 |
p
|
237 |
## status: 2
|
238 |
## STDOUT:
|
239 |
## END
|
240 |
|
241 |
|
242 |
shopt --set parse_proc
|
243 |
|
244 |
myfunc() {
|
245 |
echo hi
|
246 |
}
|
247 |
|
248 |
proc myproc {
|
249 |
echo hi
|
250 |
}
|
251 |
|
252 |
declare -F
|
253 |
|
254 |
# this doesn't really test that anymore...
|
255 |
exit 1 # force a failure
|
256 |
|
257 |
## STDOUT:
|
258 |
declare -f myfunc
|
259 |
declare -f myproc
|
260 |
## END
|
261 |
|
262 |
|
263 |
|
264 |
shopt --set parse_proc
|
265 |
|
266 |
# NOTE: we can disallow this in Oil statically ...
|
267 |
proc f {
|
268 |
proc g {
|
269 |
echo 'G'
|
270 |
}
|
271 |
g
|
272 |
}
|
273 |
f
|
274 |
g
|
275 |
## status: 2
|
276 |
## stdout-json: ""
|
277 |
|
278 |
|
279 |
|
280 |
shopt --set ysh:upgrade
|
281 |
shopt --set redefine_proc_func
|
282 |
|
283 |
for x in 1 2 {
|
284 |
proc p {
|
285 |
echo 'loop'
|
286 |
}
|
287 |
}
|
288 |
p
|
289 |
|
290 |
{
|
291 |
proc p {
|
292 |
echo 'brace'
|
293 |
}
|
294 |
}
|
295 |
p
|
296 |
|
297 |
## STDOUT:
|
298 |
loop
|
299 |
brace
|
300 |
## END
|
301 |
|
302 |
|
303 |
shopt --set ysh:upgrade
|
304 |
|
305 |
proc p ( ; ; ; block) {
|
306 |
eval (block)
|
307 |
}
|
308 |
|
309 |
p { echo literal }
|
310 |
|
311 |
var block = ^(echo expression)
|
312 |
p (; ; block)
|
313 |
|
314 |
## STDOUT:
|
315 |
literal
|
316 |
expression
|
317 |
## END
|
318 |
|
319 |
|
320 |
|
321 |
shopt --set ysh:upgrade
|
322 |
|
323 |
proc p2 (...words; ...typed; ...named; block) {
|
324 |
pp line (words)
|
325 |
pp line (typed)
|
326 |
pp line (named)
|
327 |
#pp line (block)
|
328 |
# To avoid <Block 0x??> - could change pp line
|
329 |
echo $[type(block)]
|
330 |
}
|
331 |
|
332 |
proc p1 (...words; ...typed; ...named; block) {
|
333 |
p2 @words (...typed; ...named; block)
|
334 |
}
|
335 |
|
336 |
p2 a b ('c', 'd', n=99) {
|
337 |
echo literal
|
338 |
}
|
339 |
echo
|
340 |
|
341 |
# Same thing
|
342 |
var block = ^(echo expression)
|
343 |
|
344 |
# Note: you need the second explicit ;
|
345 |
|
346 |
p2 a b ('c', 'd'; n=99; block)
|
347 |
echo
|
348 |
|
349 |
# what happens when you do this?
|
350 |
p2 a b ('c', 'd'; n=99; block) {
|
351 |
echo duplicate
|
352 |
}
|
353 |
|
354 |
## status: 1
|
355 |
## STDOUT:
|
356 |
(List) ["a","b"]
|
357 |
(List) ["c","d"]
|
358 |
(Dict) {"n":99}
|
359 |
Block
|
360 |
|
361 |
(List) ["a","b"]
|
362 |
(List) ["c","d"]
|
363 |
(Dict) {"n":99}
|
364 |
Command
|
365 |
|
366 |
## END
|
367 |
|
368 |
|
369 |
shopt -s parse_at
|
370 |
argv.py "$@"
|
371 |
argv.py @ARGV
|
372 |
#argv.py "${ARGV[@]}" # not useful, but it works!
|
373 |
|
374 |
set -- 'a b' c
|
375 |
argv.py "$@"
|
376 |
argv.py @ARGV # separate from the argv stack
|
377 |
|
378 |
f() {
|
379 |
argv.py "$@"
|
380 |
argv.py @ARGV # separate from the argv stack
|
381 |
}
|
382 |
f 1 '2 3'
|
383 |
## STDOUT:
|
384 |
[]
|
385 |
[]
|
386 |
['a b', 'c']
|
387 |
[]
|
388 |
['1', '2 3']
|
389 |
[]
|
390 |
## END
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
$SH -c '
|
396 |
shopt -s ysh:upgrade
|
397 |
|
398 |
argv.py global @ARGV
|
399 |
|
400 |
# should not be ignored
|
401 |
call ARGV->append("GG")
|
402 |
|
403 |
argv.py global @ARGV
|
404 |
'
|
405 |
## STDOUT:
|
406 |
['global']
|
407 |
['global', 'GG']
|
408 |
## END
|
409 |
|
410 |
|
411 |
|
412 |
$SH -c '
|
413 |
shopt -s ysh:upgrade
|
414 |
|
415 |
argv.py global @ARGV
|
416 |
|
417 |
proc p {
|
418 |
argv.py @ARGV
|
419 |
call ARGV->append("LL")
|
420 |
argv.py @ARGV
|
421 |
}
|
422 |
|
423 |
p local @ARGV
|
424 |
|
425 |
argv.py global @ARGV
|
426 |
|
427 |
' dummy0 'a b' c
|
428 |
|
429 |
## STDOUT:
|
430 |
['global', 'a b', 'c']
|
431 |
['local', 'a b', 'c']
|
432 |
['local', 'a b', 'c', 'LL']
|
433 |
['global', 'a b', 'c']
|
434 |
## END
|
435 |
|
436 |
|
437 |
|
438 |
shopt -s ysh:upgrade
|
439 |
|
440 |
typed proc p (w; t; n; block) {
|
441 |
pp line (w)
|
442 |
pp line (t)
|
443 |
pp line (n)
|
444 |
echo $[type(block)]
|
445 |
}
|
446 |
|
447 |
p word (42, n=99) {
|
448 |
echo block
|
449 |
}
|
450 |
|
451 |
|
452 |
## STDOUT:
|
453 |
(Str) "word"
|
454 |
(Int) 42
|
455 |
(Int) 99
|
456 |
Block
|
457 |
## END
|