This commit is contained in:
Ronaldson Bellande 2024-10-04 00:17:04 -04:00
parent 3ecd1e2b40
commit 17522200f7
3 changed files with 43 additions and 14 deletions

View File

@ -11,6 +11,9 @@
- **Background Jobs**: Run commands in the background. - **Background Jobs**: Run commands in the background.
- **Environment Variable Handling**: Access and modify environment variables. - **Environment Variable Handling**: Access and modify environment variables.
# Bellos Installer
- https://github.com/Architecture-Mechanism/bellos_installer
# Usage of Bellande Rust Executable Builder # Usage of Bellande Rust Executable Builder
- https://github.com/Architecture-Mechanism/bellande_rust_executable - https://github.com/Architecture-Mechanism/bellande_rust_executable
- ```bellande_rust_executable -d dependencies.txt -s src -m bellos.rs -o executable/bellos``` - ```bellande_rust_executable -d dependencies.txt -s src -m bellos.rs -o executable/bellos```

Binary file not shown.

View File

@ -14,10 +14,13 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::interpreter::interpreter::Interpreter; use crate::interpreter::interpreter::Interpreter;
use crate::lexer::lexer::Lexer;
use crate::parser::parser::Parser;
use crate::utilities::utilities::{ASTNode, RedirectType}; use crate::utilities::utilities::{ASTNode, RedirectType};
use glob::glob; use glob::glob;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::{self, BufReader, BufWriter, Cursor, Read, Write}; use std::io::{self, BufReader, BufWriter, Cursor, Read, Write};
use std::path::Path;
use std::process::{Child, Command, Stdio}; use std::process::{Child, Command, Stdio};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::thread; use std::thread;
@ -83,6 +86,35 @@ impl Processes {
if let Some(func) = interpreter.functions.get(&expanded_name) { if let Some(func) = interpreter.functions.get(&expanded_name) {
return interpreter.interpret_node(Box::new(func.clone())); return interpreter.interpret_node(Box::new(func.clone()));
} }
// Check if the command is a .bellos script
if expanded_name.ends_with(".bellos") {
let path = Path::new(&expanded_name);
if path.exists() {
// Read the script file
let mut file = File::open(path)
.map_err(|e| format!("Failed to open script file: {}", e))?;
let mut content = String::new();
file.read_to_string(&mut content)
.map_err(|e| format!("Failed to read script file: {}", e))?;
// Parse and execute the script content
let mut lexer = Lexer::new(content);
let tokens = lexer.tokenize();
let mut parser = Parser::new(tokens);
let ast = parser
.parse()
.map_err(|e| format!("Failed to parse script: {}", e))?;
for node in ast {
interpreter.interpret_node(Box::new(node))?;
}
return Ok(Some(0));
}
}
// If it's not a .bellos script, try to execute it as a system command
match Command::new(&expanded_name).args(&expanded_args).spawn() { match Command::new(&expanded_name).args(&expanded_args).spawn() {
Ok(mut child) => { Ok(mut child) => {
let status = child.wait().map_err(|e| e.to_string())?; let status = child.wait().map_err(|e| e.to_string())?;
@ -211,26 +243,13 @@ impl Processes {
pub fn execute_background(&self, node: ASTNode) -> Result<Option<i32>, String> { pub fn execute_background(&self, node: ASTNode) -> Result<Option<i32>, String> {
let bg_jobs = Arc::clone(&self.background_jobs); let bg_jobs = Arc::clone(&self.background_jobs);
// Create a new background process
let child = Arc::new(Mutex::new(
Command::new(std::env::current_exe().expect("Failed to get current executable path"))
.arg("--execute-bellos-script")
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.map_err(|e| format!("Failed to spawn background process: {}", e))?,
));
// Add the new job to the list
bg_jobs.lock().unwrap().push(Arc::clone(&child));
thread::spawn(move || { thread::spawn(move || {
let mut interpreter = Interpreter::new(); let mut interpreter = Interpreter::new();
if let Err(e) = interpreter.interpret_node(Box::new(node)) { if let Err(e) = interpreter.interpret_node(Box::new(node)) {
eprintln!("Background job error: {}", e); eprintln!("Background job error: {}", e);
} }
// Remove completed jobs from bg_jobs
let mut jobs = bg_jobs.lock().unwrap(); let mut jobs = bg_jobs.lock().unwrap();
jobs.retain(|job| { jobs.retain(|job| {
let mut child = job.lock().unwrap(); let mut child = job.lock().unwrap();
@ -251,6 +270,13 @@ impl Processes {
}); });
}); });
// Add a placeholder Child process to the background_jobs list
let placeholder =
Arc::new(Mutex::new(Command::new("sleep").arg("1").spawn().map_err(
|e| format!("Failed to create placeholder process: {}", e),
)?));
self.background_jobs.lock().unwrap().push(placeholder);
Ok(None) Ok(None)
} }