From 3addb767fa6ea0c1fd5bf517e734410a003de1ec Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 12 Jul 2024 20:54:23 -0400 Subject: [PATCH] Normalize top-level API --- dust-lang/src/lib.rs | 31 ++++++++++++++++++------------- dust-shell/src/main.rs | 6 +++--- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/dust-lang/src/lib.rs b/dust-lang/src/lib.rs index 19f7a43..37d1e3d 100644 --- a/dust-lang/src/lib.rs +++ b/dust-lang/src/lib.rs @@ -47,29 +47,34 @@ impl Interpreter { } /// Lexes the source code and returns a list of tokens. - pub fn lex<'src>( + pub fn lex<'id>( &self, source_id: Arc, - source: &'src str, - ) -> Result>, InterpreterError> { - let mut sources = self.sources.write().unwrap(); + source: &'id Arc, + ) -> Result>, InterpreterError> { + self.sources + .write() + .unwrap() + .insert(source_id.clone(), source.clone()); - sources.insert(source_id.clone(), Arc::from(source)); - - lex(source) + lex(source.as_ref()) .map(|tokens| tokens.into_iter().map(|(token, _)| token).collect()) - .map_err(|errors| InterpreterError { source_id, errors }) + .map_err(|errors| InterpreterError { + source_id: source_id.clone(), + errors, + }) } /// Parses the source code and returns an abstract syntax tree. - pub fn parse( + pub fn parse<'id>( &self, source_id: Arc, - source: &str, + source: &'id Arc, ) -> Result { - let mut sources = self.sources.write().unwrap(); - - sources.insert(source_id.clone(), Arc::from(source)); + self.sources + .write() + .unwrap() + .insert(source_id.clone(), source.clone()); parse(&lex(source).map_err(|errors| InterpreterError { source_id: source_id.clone(), diff --git a/dust-shell/src/main.rs b/dust-shell/src/main.rs index 2e1a174..63f793e 100644 --- a/dust-shell/src/main.rs +++ b/dust-shell/src/main.rs @@ -75,7 +75,7 @@ fn main() { }; if args.lex { - match interpreter.lex(source_id, source.as_ref()) { + match interpreter.lex(source_id, &source) { Ok(tokens) => println!("{tokens:?}"), Err(error) => { for report in error.build_reports() { @@ -95,7 +95,7 @@ fn main() { } if args.parse { - match interpreter.parse(source_id, source.as_ref()) { + match interpreter.parse(source_id, &source) { Ok(abstract_tree) => println!("{abstract_tree:?}"), Err(error) => { for report in error.build_reports() { @@ -110,7 +110,7 @@ fn main() { } if args.compile { - match interpreter.parse(source_id, source.as_ref()) { + match interpreter.parse(source_id, &source) { Ok(abstract_tree) => { let ron = ron::to_string(&abstract_tree).unwrap();