1 |
# Test var / setvar / etc.
|
2 |
|
3 |
## our_shell: ysh
|
4 |
## oils_failures_allowed: 2
|
5 |
|
6 |
|
7 |
proc f {
|
8 |
const x = 'local'
|
9 |
echo x=$x
|
10 |
setvar x = 'mutated'
|
11 |
echo x=$x
|
12 |
}
|
13 |
## status: 2
|
14 |
## STDOUT:
|
15 |
## END
|
16 |
|
17 |
|
18 |
shopt -s ysh:all
|
19 |
|
20 |
const x = 'foo'
|
21 |
echo x=$x
|
22 |
const x = 'bar'
|
23 |
echo x=$x
|
24 |
## status: 1
|
25 |
## STDOUT:
|
26 |
x=foo
|
27 |
## END
|
28 |
|
29 |
|
30 |
var x = "global"
|
31 |
echo x=$x
|
32 |
f() {
|
33 |
var x = "local"
|
34 |
echo x=$x
|
35 |
}
|
36 |
f
|
37 |
var x = "g2"
|
38 |
echo x=$x
|
39 |
const x = 'now-const'
|
40 |
echo x=$x
|
41 |
const x = 'oops'
|
42 |
echo x=$x
|
43 |
## status: 1
|
44 |
## STDOUT:
|
45 |
x=global
|
46 |
x=local
|
47 |
x=g2
|
48 |
x=now-const
|
49 |
## END
|
50 |
|
51 |
|
52 |
proc f {
|
53 |
var x = 'local'
|
54 |
echo x=$x
|
55 |
setvar x = 'mutated'
|
56 |
echo x=$x
|
57 |
}
|
58 |
|
59 |
var x = 'global'
|
60 |
echo x=$x
|
61 |
f
|
62 |
echo x=$x
|
63 |
## STDOUT:
|
64 |
x=global
|
65 |
x=local
|
66 |
x=mutated
|
67 |
x=global
|
68 |
## END
|
69 |
|
70 |
|
71 |
setvar x = 'global'
|
72 |
echo x=$x
|
73 |
setvar x = 'g2'
|
74 |
echo x=$x
|
75 |
## STDOUT:
|
76 |
x=global
|
77 |
x=g2
|
78 |
## END
|
79 |
|
80 |
|
81 |
var x = 1
|
82 |
setvar x = 42 # this is allowed
|
83 |
echo $x
|
84 |
setvar y = 50 # error because it's not declared
|
85 |
echo $y
|
86 |
## STDOUT:
|
87 |
42
|
88 |
50
|
89 |
## END
|
90 |
|
91 |
|
92 |
shopt -s ysh:all
|
93 |
|
94 |
var x = 1
|
95 |
f() {
|
96 |
# setting global is OK
|
97 |
setglobal x = 'XX'
|
98 |
echo x=$x
|
99 |
|
100 |
# local NOT DECLARED
|
101 |
setvar x = 'YY'
|
102 |
echo y=$y
|
103 |
}
|
104 |
## status: 2
|
105 |
## STDOUT:
|
106 |
## END
|
107 |
|
108 |
|
109 |
f() {
|
110 |
var x = 'local'
|
111 |
echo x=$x
|
112 |
setglobal x = 'mutated'
|
113 |
}
|
114 |
var x = 'global'
|
115 |
echo x=$x
|
116 |
f
|
117 |
echo x=$x
|
118 |
## STDOUT:
|
119 |
x=global
|
120 |
x=local
|
121 |
x=mutated
|
122 |
## END
|
123 |
|
124 |
|
125 |
var x = 'XX'
|
126 |
echo x=$x
|
127 |
setglobal x = 'xx'
|
128 |
echo x=$x
|
129 |
|
130 |
# fatal error
|
131 |
setglobal y = 'YY'
|
132 |
|
133 |
## STDOUT:
|
134 |
x=XX
|
135 |
x=xx
|
136 |
## END
|
137 |
|
138 |
|
139 |
|
140 |
var x
|
141 |
var a, b
|
142 |
|
143 |
var c: Int, d: Int
|
144 |
|
145 |
echo $x $a $b $c $d
|
146 |
|
147 |
## STDOUT:
|
148 |
null null null null null
|
149 |
## END
|
150 |
|
151 |
|
152 |
|
153 |
# The syntax consistent with JavaScript would be
|
154 |
# var x = 1, y = 2;
|
155 |
|
156 |
var x, y = 1, 2
|
157 |
echo "x=$x y=$y"
|
158 |
|
159 |
func f() {
|
160 |
# this syntax would be nice, but is illegal
|
161 |
# return (3, 4)
|
162 |
return ([3, 4])
|
163 |
}
|
164 |
|
165 |
var a, b = f()
|
166 |
echo "a=$a b=$b"
|
167 |
|
168 |
|
169 |
## STDOUT:
|
170 |
x=1 y=2
|
171 |
a=3 b=4
|
172 |
## END
|
173 |
|
174 |
|
175 |
|
176 |
func f() {
|
177 |
# this syntax would be nice, but is illegal
|
178 |
# return (3, 4)
|
179 |
return ([3, 4])
|
180 |
}
|
181 |
|
182 |
|
183 |
const a, b = f()
|
184 |
echo "a=$a b=$b"
|
185 |
|
186 |
setvar a = 9 # error
|
187 |
|
188 |
## status: 1
|
189 |
## STDOUT:
|
190 |
a=3 b=4
|
191 |
## END
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
# Python doesn't allow you to have annotation on each variable!
|
197 |
# https://www.python.org/dev/peps/pep-0526/#where-annotations-aren-t-allowed
|
198 |
var x Int = 3
|
199 |
var y Int = 4
|
200 |
echo "x=$x y=$y"
|
201 |
|
202 |
setvar x, y = 1, 9
|
203 |
echo "x=$x y=$y"
|
204 |
|
205 |
setvar y, x = x, y
|
206 |
echo "x=$x y=$y"
|
207 |
|
208 |
setvar x, y = x*2, x*3
|
209 |
echo "x=$x y=$y"
|
210 |
|
211 |
## STDOUT:
|
212 |
x=3 y=4
|
213 |
x=1 y=9
|
214 |
x=9 y=1
|
215 |
x=18 y=27
|
216 |
## END
|
217 |
|
218 |
|
219 |
var x = [1, 2, 3]
|
220 |
echo @x
|
221 |
|
222 |
setvar x[0], x[1] = x[1], x[0]
|
223 |
|
224 |
echo @x
|
225 |
|
226 |
var d = {int: 42}
|
227 |
|
228 |
setvar x[0], d.int = d.int, x[0]
|
229 |
|
230 |
echo @x
|
231 |
json write (d)
|
232 |
|
233 |
## STDOUT:
|
234 |
1 2 3
|
235 |
2 1 3
|
236 |
42 1 3
|
237 |
{
|
238 |
"int": 2
|
239 |
}
|
240 |
## END
|
241 |
|
242 |
|
243 |
shopt -s ysh:all
|
244 |
|
245 |
var d = {}
|
246 |
setvar d['f2'] = 42
|
247 |
setvar d.f3 = 43
|
248 |
|
249 |
# Use the opposite thing to retrieve
|
250 |
var f3 = d['f3']
|
251 |
var f2 = d.f2
|
252 |
echo f3=$f3
|
253 |
echo f2=$f2
|
254 |
## STDOUT:
|
255 |
f3=43
|
256 |
f2=42
|
257 |
## END
|
258 |
|
259 |
|
260 |
shopt -s ysh:all
|
261 |
var mylist = [1,2,3]
|
262 |
setvar mylist[1] = 42
|
263 |
|
264 |
write --sep ' ' @mylist
|
265 |
## STDOUT:
|
266 |
1 42 3
|
267 |
## END
|
268 |
|
269 |
|
270 |
shopt -s ysh:all parse_equals
|
271 |
|
272 |
proc local-var {
|
273 |
local x=1
|
274 |
var x = 2
|
275 |
echo x=$x
|
276 |
}
|
277 |
|
278 |
proc readonly-var {
|
279 |
readonly x=1
|
280 |
var x = 2
|
281 |
echo x=$x
|
282 |
}
|
283 |
|
284 |
try eval 'local-var'
|
285 |
echo status=$_status
|
286 |
try eval 'readonly-var'
|
287 |
echo status=$_status
|
288 |
|
289 |
## STDOUT:
|
290 |
x=2
|
291 |
status=0
|
292 |
status=1
|
293 |
## END
|
294 |
|
295 |
|
296 |
var d = {name: 'foo'}
|
297 |
= d
|
298 |
setvar d['name'] = 123
|
299 |
= d
|
300 |
setvar d['name'] = 'mystr'
|
301 |
= d
|
302 |
#setvar d['name'] = d
|
303 |
#= d
|
304 |
|
305 |
# This used to print ...
|
306 |
|
307 |
## STDOUT:
|
308 |
(OrderedDict) <'name': 'foo'>
|
309 |
(OrderedDict) <'name': 123>
|
310 |
(OrderedDict) <'name': 'mystr'>
|
311 |
(OrderedDict) <'name': ...>
|
312 |
## END
|
313 |
|
314 |
|
315 |
var L = [1,2,3]
|
316 |
= L
|
317 |
#setvar L[0] = L
|
318 |
#= L
|
319 |
## STDOUT:
|
320 |
(List) [1, 2, 3]
|
321 |
(List) [[...], 2, 3]
|
322 |
## END
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
# NOTE: This feels PROBLEMATIC without command_sub_errexit feels like it should
|
328 |
# be the last one ...
|
329 |
|
330 |
run() {
|
331 |
$REPO_ROOT/bin/osh -O parse_proc -c "$@"
|
332 |
|
333 |
# Identical
|
334 |
# $SH +O ysh:all -O parse_proc -c "$@"
|
335 |
}
|
336 |
|
337 |
set +o errexit
|
338 |
|
339 |
run '
|
340 |
var x = $(false)
|
341 |
echo inside1=$?
|
342 |
'
|
343 |
echo outside1=$?
|
344 |
|
345 |
run '
|
346 |
setvar x = $(false)
|
347 |
echo inside2=$?
|
348 |
'
|
349 |
echo outside2=$?
|
350 |
|
351 |
# Argument list
|
352 |
run '
|
353 |
call split( $(false) )
|
354 |
echo inside3=$?
|
355 |
'
|
356 |
echo outside3=$?
|
357 |
|
358 |
# Place expression
|
359 |
run '
|
360 |
var d = {}
|
361 |
setvar d[ $(false) ] = 42
|
362 |
echo inside4=$?
|
363 |
'
|
364 |
echo outside4=$?
|
365 |
|
366 |
## STDOUT:
|
367 |
outside1=1
|
368 |
outside2=1
|
369 |
outside3=1
|
370 |
outside4=1
|
371 |
## END
|
372 |
|
373 |
|
374 |
|
375 |
set +o errexit
|
376 |
|
377 |
$SH -c '
|
378 |
var d = {}
|
379 |
setvar d["key"] = 5
|
380 |
echo "d.key = $[d.key]"
|
381 |
setvar d[42] = 6
|
382 |
echo "should not get here"
|
383 |
'
|
384 |
echo outside1=$?
|
385 |
|
386 |
$SH -c '
|
387 |
var L = [42]
|
388 |
setvar L[0] = 43
|
389 |
echo "L[0] = $[L[0]]"
|
390 |
setvar L["key"] = 44
|
391 |
'
|
392 |
echo outside2=$?
|
393 |
|
394 |
## STDOUT:
|
395 |
d.key = 5
|
396 |
outside1=3
|
397 |
L[0] = 43
|
398 |
outside2=3
|
399 |
## END
|