From e39b390441ffbd2b2e031b9670367622139cbaaa Mon Sep 17 00:00:00 2001 From: John Date: Sun, 23 Feb 2025 02:44:26 -0600 Subject: [PATCH] cl-parser, cl-repl: Add ./[mod].cl to module search path --- compiler/cl-parser/src/inliner.rs | 34 ++++++++++++++++++++++--------- compiler/cl-repl/src/cli.rs | 3 +-- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/compiler/cl-parser/src/inliner.rs b/compiler/cl-parser/src/inliner.rs index 52d81f3..ca31058 100644 --- a/compiler/cl-parser/src/inliner.rs +++ b/compiler/cl-parser/src/inliner.rs @@ -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)) + } + } } } diff --git a/compiler/cl-repl/src/cli.rs b/compiler/cl-repl/src/cli.rs index 37c0323..6387216 100644 --- a/compiler/cl-repl/src/cli.rs +++ b/compiler/cl-repl/src/cli.rs @@ -72,8 +72,7 @@ pub fn run(args: Args) -> Result<(), Box> { } fn load_file(env: &mut Environment, path: impl AsRef) -> Result> { - 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) {