Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor assignment checker #563

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions crates/analyzer/src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ impl Analyzer {
}

fn check_symbol_table(path: &Path, text: &str) -> Vec<AnalyzerError> {
let path = resource_table::get_path_id(path.to_path_buf()).unwrap();
let mut ret = Vec::new();
let symbols = symbol_table::get_all();
for symbol in symbols {

// check unused variables
let path = resource_table::get_path_id(path.to_path_buf()).unwrap();
for symbol in &symbols {
if symbol.token.source == path {
if let SymbolKind::Variable(_) = symbol.kind {
if symbol.references.is_empty() && !symbol.allow_unused {
Expand All @@ -144,6 +146,10 @@ impl Analyzer {
}
}
}

// check assignment
let _assign_list = symbol_table::get_assign_list();

ret
}
}
57 changes: 35 additions & 22 deletions crates/analyzer/src/analyzer_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@ use veryl_parser::veryl_token::VerylToken;

#[derive(Error, Diagnostic, Debug)]
pub enum AnalyzerError {
#[diagnostic(
severity(Error),
code(assignment_to_input),
help(""),
url("https://doc.veryl-lang.org/book/06_appendix/02_semantic_error.html#assignment_to_input")
)]
#[error("Assignment to input {identifier}")]
AssignmentToInput {
identifier: String,
#[source_code]
input: NamedSource,
#[label("Error location")]
error_location: SourceSpan,
},
#[diagnostic(
severity(Error),
code(cyclice_type_dependency),
Expand All @@ -33,6 +19,7 @@ pub enum AnalyzerError {
#[label("Error location")]
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(duplicated_identifier),
Expand All @@ -47,6 +34,7 @@ pub enum AnalyzerError {
#[label("Error location")]
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(duplicated_assignment),
Expand All @@ -61,6 +49,7 @@ pub enum AnalyzerError {
#[label("Error location")]
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(invalid_allow),
Expand All @@ -76,6 +65,24 @@ pub enum AnalyzerError {
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(invalid_assignment),
help("remove the assignment"),
url(
"https://doc.veryl-lang.org/book/06_appendix/02_semantic_error.html#invalid_assignment"
)
)]
#[error("{identifier} can't be assigned because it is {kind}")]
InvalidAssignment {
identifier: String,
kind: String,
#[source_code]
input: NamedSource,
#[label("Error location")]
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(invalid_direction),
Expand Down Expand Up @@ -475,14 +482,6 @@ impl AnalyzerError {
NamedSource::new(token.token.source.to_string(), source.to_string())
}

pub fn assignment_to_input(source: &str, identifier: &str, token: &VerylToken) -> Self {
AnalyzerError::AssignmentToInput {
identifier: identifier.into(),
input: AnalyzerError::named_source(source, token),
error_location: token.token.into(),
}
}

pub fn cyclic_type_dependency(
source: &str,
start: &str,
Expand Down Expand Up @@ -521,6 +520,20 @@ impl AnalyzerError {
}
}

pub fn invalid_assignment(
identifier: &str,
source: &str,
kind: &str,
token: &VerylToken,
) -> Self {
AnalyzerError::InvalidAssignment {
identifier: identifier.into(),
kind: kind.into(),
input: AnalyzerError::named_source(source, token),
error_location: token.token.into(),
}
}

pub fn invalid_direction(kind: &str, source: &str, token: &VerylToken) -> Self {
AnalyzerError::InvalidDirection {
kind: kind.to_string(),
Expand Down
12 changes: 6 additions & 6 deletions crates/analyzer/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub mod check_assignment;
pub mod check_attribute;
pub mod check_direction;
pub mod check_enum;
pub mod check_function;
pub mod check_identifier;
pub mod check_instance;
pub mod check_module;
pub mod check_msb_lsb;
pub mod check_number;
pub mod check_reset;
Expand All @@ -29,7 +29,7 @@ use crate::analyzer_error::AnalyzerError;
use veryl_metadata::Lint;
use veryl_parser::veryl_walker::Handler;

use self::{check_module::CheckModule, create_type_dag::CreateTypeDag};
use self::{check_assignment::CheckAssignment, create_type_dag::CreateTypeDag};

pub struct Pass1Handlers<'a> {
check_attribute: CheckAttribute<'a>,
Expand Down Expand Up @@ -85,7 +85,7 @@ pub struct Pass2Handlers<'a> {
check_function: CheckFunction<'a>,
check_instance: CheckInstance<'a>,
check_msb_lsb: CheckMsbLsb<'a>,
check_module: CheckModule<'a>,
check_assignment: CheckAssignment<'a>,
create_reference: CreateReference<'a>,
create_type_dag: CreateTypeDag<'a>,
}
Expand All @@ -98,7 +98,7 @@ impl<'a> Pass2Handlers<'a> {
check_function: CheckFunction::new(text),
check_instance: CheckInstance::new(text),
check_msb_lsb: CheckMsbLsb::new(text),
check_module: CheckModule::new(text),
check_assignment: CheckAssignment::new(text),
create_reference: CreateReference::new(text),
create_type_dag: CreateTypeDag::new(text),
}
Expand All @@ -111,7 +111,7 @@ impl<'a> Pass2Handlers<'a> {
&mut self.check_function as &mut dyn Handler,
&mut self.check_instance as &mut dyn Handler,
&mut self.check_msb_lsb as &mut dyn Handler,
&mut self.check_module as &mut dyn Handler,
&mut self.check_assignment as &mut dyn Handler,
&mut self.create_reference as &mut dyn Handler,
&mut self.create_type_dag as &mut dyn Handler,
]
Expand All @@ -124,7 +124,7 @@ impl<'a> Pass2Handlers<'a> {
ret.append(&mut self.check_function.errors);
ret.append(&mut self.check_instance.errors);
ret.append(&mut self.check_msb_lsb.errors);
ret.append(&mut self.check_module.errors);
ret.append(&mut self.check_assignment.errors);
ret.append(&mut self.create_reference.errors);
ret.append(&mut self.create_type_dag.errors);
ret
Expand Down
Loading
Loading