Skip to content

Commit

Permalink
Added tests for extend-expression codec feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mrLSD committed Mar 25, 2024
1 parent a14cdee commit acf80b8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "semantic-analyzer"
version = "0.4.0"
version = "0.4.1"
authors = ["Evgeny Ukhanov <mrlsd@ya.ru>"]
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 @@ -20,7 +20,7 @@ doctest = false

[dependencies]
nom_locate = "4.2"
serde = { version = "1", features = ["derive"], optional = true }
serde = { version = "1", features = ["derive"], optional = true }

[dev-dependencies]
serde_json = "1"
Expand Down
13 changes: 6 additions & 7 deletions src/types/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,9 @@ pub trait ExtendedSemanticContext<I: SemanticContextInstruction> {
}

/// Semantic Context trait contains custom instruction implementation
/// to flexibly extend context instructions.
pub trait SemanticContextInstruction: Debug + Clone + PartialEq {
/// Custom instruction implementation.
/// Ast should be received from `GetAst` trait.
fn instruction(&self) -> Box<Self>;
}
/// to flexibly extend context instructions. It represents ivs derided
/// traits: `Debug` and `Serialize` + `Deserialize`
pub trait SemanticContextInstruction: Debug + Clone + PartialEq {}

/// Extended Expression for semantic analyzer.
pub trait ExtendedExpression<I: SemanticContextInstruction>: Debug + Clone + PartialEq {
Expand Down Expand Up @@ -426,7 +423,9 @@ impl<I: SemanticContextInstruction> ExtendedSemanticContext<I> for SemanticStack
/// AS argument trait, that contains instruction method that returns
/// instruction parameters.
fn extended_expression(&mut self, expr: &I) {
self.push(SemanticStackContext::ExtendedExpression(expr.instruction()));
self.push(SemanticStackContext::ExtendedExpression(Box::new(
expr.clone(),
)));
}
}

Expand Down
16 changes: 10 additions & 6 deletions tests/expressions_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,17 +1332,13 @@ fn custom_expression() {
}

#[derive(Clone, Debug, PartialEq)]
#[allow(dead_code)]
#[cfg_attr(feature = "codec", derive(serde::Serialize, serde::Deserialize))]
pub enum CustomExpressionInstruction {
GoIn { index: u32, value: u32 },
GoOut { result: u32 },
}

impl SemanticContextInstruction for CustomExpressionInstruction {
fn instruction(&self) -> Box<Self> {
Box::new(self.clone())
}
}
impl SemanticContextInstruction for CustomExpressionInstruction {}

impl ExtendedExpression<CustomExpressionInstruction>
for CustomExpression<CustomExpressionInstruction>
Expand Down Expand Up @@ -1438,4 +1434,12 @@ fn custom_expression() {
register_number: 3,
}
);

#[cfg(feature = "codec")]
{
let json = serde_json::to_string(&bs).unwrap();
let bs_decoded: Vec<SemanticStackContext<CustomExpressionInstruction>> =
serde_json::from_str(&json).unwrap();
assert_eq!(bs, bs_decoded);
}
}
6 changes: 1 addition & 5 deletions tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ impl<I: SemanticContextInstruction> ExtendedExpression<I> for CustomExpression<I
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct CustomExpressionInstruction;

impl SemanticContextInstruction for CustomExpressionInstruction {
fn instruction(&self) -> Box<Self> {
Box::new(Self)
}
}
impl SemanticContextInstruction for CustomExpressionInstruction {}

pub struct SemanticTest<I: SemanticContextInstruction> {
pub state: State<CustomExpression<I>, I>,
Expand Down

0 comments on commit acf80b8

Please sign in to comment.