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.
pub fn lex<'src>(
pub fn lex<'id>(
&self,
source_id: Arc<str>,
source: &'src str,
) -> Result<Vec<Token<'src>>, InterpreterError> {
let mut sources = self.sources.write().unwrap();
source: &'id Arc<str>,
) -> Result<Vec<Token<'id>>, 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<str>,
source: &str,
source: &'id Arc<str>,
) -> Result<AbstractTree, InterpreterError> {
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(),

View File

@ -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();