Skip to content

Commit

Permalink
[osh-language] Make 'eval break', 'source return.sh', etc. work
Browse files Browse the repository at this point in the history
Addresses issue #774.

Also fix unit test and lint error.
  • Loading branch information
Andy Chu committed Jun 12, 2020
1 parent f5e19a2 commit f32d424
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
7 changes: 5 additions & 2 deletions osh/builtin_meta.py
Expand Up @@ -15,6 +15,7 @@
from frontend import flag_spec
from frontend import consts
from frontend import reader
from osh import cmd_eval

from typing import Dict, List, Tuple, Optional, TYPE_CHECKING
if TYPE_CHECKING:
Expand Down Expand Up @@ -58,7 +59,8 @@ def Run(self, cmd_val):
src = source.EvalArg(eval_spid)
self.arena.PushSource(src)
try:
return main_loop.Batch(self.cmd_ev, c_parser, self.arena)
return main_loop.Batch(self.cmd_ev, c_parser, self.arena,
cmd_flags=cmd_eval.IsEvalSource)
finally:
self.arena.PopSource()

Expand Down Expand Up @@ -111,7 +113,8 @@ def Run(self, cmd_val):
src = source.SourcedFile(path, call_spid)
self.arena.PushSource(src)
try:
status = main_loop.Batch(self.cmd_ev, c_parser, self.arena)
status = main_loop.Batch(self.cmd_ev, c_parser, self.arena,
cmd_flags=cmd_eval.IsEvalSource)
finally:
self.arena.PopSource()
self.mem.PopSource(source_argv)
Expand Down
46 changes: 26 additions & 20 deletions osh/cmd_eval.py
Expand Up @@ -102,9 +102,12 @@
from osh import word_eval
from osh import builtin_process

# flags for main_loop.Batch, ExecuteAndCatch
IsMainProgram = 1 << 0 # the main shell program, not eval/source
Optimize = 1 << 1
# flags for main_loop.Batch, ExecuteAndCatch. TODO: Should probably in
# ExecuteAndCatch, along with SetVar() flags.
IsMainProgram = 1 << 0 # the main shell program, not eval/source/subshell
IsEvalSource = 1 << 1 # eval/source builtins
Optimize = 1 << 2



# Python type name -> Oil type name
Expand Down Expand Up @@ -1457,24 +1460,27 @@ def ExecuteAndCatch(self, node, cmd_flags=0):
try:
status = self._Execute(node)
except _ControlFlow as e:
# Return at top level is OK, unlike in bash.
if e.IsReturn():
is_return = True
status = e.StatusCode()
if cmd_flags & IsEvalSource:
raise # 'eval break' and 'source return.sh', etc.
else:
#raise # break and continue in eval
is_eval = False
# TODO: This error message is invalid. Can also happen in eval.
# We need a flag.

# Invalid control flow
self.errfmt.Print_(
"Loop and control flow can't be in different processes",
span_id=e.token.span_id)
is_fatal = True
# All shells exit 0 here. It could be hidden behind
# strict-control-flow if the incompatibility causes problems.
status = 1
# Return at top level is OK, unlike in bash.
if e.IsReturn():
is_return = True
status = e.StatusCode()
else:
#raise # break and continue in eval
is_eval = False
# TODO: This error message is invalid. Can also happen in eval.
# We need a flag.

# Invalid control flow
self.errfmt.Print_(
"Loop and control flow can't be in different processes",
span_id=e.token.span_id)
is_fatal = True
# All shells exit 0 here. It could be hidden behind
# strict-control-flow if the incompatibility causes problems.
status = 1
except error.Parse as e:
self.dumper.MaybeCollect(self, e) # Do this before unwinding stack
raise
Expand Down
4 changes: 2 additions & 2 deletions osh/string_ops_test.py
Expand Up @@ -62,12 +62,12 @@ def test_PreviousUtf8Char(self):
]
for expected_indexes, input_str in CASES:
print()
print('_PreviousUtf8Char case %r %r' % (expected_indexes, input_str))
print('PreviousUtf8Char case %r %r' % (expected_indexes, input_str))
i = len(input_str)
actual_indexes = []
while True:
try:
i = string_ops._PreviousUtf8Char(input_str, i)
i = string_ops.PreviousUtf8Char(input_str, i)
actual_indexes.append(i)
if i == 0:
break
Expand Down
2 changes: 1 addition & 1 deletion osh/word_eval.py
Expand Up @@ -31,7 +31,7 @@
from core import passwd
from core import pyutil
from qsn_ import qsn
from core.util import log, e_die, e_strict
from core.util import log, e_die
from frontend import consts
from frontend import match
from mycpp.mylib import tagswitch
Expand Down

0 comments on commit f32d424

Please sign in to comment.