diff --git a/src/direct/mod.rs b/src/direct/mod.rs index e963031..88360c0 100644 --- a/src/direct/mod.rs +++ b/src/direct/mod.rs @@ -1,6 +1,7 @@ -use std::{collections::VecDeque, path::Path}; +use std::{collections::VecDeque, env, path::Path}; use crate::{token::{Token, MessageType}, builtin::modules::Module}; +use crate::builtin::modules::io::print; #[derive(Default)] pub struct LangSpecs { @@ -58,8 +59,6 @@ pub fn from_list(text: &str) -> Vec { fn parse_directive(text: &str, token_idx: usize, specs: &mut LangSpecs, origin: &String) { - std::env::set_current_dir(Path::new(origin).parent().unwrap()).expect("unable to change cwd"); - for cap in DIRECTIVE_REGEX.captures_iter(text) { let mut enumerator = cap.iter().enumerate(); loop { @@ -91,11 +90,19 @@ fn parse_directive(text: &str, token_idx: usize, specs: &mut LangSpecs, origin: return; }, 4 => { + let cwd = Path::new(origin).parent().unwrap().canonicalize().unwrap(); for path in from_list(mat.as_str()).iter() { - if let Ok(str) = std::fs::read_to_string(path) { - specs.embedded_files.push((token_idx, str, path.to_owned())); - } else { - crate::message(MessageType::Warning, format!("Unable to read embedded file: {path}")); + let mut include_path = cwd.clone(); + include_path.push(path); + + if let Ok(path) = include_path.canonicalize() { + let include_file = path.to_str().unwrap().to_string(); + + if let Ok(str) = std::fs::read_to_string(&include_file) { + specs.embedded_files.push((token_idx, str, include_file)); + } else { + crate::message(MessageType::Warning, format!("Unable to read embedded file: {include_file}")); + } } } return; diff --git a/src/main.rs b/src/main.rs index be992cc..805d3ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ fn compile(settings: &Settings) -> Option<(Vec, Vec, Vec