Skip to content

Commit

Permalink
[word_eval] Implement 'shopt -s compat_array'.
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Apr 23, 2020
1 parent bf9f332 commit 6704770
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions osh/sh_expr_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def _EvalLhsAndLookupArith(self, node):
# BASH_LINENO, etc.
if val.tag_() in (value_e.MaybeStrArray, value_e.AssocArray) and lval.tag_() == lvalue_e.Named:
named_lval = cast(lvalue__Named, lval)
if word_eval.CheckCompatArray(named_lval.name):
if word_eval.CheckCompatArray(named_lval.name, self.exec_opts):
val = word_eval.ResolveCompatArray(val)

# This error message could be better, but we already have one
Expand All @@ -358,7 +358,7 @@ def EvalToInt(self, node):
# BASH_LINENO, etc.
if val.tag_() in (value_e.MaybeStrArray, value_e.AssocArray) and node.tag_() == arith_expr_e.VarRef:
tok = cast(Token, node)
if word_eval.CheckCompatArray(tok.val):
if word_eval.CheckCompatArray(tok.val, self.exec_opts):
val = word_eval.ResolveCompatArray(val)

# TODO: Can we avoid the runtime cost of adding location info?
Expand Down
10 changes: 5 additions & 5 deletions osh/word_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
# For compatibility, ${BASH_SOURCE} and ${BASH_SOURCE[@]} are both valid.
# ${FUNCNAME} and ${BASH_LINENO} are also the same type of of special variables.
_STRING_AND_ARRAY = ['BASH_SOURCE', 'FUNCNAME', 'BASH_LINENO']
def CheckCompatArray(var_name):
# type: (str) -> bool
return var_name in _STRING_AND_ARRAY
def CheckCompatArray(var_name, opts):
# type: (str, optview.Exec) -> bool
return opts.compat_array() or var_name in _STRING_AND_ARRAY

def ResolveCompatArray(val):
# type: (value_t) -> value_t
Expand Down Expand Up @@ -990,7 +990,7 @@ def _EvalBracedVarSub(self, part, part_vals, quoted):
# ${array@a} is a string
# TODO: An IR for ${} might simplify these lengthy conditions
pass
elif CheckCompatArray(var_name):
elif CheckCompatArray(var_name, self.exec_opts):
# for ${BASH_SOURCE}, etc.
val = ResolveCompatArray(val)
else:
Expand Down Expand Up @@ -1219,7 +1219,7 @@ def _EvalSimpleVarSub(self, token, part_vals, quoted):
# TODO: Special case for LINENO
val = self.mem.GetVar(var_name)
if val.tag_() in (value_e.MaybeStrArray, value_e.AssocArray):
if CheckCompatArray(var_name):
if CheckCompatArray(var_name, self.exec_opts):
# for $BASH_SOURCE, etc.
val = ResolveCompatArray(val)
else:
Expand Down

0 comments on commit 6704770

Please sign in to comment.