Skip to content

Commit

Permalink
Added clippy rules - increased clippy level. Fixed all clippy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mrLSD committed Nov 29, 2023
1 parent de85ea9 commit 1feb385
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 53 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "semantic-analyzer"
version = "0.3.1"
version = "0.3.2"
authors = ["Evgeny Ukhanov <[email protected]>"]
description = "Semantic analyzer library for compilers written in Rust for semantic analysis of programming languages AST"
keywords = ["compiler", "semantic-analisis", "semantic-alalyzer", "compiler-design", "semantic"]
Expand All @@ -13,5 +13,9 @@ repository = "https://github.com/mrLSD/semantic-analyzer-rs"
[lib]
doctest = false

#[lints.clippy]
#pedantic = "deny"
#nursery = "deny"

[dependencies]
nom_locate = "4.2"
11 changes: 11 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl GetName for ImportName<'_> {
}

impl<'a> ImportName<'a> {
#[must_use]
pub const fn new(ident: Ident<'a>) -> Self {
Self(ident)
}
Expand All @@ -47,6 +48,7 @@ pub struct ConstantName<'a>(Ident<'a>);

impl<'a> ConstantName<'a> {
/// Init `ConstantName`, especially useful for testing
#[must_use]
pub const fn new(name: Ident<'a>) -> Self {
Self(name)
}
Expand All @@ -69,6 +71,7 @@ impl GetName for ConstantName<'_> {
pub struct FunctionName<'a>(Ident<'a>);

impl<'a> FunctionName<'a> {
#[must_use]
pub const fn new(name: Ident<'a>) -> Self {
Self(name)
}
Expand All @@ -92,6 +95,7 @@ impl<'a> ToString for FunctionName<'a> {
pub struct ParameterName<'a>(Ident<'a>);

impl<'a> ParameterName<'a> {
#[must_use]
pub const fn new(name: Ident<'a>) -> Self {
Self(name)
}
Expand All @@ -112,6 +116,7 @@ impl ToString for ParameterName<'_> {
pub struct ValueName<'a>(Ident<'a>);

impl<'a> ValueName<'a> {
#[must_use]
pub const fn new(name: Ident<'a>) -> Self {
Self(name)
}
Expand All @@ -138,16 +143,19 @@ impl CodeLocation {
/// Initialize code location with:
/// - `location` - line of source code
/// - `offset` - position on the line of source code
#[must_use]
pub const fn new(location: u32, offset: usize) -> Self {
Self(location, offset)
}

/// Get location line in the source
#[must_use]
pub const fn line(&self) -> u32 {
self.0
}

/// Get location position on the line in the source
#[must_use]
pub const fn offset(&self) -> usize {
self.1
}
Expand Down Expand Up @@ -383,6 +391,7 @@ pub enum PrimitiveValue {
}

impl PrimitiveValue {
#[must_use]
pub const fn get_type(&self) -> Type<'_> {
match self {
Self::U8(_) => Type::Primitive(PrimitiveTypes::U8),
Expand Down Expand Up @@ -451,6 +460,8 @@ pub enum ExpressionOperations {
}

impl ExpressionOperations {
/// Get expression operation priority level
#[must_use]
pub const fn priority(&self) -> u8 {
match self {
Self::Plus => 5,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![deny(clippy::pedantic, clippy::nursery)]
#![allow(clippy::module_name_repetitions)]
//! # Semantic Analyzer
//! The semantic analyzer consists of the following basic elements:
//! - AST is an abstract syntax tree that implements a predefined set of
Expand Down
67 changes: 34 additions & 33 deletions src/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl Default for State {

impl State {
/// Init new `State`
#[must_use]
pub fn new() -> Self {
Self {
global: GlobalState {
Expand Down Expand Up @@ -129,15 +130,15 @@ impl State {
// identification for using it in functions body.

// First pass is Imports and Types
for main in data.iter() {
for main in data {
match main {
ast::MainStatement::Import(import) => self.import(import),
ast::MainStatement::Types(types) => self.types(types),
_ => (),
}
}
// Declaration pass for Constants and Functions
for main in data.iter() {
for main in data {
match main {
ast::MainStatement::Constant(constant) => self.constant(constant),
ast::MainStatement::Function(function) => self.function_declaration(function),
Expand All @@ -146,7 +147,7 @@ impl State {
}

// After getting all functions declarations, fetch only functions body
for main in data.iter() {
for main in data {
if let ast::MainStatement::Function(function) = main {
self.function_body(function);
}
Expand Down Expand Up @@ -182,7 +183,7 @@ impl State {
/// If expression contains `Constant` check is constant exists.
/// Values doesn't check as it's just `Primitive Values`.
/// Also check all expression tree branches.
/// If ConstantValue doesn't exist add error to `Error State` and `return` false result.
/// If `ConstantValue` doesn't exist add error to `Error State` and `return` false result.
pub fn check_constant_value_expression(
&mut self,
data: &Option<(ast::ExpressionOperations, Box<ast::ConstantExpression<'_>>)>,
Expand All @@ -207,7 +208,7 @@ impl State {
}
self.check_constant_value_expression(&child_data.operation)
}
_ => true,
ast::ConstantValue::Value(_) => true,
}
} else {
true
Expand Down Expand Up @@ -364,7 +365,7 @@ impl State {
self.function_call(fn_call, &body_state);
}
ast::BodyStatement::If(if_condition) => {
self.if_condition(if_condition, &body_state, None, None);
self.if_condition(if_condition, &body_state, &None, None);
}
ast::BodyStatement::Loop(loop_statement) => {
self.loop_statement(loop_statement, &body_state);
Expand Down Expand Up @@ -511,9 +512,7 @@ impl State {
let bind_data: Binding = data.clone().into();

// Find value in current state and parent states
let value = if let Some(val) = function_state.borrow().get_value_name(&bind_data.name) {
val
} else {
let Some(value) = function_state.borrow().get_value_name(&bind_data.name) else {
self.add_error(error::StateErrorResult::new(
error::StateErrorKind::ValueNotFound,
bind_data.to_string(),
Expand Down Expand Up @@ -625,16 +624,14 @@ impl State {
));
return function_body_state.borrow().last_register_number;
}
match left_res.expr_type {
Type::Primitive(_) => (),
_ => {
self.add_error(error::StateErrorResult::new(
error::StateErrorKind::ConditionExpressionNotSupported,
left_res.expr_type.to_string(),
data.left.left.location(),
));
return function_body_state.borrow().last_register_number;
}
if let Type::Primitive(_) = left_res.expr_type {
} else {
self.add_error(error::StateErrorResult::new(
error::StateErrorKind::ConditionExpressionNotSupported,
left_res.expr_type.to_string(),
data.left.left.location(),
));
return function_body_state.borrow().last_register_number;
}

// Increment register
Expand Down Expand Up @@ -675,7 +672,7 @@ impl State {
/// # If-condition body
/// Analyze body for ant if condition:
/// - if, else, if-else
/// NOTE: label_end - is always already exists
/// NOTE: `label_end` - is always already exists
/// ## Return
/// Return body statement "return" status
pub fn if_condition_body(
Expand All @@ -686,7 +683,7 @@ impl State {
label_loop: Option<(&LabelName, &LabelName)>,
) -> bool {
let mut return_is_called = false;
for body in body.iter() {
for body in body {
if return_is_called {
self.add_error(error::StateErrorResult::new(
error::StateErrorKind::ForbiddenCodeAfterReturnDeprecated,
Expand All @@ -708,7 +705,7 @@ impl State {
self.if_condition(
if_condition,
if_body_state,
Some(label_end.clone()),
&Some(label_end.clone()),
label_loop,
);
}
Expand Down Expand Up @@ -747,7 +744,7 @@ impl State {
let mut return_is_called = false;
let mut break_is_called = false;
let mut continue_is_called = false;
for body in body.iter() {
for body in body {
if return_is_called {
self.add_error(error::StateErrorResult::new(
error::StateErrorKind::ForbiddenCodeAfterReturnDeprecated,
Expand Down Expand Up @@ -784,7 +781,7 @@ impl State {
self.if_condition(
if_condition,
if_body_state,
Some(label_if_end.clone()),
&Some(label_if_end.clone()),
Some((label_loop_start, label_loop_end)),
);
}
Expand Down Expand Up @@ -891,16 +888,20 @@ impl State {
/// context, and main goal is to end all of if-condition nodes in
/// the same flow with same `if-end` label. It's especially important
/// for `else-if` condition.
///
/// ## Panics
/// `label_loop` is must be set, it's special case for the Loop,
/// when `label_loop` should always be set. If it doesn't set, it's
/// unexpected behavior and program algorithm error
pub fn if_condition(
&mut self,
data: &ast::IfStatement<'_>,
function_body_state: &Rc<RefCell<BlockState>>,
label_end: Option<LabelName>,
label_end: &Option<LabelName>,
label_loop: Option<(&LabelName, &LabelName)>,
) {
// It can't contain `else` and `if-else` on the same time
if data.else_statement.is_some() && data.else_if_statement.is_some() {
let stm = data.else_if_statement.clone().unwrap();
if let (Some(_), Some(stm)) = (&data.else_statement, &data.else_if_statement) {
self.add_error(error::StateErrorResult::new(
error::StateErrorKind::IfElseDuplicated,
String::from("if-condition"),
Expand Down Expand Up @@ -933,7 +934,7 @@ impl State {
|label| label,
);
// To set if-end as single return point check is it previously set
let is_set_label_if_end = label_end.clone().is_some();
let is_set_label_if_end = label_end.is_some();
let is_else = data.else_statement.is_some() || data.else_if_statement.is_some();

// Analyse if-conditions
Expand Down Expand Up @@ -1022,7 +1023,7 @@ impl State {
self.if_condition(
else_if_statement,
function_body_state,
Some(label_if_end.clone()),
&Some(label_if_end.clone()),
label_loop,
);
}
Expand Down Expand Up @@ -1073,7 +1074,7 @@ impl State {
let mut return_is_called = false;
let mut break_is_called = false;
let mut continue_is_called = false;
for body in data.iter() {
for body in data {
if return_is_called {
self.add_error(error::StateErrorResult::new(
error::StateErrorKind::ForbiddenCodeAfterReturnDeprecated,
Expand Down Expand Up @@ -1109,7 +1110,7 @@ impl State {
ast::LoopBodyStatement::If(if_condition) => self.if_condition(
if_condition,
&loop_body_state,
None,
&None,
Some((&label_loop_begin, &label_loop_end)),
),
ast::LoopBodyStatement::Loop(loop_statement) => {
Expand Down Expand Up @@ -1326,7 +1327,7 @@ impl State {
let last_register_number = body_state.borrow().last_register_number;
body_state.borrow_mut().expression_struct_value(
val.clone(),
attributes.clone().attr_index,
attributes.attr_index,
last_register_number,
);

Expand Down Expand Up @@ -1427,7 +1428,7 @@ impl State {
ast::ExpressionValue::Expression(Box::new(ast::Expression {
expression_value: data.expression_value,
operation: Some((
op.clone(),
op,
Box::new(ast::Expression {
expression_value: expr.expression_value,
operation: None,
Expand Down
Loading

0 comments on commit 1feb385

Please sign in to comment.