Skip to content

Commit

Permalink
extends macro to handle multiple borrows on AstNode parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mhasel committed Jun 19, 2024
1 parent 7c31425 commit f3b12b3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions compiler/plc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,13 @@ pub enum AstStatement {
/// Will try to return a reference to the variants inner type, specified via the `t:ty` parameter.
/// Converts the `try_from`-`Result` into an `Option`
macro_rules! try_from {
() => { None };
($ex:expr, $t:ty) => {
<&$t>::try_from($ex.get_stmt()).ok()
};
($($ex:tt)*, $t:ty) => {
try_from!($($ex)*, $t).ok()
};
}

impl Debug for AstNode {
Expand Down
22 changes: 12 additions & 10 deletions compiler/plc_ast/src/pre_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
},
literals::AstLiteral,
provider::IdProvider,
try_from,
};
use plc_source::source_location::SourceLocation;

Expand Down Expand Up @@ -96,16 +97,17 @@ pub fn pre_process(unit: &mut CompilationUnit, mut id_provider: IdProvider) {

let initialized_enum_elements = flatten_expression_list(original_elements)
.iter()
.map(|it| match &it.stmt {
AstStatement::Assignment(Assignment { left, right }) => {
//<element-name, initializer, location>
(
extract_flat_ref_name(left.as_ref()),
Some(*right.clone()),
it.get_location(),
)
}
_ => (extract_flat_ref_name(it), None, it.get_location()),
.map(|it| {
try_from!(it, Assignment).map_or_else(
|| (extract_flat_ref_name(it), None, it.get_location()),
|Assignment { left, right }| {
(
extract_flat_ref_name(left.as_ref()),
Some(*right.clone()),
it.get_location(),
)
},
)
})
.map(|(element_name, initializer, location)| {
let enum_literal = initializer.unwrap_or_else(|| {
Expand Down
14 changes: 8 additions & 6 deletions compiler/plc_xml/src/xml_parser/fbd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use ast::ast::{AstFactory, AstNode, AstStatement};
use ast::{
ast::{AstFactory, AstNode, CallStatement},
try_from,
};

use plc::index::FxIndexMap;
use plc_source::source_location::SourceLocation;
Expand Down Expand Up @@ -54,11 +57,10 @@ impl<'xml> FunctionBlockDiagram<'xml> {
let (rhs, remove_id) = ast_association
.get(&ref_id)
.map(|stmt| {
if matches!(stmt.get_stmt(), AstStatement::CallStatement(..)) {
(stmt.clone(), Some(ref_id))
} else {
self.transform_node(ref_id, session, ast_association)
}
try_from!(stmt, CallStatement).map_or_else(
|| self.transform_node(ref_id, session, ast_association),
|_| (stmt.clone(), Some(ref_id)),
)
})
.expect("Expected AST statement, found None");

Expand Down

0 comments on commit f3b12b3

Please sign in to comment.