Skip to content

Commit

Permalink
[translation] Routine work on osh/word_eval.py
Browse files Browse the repository at this point in the history
- Account for varargs in 4 methods
- Annotate empty lists

Down to 159 compile errors.
  • Loading branch information
Andy Chu committed Jan 28, 2020
1 parent 4178d4f commit 7731678
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
5 changes: 4 additions & 1 deletion mycpp/cppgen_pass.py
Expand Up @@ -1537,7 +1537,10 @@ def visit_func_def(self, o: 'mypy.nodes.FuncDef') -> T:
class_name == 'ParseContext' and func_name == 'MakeOshParser' or
class_name == 'ErrorFormatter' and func_name == 'PrettyPrintError' or
class_name is None and func_name == 'PrettyPrintError' or
class_name == 'WordParser' and func_name == '_ParseVarExpr'
class_name == 'WordParser' and func_name == '_ParseVarExpr' or
class_name == '_WordEvaluator' and
func_name in ('EvalWordSequence2', '_EvalWordToParts',
'_EmptyStrOrError', '_EvalWordPart')
):

default_val = o.arguments[-1].initializer
Expand Down
50 changes: 25 additions & 25 deletions osh/word_eval.py
Expand Up @@ -71,8 +71,8 @@ def EvalSingleQuoted(part):
elif part.left.id == Id.Left_SingleQuoteC:
# NOTE: This could be done at compile time
# TODO: Add location info for invalid backslash
s = ''.join(word_compile.EvalCStringToken(t.id, t.val)
for t in part.tokens)
tmp = [word_compile.EvalCStringToken(t.id, t.val) for t in part.tokens]
s = ''.join(tmp)
else:
raise AssertionError(Id_str(part.left.id))
return s
Expand Down Expand Up @@ -178,7 +178,7 @@ def _MakeWordFrames(part_vals):
def _DecayPartValuesToString(part_vals, join_char):
# type: (List[part_value_t], str) -> str
# Decay ${a=x"$@"x} to string.
out = []
out = [] # type: List[part_value_t]
for p in part_vals:
UP_p = p
with tagswitch(p) as case:
Expand Down Expand Up @@ -241,7 +241,7 @@ def _PerformSlice(val, # type: value_t
length, part=part)

# NOTE: unset elements don't count towards the length.
strs = []
strs = [] # type: List[str]
for s in val.strs[begin:]:
if s is not None:
strs.append(s)
Expand Down Expand Up @@ -449,7 +449,7 @@ def _ApplyTestOp(self,
elif op.op_id in (Id.VTest_ColonEquals, Id.VTest_Equals):
if is_falsey:
# Collect new part vals.
assign_part_vals = [] # type: List[part_value_t]
assign_part_vals = [] # type: List[part_value_t]
self._EvalWordToParts(op.arg_word, quoted, assign_part_vals,
is_subst=True)

Expand All @@ -462,7 +462,7 @@ def _ApplyTestOp(self,
elif op.op_id in (Id.VTest_ColonQMark, Id.VTest_QMark):
if is_falsey:
# The arg is the error mesage
error_part_vals = [] # type: List[part_value_t]
error_part_vals = [] # type: List[part_value_t]
self._EvalWordToParts(op.arg_word, quoted, error_part_vals,
is_subst=True)
return error_part_vals, effect_e.Error
Expand Down Expand Up @@ -615,7 +615,7 @@ def _ApplyPrefixOp(self, val, prefix_op, token):
val = cast(value__MaybeStrArray, UP_val)
# translation issue: tuple indices not supported in list comprehensions
#indices = [str(i) for i, s in enumerate(val.strs) if s is not None]
indices = []
indices = [] # type: List[int]
for i, s in enumerate(val.strs):
if s is not None:
indices.append(str(i))
Expand Down Expand Up @@ -655,15 +655,15 @@ def _ApplyUnarySuffixOp(self, val, op):
elif case(value_e.MaybeStrArray):
val = cast(value__MaybeStrArray, UP_val)
# ${a[@]#prefix} is VECTORIZED on arrays. Oil should have this too.
strs = []
strs = [] # type: List[str]
for s in val.strs:
if s is not None:
strs.append(string_ops.DoUnarySuffixOp(s, op, arg_val.s))
new_val = value.MaybeStrArray(strs)

elif case(value_e.AssocArray):
val = cast(value__AssocArray, UP_val)
strs = []
strs = [] # type: List[str]
for s in val.d.itervalues():
strs.append(string_ops.DoUnarySuffixOp(s, op, arg_val.s))
new_val = value.MaybeStrArray(strs)
Expand Down Expand Up @@ -1009,15 +1009,15 @@ def _EvalBracedVarSub(self, part, part_vals, quoted):

elif case2(value_e.MaybeStrArray):
val = cast(value__MaybeStrArray, UP_val)
strs = []
strs = [] # type: List[str]
for s in val.strs:
if s is not None:
strs.append(replacer.Replace(s, op))
val = value.MaybeStrArray(strs)

elif case2(value_e.AssocArray):
val = cast(value__AssocArray, UP_val)
strs = []
strs = [] # type: List[str]
for s in val.d.itervalues():
strs.append(replacer.Replace(s, op))
val = value.MaybeStrArray(strs)
Expand Down Expand Up @@ -1073,7 +1073,7 @@ def _EvalBracedVarSub(self, part, part_vals, quoted):

def _PartValsToString(self, part_vals, span_id):
# type: (List[part_value_t], int) -> str
strs = []
strs = [] # type: List[str]
for part_val in part_vals:
UP_part_val = part_val
with tagswitch(part_val) as case:
Expand Down Expand Up @@ -1151,7 +1151,7 @@ def EvalSimpleVarSubToString(self, tok):
Example: var x = "$foo-${foo}"
"""
part_vals = [] # type: List[part_value_t]
part_vals = [] # type: List[part_value_t]
self._EvalSimpleVarSub(tok, part_vals, False)
return self._PartValsToString(part_vals, tok.span_id)

Expand Down Expand Up @@ -1357,11 +1357,11 @@ def EvalWordToString(self, word, do_fnmatch=False, do_ere=False):

assert isinstance(word, compound_word)

part_vals = [] # type: List[part_value_t]
part_vals = [] # type: List[part_value_t]
for p in word.parts:
self._EvalWordPart(p, part_vals, quoted=False)

strs = []
strs = [] # type: List[str]
for part_val in part_vals:
UP_part_val = part_val
with tagswitch(part_val) as case:
Expand Down Expand Up @@ -1492,7 +1492,7 @@ def _EvalWordFrame(self, frame, argv):
will_glob = not self.exec_opts.noglob

# Array of strings, some of which are BOTH IFS-escaped and GLOB escaped!
frags = []
frags = [] # type: List[str]
for frag, quoted, do_split in frame:
if will_glob:
if quoted:
Expand Down Expand Up @@ -1538,10 +1538,10 @@ def _EvalWordToArgv(self, w):
Example: declare -"${a[@]}" b=(1 2)
where a is [x b=a d=a]
"""
part_vals = [] # type: List[part_value_t]
part_vals = [] # type: List[part_value_t]
self._EvalWordToParts(w, False, part_vals) # not double quoted
frames = _MakeWordFrames(part_vals)
argv = []
argv = [] # type: List[str]
for frame in frames:
if len(frame): # empty array gives empty frame!
tmp = [s for (s, _, _) in frame]
Expand Down Expand Up @@ -1583,7 +1583,7 @@ def _SplitAssignArg(arg, # type: str

flags = [arg0]
flag_spids = [word_.LeftMostSpanForWord(words[0])]
assign_args = []
assign_args = [] # type: List[assign_arg]

n = len(words)
for i in xrange(1, n): # skip first word
Expand Down Expand Up @@ -1640,8 +1640,8 @@ def StaticEvalWordSequence2(self, words, allow_assign):
# type: (List[compound_word], bool) -> cmd_value_t
"""Static word evaluation for Oil."""
#log('W %s', words)
strs = []
spids = []
strs = [] # type: List[str]
spids = [] # type: List[int]

n = 0
for i, w in enumerate(words):
Expand Down Expand Up @@ -1670,7 +1670,7 @@ def StaticEvalWordSequence2(self, words, allow_assign):
spids.append(word_spid)
continue

part_vals = [] # type: List[part_value_t]
part_vals = [] # type: List[part_value_t]
self._EvalWordToParts(w, False, part_vals) # not double quoted

if 0:
Expand Down Expand Up @@ -1726,12 +1726,12 @@ def EvalWordSequence2(self, words, allow_assign=False):
# 5. globbing -- several exec_opts affect this: nullglob, safeglob, etc.

#log('W %s', words)
strs = [] # type: List[str]
spids = []
strs = [] # type: List[str]
spids = [] # type: List[int]

n = 0
for i, w in enumerate(words):
part_vals = [] # type: List[part_value_t]
part_vals = [] # type: List[part_value_t]
self._EvalWordToParts(w, False, part_vals) # not double quoted

# DYNAMICALLY detect if we're going to run an assignment builtin, and
Expand Down

0 comments on commit 7731678

Please sign in to comment.