1 ## oils_failures_allowed: 0
2
3 #### Minus operator is left associative
4
5 var a = 1 - 0 - 1
6 var b = (1 - 0) - 1
7 echo a=$a b=$b
8
9 var a = 3 - 1 - 2
10 var b = (3 - 1) - 2
11 echo a=$a b=$b
12
13 ## STDOUT:
14 a=0 b=0
15 a=0 b=0
16 ## END
17
18 #### Division operators are left associative
19
20 var a = 10 / 4 / 2
21 var b = 10 / 4 / 2
22 echo a=$a b=$b
23
24 var a = 9 // 3 // 3
25 var b = (9 // 3) // 3
26 echo a=$a b=$b
27
28 var a = 11 % 6 % 3
29 var b = (11 % 6) % 3
30 echo a=$a b=$b
31
32 ## STDOUT:
33 a=1.25 b=1.25
34 a=1 b=1
35 a=2 b=2
36 ## END
37
38 #### Exponentiation is right associative
39
40 var a = 3 ** 2 ** 2
41 var b = 3 ** (2 ** 2)
42 echo a=$a b=$b
43
44 ## STDOUT:
45 a=81 b=81
46 ## END
47
48 #### Binary operators, with conversions from string
49
50 echo ' i i' $[1 + 2]
51 echo 'si i' $['1' + 2]
52 echo ' i si' $[1 + '2']
53 echo ---
54
55 echo ' f f' $[2.5 - 1.5]
56 echo 'sf f' $['2.5' - 1.5]
57 echo ' f sf' $[2.5 - '1.5']
58 echo ---
59
60 echo ' i f' $[4 * 1.5]
61 echo 'si f' $['4' * 1.5]
62 echo ' i sf' $[4 * '1.5']
63 echo ---
64
65 echo ' f i' $[5.0 / 2]
66 echo 'sf i' $['5.0' / 2]
67 echo ' f si' $[5.0 / '2']
68
69 ## STDOUT:
70 i i 3
71 si i 3
72 i si 3
73 ---
74 f f 1.0
75 sf f 1.0
76 f sf 1.0
77 ---
78 i f 6.0
79 si f 6.0
80 i sf 6.0
81 ---
82 f i 2.5
83 sf i 2.5
84 f si 2.5
85 ## END
86
87 #### Floating Point Division with /
88
89 var i = '1.0' / '0.05'
90
91 echo $i
92
93 ## STDOUT:
94 20.0
95 ## END
96
97
98 #### Operations That Convert to Integer: // % **
99 shopt -s parse_brace
100
101 var m = ' 5 ' // 2
102
103 var n = ' 5 ' % 2
104
105 var p = ' 5 ' ** 2
106
107 write -- $m $n $p
108
109 try {
110 var z = 'a' // 3
111 }
112 echo _status $_status
113
114 try {
115 var z = 'z' % 3
116 }
117 echo _status $_status
118
119 ## STDOUT:
120 2
121 1
122 25
123 _status 3
124 _status 3
125 ## END
126
127 #### Division by zero
128 shopt -s parse_brace
129
130 try {
131 = 42 / 0
132 }
133 echo "status / is $_status"
134
135 try {
136 = 42 // 0
137 }
138 echo "status // is $_status"
139
140 try {
141 = 42 % 0
142 }
143 echo "status % is $_status"
144
145 ## STDOUT:
146 status / is 3
147 status // is 3
148 status % is 3
149 ## END
150
151 #### Unary Operations
152
153 var a = ~1
154
155 var b = -1
156 var c = -2.3
157
158 var d = not true
159
160
161 write -- $a $b $c $d
162
163 ## STDOUT:
164 -2
165 -1
166 -2.3
167 false
168 ## END
169
170
171 #### unary minus on strings
172 json write (-3)
173 json write (-'4')
174 json write (-'5.5')
175
176 # Not accepted
177 json write (-'abc')
178
179 ## status: 3
180 ## STDOUT:
181 -3
182 -4
183 -5.5
184 ## END
185
186 #### unary ~ complement on strings
187 json write (~0)
188 json write (~'1')
189 json write (~' 2 ')
190 # Not accepted
191 json write (~'3.5')
192
193 ## status: 3
194 ## STDOUT:
195 -1
196 -2
197 -3
198 ## END
199
200 #### unary ~ doesn't work on bool
201 = ~false
202 ## status: 3
203 ## STDOUT:
204 ## END
205
206 #### unary ~ doesn't work on float
207 = ~1.0
208 ## status: 3
209 ## STDOUT:
210 ## END
211
212 #### unary - applied to bool is not allowed
213 = ~false
214 ## status: 3
215 ## STDOUT:
216 ## END
217
218 #### Big float constants becomes inf and -inf, tiny become 0.0 and -0.0
219
220 $SH -c '
221 var x = 0.12345
222 pp test_ (x)
223 '
224 echo float=$?
225
226 $SH -c '
227 # Becomes infinity
228 var x = 0.123456789e1234567
229 pp test_ (x)
230
231 var x = -0.123456789e1234567
232 pp test_ (x)
233 '
234 echo float=$?
235
236 $SH -c '
237 # Becomes infinity
238 var x = 0.123456789e-1234567
239 pp test_ (x)
240
241 var x = -0.123456789e-1234567
242 pp test_ (x)
243 '
244 echo float=$?
245
246 ## STDOUT:
247 (Float) 0.12345
248 float=0
249 (Float) INFINITY
250 (Float) -INFINITY
251 float=0
252 (Float) 0.0
253 (Float) -0.0
254 float=0
255 ## END
256
257 #### Int constants bigger than 64 bits
258
259 # Decimal
260 $SH -c '
261 var x = 1111
262 pp test_ (x)
263 '
264 echo dec=$?
265
266 $SH -c '
267 var x = 1111_2222_3333_4444_5555_6666
268 pp test_ (x)
269 '
270 echo dec=$?
271
272 # Binary
273 $SH -c '
274 var x = 0b11
275 pp test_ (x)
276 '
277 echo bin=$?
278
279 $SH -c '
280 var x = 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
281 pp test_ (x)
282 '
283 echo bin=$?
284
285 # Octal
286 $SH -c '
287 var x = 0o77
288 pp test_ (x)
289 '
290 echo oct=$?
291
292 $SH -c '
293 var x = 0o1111_2222_3333_4444_5555_6666
294 pp test_ (x)
295 '
296 echo oct=$?
297
298 # Hex
299 $SH -c '
300 var x = 0xff
301 pp test_ (x)
302 '
303 echo hex=$?
304
305 $SH -c '
306 var x = 0xaaaa_bbbb_cccc_dddd_eeee_ffff
307 pp test_ (x)
308 '
309 echo hex=$?
310
311 ## STDOUT:
312 (Int) 1111
313 dec=0
314 dec=2
315 (Int) 3
316 bin=0
317 bin=2
318 (Int) 63
319 oct=0
320 oct=2
321 (Int) 255
322 hex=0
323 hex=2
324 ## END
325
326 #### Bit shift by negative number is not allowed
327
328 shopt -s ysh:upgrade
329
330 pp test_ (1 << 1)
331 pp test_ (1 << 0)
332 try {
333 pp test_ (1 << -1)
334 }
335 echo failed $[_error.code]
336 echo
337
338 pp test_ (16 >> 2)
339 pp test_ (16 >> 1)
340 pp test_ (16 >> 0)
341 try {
342 pp test_ (16 >> -1)
343 }
344 echo failed $[_error.code]
345
346 ## STDOUT:
347 (Int) 2
348 (Int) 1
349 failed 3
350
351 (Int) 4
352 (Int) 8
353 (Int) 16
354 failed 3
355 ## END
356
357 #### 64-bit operations
358
359 shopt -s ysh:upgrade
360
361 var i = 1 << 32
362 var s = str(i)
363
364 echo "i = $i, s = $s"
365
366 if (s ~== i) {
367 echo equal
368 }
369
370 ## STDOUT:
371 i = 4294967296, s = 4294967296
372 equal
373 ## END
374
375 #### 64-bit integer doesn't overflow
376
377 # same as spec/arith.test.sh case 38
378
379 var a= 1 << 31
380 echo $a
381
382 var b = a + a
383 echo $b
384
385 var c = b + a
386 echo $c
387
388 var x = 1 << 62
389 var y = x - 1
390 echo "max positive = $[ x + y ]"
391
392 #echo "overflow $[ x + x ]"
393
394 ## STDOUT:
395 2147483648
396 4294967296
397 6442450944
398 max positive = 9223372036854775807
399 ## END
400
401 #### Integer literals
402 var d = 123
403 var b = 0b11
404 var o = 0o123
405 var h = 0xff
406 echo $d $b $o $h
407 ## STDOUT:
408 123 3 83 255
409 ## END
410
411 #### Integer literals with underscores
412 const dec = 65_536
413 const bin = 0b0001_0101
414 const oct = 0o001_755
415 const hex = 0x0001_000f
416
417 echo SHELL
418 echo $dec
419 echo $bin
420 echo $oct
421 echo $hex
422 const x = 1_1 + 0b1_1 + 0o1_1 + 0x1_1
423 echo sum $x
424
425 # This works under Python 3.6, but the continuous build has earlier versions
426 if false; then
427 echo ---
428 echo PYTHON
429
430 python3 -c '
431 print(65_536)
432 print(0b0001_0101)
433 print(0o001_755)
434 print(0x0001_000f)
435
436 # Weird syntax
437 print("sum", 1_1 + 0b1_1 + 0o1_1 + 0x1_1)
438 '
439 fi
440
441 ## STDOUT:
442 SHELL
443 65536
444 21
445 1005
446 65551
447 sum 40
448 ## END
449
450 #### Exponentiation with **
451 var x = 2**3
452 echo $x
453
454 var y = 2.0 ** 3.0 # NOT SUPPORTED
455 echo 'should not get here'
456
457 ## status: 3
458 ## STDOUT:
459 8
460 ## END
461
462 #### Float Division
463 pp test_ (5/2)
464 pp test_ (-5/2)
465 pp test_ (5/-2)
466 pp test_ (-5/-2)
467
468 echo ---
469
470 var x = 9
471 setvar x /= 2
472 pp test_ (x)
473
474 var x = -9
475 setvar x /= 2
476 pp test_ (x)
477
478 var x = 9
479 setvar x /= -2
480 pp test_ (x)
481
482 var x = -9
483 setvar x /= -2
484 pp test_ (x)
485
486
487 ## STDOUT:
488 (Float) 2.5
489 (Float) -2.5
490 (Float) -2.5
491 (Float) 2.5
492 ---
493 (Float) 4.5
494 (Float) -4.5
495 (Float) -4.5
496 (Float) 4.5
497 ## END
498
499 #### Integer Division (rounds toward zero)
500 pp test_ (5//2)
501 pp test_ (-5//2)
502 pp test_ (5//-2)
503 pp test_ (-5//-2)
504
505 echo ---
506
507 var x = 9
508 setvar x //= 2
509 pp test_ (x)
510
511 var x = -9
512 setvar x //= 2
513 pp test_ (x)
514
515 var x = 9
516 setvar x //= -2
517 pp test_ (x)
518
519 var x = -9
520 setvar x //= -2
521 pp test_ (x)
522
523 ## STDOUT:
524 (Int) 2
525 (Int) -2
526 (Int) -2
527 (Int) 2
528 ---
529 (Int) 4
530 (Int) -4
531 (Int) -4
532 (Int) 4
533 ## END
534
535 #### % operator is remainder
536 pp test_ ( 5 % 3)
537 pp test_ (-5 % 3)
538
539 # negative divisor illegal (tested in test/ysh-runtime-errors.sh)
540 #pp test_ ( 5 % -3)
541 #pp test_ (-5 % -3)
542
543 var z = 10
544 setvar z %= 3
545 pp test_ (z)
546
547 var z = -10
548 setvar z %= 3
549 pp test_ (z)
550
551 ## STDOUT:
552 (Int) 2
553 (Int) -2
554 (Int) 1
555 (Int) -1
556 ## END
557
558 #### Bitwise logical
559 var a = 0b0101 & 0b0011
560 echo $a
561 var b = 0b0101 | 0b0011
562 echo $b
563 var c = 0b0101 ^ 0b0011
564 echo $c
565 var d = ~b
566 echo $d
567 ## STDOUT:
568 1
569 7
570 6
571 -8
572 ## END
573
574 #### Shift operators
575 var a = 1 << 4
576 echo $a
577 var b = 16 >> 4
578 echo $b
579 ## STDOUT:
580 16
581 1
582 ## END
583
584 #### multiline strings, list, tuple syntax for list, etc.
585 var dq = "
586 dq
587 2
588 "
589 echo dq=$[len(dq)]
590
591 var sq = '
592 sq
593 2
594 '
595 echo sq=$[len(sq)]
596
597 var mylist = [
598 1,
599 2,
600 3,
601 ]
602 echo mylist=$[len(mylist)]
603
604 var mytuple = (1,
605 2, 3)
606 echo mytuple=$[len(mytuple)]
607
608 ## STDOUT:
609 dq=6
610 sq=6
611 mylist=3
612 mytuple=3
613 ## END
614