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 119ea09
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 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
20 changes: 9 additions & 11 deletions compiler/plc_ast/src/pre_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
DataTypeDeclaration, Operator, Pou, UserTypeDeclaration, Variable,
},
literals::AstLiteral,
provider::IdProvider,
provider::IdProvider, try_from,
};
use plc_source::source_location::SourceLocation;

Expand Down Expand Up @@ -96,17 +96,15 @@ 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(),
.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(),
)
}
_ => (extract_flat_ref_name(it), None, it.get_location()),
})
)
)
.map(|(element_name, initializer, location)| {
let enum_literal = initializer.unwrap_or_else(|| {
build_enum_initializer(&last_name, &location, &mut id_provider, enum_name)
Expand Down
11 changes: 5 additions & 6 deletions compiler/plc_xml/src/xml_parser/fbd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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 +54,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 119ea09

Please sign in to comment.