relative include paths now get resolved properly

This commit is contained in:
Sven Vogel 2024-04-17 22:31:32 +02:00
parent a567fc75b9
commit 5a3c4bffd6
2 changed files with 15 additions and 8 deletions

View File

@ -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<String> {
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;

View File

@ -33,7 +33,7 @@ fn compile(settings: &Settings) -> Option<(Vec<Func>, Vec<Declr>, Vec<BuiltinFun
let mut diagnostics = Diagnostics::new(&settings);
diagnostics.add_source_origin(code, src.path());
if let Ok(mut tokens) = tokenize(code, &mut diagnostics) {
let specs = crate::direct::resolve_directives(&mut tokens, src.path());