Skip to content

Commit b936463

Browse files
authored
Merge pull request #34 from mrLSD/feat/ast-string-and-fixes
Feat: Updated Ast String types. Changed tests. Increase test-coverage
2 parents 73c7f40 + 0554833 commit b936463

File tree

7 files changed

+57
-27
lines changed

7 files changed

+57
-27
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ jobs:
4242
run: cargo test --all-targets --all-features
4343
env:
4444
CARGO_INCREMENTAL: '0'
45-
RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort'
46-
RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort'
45+
RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Cllvm-args=--inline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort'
46+
RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Cllvm-args=--inline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort'
4747
- name: Install grcov
4848
run: |
4949
if [[ ! -f ~/.cargo/bin/grcov ]]; then

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "semantic-analyzer"
3-
version = "0.4.3"
3+
version = "0.4.4"
44
authors = ["Evgeny Ukhanov <[email protected]>"]
55
description = "Semantic analyzer library for compilers written in Rust for semantic analysis of programming languages AST"
66
keywords = ["compiler", "semantic-analisis", "semantic-alalyzer", "compiler-design", "semantic"]

src/ast.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<'de: 'a, 'a> Deserialize<'de> for Ident<'a> {
8282
{
8383
// grcov-excl-start
8484
struct IdentVisitor<'a> {
85-
marker: std::marker::PhantomData<fn() -> Ident<'a>>,
85+
marker: PhantomData<fn() -> Ident<'a>>,
8686
}
8787

8888
impl<'de: 'a, 'a> Visitor<'de> for IdentVisitor<'a> {
@@ -146,7 +146,7 @@ impl<'de: 'a, 'a> Deserialize<'de> for Ident<'a> {
146146
"Ident",
147147
FIELDS,
148148
IdentVisitor {
149-
marker: std::marker::PhantomData,
149+
marker: PhantomData,
150150
},
151151
)
152152
}
@@ -327,6 +327,7 @@ pub enum PrimitiveTypes {
327327
I64,
328328
F32,
329329
F64,
330+
String,
330331
Bool,
331332
Char,
332333
Ptr,
@@ -346,6 +347,7 @@ impl GetName for PrimitiveTypes {
346347
Self::I64 => "i64".to_string(),
347348
Self::F32 => "f32".to_string(),
348349
Self::F64 => "f64".to_string(),
350+
Self::String => "string".to_string(),
349351
Self::Bool => "bool".to_string(),
350352
Self::Char => "char".to_string(),
351353
Self::Ptr => "ptr".to_string(),
@@ -587,6 +589,7 @@ pub enum PrimitiveValue {
587589
I64(i64),
588590
F32(f32),
589591
F64(f64),
592+
String(String),
590593
Bool(bool),
591594
Char(char),
592595
Ptr,
@@ -607,6 +610,7 @@ impl PrimitiveValue {
607610
Self::I64(_) => Type::Primitive(PrimitiveTypes::I64),
608611
Self::F32(_) => Type::Primitive(PrimitiveTypes::F32),
609612
Self::F64(_) => Type::Primitive(PrimitiveTypes::F64),
613+
Self::String(_) => Type::Primitive(PrimitiveTypes::String),
610614
Self::Char(_) => Type::Primitive(PrimitiveTypes::Char),
611615
Self::Bool(_) => Type::Primitive(PrimitiveTypes::Bool),
612616
Self::Ptr => Type::Primitive(PrimitiveTypes::Ptr),
@@ -623,7 +627,7 @@ impl PrimitiveValue {
623627
/// example, it's numbers: 10, 3.2 and other primitive values)
624628
/// - `FunctionCall` - function call (with parameters) of expression
625629
/// - `StructValue` - value of expression based on `Struct` types.
626-
/// - `Expression` - expression representation (sub branch)
630+
/// - `Expression` - expression representation (sub-branch)
627631
#[derive(Debug, Clone, PartialEq)]
628632
#[cfg_attr(
629633
feature = "codec",
@@ -640,7 +644,7 @@ pub enum ExpressionValue<'a, I: SemanticContextInstruction, E: ExtendedExpressio
640644
FunctionCall(FunctionCall<'a, I, E>),
641645
/// Value of expression based on `Struct` types.
642646
StructValue(ExpressionStructValue<'a>),
643-
/// Expression representation (sub branch)
647+
/// Expression representation (sub-branch)
644648
Expression(Box<Expression<'a, I, E>>),
645649
/// Extended expression
646650
ExtendedExpression(Box<E>),

src/types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ impl From<ast::PrimitiveValue> for PrimitiveValue {
434434
ast::PrimitiveValue::I64(v) => Self::I64(v),
435435
ast::PrimitiveValue::F32(v) => Self::F32(v),
436436
ast::PrimitiveValue::F64(v) => Self::F64(v),
437+
ast::PrimitiveValue::String(v) => Self::String(v),
437438
ast::PrimitiveValue::Bool(v) => Self::Bool(v),
438439
ast::PrimitiveValue::Char(v) => Self::Char(v),
439440
ast::PrimitiveValue::Ptr => Self::Ptr,

src/types/types.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ pub enum PrimitiveTypes {
155155
I64,
156156
F32,
157157
F64,
158+
String,
158159
Bool,
159160
Char,
160161
Ptr,
@@ -174,6 +175,7 @@ impl Display for PrimitiveTypes {
174175
Self::I64 => "i64",
175176
Self::F32 => "f32",
176177
Self::F64 => "f64",
178+
Self::String => "string",
177179
Self::Bool => "bool",
178180
Self::Char => "char",
179181
Self::Ptr => "ptr",
@@ -196,6 +198,7 @@ impl From<ast::PrimitiveTypes> for PrimitiveTypes {
196198
ast::PrimitiveTypes::I64 => Self::I64,
197199
ast::PrimitiveTypes::F32 => Self::F32,
198200
ast::PrimitiveTypes::F64 => Self::F64,
201+
ast::PrimitiveTypes::String => Self::String,
199202
ast::PrimitiveTypes::Bool => Self::Bool,
200203
ast::PrimitiveTypes::Char => Self::Char,
201204
ast::PrimitiveTypes::Ptr => Self::Ptr,
@@ -262,7 +265,7 @@ impl From<ast::StructTypes<'_>> for StructTypes {
262265
pub struct StructAttributeType {
263266
/// Attribute name for struct type
264267
pub attr_name: ValueName,
265-
/// Attribute index representation for for struct type
268+
/// Attribute index representation for struct type
266269
pub attr_index: u32,
267270
/// Attribute type for struct type
268271
pub attr_type: Type,

tests/expressions_tests.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,28 @@ fn expression_ast_transform_primitive_value_f64() {
289289
assert_eq!(expr.to_string(), "3.1");
290290
}
291291

292+
#[test]
293+
fn expression_ast_transform_primitive_value_string() {
294+
let test_res = "test".to_string();
295+
let val = ast::PrimitiveValue::String(test_res.clone());
296+
assert_eq!(
297+
val.get_type(),
298+
ast::Type::Primitive(ast::PrimitiveTypes::String)
299+
);
300+
let expr_val: PrimitiveValue = val.clone().into();
301+
assert_eq!(PrimitiveValue::String(test_res.clone()), expr_val);
302+
assert_eq!(expr_val.to_string(), test_res);
303+
let expr: Expression = ast::Expression {
304+
expression_value: ast::ExpressionValue::<
305+
CustomExpressionInstruction,
306+
CustomExpression<CustomExpressionInstruction>,
307+
>::PrimitiveValue(val),
308+
operation: None,
309+
}
310+
.into();
311+
assert_eq!(expr.to_string(), test_res);
312+
}
313+
292314
#[test]
293315
fn expression_ast_transform_primitive_value_bool() {
294316
let val = ast::PrimitiveValue::Bool(true);
@@ -428,7 +450,7 @@ fn expression_value_name_not_found() {
428450
fn expression_value_name_exists() {
429451
let block_state = Rc::new(RefCell::new(BlockState::new(None)));
430452
let mut t = SemanticTest::new();
431-
let value_name = ast::ValueName::new(ast::Ident::new("x"));
453+
let value_name = ast::ValueName::new(Ident::new("x"));
432454
let expr = ast::Expression {
433455
expression_value: ast::ExpressionValue::ValueName(value_name.clone()),
434456
operation: None,
@@ -464,7 +486,7 @@ fn expression_value_name_exists() {
464486
fn expression_const_exists() {
465487
let block_state = Rc::new(RefCell::new(BlockState::new(None)));
466488
let mut t = SemanticTest::new();
467-
let src = ast::Ident::new("x");
489+
let src = Ident::new("x");
468490
let const_name = ast::ValueName::new(src);
469491
let expr = ast::Expression {
470492
expression_value: ast::ExpressionValue::ValueName(const_name.clone()),

tests/types_tests.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ fn types_ast_transform() {
7373
attr_name: Ident::new("attr12"),
7474
attr_type: ast::Type::Primitive(ast::PrimitiveTypes::Char),
7575
};
76-
let ty13 = ast::StructType {
77-
attr_name: Ident::new("attr13"),
78-
attr_type: ast::Type::Primitive(ast::PrimitiveTypes::U64),
79-
};
8076
let ty14 = ast::StructType {
8177
attr_name: Ident::new("attr14"),
8278
attr_type: ast::Type::Primitive(ast::PrimitiveTypes::Ptr),
@@ -96,6 +92,10 @@ fn types_ast_transform() {
9692
attributes: vec![],
9793
}),
9894
};
95+
let ty18 = ast::StructType {
96+
attr_name: Ident::new("attr18"),
97+
attr_type: ast::Type::Primitive(ast::PrimitiveTypes::String),
98+
};
9999
let type_ast = ast::StructTypes {
100100
name: Ident::new("type2"),
101101
attributes: vec![
@@ -111,11 +111,11 @@ fn types_ast_transform() {
111111
ty10.clone(),
112112
ty11.clone(),
113113
ty12.clone(),
114-
ty13.clone(),
115114
ty14.clone(),
116115
ty15.clone(),
117116
ty16.clone(),
118117
ty17.clone(),
118+
ty18.clone(),
119119
],
120120
};
121121
let type_into2: StructTypes = type_ast.clone().into();
@@ -226,45 +226,45 @@ fn types_ast_transform() {
226226
assert_eq!(attr12.attr_type.to_string(), "char");
227227
assert_eq!(attr12.attr_index, 11);
228228

229-
let attr13 = type_into2.attributes.get(&("attr13".into())).unwrap();
230-
assert_eq!(ty13.attr_type.name(), "u64");
231-
let ty13: StructAttributeType = ty13.into();
232-
assert_eq!(attr13.attr_name, ty13.attr_name);
233-
assert_eq!(attr13.attr_type, ty13.attr_type);
234-
assert_eq!(attr13.attr_type.to_string(), "u64");
235-
assert_eq!(attr13.attr_index, 12);
236-
237229
let attr14 = type_into2.attributes.get(&("attr14".into())).unwrap();
238230
assert_eq!(ty14.attr_type.name(), "ptr");
239231
let ty14: StructAttributeType = ty14.into();
240232
assert_eq!(attr14.attr_name, ty14.attr_name);
241233
assert_eq!(attr14.attr_type, ty14.attr_type);
242234
assert_eq!(attr14.attr_type.to_string(), "ptr");
243-
assert_eq!(attr14.attr_index, 13);
235+
assert_eq!(attr14.attr_index, 12);
244236

245237
let attr15 = type_into2.attributes.get(&("attr15".into())).unwrap();
246238
assert_eq!(ty15.attr_type.name(), "()");
247239
let ty15: StructAttributeType = ty15.into();
248240
assert_eq!(attr15.attr_name, ty15.attr_name);
249241
assert_eq!(attr15.attr_type, ty15.attr_type);
250242
assert_eq!(attr15.attr_type.to_string(), "()");
251-
assert_eq!(attr15.attr_index, 14);
243+
assert_eq!(attr15.attr_index, 13);
252244

253245
let attr16 = type_into2.attributes.get(&("attr16".into())).unwrap();
254246
assert_eq!(ty16.attr_type.name(), "[\"i16\";10]");
255247
let ty16: StructAttributeType = ty16.into();
256248
assert_eq!(attr16.attr_name, ty16.attr_name);
257249
assert_eq!(attr16.attr_type, ty16.attr_type);
258250
assert_eq!(attr16.attr_type.to_string(), "[\"i16\";10]");
259-
assert_eq!(attr16.attr_index, 15);
251+
assert_eq!(attr16.attr_index, 14);
260252

261253
let attr17 = type_into2.attributes.get(&("attr17".into())).unwrap();
262254
assert_eq!(ty17.attr_type.name(), "type5");
263255
let ty17: StructAttributeType = ty17.into();
264256
assert_eq!(attr17.attr_name, ty17.attr_name);
265257
assert_eq!(attr17.attr_type, ty17.attr_type);
266258
assert_eq!(attr17.attr_type.to_string(), "type5");
267-
assert_eq!(attr17.attr_index, 16);
259+
assert_eq!(attr17.attr_index, 15);
260+
261+
let attr18 = type_into2.attributes.get(&("attr18".into())).unwrap();
262+
assert_eq!(ty18.attr_type.name(), "string");
263+
let ty18: StructAttributeType = ty18.into();
264+
assert_eq!(attr18.attr_name, ty18.attr_name);
265+
assert_eq!(attr18.attr_type, ty18.attr_type);
266+
assert_eq!(attr18.attr_type.to_string(), "string");
267+
assert_eq!(attr18.attr_index, 16);
268268

269269
//=======================
270270
// Common type tests

0 commit comments

Comments
 (0)