Permalink
Browse files

[osh2oil] Change the translation of 'case' to Oil's 'match with'.

- Also, add translation tests for pattern matching.
- Fix some cases about the argv array.  Probably need another pass.
  • Loading branch information...
Andy Chu
Andy Chu committed Aug 26, 2018
1 parent 5683495 commit b2cfe6729c954ee3f9d372b36cae611f67ff9256
Showing with 61 additions and 19 deletions.
  1. +57 −11 test/osh2oil.sh
  2. +4 −8 tools/osh2oil.py
View
@@ -80,15 +80,22 @@ OIL
arg-array() {
# Only "$@" goes to @Argv
# "__$@__" goes "$join(Argv)__"
# "__$@__" should be
#
# "__$Argv[0]" @Argv[1:-1] "$Argv[-1]__"
#
# But this is probably too rare to really happen. Just remove it.
# Yeah the rest go to $join(Argv)
# does join respect IFS though? Have to work that out.
# or maybe $ifsjoin(Argv) -- make explicit the global variable.
# NOTE: This is with autosplit? What about without splitting?
osh0-oil3 << 'OSH' 3<< 'OIL'
echo $@ $* "$@" "$*" "__$@__" "__$*__"
OSH
echo $Status $len(Argv) @Argv
echo $ifsjoin(Argv) $ifsjoin(Argv) @Argv "$ifsjoin(Argv)" "__$ifsjoin(Argv)__" "__$ifsjoin(Argv)__"
OIL
}
@@ -791,6 +798,8 @@ if true {
OIL
}
# TODO: This should match $foo with ...
case_() {
osh0-oil3 << 'OSH' 3<< 'OIL'
case $var in
@@ -802,13 +811,13 @@ case $var in
;;
esac
OSH
matchstr $var {
foo|bar {
match $var {
with foo|bar
test -f foo && echo file
}
* {
with *
echo default
}
}
OIL
@@ -819,11 +828,10 @@ case "$var" in
echo bar # no dsemi
esac
OSH
matchstr $var {
* {
match $var {
with *
echo foo
echo bar # no dsemi
}
}
OIL
}
@@ -962,7 +970,45 @@ echo $(( a << 1 .| b .& 1 ))
OIL
}
# newerThan, olderThan, etc.
dbracket-pattern() {
osh0-oil3 << 'OSH' 3<< 'OIL'
if [[ $foo == *.py ]]; then
echo Python
fi
OSH
if (foo ~ *.py) {
echo Python
}
OIL
# Negated match
osh0-oil3 << 'OSH' 3<< 'OIL'
if [[ $foo != *.py ]]; then
echo Python
fi
OSH
if (foo !~ *.py) {
echo Python
}
OIL
osh0-oil3 << 'OSH' 3<< 'OIL'
regex='.*\.py'
if [[ $foo =~ $regex ]]; then
echo Python
fi
OSH
setglobal regex = '.*\.py'
if (foo ~ ERE/$regex/) {
echo Python
}
OIL
}
# Honestly test -dir / is OK?
#
# What about newerThan, olderThan, etc.
dbracket() {
osh0-oil3 << 'OSH' 3<< 'OIL'
[[ -d / ]] && echo "is dir"
View
@@ -739,7 +739,7 @@ def DoCommand(self, node, local_symbols, at_top_level=False):
case_spid, in_spid, esac_spid = node.spids
self.cursor.PrintUntil(case_spid)
self.cursor.SkipUntil(case_spid + 1)
self.f.write('matchstr')
self.f.write('match')
# Reformat "$1" to $1
self.DoWordInCommand(node.to_match, local_symbols)
@@ -768,24 +768,20 @@ def DoCommand(self, node, local_symbols, at_top_level=False):
for pat in arm.pat_list:
pass
# Skip this
self.f.write('with ')
# Remove the )
self.cursor.PrintUntil(rparen_spid)
self.cursor.SkipUntil(rparen_spid + 1)
self.f.write(' {') # surround it with { }
for child in arm.action:
self.DoCommand(child, local_symbols)
if dsemi_spid != const.NO_INTEGER:
# Remove ;;
self.cursor.PrintUntil(dsemi_spid)
self.cursor.SkipUntil(dsemi_spid + 1)
# NOTE: indentation here will be off because ;; is likely indented
# with body.
self.f.write('}')
elif last_spid != const.NO_INTEGER:
self.cursor.PrintUntil(last_spid)
# NOTE: Indentation is also off here. Arbitrarily put 4 spaces.
self.f.write(' }\n')
else:
raise AssertionError(
"Expected with dsemi_spid or last_spid in case arm")

0 comments on commit b2cfe67

Please sign in to comment.