@@ -0,0 +1,161 @@ |
| | 1 | +use rush_expand::Context; |
| | 2 | +use rush_parser::{CaseStatement, CompleteCommand, ForStatement, IfStatement, WhileStatement}; |
| | 3 | +use crate::{ExecutionResult, PipelineError}; |
| | 4 | + |
| | 5 | +/// Execute an if statement |
| | 6 | +pub fn execute_if( |
| | 7 | + if_stmt: &IfStatement, |
| | 8 | + context: &mut Context, |
| | 9 | +) -> Result<ExecutionResult, PipelineError> { |
| | 10 | + // Execute the condition and check if it succeeded |
| | 11 | + let condition_result = execute_complete_command(&if_stmt.condition, context)?; |
| | 12 | + |
| | 13 | + if condition_result.success() { |
| | 14 | + // Condition was true, execute then body |
| | 15 | + execute_command_list(&if_stmt.then_body, context) |
| | 16 | + } else { |
| | 17 | + // Check elif clauses |
| | 18 | + for elif in &if_stmt.elif_clauses { |
| | 19 | + let elif_result = execute_complete_command(&elif.condition, context)?; |
| | 20 | + if elif_result.success() { |
| | 21 | + return execute_command_list(&elif.then_body, context); |
| | 22 | + } |
| | 23 | + } |
| | 24 | + |
| | 25 | + // No elif matched, execute else body if present |
| | 26 | + if let Some(else_body) = &if_stmt.else_body { |
| | 27 | + execute_command_list(else_body, context) |
| | 28 | + } else { |
| | 29 | + // No else clause, return success (standard shell behavior) |
| | 30 | + Ok(crate::command::success_result()) |
| | 31 | + } |
| | 32 | + } |
| | 33 | +} |
| | 34 | + |
| | 35 | +/// Execute a while loop |
| | 36 | +pub fn execute_while( |
| | 37 | + while_stmt: &WhileStatement, |
| | 38 | + context: &mut Context, |
| | 39 | +) -> Result<ExecutionResult, PipelineError> { |
| | 40 | + let mut last_result = crate::command::success_result(); |
| | 41 | + |
| | 42 | + loop { |
| | 43 | + // Execute the condition |
| | 44 | + let condition_result = execute_complete_command(&while_stmt.condition, context)?; |
| | 45 | + |
| | 46 | + if !condition_result.success() { |
| | 47 | + // Condition failed, exit loop |
| | 48 | + break; |
| | 49 | + } |
| | 50 | + |
| | 51 | + // Execute the loop body |
| | 52 | + last_result = execute_command_list(&while_stmt.body, context)?; |
| | 53 | + } |
| | 54 | + |
| | 55 | + Ok(last_result) |
| | 56 | +} |
| | 57 | + |
| | 58 | +/// Execute a for loop |
| | 59 | +pub fn execute_for( |
| | 60 | + for_stmt: &ForStatement, |
| | 61 | + context: &mut Context, |
| | 62 | +) -> Result<ExecutionResult, PipelineError> { |
| | 63 | + use rush_expand::expand_word; |
| | 64 | + |
| | 65 | + let mut last_result = crate::command::success_result(); |
| | 66 | + |
| | 67 | + // Expand all the words in the list |
| | 68 | + let mut values = Vec::new(); |
| | 69 | + for word in &for_stmt.words { |
| | 70 | + let expanded = expand_word(word, context) |
| | 71 | + .map_err(|e| PipelineError::ExpansionError(e.to_string()))?; |
| | 72 | + values.push(expanded); |
| | 73 | + } |
| | 74 | + |
| | 75 | + // Execute the loop body for each value |
| | 76 | + for value in values { |
| | 77 | + // Set the loop variable |
| | 78 | + context.set_var(&for_stmt.var_name, &value); |
| | 79 | + |
| | 80 | + // Execute the loop body |
| | 81 | + last_result = execute_command_list(&for_stmt.body, context)?; |
| | 82 | + } |
| | 83 | + |
| | 84 | + Ok(last_result) |
| | 85 | +} |
| | 86 | + |
| | 87 | +/// Execute a case statement |
| | 88 | +pub fn execute_case( |
| | 89 | + case_stmt: &CaseStatement, |
| | 90 | + context: &mut Context, |
| | 91 | +) -> Result<ExecutionResult, PipelineError> { |
| | 92 | + use rush_expand::expand_word; |
| | 93 | + |
| | 94 | + // Expand the word to match against |
| | 95 | + let word_value = expand_word(&case_stmt.word, context) |
| | 96 | + .map_err(|e| PipelineError::ExpansionError(e.to_string()))?; |
| | 97 | + |
| | 98 | + // Try each case clause |
| | 99 | + for clause in &case_stmt.clauses { |
| | 100 | + // Check if any pattern matches |
| | 101 | + for pattern in &clause.patterns { |
| | 102 | + let pattern_value = expand_word(pattern, context) |
| | 103 | + .map_err(|e| PipelineError::ExpansionError(e.to_string()))?; |
| | 104 | + |
| | 105 | + // TODO: Implement glob pattern matching |
| | 106 | + // For now, just do exact string matching |
| | 107 | + if word_value == pattern_value { |
| | 108 | + // Pattern matched, execute this clause's commands |
| | 109 | + return execute_command_list(&clause.body, context); |
| | 110 | + } |
| | 111 | + } |
| | 112 | + } |
| | 113 | + |
| | 114 | + // No clause matched, return success (standard shell behavior) |
| | 115 | + Ok(crate::command::success_result()) |
| | 116 | +} |
| | 117 | + |
| | 118 | +/// Execute a complete command (helper for recursive execution) |
| | 119 | +fn execute_complete_command( |
| | 120 | + cmd: &CompleteCommand, |
| | 121 | + context: &mut Context, |
| | 122 | +) -> Result<ExecutionResult, PipelineError> { |
| | 123 | + match cmd { |
| | 124 | + CompleteCommand::Simple(simple_cmd) => { |
| | 125 | + Ok(crate::execute_simple_with_redirects(simple_cmd, context, false)?) |
| | 126 | + } |
| | 127 | + CompleteCommand::Pipeline(pipeline) => { |
| | 128 | + crate::execute_pipeline(pipeline, context) |
| | 129 | + } |
| | 130 | + CompleteCommand::AndOrList(and_or_list) => { |
| | 131 | + crate::execute_and_or_list(and_or_list, context) |
| | 132 | + } |
| | 133 | + CompleteCommand::If(if_stmt) => { |
| | 134 | + execute_if(if_stmt, context) |
| | 135 | + } |
| | 136 | + CompleteCommand::While(while_stmt) => { |
| | 137 | + execute_while(while_stmt, context) |
| | 138 | + } |
| | 139 | + CompleteCommand::For(for_stmt) => { |
| | 140 | + execute_for(for_stmt, context) |
| | 141 | + } |
| | 142 | + CompleteCommand::Case(case_stmt) => { |
| | 143 | + execute_case(case_stmt, context) |
| | 144 | + } |
| | 145 | + } |
| | 146 | +} |
| | 147 | + |
| | 148 | +/// Execute a list of commands |
| | 149 | +fn execute_command_list( |
| | 150 | + commands: &[CompleteCommand], |
| | 151 | + context: &mut Context, |
| | 152 | +) -> Result<ExecutionResult, PipelineError> { |
| | 153 | + let mut last_result = crate::command::success_result(); |
| | 154 | + |
| | 155 | + for cmd in commands { |
| | 156 | + last_result = execute_complete_command(cmd, context)?; |
| | 157 | + context.set_exit_status(last_result.exit_code()); |
| | 158 | + } |
| | 159 | + |
| | 160 | + Ok(last_result) |
| | 161 | +} |