($num:ident) => { IR::StackPush(*$num as u64) }
}
-fn import(specifier: &str, imported: &mut HashSet<PathBuf>, is_entrypoint: bool) -> Option<ModuleWithImports> {
- // TODO paths relative to the files, not just the invocation
- let path = PathBuf::from(specifier).canonicalize().unwrap();
+fn import(importer_dir: &PathBuf, specifier: &str, imported: &mut HashSet<PathBuf>, is_entrypoint: bool) -> Option<ModuleWithImports> {
+ let mut path = PathBuf::from(specifier);
+ if path.is_relative() {
+ let mut new_path = importer_dir.clone();
+ new_path.push(path);
+ path = new_path.canonicalize().unwrap();
+ }
if imported.contains(&path) {
return None;
}
let contents = std::fs::read_to_string(&path).unwrap();
- Some(generate_internal(&Module::parse(tokenize(&contents), is_entrypoint), imported))
+ Some(generate_internal(path, &Module::parse(tokenize(&contents), is_entrypoint), imported))
}
fn collapse_module(mut module_w: ModuleWithImports) -> IRModule {
pub fn compile(path: &str) -> IRModule {
let mut imported = HashSet::new();
- let module = import(path, &mut imported, true).unwrap();
+ let module = import(&std::env::current_dir().unwrap(), path, &mut imported, true).unwrap();
collapse_module(module) // TODO remove unused words
}
#[derive(Debug)]
struct ModuleWithImports {
module: Option<IRModule>,
- imports: Option<Vec<ModuleWithImports>>
+ imports: Option<Vec<ModuleWithImports>>,
}
-fn generate_internal(module: &Module, imported: &mut HashSet<PathBuf>) -> ModuleWithImports {
+fn generate_internal(path: PathBuf, module: &Module, imported: &mut HashSet<PathBuf>) -> ModuleWithImports {
// Eventually these will end up being sections in assembly
let mut text = vec![];
let mut data = vec![];
},
Token::String(text) => {
if last_was_import {
- if let Some(module) = import(text, imported, false) {
+ if let Some(module) = import(&path.parent().unwrap().to_path_buf(), text, imported, false) {
imports.push(module);
}
IR::ImportString // This will be elided later
text: text.into_iter().flatten().collect::<Vec<_>>(),
data,
}),
- imports: Some(imports)
+ imports: Some(imports),
}
}