Skip to content

Commit

Permalink
Improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 5, 2024
1 parent 0e767b3 commit 3e44ea6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
23 changes: 12 additions & 11 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion main.why
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ struct A {
b: int
}

fn main() -> int {
fn main() -> intt {
return 12;
}
6 changes: 5 additions & 1 deletion src/root/errors/name_resolver_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ pub enum NRErrors {
#[error("Cannot find method ({0}) of type ({1})")]
CannotFindMethod(String, String),
#[error("Two attributes found with the same name ({0})")]
SameAttributeName(String)
SameAttributeName(String),
#[error("Function reference cannot have indirection here")]
FunctionIndirectionError,
#[error("Identifier ({0}) not found")]
IdentifierNotFound(String)
}
11 changes: 4 additions & 7 deletions src/root/name_resolver/name_resolvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::rc::Rc;
use derive_getters::Getters;
use crate::root::compiler::local_variable_table::LocalVariableTable;
use crate::root::errors::name_resolver_errors::NRErrors;
use crate::root::errors::name_resolver_errors::NRErrors::IdentifierNotFound;
use crate::root::errors::WError;
use crate::root::name_resolver::resolve_function_signatures::FunctionSignature;
use crate::root::parser::parse::Location;
Expand Down Expand Up @@ -195,10 +196,6 @@ impl GlobalDefinitionTable {
todo!()
}

pub fn resolve_global_name(&self, name: &UnresolvedNameToken) -> NameResult {
todo!()
}

pub fn resolve_global_name_to_id(&self, name: &UnresolvedNameToken, location: &Location) -> Result<NameResultId, WError> {
let path = name.location().path();

Expand All @@ -214,7 +211,7 @@ impl GlobalDefinitionTable {
return Err(WError::n(NRErrors::FunctionSubname(next.clone(), name.base().clone()), location.clone()));
}
if name.indirection().has_indirection() {
// TODO
return Err(WError::n(NRErrors::FunctionIndirectionError, name.location().clone()));
}
Ok(Some(NameResultId::Function(*fid)))
}
Expand Down Expand Up @@ -299,12 +296,12 @@ impl GlobalDefinitionTable {
return Err(WError::n(NRErrors::FunctionSubname(next.clone(), name.base().clone()), location.clone()));
}
if name.indirection().has_indirection() {
// TODO
return Err(WError::n(NRErrors::FunctionIndirectionError, name.location().clone()));
}

return Ok(NameResultId::Function(*id))
}

todo!()
Err(WError::n(IdentifierNotFound(name.base().clone()), name.location().clone()))
}
}
8 changes: 6 additions & 2 deletions src/root/parser/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ impl Location {

const CHAR_LIMIT: usize = 61;


impl Display for Location {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
// TODO: Inefficient!
// (Maybe fine because it is a 'bad' path?)

writeln!(f, "{}", cformat!("<c,bold>In File:</>"))?;
writeln!(f, " {}", self.path.as_path().to_string_lossy())?;
writeln!(f, "{}", cformat!("<c,bold>At:</>"))?;
Expand All @@ -65,7 +69,6 @@ impl Display for Location {
offset += 1;
}
}
println!("O: {offset}");

let mut line_iter = file.lines();

Expand Down Expand Up @@ -103,7 +106,8 @@ impl Display for Location {
end += 1;

writeln!(f, "{:0width$} | {}", self.line, line.chars().skip(start).take(end - start).collect::<String>(), width=largest_num_len)?;
writeln!(f, "{:0width$} | {}^Here", "E", (0..(offset - start)).map(|_| ' ').collect::<String>(), width=largest_num_len)?;
let err_line = format!("{:0width$} | {}^Here", "E", (0..(offset - start)).map(|_| ' ').collect::<String>(), width=largest_num_len);
writeln!(f, "{}", cformat!("<r,bold>{}</>", err_line))?;

if let Some(line) = line_iter.next() {
let line = if line.chars().count() > CHAR_LIMIT { format!("{} ...", line.chars().take(CHAR_LIMIT - 4).collect::<String>()) } else { line.to_string() };
Expand Down

0 comments on commit 3e44ea6

Please sign in to comment.