1
2 #### command sub $(echo hi)
3 var x = $(echo hi)
4 var y = $(echo '')
5 # Make sure we can operate on these values
6 echo x=${x:-default} y=${y:-default}
7 ## STDOUT:
8 x=hi y=default
9 ## END
10
11
12 #### Set $HOME using 'var' (i.e. Oil string var in word evaluator)
13 var HOME = "foo"
14 echo $HOME
15 echo ~
16 ## STDOUT:
17 foo
18 foo
19 ## END
20
21 #### Use shell var in Oil expression
22 x='abc'
23 var length = len(x) # length in BYTES, unlike ${#x}
24 echo $length
25 ## STDOUT:
26 3
27 ## END
28
29 #### Length doesn't apply to BashArray
30 x=(a b c)
31 x[10]=A
32 x[20]=B
33
34 # shell style: length is 5
35 echo shell=${#x[@]}
36
37 # Length could be 20, but we may change the representation to Dict[int, str]
38 echo ysh=$[len(x)]
39
40 ## status: 3
41 ## STDOUT:
42 shell=5
43 ## END
44
45 #### $[len(x)] inside strings
46 var s = "abc"
47 echo -$[len(s)]-
48
49 # This already has a meaning ...
50 #echo "-$len(x)-"
51 #echo "-${len}(x)-"
52
53 ## STDOUT:
54 -3-
55 ## END
56
57 #### Func with multiple args in multiple contexts
58 shopt --set ysh:upgrade # needed for math.ysh
59
60 source $LIB_YSH/math.ysh
61
62 var x = max(1+2, 3+4)
63 echo $x $[max(1+2, 3+4)]
64
65 ## STDOUT:
66 7 7
67 ## END
68
69
70 #### Trailing Comma in Param list
71 shopt --set ysh:upgrade # needed for math.ysh
72
73 source $LIB_YSH/math.ysh
74
75 var x = max(1+2, 3+4,)
76 echo $x $[max(1+2, 3+4,)]
77
78 ## STDOUT:
79 7 7
80 ## END
81
82 #### nested expr contexts
83 var s = "123"
84
85 # lex_mode_e.ShCommand -> Expr -> ShCommand -> Expr
86 var x = $(echo $'len\n' $[len(s)])
87 echo $x
88 ## STDOUT:
89 len 3
90 ## END
91
92
93 # TODO:
94 # - test keyword args
95 # - test splatting *args, **kwargs
96 # - Multiline parsing
97 #
98 # var x = max(
99 # 1+2,
100 # 3+4,
101 # )
102 # echo $x $max(
103 # 1+2,
104 # 3+4,
105 # )
106
107 #### YSH var used with shell arithmetic
108 var w = "3"
109 echo lt=$(( w < 4 ))
110 echo gt=$(( w > 4 ))
111
112 var z = 3
113 echo lt=$(( z < 4 ))
114 echo gt=$(( z > 4 ))
115 ## STDOUT:
116 lt=1
117 gt=0
118 lt=1
119 gt=0
120 ## END
121
122 #### Parse { var x = 42 }
123 shopt -s ysh:upgrade
124 g() { var x = 42 }
125
126 var x = 1
127 f() { var x = 42; setvar x = 43 }
128 f
129 echo x=$x
130 ## STDOUT:
131 x=1
132 ## END
133
134 #### double quoted
135 var foo = "bar"
136 var x = "-$foo-${foo}-${undef:-default}-"
137 echo $x
138 ## STDOUT:
139 -bar-bar-default-
140 ## END
141
142 #### double quoted respects strict_array
143 shopt -s strict:all
144 declare -a a=(one two three)
145 var x = "-${a[@]}-"
146 echo $x
147 ## status: 1
148 ## stdout-json: ""
149
150 #### simple var sub $name $0 $1 $? etc.
151 ( exit 42 )
152 var status = $?
153 echo status=$status
154
155 set -- a b c
156 var one = $1
157 var two = $2
158 echo $one $two
159
160 var named = "$one" # equivalent to 'one'
161 echo named=$named
162
163 ## STDOUT:
164 status=42
165 a b
166 named=a
167 ## END
168
169 #### braced var sub ${x:-default}
170
171 # without double quotes
172
173 var b = ${foo:-default}
174 echo $b
175 var c = ${bar:-"-$b-"}
176 echo $c
177
178 var d = "${bar:-"-$c-"}" # another one
179 echo $d
180
181 ## STDOUT:
182 default
183 -default-
184 --default--
185 ## END
186
187 #### braced var sub respects strict_array
188 set -- a b c
189 var x = ${undef:-"$@"}
190 echo $x
191 shopt -s strict_array
192 setvar x = ${undef:-"$@"}
193 echo $x
194 ## status: 1
195 ## STDOUT:
196 a b c
197 ## END
198
199
200 #### null / true / false
201 shopt -s ysh:upgrade
202 var n = null
203 if (n) {
204 echo yes
205 } else {
206 echo no
207 }
208 var t = true
209 if (t) {
210 echo yes
211 } else {
212 echo no
213 }
214 var f = false
215 if (f) {
216 echo yes
217 } else {
218 echo no
219 }
220 ## STDOUT:
221 no
222 yes
223 no
224 ## END
225
226 #### multiline dict
227
228 # Note: a pair has to be all on one line. We could relax that but there isn't
229 # a strong reason to now.
230
231 var mydict = { a:1,
232 b: 2,
233 }
234 echo mydict=$[len(mydict)]
235 ## STDOUT:
236 mydict=2
237 ## END
238
239 #### multiline array and command sub (only here docs disallowed)
240 var array = %(
241 one
242 two
243 three
244 )
245 echo array=$[len(array)]
246
247 var comsub = $(
248 echo hi
249 echo bye
250 )
251 echo comsub=$[len(comsub)]
252
253 ## STDOUT:
254 array=3
255 comsub=6
256 ## END
257
258 #### obj=>method() - remove?
259 var s = 'hi'
260
261 # TODO: This does a bound method thing we probably don't want
262 var s2 = s=>upper()
263 echo $s2
264 ## STDOUT:
265 HI
266 ## END
267
268 #### s->upper does NOT work, should be s.upper() or =>
269 var s = 'hi'
270 var method = s->upper
271 echo $method
272 ## status: 3
273 ## stdout-json: ""
274
275 #### d.key
276 var d = {name: 'andy'}
277 var x = d.name
278 echo $x
279 ## STDOUT:
280 andy
281 ## END
282
283 #### a ++ b for string/list concatenation
284 shopt -s parse_brace
285
286 var i = 'abc'
287 var j = 'de'
288 var k = i ++ j
289 echo string $k
290
291
292 var a = [1, 2]
293 var b = [3]
294 var c = a ++ b
295 echo list len=$[len(c)]
296
297 echo ---
298
299 try {
300 = 'ab' ++ 3
301 }
302 echo Str Int $_status
303
304 try {
305 = [1, 2] ++ 3
306 }
307 echo List Int $_status
308
309 try {
310 = 3 ++ 'ab'
311 }
312 echo Int Str $_status
313
314 ## STDOUT:
315 string abcde
316 list len=3
317 ---
318 Str Int 3
319 List Int 3
320 Int Str 3
321 ## END
322
323 #### s ~~ glob and s !~~ glob
324 shopt -s ysh:all
325
326 if ('foo.py' ~~ '*.py') {
327 echo yes
328 }
329 if ('foo.py' !~~ '*.sh') {
330 echo no
331 }
332 ## STDOUT:
333 yes
334 no
335 ## END
336
337 #### Type Errors
338 shopt --set parse_brace
339
340 # TODO: It might be nice to get a message
341 try {
342 var x = {} + []
343 }
344 echo $_status
345
346 try {
347 setvar x = {} + 3
348 }
349 echo $_status
350
351 try {
352 = 'foo' ++ 3
353 }
354 echo $_status
355
356 try {
357 = 'foo' ++ 3
358 }
359 echo $_status
360
361 ## STDOUT:
362 3
363 3
364 3
365 3
366 ## END
367
368
369 #### can't use ++ on integers
370 var x = 12 ++ 3
371 echo $x
372 ## status: 3
373 ## STDOUT:
374 ## END
375
376 #### can't do mystr ++ mylist
377 = ["s"] + "t"
378 ## status: 3
379 ## STDOUT:
380 ## END
381
382
383 #### expression literals
384 var e = ^[1 + 2]
385
386 echo type=$[type(e)]
387 echo $[io->evalExpr(e)]
388
389 var e = ^[2 < 1]
390 echo $[io->evalExpr(e)]
391
392 var x = 42
393 var e = ^[42 === x and true]
394 echo $[io->evalExpr(e)]
395
396 var mylist = ^[3, 4]
397 pp test_ (io->evalExpr(mylist))
398
399 ## STDOUT:
400 type=Expr
401 3
402 false
403 true
404 (List) [3,4]
405 ## END
406
407 #### No list comprehension in ^[]
408
409 var mylist = ^[x for x in y]
410 pp test_ (io->evalExpr(mylist))
411
412 ## status: 2
413 ## STDOUT:
414 ## END
415
416
417 #### expression literals, evaluation failure
418 var e = ^[1 / 0]
419 call io->evalExpr(e)
420 ## status: 3
421 ## STDOUT:
422 ## END
423
424 #### expression literals, lazy evaluation
425 var x = 0
426 var e = ^[x]
427
428 setvar x = 1
429 echo result=$[io->evalExpr(e)]
430 ## STDOUT:
431 result=1
432 ## END
433
434 #### expression literals, sugar for strings
435 var x = 0
436 var e = ^"x is $x"
437
438 setvar x = 1
439 echo result=$[io->evalExpr(e)]
440 ## STDOUT:
441 result=x is 1
442 ## END