cl-parser, cl-repl: Add ./[mod].cl to module search path

This commit is contained in:
John 2025-02-23 02:44:26 -06:00
parent 2fd08193fd
commit e39b390441
2 changed files with 25 additions and 12 deletions

View File

@ -81,18 +81,32 @@ impl Fold for ModuleInliner {
self.path.set_extension("cl");
let file = match std::fs::read_to_string(&self.path) {
Err(error) => return self.handle_io_error(error),
Err(error) => {
let Some(basename) = self.path.file_name() else {
return self.handle_io_error(error);
};
let path = self
.path
.parent()
.and_then(Path::parent)
.map(|path| path.join(basename))
.unwrap_or_default();
match std::fs::read_to_string(&path) {
Err(error) => return self.handle_io_error(error),
Ok(file) => file,
}
}
Ok(file) => file,
};
let kind = match Parser::new(Lexer::new(&file)).parse() {
Err(e) => return self.handle_parse_error(e),
Ok(file) => ModuleKind::Inline(file),
};
// cd path/mod
self.path.set_extension("");
// The newly loaded module may need further inlining
self.fold_module_kind(kind)
match Parser::new(Lexer::new(&file)).parse() {
Err(e) => self.handle_parse_error(e),
Ok(file) => {
self.path.set_extension("");
// The newly loaded module may need further inlining
ModuleKind::Inline(self.fold_file(file))
}
}
}
}

View File

@ -72,8 +72,7 @@ pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
}
fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue, Box<dyn Error>> {
let inliner =
cl_parser::inliner::ModuleInliner::new(path.as_ref().parent().unwrap_or(Path::new("")));
let inliner = cl_parser::inliner::ModuleInliner::new(path.as_ref().with_extension(""));
let file = std::fs::read_to_string(path)?;
let code = Parser::new(Lexer::new(&file)).parse()?;
let code = match inliner.inline(code) {