Skip to content

Commit

Permalink
Add if.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 7, 2024
1 parent 8c07f0a commit a4a8ed6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/elements/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ pub mod simple;
pub mod paren;
pub mod brace;
pub mod r#while;
pub mod r#if;

use crate::{ShellCore, Feeder, Script};
use self::simple::SimpleCommand;
use self::paren::ParenCommand;
use self::brace::BraceCommand;
use self::r#while::WhileCommand;
use self::r#if::IfCommand;
use std::fmt;
use std::fmt::Debug;
use super::{io, Pipe};
Expand Down Expand Up @@ -99,5 +101,6 @@ pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Box<dyn Comman
else if let Some(a) = ParenCommand::parse(feeder, core) { Some(Box::new(a)) }
else if let Some(a) = BraceCommand::parse(feeder, core) { Some(Box::new(a)) }
else if let Some(a) = WhileCommand::parse(feeder, core) { Some(Box::new(a)) }
else if let Some(a) = IfCommand::parse(feeder, core) { Some(Box::new(a)) }
else{ None }
}
28 changes: 28 additions & 0 deletions src/elements/command/if.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//SPDX-FileCopyrightText: 2023 Ryuichi Ueda <ryuichiueda@gmail.com>
//SPDX-License-Identifier: BSD-3-Clause

use crate::{ShellCore, Feeder, Script};
use nix::unistd::Pid;
use super::{Command, Pipe, Redirect};

#[derive(Debug)]
pub struct IfCommand {
pub text: String,
pub if_elif_scripts: Vec<Script>,
pub then_scripts: Vec<Script>,
pub else_script: Option<Script>,
pub redirects: Vec<Redirect>,
force_fork: bool,
}

impl Command for IfCommand {
fn exec(&mut self, _: &mut ShellCore, _: &mut Pipe) -> Option<Pid> {None}
fn run_command(&mut self, _: &mut ShellCore, _: bool) {}
fn get_text(&self) -> String { self.text.clone() }
fn get_redirects(&mut self) -> &mut Vec<Redirect> { &mut self.redirects }
fn set_force_fork(&mut self) { self.force_fork = true; }
}

impl IfCommand {
pub fn parse(_: &mut Feeder, _: &mut ShellCore) -> Option<IfCommand> {None}
}
2 changes: 1 addition & 1 deletion src/elements/command/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use nix::errno::Errno;

fn reserved(w: &str) -> bool {
match w {
"{" | "}" | "while" | "do" | "done" => true,
"{" | "}" | "while" | "do" | "done" | "if" | "then" | "elif" | "else" | "fi" => true,
_ => false,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/elements/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Script {
( None, _) => {},
}

let ng_ends = vec![")", "}", "then", "else", "fi", "elif", "do", "done", "while", "||", "&&", "|", "&"];
let ng_ends = vec![")", "}", "then", "else", "if", "fi", "elif", "do", "done", "while", "||", "&&", "|", "&"];
match ( ng_ends.iter().find(|e| feeder.starts_with(e)), nest.1.len() ) {
(Some(end), _) => return Status::UnexpectedSymbol(end.to_string()),
(None, 0) => return Status::NormalEnd,
Expand Down

0 comments on commit a4a8ed6

Please sign in to comment.