Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 8, 2024
1 parent aa8a590 commit 57090aa
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/elements/command/if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,36 @@ impl IfCommand {
}
}

fn eat_script(word: &str, feeder: &mut Feeder, ans: &mut IfCommand, core: &mut ShellCore) -> bool {
let mut s = None;
if command::eat_inner_script(feeder, core, word, Self::end_words(word), &mut s) {
ans.text.push_str(word);
ans.text.push_str(&s.as_mut().unwrap().get_text());

match word {
"if" | "elif" => ans.if_elif_scripts.push(s.unwrap()),
"then" => ans.then_scripts.push(s.unwrap()),
"else" => ans.else_script = s,
_ => panic!("SUSH INTERNAL ERROR (if parse error)"),
};
fn set_script(word: &str, ans: &mut IfCommand, script: Option<Script>) {
match word {
"if" | "elif" => ans.if_elif_scripts.push(script.unwrap()),
"then" => ans.then_scripts.push(script.unwrap()),
"else" => ans.else_script = script,
_ => panic!("SUSH INTERNAL ERROR (if parse error)"),
};
}

true
}else{
false
fn eat_conditioned_script(word: &str, feeder: &mut Feeder,
ans: &mut IfCommand, core: &mut ShellCore) -> bool {
let mut s = None;
if ! command::eat_inner_script(feeder, core, word, Self::end_words(word), &mut s) {
return false;
}

ans.text.push_str(word);
ans.text.push_str(&s.as_mut().unwrap().get_text());
Self::set_script(word, ans, s);
true
}

pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<IfCommand> {
let mut ans = Self::new();

let mut if_or_elif = "if";
while Self::eat_script(if_or_elif, feeder, &mut ans, core)
&& Self::eat_script("then", feeder, &mut ans, core) {
while Self::eat_conditioned_script(if_or_elif, feeder, &mut ans, core)
&& Self::eat_conditioned_script("then", feeder, &mut ans, core) {

Self::eat_script("else", feeder, &mut ans, core); //optional
Self::eat_conditioned_script("else", feeder, &mut ans, core); //optional

if feeder.starts_with("fi") { // If "else" exists, always it comes here.
ans.text.push_str(&feeder.consume(2));
Expand Down

0 comments on commit 57090aa

Please sign in to comment.