Skip to content

Commit

Permalink
Executing grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed Aug 16, 2021
1 parent f90f42c commit 4b06934
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
10 changes: 7 additions & 3 deletions yash-semantics/src/command_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ impl Command for syntax::Command {
use syntax::Command::*;
match self {
Simple(command) => command.execute(env).await,
#[allow(clippy::unit_arg)]
Compound(_) | Function(_) => Ok(println!("{}", self)),
// TODO execute compound command / function definition
Compound(command) => command.execute(env).await,
Function(_) => {
// TODO execute function definition
env.print_error(&format_args!("Not implemented: {}", self))
.await;
Ok(())
}
}
}
}
Expand Down
69 changes: 69 additions & 0 deletions yash-semantics/src/compound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2021 WATANABE Yuki
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Implementation of the compound command semantics.

use super::Command;
use async_trait::async_trait;
use yash_env::exec::Result;
use yash_env::Env;
use yash_syntax::syntax;

#[async_trait(?Send)]
impl Command for syntax::FullCompoundCommand {
async fn execute(&self, env: &mut Env) -> Result {
// TODO Open redirections
self.command.execute(env).await
}
}

#[async_trait(?Send)]
impl Command for syntax::CompoundCommand {
async fn execute(&self, env: &mut Env) -> Result {
use syntax::CompoundCommand::*;
match self {
Grouping(list) => list.execute(env).await,
_ => {
// TODO execute subshell
// TODO execute for loop
// TODO execute while/until loop
// TODO execute case
// TODO execute if
env.print_error(&format_args!("Not implemented: {}", self))
.await;
Ok(())
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::tests::return_builtin;
use futures_executor::block_on;
use yash_env::exec::ExitStatus;

#[test]
fn grouping_executes_list() {
let mut env = Env::new_virtual();
env.builtins.insert("return", return_builtin());
let command: syntax::CompoundCommand = "{ return -n 42; }".parse().unwrap();
let result = block_on(command.execute(&mut env));
assert_eq!(result, Ok(()));
assert_eq!(env.exit_status, ExitStatus(42));
}
}
1 change: 1 addition & 0 deletions yash-semantics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

mod command_impl;
pub mod command_search;
mod compound;
mod pipeline;
mod simple_command;

Expand Down

0 comments on commit 4b06934

Please sign in to comment.