latest pushes
This commit is contained in:
parent
482ffe0604
commit
b0a2610d70
@ -5,41 +5,41 @@
|
||||
|
||||
# Addition
|
||||
result=$((5 + 3))
|
||||
echo Addition: 5 + 3 = $result
|
||||
echo Addition: "5 + 3 =" $result
|
||||
|
||||
# Subtraction
|
||||
result=$((10 - 4))
|
||||
echo Subtraction: 10 - 4 = $result
|
||||
echo Subtraction: "10 - 4 =" $result
|
||||
|
||||
# Multiplication
|
||||
result=$((6 * 7))
|
||||
echo Multiplication: 6 * 7 = $result
|
||||
echo Multiplication: "6 * 7 =" $result
|
||||
|
||||
# Division
|
||||
result=$((20 / 4))
|
||||
echo Division: 20 / 4 = $result
|
||||
echo Division: "20 / 4 =" $result
|
||||
|
||||
# Modulus
|
||||
result=$((17 % 5))
|
||||
echo Modulus: 17 % 5 = $result
|
||||
echo Modulus: "17 % 5 =" $result
|
||||
|
||||
# Compound operation
|
||||
result=$(( (10 + 5) * 2 ))
|
||||
echo Compound: (10 + 5) * 2 = $result
|
||||
echo Compound: "(10 + 5) * 2 =" $result
|
||||
|
||||
# Using variables
|
||||
a=7
|
||||
b=3
|
||||
result=$((a + b))
|
||||
echo Variables: $a + $b = $result
|
||||
echo Variables: "$a + $b =" $result
|
||||
|
||||
# Increment
|
||||
count=0
|
||||
count=$((count + 1))
|
||||
echo Increment: count after increment = $count
|
||||
echo Increment: count after increment "=" $count
|
||||
|
||||
# Decrement
|
||||
count=$((count - 1))
|
||||
echo Decrement: count after decrement = $count
|
||||
echo Decrement: count after decrement "=" $count
|
||||
|
||||
echo Basic math operations completed.
|
||||
|
@ -5,27 +5,27 @@
|
||||
|
||||
# Create a test file
|
||||
echo "Creating test file..."
|
||||
echo "Hello, World!" > test.txt
|
||||
write test.txt "Hello, World!"
|
||||
|
||||
# Read the contents of the file
|
||||
echo "\nReading test file:"
|
||||
cat test.txt
|
||||
read test.txt
|
||||
|
||||
# Append to the file
|
||||
echo "\nAppending to test file..."
|
||||
echo "This is a new line" >> test.txt
|
||||
append test.txt "This is a new line"
|
||||
|
||||
# Read the updated contents
|
||||
echo "\nReading updated test file:"
|
||||
cat test.txt
|
||||
read test.txt
|
||||
|
||||
# Write to a new file
|
||||
echo "\nWriting to a new file..."
|
||||
echo "This is a new file" > new_file.txt
|
||||
write new_file.txt "This is a new file"
|
||||
|
||||
# Read the new file
|
||||
echo "\nReading new file:"
|
||||
cat new_file.txt
|
||||
read new_file.txt
|
||||
|
||||
# List files in the current directory
|
||||
echo "\nListing files in the current directory:"
|
||||
@ -35,26 +35,6 @@ ls -l
|
||||
echo "\nRenaming file..."
|
||||
mv new_file.txt renamed_file.txt
|
||||
|
||||
# Check if file exists
|
||||
echo "\nChecking if files exist:"
|
||||
if [ -f "test.txt" ]; then
|
||||
echo "test.txt exists"
|
||||
else
|
||||
echo "test.txt does not exist"
|
||||
fi
|
||||
|
||||
if [ -f "new_file.txt" ]; then
|
||||
echo "new_file.txt exists"
|
||||
else
|
||||
echo "new_file.txt does not exist"
|
||||
fi
|
||||
|
||||
if [ -f "renamed_file.txt" ]; then
|
||||
echo "renamed_file.txt exists"
|
||||
else
|
||||
echo "renamed_file.txt does not exist"
|
||||
fi
|
||||
|
||||
# Delete files
|
||||
echo "\nDeleting files..."
|
||||
rm test.txt renamed_file.txt
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
# Simple function
|
||||
function greet() {
|
||||
echo "Hello, $1!"
|
||||
echo Hello
|
||||
}
|
||||
|
||||
echo "Testing simple function:"
|
||||
|
@ -6,31 +6,32 @@
|
||||
# String concatenation
|
||||
first_name=John
|
||||
last_name=Doe
|
||||
full_name=$first_name $last_name
|
||||
full_name="$first_name $last_name"
|
||||
echo Full name: $full_name
|
||||
|
||||
# String length
|
||||
string="Hello, World!"
|
||||
echo "The string '$string' has ${#string} characters."
|
||||
echo The string '$string' has ${#string} characters.
|
||||
|
||||
# Substring extraction
|
||||
echo "The first 5 characters are: ${string:0:5}"
|
||||
echo The first 5 characters are: ${string:0:5}
|
||||
|
||||
# String replacement
|
||||
sentence="The quick brown fox jumps over the lazy dog"
|
||||
echo "Original sentence: $sentence"
|
||||
echo Original sentence: $sentence
|
||||
new_sentence=${sentence/fox/cat}
|
||||
echo "Modified sentence: $new_sentence"
|
||||
echo Modified sentence: $new_sentence
|
||||
|
||||
# Converting to uppercase and lowercase
|
||||
echo "Uppercase: ${string^^}"
|
||||
echo "Lowercase: ${string,,}"
|
||||
echo Uppercase: ${string^^}
|
||||
echo Lowercase: ${string,,}
|
||||
|
||||
# Trimming whitespace
|
||||
padded_string=" trim me "
|
||||
echo "Original string: '$padded_string'"
|
||||
trimmed_string=${padded_string## }
|
||||
trimmed_string=${trimmed_string%% }
|
||||
echo "Trimmed string: '$trimmed_string'"
|
||||
echo Original string: '$padded_string'
|
||||
trimmed_string="${padded_string#"${padded_string%%[![:space:]]*}"}" # Trim leading whitespace
|
||||
trimmed_string="${trimmed_string%"${trimmed_string##*[![:space:]]}"}" # Trim trailing whitespace
|
||||
echo Trimmed string: '$trimmed_string'
|
||||
|
||||
echo "String manipulation operations completed."
|
||||
# Completion message
|
||||
echo String manipulation operations completed.
|
||||
|
Binary file not shown.
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user