Skip to content

Commit

Permalink
Feat: Remove String from AST (#35)
Browse files Browse the repository at this point in the history
* Remove String from AST. Related to #31

* Updated to new clippy rules for - ast::ExpressionValue::_marker
  • Loading branch information
mrLSD authored Aug 17, 2024
1 parent b936463 commit 52b54f9
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 56 deletions.
7 changes: 1 addition & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "semantic-analyzer"
version = "0.4.4"
version = "0.4.5"
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,11 +13,6 @@ repository = "https://github.com/mrLSD/semantic-analyzer-rs"
[lib]
doctest = false

# It requieres MSRV: 1.74
# [lints.clippy]
# pedantic = "deny"
# nursery = "deny"

[dependencies]
nom_locate = "4.2"
serde = { version = "1", features = ["derive"], optional = true }
Expand Down
7 changes: 2 additions & 5 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ pub enum PrimitiveTypes {
I64,
F32,
F64,
String,
Bool,
Char,
Ptr,
Expand All @@ -347,7 +346,6 @@ impl GetName for PrimitiveTypes {
Self::I64 => "i64".to_string(),
Self::F32 => "f32".to_string(),
Self::F64 => "f64".to_string(),
Self::String => "string".to_string(),
Self::Bool => "bool".to_string(),
Self::Char => "char".to_string(),
Self::Ptr => "ptr".to_string(),
Expand Down Expand Up @@ -589,7 +587,6 @@ pub enum PrimitiveValue {
I64(i64),
F32(f32),
F64(f64),
String(String),
Bool(bool),
Char(char),
Ptr,
Expand All @@ -610,7 +607,6 @@ impl PrimitiveValue {
Self::I64(_) => Type::Primitive(PrimitiveTypes::I64),
Self::F32(_) => Type::Primitive(PrimitiveTypes::F32),
Self::F64(_) => Type::Primitive(PrimitiveTypes::F64),
Self::String(_) => Type::Primitive(PrimitiveTypes::String),
Self::Char(_) => Type::Primitive(PrimitiveTypes::Char),
Self::Bool(_) => Type::Primitive(PrimitiveTypes::Bool),
Self::Ptr => Type::Primitive(PrimitiveTypes::Ptr),
Expand Down Expand Up @@ -649,7 +645,8 @@ pub enum ExpressionValue<'a, I: SemanticContextInstruction, E: ExtendedExpressio
/// Extended expression
ExtendedExpression(Box<E>),
#[cfg_attr(feature = "codec", serde(skip))]
_Marker(Infallible, PhantomData<I>),
#[allow(non_camel_case_types)]
_marker(Infallible, PhantomData<I>),
}

/// `ExpressionOperations` expression operation element of AST.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(clippy::pedantic, clippy::nursery)]
#![deny(clippy::pedantic, clippy::nursery, clippy::as_conversions)]
#![allow(clippy::module_name_repetitions, clippy::doc_lazy_continuation)]
//! # Semantic Analyzer
//! The semantic analyzer consists of the following basic elements:
Expand Down
2 changes: 1 addition & 1 deletion src/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ where
// If expression return error immediately return error
// because next analyzer should use success result.
let right_value = match &right_expression.expression_value {
ast::ExpressionValue::_Marker(..) => unreachable!(),
ast::ExpressionValue::_marker(..) => unreachable!(),
// Check is expression Value entity
ast::ExpressionValue::ValueName(value) => {
// Get value from block state
Expand Down
3 changes: 2 additions & 1 deletion src/types/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ impl<I: SemanticContextInstruction, E: ExtendedExpression<I>> From<ast::Expressi
{
fn from(value: ast::ExpressionValue<'_, I, E>) -> Self {
match value {
ast::ExpressionValue::_Marker(..) => unreachable!(),
#[allow(unreachable_patterns)]
ast::ExpressionValue::_marker(..) => unreachable!(),
ast::ExpressionValue::ValueName(v) => Self::ValueName(v.into()),
ast::ExpressionValue::PrimitiveValue(v) => Self::PrimitiveValue(v.into()),
ast::ExpressionValue::StructValue(v) => Self::StructValue(v.into()),
Expand Down
3 changes: 0 additions & 3 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ pub enum PrimitiveValue {
F32(f32),
F64(f64),
Bool(bool),
String(String),
Char(char),
Ptr,
None,
Expand All @@ -434,7 +433,6 @@ impl From<ast::PrimitiveValue> for PrimitiveValue {
ast::PrimitiveValue::I64(v) => Self::I64(v),
ast::PrimitiveValue::F32(v) => Self::F32(v),
ast::PrimitiveValue::F64(v) => Self::F64(v),
ast::PrimitiveValue::String(v) => Self::String(v),
ast::PrimitiveValue::Bool(v) => Self::Bool(v),
ast::PrimitiveValue::Char(v) => Self::Char(v),
ast::PrimitiveValue::Ptr => Self::Ptr,
Expand All @@ -457,7 +455,6 @@ impl Display for PrimitiveValue {
Self::F32(val) => val.clone().to_string(),
Self::F64(val) => val.clone().to_string(),
Self::Bool(val) => val.to_string(),
Self::String(s) => s.clone(),
Self::Char(c) => format!("{c}"),
Self::Ptr => "ptr".to_string(),
Self::None => "None".to_string(),
Expand Down
3 changes: 0 additions & 3 deletions src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ pub enum PrimitiveTypes {
I64,
F32,
F64,
String,
Bool,
Char,
Ptr,
Expand All @@ -175,7 +174,6 @@ impl Display for PrimitiveTypes {
Self::I64 => "i64",
Self::F32 => "f32",
Self::F64 => "f64",
Self::String => "string",
Self::Bool => "bool",
Self::Char => "char",
Self::Ptr => "ptr",
Expand All @@ -198,7 +196,6 @@ impl From<ast::PrimitiveTypes> for PrimitiveTypes {
ast::PrimitiveTypes::I64 => Self::I64,
ast::PrimitiveTypes::F32 => Self::F32,
ast::PrimitiveTypes::F64 => Self::F64,
ast::PrimitiveTypes::String => Self::String,
ast::PrimitiveTypes::Bool => Self::Bool,
ast::PrimitiveTypes::Char => Self::Char,
ast::PrimitiveTypes::Ptr => Self::Ptr,
Expand Down
22 changes: 0 additions & 22 deletions tests/expressions_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,28 +289,6 @@ fn expression_ast_transform_primitive_value_f64() {
assert_eq!(expr.to_string(), "3.1");
}

#[test]
fn expression_ast_transform_primitive_value_string() {
let test_res = "test".to_string();
let val = ast::PrimitiveValue::String(test_res.clone());
assert_eq!(
val.get_type(),
ast::Type::Primitive(ast::PrimitiveTypes::String)
);
let expr_val: PrimitiveValue = val.clone().into();
assert_eq!(PrimitiveValue::String(test_res.clone()), expr_val);
assert_eq!(expr_val.to_string(), test_res);
let expr: Expression = ast::Expression {
expression_value: ast::ExpressionValue::<
CustomExpressionInstruction,
CustomExpression<CustomExpressionInstruction>,
>::PrimitiveValue(val),
operation: None,
}
.into();
assert_eq!(expr.to_string(), test_res);
}

#[test]
fn expression_ast_transform_primitive_value_bool() {
let val = ast::PrimitiveValue::Bool(true);
Expand Down
15 changes: 1 addition & 14 deletions tests/types_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ fn types_ast_transform() {
attributes: vec![],
}),
};
let ty18 = ast::StructType {
attr_name: Ident::new("attr18"),
attr_type: ast::Type::Primitive(ast::PrimitiveTypes::String),
};
let type_ast = ast::StructTypes {
name: Ident::new("type2"),
attributes: vec![
Expand All @@ -115,12 +111,11 @@ fn types_ast_transform() {
ty15.clone(),
ty16.clone(),
ty17.clone(),
ty18.clone(),
],
};
let type_into2: StructTypes = type_ast.clone().into();
assert_eq!(type_into2.name, "type2");
assert_eq!(type_into2.attributes.len(), 17);
assert_eq!(type_into2.attributes.len(), 16);

// Index=0 the same, so we can check directly
assert_eq!(ty1.attr_type.name(), "u8");
Expand Down Expand Up @@ -258,14 +253,6 @@ fn types_ast_transform() {
assert_eq!(attr17.attr_type.to_string(), "type5");
assert_eq!(attr17.attr_index, 15);

let attr18 = type_into2.attributes.get(&("attr18".into())).unwrap();
assert_eq!(ty18.attr_type.name(), "string");
let ty18: StructAttributeType = ty18.into();
assert_eq!(attr18.attr_name, ty18.attr_name);
assert_eq!(attr18.attr_type, ty18.attr_type);
assert_eq!(attr18.attr_type.to_string(), "string");
assert_eq!(attr18.attr_index, 16);

//=======================
// Common type tests
let pty = Type::Primitive(PrimitiveTypes::U16);
Expand Down

0 comments on commit 52b54f9

Please sign in to comment.