1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:28:11 +00:00

Shell: Add runtime errors and implement break/continue

Such errors are raised when SyntaxError nodes are executed, and are also
used for internal control flow.
The 'break' and 'continue' commands are currently only allowed inside
for loops, and outside function bodies.

This also adds a 'loop' keyword for infinite loops.
This commit is contained in:
AnotherTest 2020-12-10 18:25:13 +03:30 committed by Andreas Kling
parent 9bd81f34a5
commit 5e5eb615ec
14 changed files with 384 additions and 62 deletions

View file

@ -554,6 +554,8 @@ int Shell::run_command(const StringView& cmd)
// should not be used for execution!
ASSERT(!m_default_constructed);
take_error();
if (cmd.is_empty())
return 0;
@ -891,6 +893,10 @@ RefPtr<Job> Shell::run_command(const AST::Command& command)
void Shell::run_tail(const AST::Command& invoking_command, const AST::NodeWithAction& next_in_chain, int head_exit_code)
{
if (m_error != ShellError::None) {
possibly_print_error();
return;
}
auto evaluate = [&] {
if (next_in_chain.node->would_execute()) {
next_in_chain.node->run(*this);
@ -929,6 +935,11 @@ void Shell::run_tail(RefPtr<Job> job)
NonnullRefPtrVector<Job> Shell::run_commands(Vector<AST::Command>& commands)
{
if (m_error != ShellError::None) {
possibly_print_error();
return {};
}
NonnullRefPtrVector<Job> spawned_jobs;
for (auto& command : commands) {
@ -1722,6 +1733,24 @@ void Shell::save_to(JsonObject& object)
object.set("jobs", move(job_objects));
}
void Shell::possibly_print_error() const
{
switch (m_error) {
case ShellError::EvaluatedSyntaxError:
warnln("Shell Syntax Error: {}", m_error_description);
return;
case ShellError::InvalidGlobError:
case ShellError::NonExhaustiveMatchRules:
warnln("Shell: {}", m_error_description);
return;
case ShellError::InternalControlFlowBreak:
case ShellError::InternalControlFlowContinue:
return;
case ShellError::None:
return;
}
}
void FileDescriptionCollector::collect()
{
for (auto fd : m_fds)