|
20 | 20 | from display import ui
|
21 | 21 | from frontend import flag_util
|
22 | 22 | from frontend import args
|
23 |
| -from mycpp.mylib import log |
| 23 | +from mycpp.mylib import log, tagswitch |
24 | 24 | from osh import cmd_eval
|
25 | 25 | from osh import sh_expr_eval
|
26 | 26 | from data_lang import j8_lite
|
@@ -242,29 +242,33 @@ def _AssignVarForBuiltin(mem, rval, pair, which_scopes, flags, arith_ev,
|
242 | 242 | "Can't convert type %s into BashArray" %
|
243 | 243 | ui.ValType(old_val), pair.blame_word)
|
244 | 244 | elif flag_A:
|
245 |
| - if old_val.tag() == value_e.Undef: |
246 |
| - # Note: We explicitly initialize BashAssoc for Undef. |
247 |
| - val = bash_impl.BashAssoc_New() |
248 |
| - elif old_val.tag() == value_e.Str: |
249 |
| - # Note: We explicitly initialize BashAssoc for Str. When |
250 |
| - # applying +=() to Str, we associate an old value to the key |
251 |
| - # '0'. OSH disables this when strict_array is turned on. |
252 |
| - assoc_val = bash_impl.BashAssoc_New() |
253 |
| - if pair.plus_eq: |
254 |
| - if mem.exec_opts.strict_array(): |
255 |
| - e_die("Can't convert Str to BashAssoc (strict_array)", |
256 |
| - pair.blame_word) |
257 |
| - bash_impl.BashAssoc_SetElement(assoc_val, '0', |
258 |
| - cast(value.Str, old_val).s) |
259 |
| - val = assoc_val |
260 |
| - elif old_val.tag() == value_e.BashAssoc: |
261 |
| - # We do not need adjustments for -A. |
262 |
| - pass |
263 |
| - else: |
264 |
| - # Note: BashArray cannot be converted to a BashAssoc |
265 |
| - e_die( |
266 |
| - "Can't convert type %s into BashAssoc" % |
267 |
| - ui.ValType(old_val), pair.blame_word) |
| 245 | + with tagswitch(old_val) as case: |
| 246 | + if case(value_e.Undef): |
| 247 | + # Note: We explicitly initialize BashAssoc for Undef. |
| 248 | + val = bash_impl.BashAssoc_New() |
| 249 | + elif case(value_e.Str): |
| 250 | + # Note: We explicitly initialize BashAssoc for Str. When |
| 251 | + # applying +=() to Str, we associate an old value to the |
| 252 | + # key '0'. OSH disables this when strict_array is turned |
| 253 | + # on. |
| 254 | + assoc_val = bash_impl.BashAssoc_New() |
| 255 | + if pair.plus_eq: |
| 256 | + if mem.exec_opts.strict_array(): |
| 257 | + e_die( |
| 258 | + "Can't convert Str to BashAssoc (strict_array)", |
| 259 | + pair.blame_word) |
| 260 | + bash_impl.BashAssoc_SetElement( |
| 261 | + assoc_val, '0', |
| 262 | + cast(value.Str, old_val).s) |
| 263 | + val = assoc_val |
| 264 | + elif case(value_e.BashAssoc): |
| 265 | + # We do not need adjustments for -A. |
| 266 | + pass |
| 267 | + else: |
| 268 | + # Note: BashArray cannot be converted to a BashAssoc |
| 269 | + e_die( |
| 270 | + "Can't convert type %s into BashAssoc" % |
| 271 | + ui.ValType(old_val), pair.blame_word) |
268 | 272 |
|
269 | 273 | val = cmd_eval.ListInitializeTarget(val, initializer, pair.plus_eq,
|
270 | 274 | mem.exec_opts, pair.blame_word)
|
|
0 commit comments