Permalink
Browse files

Translate (( n++ )) to sh-expr ' n++ '.

With tests.  Now everything in oil/ should translate.
  • Loading branch information...
Andy Chu
Andy Chu committed Aug 30, 2018
1 parent ed1c953 commit 807f0d9a8175b22b18199dabf938be928076bbdd
Showing with 31 additions and 12 deletions.
  1. +9 −2 osh/cmd_parse.py
  2. +1 −1 osh/word_parse.py
  3. +11 −1 test/osh2oil.sh
  4. +10 −8 tools/osh2oil.py
View
@@ -1181,10 +1181,17 @@ def ParseDBracket(self):
def ParseDParen(self):
maybe_error_word = self.cur_word
left_spid = word.LeftMostSpanForWord(self.cur_word)
self._Next() # skip ((
anode = self.w_parser.ReadDParen()
anode, right_spid = self.w_parser.ReadDParen()
assert anode is not None
return ast.DParen(anode)
node = ast.DParen(anode)
node.spids.append(left_spid)
node.spids.append(right_spid)
return node
def ParseCommand(self):
"""
View
@@ -840,7 +840,7 @@ def ReadDParen(self):
self.cur_token.val, token=self.cur_token)
self._Next(lex_mode_e.OUTER)
return anode
return anode, self.cur_token.span_id
def _NextNonSpace(self):
"""Same logic as _ReadWord, but for ReadForExpresion."""
View
@@ -966,6 +966,16 @@ echo $<[echo hi] $>[echo hi]
OIL
}
dparen() {
osh0-oil3 << 'OSH' 3<< 'OIL'
(( n++ ))
echo done
OSH
sh-expr ' n++ '
echo done
OIL
}
arith-sub() {
# NOTE: This probably needs double quotes?
osh0-oil3 << 'OSH' 3<< 'OIL'
@@ -981,7 +991,6 @@ echo $[ 1+ 2 ]
OSH
echo $( 1+ 2 )
OIL
}
arith-ops() {
@@ -1169,6 +1178,7 @@ readonly -a PASSING=(
here-doc
pipeline
and-or
dparen
# Word stuff
escaped-literal
View
@@ -666,15 +666,17 @@ def DoCommand(self, node, local_symbols, at_top_level=False):
self.f.write('}')
elif node.tag == command_e.DParen:
# TODO: (( a == 0 )) is sh-expr ' a == 0 '
# (( a == 0 )) is sh-expr ' a == 0 '
#
# NOTE: I have a (( n++ )) in one script. That can be 'set n++' or
# 'set n += 1'.
#
# Auto-translation is:
#
# sh-expr 'n++'
raise NotImplementedError('DParen')
# NOTE: (( n++ )) is auto-translated to sh-expr 'n++', but could be set
# n++.
left_spid, right_spid = node.spids
self.cursor.PrintUntil(left_spid)
self.cursor.SkipUntil(left_spid + 1)
self.f.write("sh-expr '")
self.cursor.PrintUntil(right_spid - 1) # before ))
self.cursor.SkipUntil(right_spid + 1) # after )) -- each one is a token
self.f.write("'")
elif node.tag == command_e.DBracket:
# [[ 1 -eq 2 ]] to (1 == 2)

0 comments on commit 807f0d9

Please sign in to comment.