From acb29e7123b245cf57aeb345514c83cf32f1af67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Tue, 3 Jan 2023 22:50:47 +0100 Subject: [PATCH] Resolver - Refactored error handling --- src/resolver.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/resolver.rs b/src/resolver.rs index bc48b69..34a022a 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -1,16 +1,19 @@ use std::collections::HashMap; -use crate::{ast, errors::ParserError, tokens::Token}; +use crate::{ + ast, + errors::{ErrorKind, SloxError, SloxResult}, + tokens::Token, +}; pub type ResolvedVariables = HashMap<*const ast::ExprNode, usize>; -pub fn resolve_variables(program: &ast::ProgramNode) -> Result { +pub fn resolve_variables(program: &ast::ProgramNode) -> Result { let mut state = ResolverState::default(); - program.resolve(&mut state)?; - Ok(state.resolved) + program.resolve(&mut state).map(|_| state.resolved) } -type ResolverResult = Result<(), ParserError>; +type ResolverResult = SloxResult<()>; #[derive(Default)] struct ResolverState { @@ -185,9 +188,10 @@ impl VarResolver for ast::ExprNode { match self { ast::ExprNode::Variable { name } => { if rs.check(&name.lexeme) == Some(false) { - Err(ParserError::new( + Err(SloxError::with_token( + ErrorKind::Parse, name, - "can't read local variable in its own initializer", + "can't read local variable in its own initializer".to_owned(), )) } else { rs.resolve_local(self, name);