Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/modules/imports/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,18 @@ impl Import {
format!("Standard library module '{}' does not exist", self.path.value))
}
} else {
match fs::read_to_string(self.path.value.clone()) {
Ok(content) => Ok(content),
Err(err) => error!(meta, self.token_path.clone() => {
message: format!("Could not read file '{}'", self.path.value),
comment: err.to_string()
})
if let Ok(content) = fs::read_to_string(&self.path.value) {
return Ok(content);
}
for path in &meta.context.import_paths {
if let Ok(content) = fs::read_to_string(path.join(&self.path.value)) {
return Ok(content);
}
}
error!(meta, self.token_path.clone() => {
message: format!("Could not find '{}'", self.path.value),
comment: format!("Tried lookup paths: {:?}", meta.context.import_paths)
})
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/utils/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::modules::types::Type;
use amber_meta::ContextHelper;
use heraclitus_compiler::prelude::*;
use std::collections::{HashMap, HashSet};
use std::env;
use std::path::PathBuf;

#[derive(Clone, Debug)]
pub struct FunctionDecl {
Expand Down Expand Up @@ -123,11 +125,15 @@ pub struct Context {
/// List of compiler flags
#[context]
pub cc_flags: HashSet<CCFlags>,
/// List of lookup paths
pub import_paths: Vec<PathBuf>,
}

// FIXME: Move the scope related structures to the separate file
impl Context {
pub fn new(path: Option<String>, expr: Vec<Token>) -> Self {
let import_paths = env::var("AMBER_PATH").unwrap_or_default();
let import_paths = env::split_paths(&import_paths).collect();
Self {
index: 0,
expr,
Expand All @@ -141,6 +147,7 @@ impl Context {
pub_funs: vec![],
fun_ret_type: None,
cc_flags: HashSet::new(),
import_paths,
}
}

Expand Down