Permalink
Browse files

Small word_eval refactoring.

Rename EvalWordToAny to EvalRhsWord, since that's what it's really used
for.  Change the completion usage, which is getting argv but not really
  using it.

If the RHS is not an ArrayLiteralPart, delegate to EvalWordToString.
  • Loading branch information...
Andy Chu
Andy Chu committed Jan 5, 2018
1 parent 34d0eda commit 1a0d3685f305db23e607418840a96581d2a278ef
Showing with 24 additions and 29 deletions.
  1. +2 −2 core/cmd_exec.py
  2. +1 −1 core/completion.py
  3. +20 −25 core/word_eval.py
  4. +1 −1 scripts/count.sh
View
@@ -739,7 +739,7 @@ def _Dispatch(self, node, fork_external):
for pair in node.pairs:
if pair.op == assign_op_e.PlusEqual:
assert pair.rhs, pair.rhs # I don't think a+= is valid?
val = self.word_ev.EvalWordToAny(pair.rhs)
val = self.word_ev.EvalRhsWord(pair.rhs)
old_val, lval = expr_eval.EvalLhs(pair.lhs, self.arith_ev, self.mem,
self.exec_opts)
sig = (old_val.tag, val.tag)
@@ -762,7 +762,7 @@ def _Dispatch(self, node, fork_external):
# RHS can be a string or array.
if pair.rhs:
val = self.word_ev.EvalWordToAny(pair.rhs)
val = self.word_ev.EvalRhsWord(pair.rhs)
assert isinstance(val, runtime.value), val
else:
# e.g. 'readonly x' or 'local x'
View
@@ -474,7 +474,7 @@ def _GetCompletionType(w_parser, c_parser, ev, status_out):
argv = []
for w in node.words:
try:
val = ev.EvalWordToAny(w)
val = ev.EvalWordToString(w)
except util.FatalRuntimeError:
# Why would it fail?
continue
View
@@ -457,7 +457,7 @@ def _ApplyUnarySuffixOp(self, val, op):
def _EvalDoubleQuotedPart(self, part):
"""DoubleQuotedPart -> part_value
TODO: This is pretty similar to EvalWordToAny? Consolidate?
TODO: This is pretty similar to EvalRhsWord? Consolidate?
Should share _EvalParts, which does flattening.
"""
# Example of returning array:
@@ -742,10 +742,10 @@ def _EvalBracedVarSub(self, part, quoted):
def _EvalWordPart(self, part, quoted=False):
"""Evaluate a word part.
TODO: This append to part_vals=[]
Returns:
A LIST of part_value, rather than just a single part_value, because of
the quirk where ${a:-'x'y} is a single WordPart, but yields two
part_values.
None
"""
if part.tag == word_part_e.ArrayLiteralPart:
raise AssertionError(
@@ -829,7 +829,7 @@ class _WordEvaluator:
Public entry points:
EvalWordToString
EvalWordToAny
EvalRhsWord
EvalWordSequence
Error
"""
@@ -842,7 +842,7 @@ def __init__(self, mem, exec_opts, part_ev, splitter):
self.globber = glob_.Globber(exec_opts)
def _EvalParts(self, word, quoted=False):
"""Helper for EvalWordToAny, EvalWordSequence, etc.
"""Helper for EvalRhsWord, EvalWordSequence, etc.
Returns:
List of part_value.
@@ -894,17 +894,21 @@ def EvalWordToString(self, word, do_fnmatch=False, decay=False):
if part_val.tag != part_value_e.StringPartValue:
# Example: echo f > "$@". TODO: Add proper context.
e_die("Expected string, got %s", part_val)
if do_fnmatch:
if part_val.do_split_glob:
strs.append(part_val.s)
else:
strs.append(glob_.GlobEscape(part_val.s))
# TODO: Maybe add detail like this.
#e_die('RHS of assignment should only have strings. '
# 'To assign arrays, using b=( "${a[@]}" )')
# [[ foo == */"*".py ]] or case *.py) ... esac
if do_fnmatch and not part_val.do_split_glob:
s = glob_.GlobEscape(part_val.s)
else:
strs.append(part_val.s)
s = part_val.s
strs.append(s)
return runtime.Str(''.join(strs))
def EvalWordToAny(self, word):
def EvalRhsWord(self, word):
"""word_t -> value_t.
Used for RHS of assignment. There is no splitting.
@@ -919,24 +923,15 @@ def EvalWordToAny(self, word):
# don't look like assignments.
if (len(word.parts) == 1 and
word.parts[0].tag == word_part_e.ArrayLiteralPart):
array_words = word.parts[0].words
words = braces.BraceExpandWords(array_words)
strs = self._EvalWordSequence(words)
#log('ARRAY LITERAL EVALUATED TO -> %s', strs)
return runtime.StrArray(strs)
part_vals = self._EvalParts(word)
#log('EvalWordToAny part_vals %s', part_vals)
for p in part_vals:
if p.tag != part_value_e.StringPartValue:
# TODO: strict-array should cause this; otherwise
# _DecayPartValuesToString.
e_die('RHS of assignment should only have strings. '
'To assign arrays, using b=( "${a[@]}" )')
# Just join all the parts. No funny business.
return runtime.Str(''.join(p.s for p in part_vals))
# If RHS doens't look like a=( ... ), then it must be a string.
return self.EvalWordToString(word)
def _EvalWordFrame(self, frame, argv):
all_empty = True
View
@@ -120,7 +120,7 @@ runtime() {
echo
echo 'Libraries'
wc -l core/{args,glob_}.py | sort -n
wc -l core/{args,glob_,legacy}.py | sort -n
echo
}

0 comments on commit 1a0d368

Please sign in to comment.