Skip to content

Commit

Permalink
Merge pull request #34 from mrLSD/feat/ast-string-and-fixes
Browse files Browse the repository at this point in the history
Feat: Updated Ast String types. Changed tests. Increase test-coverage
  • Loading branch information
mrLSD committed Aug 4, 2024
2 parents 73c7f40 + 0554833 commit b936463
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "semantic-analyzer"
version = "0.4.3"
version = "0.4.4"
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 Down
12 changes: 8 additions & 4 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<'de: 'a, 'a> Deserialize<'de> for Ident<'a> {
{
// grcov-excl-start
struct IdentVisitor<'a> {
marker: std::marker::PhantomData<fn() -> Ident<'a>>,
marker: PhantomData<fn() -> Ident<'a>>,
}

impl<'de: 'a, 'a> Visitor<'de> for IdentVisitor<'a> {
Expand Down Expand Up @@ -146,7 +146,7 @@ impl<'de: 'a, 'a> Deserialize<'de> for Ident<'a> {
"Ident",
FIELDS,
IdentVisitor {
marker: std::marker::PhantomData,
marker: PhantomData,
},
)
}
Expand Down Expand Up @@ -327,6 +327,7 @@ pub enum PrimitiveTypes {
I64,
F32,
F64,
String,
Bool,
Char,
Ptr,
Expand All @@ -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(),
Expand Down Expand Up @@ -587,6 +589,7 @@ pub enum PrimitiveValue {
I64(i64),
F32(f32),
F64(f64),
String(String),
Bool(bool),
Char(char),
Ptr,
Expand All @@ -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),
Expand All @@ -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",
Expand All @@ -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<Expression<'a, I, E>>),
/// Extended expression
ExtendedExpression(Box<E>),
Expand Down
1 change: 1 addition & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ 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 Down
5 changes: 4 additions & 1 deletion src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ pub enum PrimitiveTypes {
I64,
F32,
F64,
String,
Bool,
Char,
Ptr,
Expand All @@ -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",
Expand All @@ -196,6 +198,7 @@ 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 Expand Up @@ -262,7 +265,7 @@ impl From<ast::StructTypes<'_>> 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,
Expand Down
26 changes: 24 additions & 2 deletions tests/expressions_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 Expand Up @@ -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,
Expand Down Expand Up @@ -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()),
Expand Down
34 changes: 17 additions & 17 deletions tests/types_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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![
Expand All @@ -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();
Expand Down Expand Up @@ -226,45 +226,45 @@ 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(), "()");
let ty15: StructAttributeType = ty15.into();
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]");
let ty16: StructAttributeType = ty16.into();
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");
let ty17: StructAttributeType = ty17.into();
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
Expand Down

0 comments on commit b936463

Please sign in to comment.