Normalize top-level API

This commit is contained in:
Jeff 2024-07-12 20:54:23 -04:00
parent 62ece61ce6
commit 3addb767fa
2 changed files with 21 additions and 16 deletions

View File

@ -47,29 +47,34 @@ impl Interpreter {
} }
/// Lexes the source code and returns a list of tokens. /// Lexes the source code and returns a list of tokens.
pub fn lex<'src>( pub fn lex<'id>(
&self, &self,
source_id: Arc<str>, source_id: Arc<str>,
source: &'src str, source: &'id Arc<str>,
) -> Result<Vec<Token<'src>>, InterpreterError> { ) -> Result<Vec<Token<'id>>, InterpreterError> {
let mut sources = self.sources.write().unwrap(); self.sources
.write()
.unwrap()
.insert(source_id.clone(), source.clone());
sources.insert(source_id.clone(), Arc::from(source)); lex(source.as_ref())
lex(source)
.map(|tokens| tokens.into_iter().map(|(token, _)| token).collect()) .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. /// Parses the source code and returns an abstract syntax tree.
pub fn parse( pub fn parse<'id>(
&self, &self,
source_id: Arc<str>, source_id: Arc<str>,
source: &str, source: &'id Arc<str>,
) -> Result<AbstractTree, InterpreterError> { ) -> Result<AbstractTree, InterpreterError> {
let mut sources = self.sources.write().unwrap(); self.sources
.write()
sources.insert(source_id.clone(), Arc::from(source)); .unwrap()
.insert(source_id.clone(), source.clone());
parse(&lex(source).map_err(|errors| InterpreterError { parse(&lex(source).map_err(|errors| InterpreterError {
source_id: source_id.clone(), source_id: source_id.clone(),

View File

@ -75,7 +75,7 @@ fn main() {
}; };
if args.lex { if args.lex {
match interpreter.lex(source_id, source.as_ref()) { match interpreter.lex(source_id, &source) {
Ok(tokens) => println!("{tokens:?}"), Ok(tokens) => println!("{tokens:?}"),
Err(error) => { Err(error) => {
for report in error.build_reports() { for report in error.build_reports() {
@ -95,7 +95,7 @@ fn main() {
} }
if args.parse { if args.parse {
match interpreter.parse(source_id, source.as_ref()) { match interpreter.parse(source_id, &source) {
Ok(abstract_tree) => println!("{abstract_tree:?}"), Ok(abstract_tree) => println!("{abstract_tree:?}"),
Err(error) => { Err(error) => {
for report in error.build_reports() { for report in error.build_reports() {
@ -110,7 +110,7 @@ fn main() {
} }
if args.compile { if args.compile {
match interpreter.parse(source_id, source.as_ref()) { match interpreter.parse(source_id, &source) {
Ok(abstract_tree) => { Ok(abstract_tree) => {
let ron = ron::to_string(&abstract_tree).unwrap(); let ron = ron::to_string(&abstract_tree).unwrap();