diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7d167e2..6624647 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -42,8 +42,8 @@ jobs: run: cargo test --all-targets --all-features env: CARGO_INCREMENTAL: '0' - RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort' - RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort' + RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Cllvm-args=--inline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort' + RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Cllvm-args=--inline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort' - name: Install grcov run: | if [[ ! -f ~/.cargo/bin/grcov ]]; then diff --git a/Cargo.toml b/Cargo.toml index 9603769..468e7f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "semantic-analyzer" -version = "0.4.2" +version = "0.4.4" authors = ["Evgeny Ukhanov "] 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"] diff --git a/src/ast.rs b/src/ast.rs index 3c1b3d1..9725662 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -82,7 +82,7 @@ impl<'de: 'a, 'a> Deserialize<'de> for Ident<'a> { { // grcov-excl-start struct IdentVisitor<'a> { - marker: std::marker::PhantomData Ident<'a>>, + marker: PhantomData Ident<'a>>, } impl<'de: 'a, 'a> Visitor<'de> for IdentVisitor<'a> { @@ -146,7 +146,7 @@ impl<'de: 'a, 'a> Deserialize<'de> for Ident<'a> { "Ident", FIELDS, IdentVisitor { - marker: std::marker::PhantomData, + marker: PhantomData, }, ) } @@ -327,6 +327,7 @@ pub enum PrimitiveTypes { I64, F32, F64, + String, Bool, Char, Ptr, @@ -346,6 +347,7 @@ 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(), @@ -587,6 +589,7 @@ pub enum PrimitiveValue { I64(i64), F32(f32), F64(f64), + String(String), Bool(bool), Char(char), Ptr, @@ -607,6 +610,7 @@ 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), @@ -623,7 +627,7 @@ impl PrimitiveValue { /// example, it's numbers: 10, 3.2 and other primitive values) /// - `FunctionCall` - function call (with parameters) of expression /// - `StructValue` - value of expression based on `Struct` types. -/// - `Expression` - expression representation (sub branch) +/// - `Expression` - expression representation (sub-branch) #[derive(Debug, Clone, PartialEq)] #[cfg_attr( feature = "codec", @@ -640,7 +644,7 @@ pub enum ExpressionValue<'a, I: SemanticContextInstruction, E: ExtendedExpressio FunctionCall(FunctionCall<'a, I, E>), /// Value of expression based on `Struct` types. StructValue(ExpressionStructValue<'a>), - /// Expression representation (sub branch) + /// Expression representation (sub-branch) Expression(Box>), /// Extended expression ExtendedExpression(Box), diff --git a/src/types/mod.rs b/src/types/mod.rs index 21c6603..b90d658 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -434,6 +434,7 @@ impl From 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, diff --git a/src/types/types.rs b/src/types/types.rs index 074fd71..fc74723 100644 --- a/src/types/types.rs +++ b/src/types/types.rs @@ -155,6 +155,7 @@ pub enum PrimitiveTypes { I64, F32, F64, + String, Bool, Char, Ptr, @@ -174,6 +175,7 @@ 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", @@ -196,6 +198,7 @@ impl From 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, @@ -262,7 +265,7 @@ impl From> for StructTypes { pub struct StructAttributeType { /// Attribute name for struct type pub attr_name: ValueName, - /// Attribute index representation for for struct type + /// Attribute index representation for struct type pub attr_index: u32, /// Attribute type for struct type pub attr_type: Type, diff --git a/tests/binding_tests.rs b/tests/binding_tests.rs index 3521f11..f3a1cbf 100644 --- a/tests/binding_tests.rs +++ b/tests/binding_tests.rs @@ -32,7 +32,7 @@ fn binding_transform() { assert_eq!(binding.clone().to_string(), "x"); assert_eq!(binding.value, Box::new(expr_ast.into())); // For grcov - format!("{:?}", binding_ast.clone()); + let _ = format!("{:?}", binding_ast.clone()); } #[test] diff --git a/tests/expressions_tests.rs b/tests/expressions_tests.rs index 501b344..479f429 100644 --- a/tests/expressions_tests.rs +++ b/tests/expressions_tests.rs @@ -70,7 +70,7 @@ fn expression_ast_transform() { assert_eq!(value_name_into.to_string(), "x"); let expr_into: Expression = expr.into(); // For grcov - format!("{expr_into:?}"); + let _ = format!("{expr_into:?}"); assert_eq!(expr_into.expression_value.to_string(), "x"); assert_eq!(expr_into.to_string(), "x"); let value_name_into2: ValueName = String::from("x1").into(); @@ -289,6 +289,28 @@ 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, + >::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); @@ -398,7 +420,7 @@ fn expression_ast_transform_expression() { operation: None, } .into(); - format!("{expr:#?}"); + let _ = format!("{expr:#?}"); assert_eq!(expr.to_string(), "ptr"); } @@ -428,7 +450,7 @@ fn expression_value_name_not_found() { fn expression_value_name_exists() { let block_state = Rc::new(RefCell::new(BlockState::new(None))); let mut t = SemanticTest::new(); - let value_name = ast::ValueName::new(ast::Ident::new("x")); + let value_name = ast::ValueName::new(Ident::new("x")); let expr = ast::Expression { expression_value: ast::ExpressionValue::ValueName(value_name.clone()), operation: None, @@ -464,7 +486,7 @@ fn expression_value_name_exists() { fn expression_const_exists() { let block_state = Rc::new(RefCell::new(BlockState::new(None))); let mut t = SemanticTest::new(); - let src = ast::Ident::new("x"); + let src = Ident::new("x"); let const_name = ast::ValueName::new(src); let expr = ast::Expression { expression_value: ast::ExpressionValue::ValueName(const_name.clone()), @@ -785,7 +807,7 @@ fn expression_struct_value() { let expression_st_value_into: ExpressionValue = expression_st_value.clone().into(); assert_eq!(expression_st_value_into.to_string(), "x"); // For grcov - format!("{expression_st_value_into:?}"); + let _ = format!("{expression_st_value_into:?}"); let expr = ast::Expression { expression_value: expression_st_value, operation: None, @@ -849,7 +871,7 @@ fn expression_func_call() { let expr_value_into: ExpressionValue = ast_fn_call.clone().into(); assert_eq!(expr_value_into.to_string(), "fn1"); // For grcov - format!("{expr_value_into:?}"); + let _ = format!("{expr_value_into:?}"); let expr = ast::Expression { expression_value: ast_fn_call, operation: None, @@ -1369,9 +1391,9 @@ fn custom_expression() { let expr_into: Expression = expr.clone().into(); // For grcov - format!("{:#?}", expr_into); + let _ = format!("{:#?}", expr_into); // For grcov - format!("{:#?}", expr_into.to_string()); + let _ = format!("{:#?}", expr_into.to_string()); let res = state.expression(&expr, &block_state).unwrap(); assert!(state.errors.is_empty()); diff --git a/tests/if_tests.rs b/tests/if_tests.rs index e5d9cb8..c0560ae 100644 --- a/tests/if_tests.rs +++ b/tests/if_tests.rs @@ -112,7 +112,7 @@ fn if_single_transform() { assert_eq!(if_compare1, if_compare2); assert_eq!(if_statement1.location(), CodeLocation::new(1, 0)); // For grcov - format!("{if_statement1:?}"); + let _ = format!("{if_statement1:?}"); } #[test] @@ -252,7 +252,7 @@ fn if_logic_transform() { else_if_statement: None, }; // For grcov - format!("{if_statement3:?}"); + let _ = format!("{if_statement3:?}"); let expr_logic_cond_into: ExpressionLogicCondition = expr_logic_cond.into(); assert_eq!( expr_logic_cond_into.left, diff --git a/tests/let_binding_tests.rs b/tests/let_binding_tests.rs index 2597b22..854ff40 100644 --- a/tests/let_binding_tests.rs +++ b/tests/let_binding_tests.rs @@ -39,7 +39,7 @@ fn let_binding_transform() { ); assert_eq!(let_binding.value, Box::new(expr_ast.into())); // For grcov - format!("{:?}", let_binding_ast.clone()); + let _ = format!("{:?}", let_binding_ast.clone()); } #[test] diff --git a/tests/loop_tests.rs b/tests/loop_tests.rs index 1345f78..7577999 100644 --- a/tests/loop_tests.rs +++ b/tests/loop_tests.rs @@ -63,11 +63,11 @@ fn loop_transform() { ast::LoopBodyStatement::Continue, ]; // For grcov - format!("{loop_stmts:#?}"); + let _ = format!("{loop_stmts:#?}"); for loop_stmt in loop_stmts { let loop_stmt_into: LoopBodyStatement = loop_stmt.into(); // For grcov - format!("{loop_stmt_into:#?}"); + let _ = format!("{loop_stmt_into:#?}"); match loop_stmt_into { LoopBodyStatement::LetBinding(val) => assert_eq!(val, let_binding.clone().into()), LoopBodyStatement::Binding(val) => assert_eq!(val, binding.clone().into()), diff --git a/tests/main_tests.rs b/tests/main_tests.rs index 8a63856..9252663 100644 --- a/tests/main_tests.rs +++ b/tests/main_tests.rs @@ -110,7 +110,7 @@ fn main_run() { CustomExpression, > = vec![import_stm, constant_stm, ty_stm, fn_stm, fn2_stm]; // For grcov - format!("{main_stm:#?}"); + let _ = format!("{main_stm:#?}"); t.state.run(&main_stm); assert!(t.is_empty_error()); diff --git a/tests/state_tests.rs b/tests/state_tests.rs index 9ca24b8..d0aec9f 100644 --- a/tests/state_tests.rs +++ b/tests/state_tests.rs @@ -22,7 +22,7 @@ mod utils; fn state_init() { let st = State::, CustomExpressionInstruction>::default(); // For grcov - format!("{st:?}"); + let _ = format!("{st:?}"); assert!(st.global.types.is_empty()); assert!(st.global.constants.is_empty()); assert!(st.global.functions.is_empty()); @@ -247,7 +247,7 @@ fn block_state_last_register_inc() { bst3.inc_register(); assert_eq!(bst3.last_register_number, 1); // For grcov - format!("{bst3:?}"); + let _ = format!("{bst3:?}"); } #[test] diff --git a/tests/types_tests.rs b/tests/types_tests.rs index 34729aa..5cbbe84 100644 --- a/tests/types_tests.rs +++ b/tests/types_tests.rs @@ -73,10 +73,6 @@ fn types_ast_transform() { attr_name: Ident::new("attr12"), attr_type: ast::Type::Primitive(ast::PrimitiveTypes::Char), }; - let ty13 = ast::StructType { - attr_name: Ident::new("attr13"), - attr_type: ast::Type::Primitive(ast::PrimitiveTypes::U64), - }; let ty14 = ast::StructType { attr_name: Ident::new("attr14"), attr_type: ast::Type::Primitive(ast::PrimitiveTypes::Ptr), @@ -96,6 +92,10 @@ 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![ @@ -111,11 +111,11 @@ fn types_ast_transform() { ty10.clone(), ty11.clone(), ty12.clone(), - ty13.clone(), ty14.clone(), ty15.clone(), ty16.clone(), ty17.clone(), + ty18.clone(), ], }; let type_into2: StructTypes = type_ast.clone().into(); @@ -226,21 +226,13 @@ fn types_ast_transform() { assert_eq!(attr12.attr_type.to_string(), "char"); assert_eq!(attr12.attr_index, 11); - let attr13 = type_into2.attributes.get(&("attr13".into())).unwrap(); - assert_eq!(ty13.attr_type.name(), "u64"); - let ty13: StructAttributeType = ty13.into(); - assert_eq!(attr13.attr_name, ty13.attr_name); - assert_eq!(attr13.attr_type, ty13.attr_type); - assert_eq!(attr13.attr_type.to_string(), "u64"); - assert_eq!(attr13.attr_index, 12); - let attr14 = type_into2.attributes.get(&("attr14".into())).unwrap(); assert_eq!(ty14.attr_type.name(), "ptr"); let ty14: StructAttributeType = ty14.into(); assert_eq!(attr14.attr_name, ty14.attr_name); assert_eq!(attr14.attr_type, ty14.attr_type); assert_eq!(attr14.attr_type.to_string(), "ptr"); - assert_eq!(attr14.attr_index, 13); + assert_eq!(attr14.attr_index, 12); let attr15 = type_into2.attributes.get(&("attr15".into())).unwrap(); assert_eq!(ty15.attr_type.name(), "()"); @@ -248,7 +240,7 @@ fn types_ast_transform() { assert_eq!(attr15.attr_name, ty15.attr_name); assert_eq!(attr15.attr_type, ty15.attr_type); assert_eq!(attr15.attr_type.to_string(), "()"); - assert_eq!(attr15.attr_index, 14); + assert_eq!(attr15.attr_index, 13); let attr16 = type_into2.attributes.get(&("attr16".into())).unwrap(); assert_eq!(ty16.attr_type.name(), "[\"i16\";10]"); @@ -256,7 +248,7 @@ fn types_ast_transform() { assert_eq!(attr16.attr_name, ty16.attr_name); assert_eq!(attr16.attr_type, ty16.attr_type); assert_eq!(attr16.attr_type.to_string(), "[\"i16\";10]"); - assert_eq!(attr16.attr_index, 15); + assert_eq!(attr16.attr_index, 14); let attr17 = type_into2.attributes.get(&("attr17".into())).unwrap(); assert_eq!(ty17.attr_type.name(), "type5"); @@ -264,7 +256,15 @@ fn types_ast_transform() { assert_eq!(attr17.attr_name, ty17.attr_name); assert_eq!(attr17.attr_type, ty17.attr_type); assert_eq!(attr17.attr_type.to_string(), "type5"); - assert_eq!(attr17.attr_index, 16); + 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