Improved semantic errors

This commit is contained in:
2026-06-16 15:37:34 +02:00
parent bbb94a4f97
commit ecfff0ac0d
2 changed files with 24 additions and 12 deletions
+11 -9
View File
@@ -186,7 +186,7 @@ fn is_source_newer(source *Path, output *Path) std::io::Result<bool> {
pub enum TranspileError<'a> { pub enum TranspileError<'a> {
Parse[error::ParseError<'a, Vec<mist_parser::ast::TopLevel>>], Parse[error::ParseError<'a, Vec<mist_parser::ast::TopLevel>>],
Semantic[mist_parser::semantics::SemanticError] Semantic[Vec<mist_parser::semantics::SemanticError>]
} }
pub fn transpile_code<'a>(path *PathBuf, source *'a str, mut output_file PathBuf, mod_decl *str, curr_mod_decl *Option<[mist_parser::ast::Visibility, mist_parser::ast::Identifier]>) pub fn transpile_code<'a>(path *PathBuf, source *'a str, mut output_file PathBuf, mod_decl *str, curr_mod_decl *Option<[mist_parser::ast::Visibility, mist_parser::ast::Identifier]>)
@@ -262,14 +262,16 @@ pub fn decode_err<'a>(err *TranspileError<'a>, path *PathBuf) {
let curr_dir = std::env::current_dir().unwrap(); let curr_dir = std::env::current_dir().unwrap();
match err { match err {
TranspileError::Semantic[e] => { TranspileError::Semantic[e_list] => {
eprintln!( for e in e_list {
"\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}", eprintln!(
path.strip_prefix(curr_dir).unwrap_or_else(|_| path).as_os_str().display(), "\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}",
e.line, path.strip_prefix(&curr_dir).unwrap_or_else(|_| path).as_os_str().display(),
e.column, e.line,
e.error_message, e.column,
); e.error_message,
);
}
} }
TranspileError::Parse[e] => { TranspileError::Parse[e] => {
+13 -3
View File
@@ -33,11 +33,12 @@ pub fn find_function_call<'a>(
}) })
} }
pub fn check_class_semantics<'a>(top_level: &TopLevel) -> Result<(), SemanticError> { pub fn check_class_semantics<'a>(top_level: &TopLevel) -> Result<(), Vec<SemanticError>> {
if let TopLevelKind::ClassDecl { if let TopLevelKind::ClassDecl {
fields, fields,
constructor, constructor,
items, items,
name,
.. ..
} = &top_level.0.item } = &top_level.0.item
{ {
@@ -63,15 +64,24 @@ pub fn check_class_semantics<'a>(top_level: &TopLevel) -> Result<(), SemanticErr
let mut mutability_iter = mutability.into_iter(); let mut mutability_iter = mutability.into_iter();
let mut errs = Vec::new();
for field in &fields { for field in &fields {
if mutability_iter.find(|v| v == &field.item).is_none() { if mutability_iter.find(|v| v == &field.item).is_none() {
return Err(SemanticError { errs.push(SemanticError {
line: field.line, line: field.line,
column: field.column, column: field.column,
error_message: format!("Field `{}` is uninitialized", field.item.0), error_message: format!(
"class field `{}.{}` is uninitialized",
name.0, field.item.0
),
}); });
} }
} }
if errs.len() > 0 {
return Err(errs);
}
} }
Ok(()) Ok(())