latest pushes
This commit is contained in:
@@ -38,10 +38,8 @@ impl Executor {
|
||||
|
||||
pub fn run(&mut self, args: Vec<String>) -> Result<(), String> {
|
||||
if args.len() > 1 {
|
||||
// Execute script file
|
||||
self.execute_script(&args[1])
|
||||
} else {
|
||||
// Interactive mode
|
||||
self.run_interactive_mode()
|
||||
}
|
||||
}
|
||||
@@ -64,34 +62,13 @@ impl Executor {
|
||||
let line = line.map_err(|e| format!("Error reading line {}: {}", index + 1, e))?;
|
||||
let trimmed_line = line.trim();
|
||||
if trimmed_line.is_empty() || trimmed_line.starts_with('#') {
|
||||
continue; // Skip empty lines and comments
|
||||
}
|
||||
|
||||
// Handle variable assignments and arithmetic operations
|
||||
if trimmed_line.contains('=') {
|
||||
let parts: Vec<&str> = trimmed_line.splitn(2, '=').collect();
|
||||
if parts.len() == 2 {
|
||||
let var_name = parts[0].trim().to_string();
|
||||
let var_value = parts[1].trim().to_string();
|
||||
|
||||
if var_value.starts_with("$((") && var_value.ends_with("))") {
|
||||
// Arithmetic expression
|
||||
let result = self.interpreter.evaluate_arithmetic(&var_value)?;
|
||||
self.interpreter
|
||||
.variables
|
||||
.insert(var_name, result.to_string());
|
||||
} else {
|
||||
// Regular variable assignment
|
||||
let expanded_value = self.interpreter.expand_variables(&var_value);
|
||||
self.interpreter.variables.insert(var_name, expanded_value);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Err(e) = self.process_content(trimmed_line) {
|
||||
return Err(format!("Error on line {}: {}", index + 1, e));
|
||||
eprintln!("Error on line {}: {}", index + 1, e);
|
||||
}
|
||||
io::stdout().flush().unwrap();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -114,28 +91,6 @@ impl Executor {
|
||||
}
|
||||
|
||||
fn process_content(&mut self, content: &str) -> Result<(), String> {
|
||||
// Handle variable assignments and arithmetic operations
|
||||
if content.contains('=') {
|
||||
let parts: Vec<&str> = content.splitn(2, '=').collect();
|
||||
if parts.len() == 2 {
|
||||
let var_name = parts[0].trim().to_string();
|
||||
let var_value = parts[1].trim().to_string();
|
||||
|
||||
if var_value.starts_with("$((") && var_value.ends_with("))") {
|
||||
// Arithmetic expression
|
||||
let result = self.interpreter.evaluate_arithmetic(&var_value)?;
|
||||
self.interpreter
|
||||
.variables
|
||||
.insert(var_name, result.to_string());
|
||||
} else {
|
||||
// Regular variable assignment
|
||||
let expanded_value = self.interpreter.expand_variables(&var_value);
|
||||
self.interpreter.variables.insert(var_name, expanded_value);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
let ast_nodes = self.parse_content(content)?;
|
||||
self.execute(ast_nodes)
|
||||
}
|
||||
@@ -159,6 +114,11 @@ impl Executor {
|
||||
ASTNode::Command { name, args } => Arc::get_mut(&mut self.processes)
|
||||
.unwrap()
|
||||
.execute_command(&mut self.interpreter, name, args),
|
||||
ASTNode::Assignment { name, value } => {
|
||||
let expanded_value = self.interpreter.expand_variables(&value);
|
||||
self.interpreter.variables.insert(name, expanded_value);
|
||||
Ok(None)
|
||||
}
|
||||
ASTNode::Pipeline(commands) => {
|
||||
self.processes.execute_pipeline(&self.interpreter, commands)
|
||||
}
|
||||
|
@@ -45,30 +45,11 @@ impl Processes {
|
||||
.map(|arg| interpreter.expand_variables(arg))
|
||||
.collect();
|
||||
|
||||
// Handle arithmetic assignments
|
||||
if expanded_name.contains('=') {
|
||||
let parts: Vec<&str> = expanded_name.splitn(2, '=').collect();
|
||||
if parts.len() == 2 {
|
||||
let var_name = parts[0].trim().to_string();
|
||||
let var_value = parts[1].trim().to_string();
|
||||
|
||||
if var_value.starts_with("$((") && var_value.ends_with("))") {
|
||||
// Arithmetic expression
|
||||
let result = interpreter.evaluate_arithmetic(&var_value)?;
|
||||
interpreter.variables.insert(var_name, result.to_string());
|
||||
} else {
|
||||
// Regular variable assignment
|
||||
let expanded_value = interpreter.expand_variables(&var_value);
|
||||
interpreter.variables.insert(var_name, expanded_value);
|
||||
}
|
||||
return Ok(Some(0));
|
||||
}
|
||||
}
|
||||
|
||||
match expanded_name.as_str() {
|
||||
"echo" => {
|
||||
let output = expanded_args.join(" ");
|
||||
println!("{}", output);
|
||||
io::stdout().flush().unwrap();
|
||||
Ok(Some(0))
|
||||
}
|
||||
"cd" => {
|
||||
@@ -149,11 +130,7 @@ impl Processes {
|
||||
Ok(Some(0))
|
||||
}
|
||||
_ => {
|
||||
if let Some(func) = interpreter.functions.get(&expanded_name) {
|
||||
return interpreter.interpret_node(Box::new(func.clone()));
|
||||
}
|
||||
|
||||
// If it's not a built-in command or variable assignment, try to execute as external command
|
||||
// If it's not a built-in command, try to execute as external command
|
||||
match Command::new(&expanded_name).args(&expanded_args).spawn() {
|
||||
Ok(mut child) => {
|
||||
let status = child.wait().map_err(|e| e.to_string())?;
|
||||
|
@@ -222,7 +222,7 @@ impl Interpreter {
|
||||
let a = self.get_var_value(tokens[0])?;
|
||||
let b = self.get_var_value(tokens[2])?;
|
||||
|
||||
match tokens[1] {
|
||||
let result = match tokens[1] {
|
||||
"+" => Ok(a + b),
|
||||
"-" => Ok(a - b),
|
||||
"*" => Ok(a * b),
|
||||
@@ -241,7 +241,9 @@ impl Interpreter {
|
||||
}
|
||||
}
|
||||
_ => Err(format!("Unsupported operation: {}", tokens[1])),
|
||||
}
|
||||
};
|
||||
|
||||
result
|
||||
} else {
|
||||
Err("Invalid arithmetic expression".to_string())
|
||||
}
|
||||
|
Reference in New Issue
Block a user