From 4439196bc53825bbed0d1d38e90d94ed2f3c5ed8 Mon Sep 17 00:00:00 2001 From: farkon00 Date: Thu, 3 Nov 2022 18:10:14 +0200 Subject: [PATCH] Added loading std from any location --- src/main.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 550e20c..1757f6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -588,15 +588,32 @@ impl Context { Ok(()) } - fn process_file(&mut self, loc: Loc, file_path: String, diag: &mut impl Diagnoster) -> Option<()> { - let source = match fs::read_to_string(&file_path) { - Ok(source) => source, + fn load_source(&self, loc: Loc, file_path: &str, diag: &mut impl Diagnoster) -> Option { + match fs::read_to_string(file_path) { + Ok(source) => Some(source), Err(err) => { diag.report(&loc, Severity::Error, &format!("could not load file {}: {}", file_path, err)); - return None + None + } + } + } + + fn process_file(&mut self, loc: Loc, file_path: String, diag: &mut impl Diagnoster) -> Option<()> { + let source = if file_path.starts_with("std/") { + match env::var("NOQ_STD_PATH") { + Ok(std_path) => + self.load_source(loc, + &(std_path + file_path.strip_prefix("std/").unwrap()), diag), + Err(_) => self.load_source(loc, &file_path, diag) } + } + else { + self.load_source(loc, &file_path, diag) }; - let mut lexer = Lexer::new(source.chars().collect(), Some(file_path)); + if source.is_none() { + return None; + } + let mut lexer = Lexer::new(source?.chars().collect(), Some(file_path)); while lexer.peek_token().kind != TokenKind::End { self.process_command(Command::parse(&mut lexer, diag)?, diag)? }