use crate::error::error::BellronosError;
use crate::interpreter::interpreter::Value;
use std::process::Command;
pub struct LanguageInterop;
impl LanguageInterop {
pub fn new() -> Self {
LanguageInterop
}
pub fn execute(&self, language: &str, code: &str) -> Result<Value, BellronosError> {
match language {
"c" => self.execute_c(code),
"python" => self.execute_python(code),
"javascript" => self.execute_javascript(code),
"java" => self.execute_java(code),
"rust" => self.execute_rust(code),
"swift" => self.execute_swift(code),
_ => Err(BellronosError::Runtime(format!(
"Unsupported language: {}",
language
))),
}
}
fn execute_c(&self, code: &str) -> Result<Value, BellronosError> {
let temp_file = "temp.c";
std::fs::write(temp_file, code)?;
let output = Command::new("gcc")
.args(&[temp_file, "-o", "temp_c"])
.output()?;
if !output.status.success() {
return Err(BellronosError::Runtime(format!(
"C compilation error: {}",
String::from_utf8_lossy(&output.stderr)
)));
}
let output = Command::new("./temp_c").output()?;
std::fs::remove_file(temp_file)?;
std::fs::remove_file("temp_c")?;
Ok(Value::String(
String::from_utf8_lossy(&output.stdout).to_string(),
))
}
fn execute_python(&self, code: &str) -> Result<Value, BellronosError> {
let output = Command::new("python").arg("-c").arg(code).output()?;
Ok(Value::String(
String::from_utf8_lossy(&output.stdout).to_string(),
))
}
fn execute_javascript(&self, code: &str) -> Result<Value, BellronosError> {
let output = Command::new("node").arg("-e").arg(code).output()?;
Ok(Value::String(
String::from_utf8_lossy(&output.stdout).to_string(),
))
}
fn execute_java(&self, code: &str) -> Result<Value, BellronosError> {
let temp_file = "Temp.java";
std::fs::write(temp_file, code)?;
let output = Command::new("javac").arg(temp_file).output()?;
if !output.status.success() {
return Err(BellronosError::Runtime(format!(
"Java compilation error: {}",
String::from_utf8_lossy(&output.stderr)
)));
}
let output = Command::new("java").arg("Temp").output()?;
std::fs::remove_file(temp_file)?;
std::fs::remove_file("Temp.class")?;
Ok(Value::String(
String::from_utf8_lossy(&output.stdout).to_string(),
))
}
fn execute_rust(&self, code: &str) -> Result<Value, BellronosError> {
let temp_file = "temp.rs";
std::fs::write(temp_file, code)?;
let output = Command::new("rustc").arg(temp_file).output()?;
if !output.status.success() {
return Err(BellronosError::Runtime(format!(
"Rust compilation error: {}",
String::from_utf8_lossy(&output.stderr)
)));
}
let output = Command::new("./temp").output()?;
std::fs::remove_file(temp_file)?;
std::fs::remove_file("temp")?;
Ok(Value::String(
String::from_utf8_lossy(&output.stdout).to_string(),
))
}
fn execute_swift(&self, code: &str) -> Result<Value, BellronosError> {
let temp_file = "temp.swift";
std::fs::write(temp_file, code)?;
let output = Command::new("swift").arg(temp_file).output()?;
std::fs::remove_file(temp_file)?;
if output.status.success() {
Ok(Value::String(
String::from_utf8_lossy(&output.stdout).to_string(),
))
} else {
Err(BellronosError::Runtime(format!(
"Swift execution error: {}",
String::from_utf8_lossy(&output.stderr)
)))
}
}
}